Skip to content

Commit 42151f5

Browse files
authored
Added a dedicated description paragraph to be able to parse the description solely as tooltip if there is no other heading in the page at all (#103)
Signed-off-by: Hofi <hofione@gmail.com>
2 parents 753458c + 640d534 commit 42151f5

File tree

4 files changed

+52
-31
lines changed

4 files changed

+52
-31
lines changed

_js/custom/navigation.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -379,27 +379,35 @@ $(function () {
379379
loadContentFromUrl(
380380
url,
381381
newContent => {
382+
var content = '';
382383
var startHeading = newContent.querySelector('#' + startHeadingId);
384+
383385
if (startHeading) {
384-
var content = '';
385386
var heading = startHeading.outerHTML; // Include the starting <h> element itself
386-
var nextSibling = startHeading.nextElementSibling;
387-
388-
// If handling a page title it will not have next sibling normally at all (we are removed and handling differently the description from the generated page)
389-
// In that case the description is all the normal texts parts in the content, from the top (right bellow the title) to the first heading <h1-6>, try to get, it if there's any.
390-
// If not presented, drop the whole content, return an empty one (a.k.a. do not show title only tooltips)
391-
if (nextSibling == null && false == hasAnchor) {
392-
startHeading = newContent.querySelector('.page__content');
393-
nextSibling = startHeading.firstElementChild;
394-
// First element is the TOC, skip it to be able to produce an empty content
395-
if (nextSibling && nextSibling.classList.contains('sidebar__right'))
396-
nextSibling = nextSibling.nextElementSibling;
387+
var description = newContent.querySelector('#page-description');
388+
389+
if (description && description.outerHTML.length > 0) {
390+
content = description.outerHTML;
397391
}
398-
// Collect all siblings until the next heading or the end of the initial content
399-
// FIXME: This magic 6 must be maintained together now with generate_links.rb (and other places ?!), eliminate it!
400-
while (nextSibling && nextSibling.tagName !== 'H1' && nextSibling.tagName !== 'H2' && nextSibling.tagName !== 'H3' && nextSibling.tagName !== 'H4' && nextSibling.tagName !== 'H5' && nextSibling.tagName !== 'H6') {
401-
content += nextSibling.outerHTML;
402-
nextSibling = nextSibling.nextElementSibling;
392+
else {
393+
var nextSibling = startHeading.nextElementSibling;
394+
395+
// If handling a page title it will not have next sibling normally at all (we are removed and handling differently the description from the generated page)
396+
// In that case the description is all the normal texts parts in the content, from the top (right bellow the title) to the first heading <h1-6>, try to get, it if there's any.
397+
// If not presented, drop the whole content, return an empty one (a.k.a. do not show title only tooltips)
398+
if (nextSibling == null && false == hasAnchor) {
399+
startHeading = newContent.querySelector('.page__content');
400+
nextSibling = startHeading.firstElementChild;
401+
// First element is the TOC, skip it to be able to produce an empty content
402+
if (nextSibling && nextSibling.classList.contains('sidebar__right'))
403+
nextSibling = nextSibling.nextElementSibling;
404+
}
405+
// Collect all siblings until the next heading or the end of the initial content
406+
// FIXME: This magic 6 must be maintained together now with generate_links.rb (and other places ?!), eliminate it!
407+
while (nextSibling && nextSibling.tagName !== 'H1' && nextSibling.tagName !== 'H2' && nextSibling.tagName !== 'H3' && nextSibling.tagName !== 'H4' && nextSibling.tagName !== 'H5' && nextSibling.tagName !== 'H6') {
408+
content += nextSibling.outerHTML;
409+
nextSibling = nextSibling.nextElementSibling;
410+
}
403411
}
404412

405413
if (content.length != 0 || hasAnchor)
@@ -468,8 +476,10 @@ $(function () {
468476
}
469477

470478
function getRealZIndex(element) {
471-
var zIndex = getComputedStyle(element).zIndex;
479+
if (element == null)
480+
return null;
472481

482+
var zIndex = getComputedStyle(element).zIndex;
473483
// If the element's z-index is not auto, return it
474484
if (zIndex !== "auto")
475485
return parseInt(zIndex);

_js/main.min.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8670,19 +8670,24 @@ $(function() {
86708670
var hasAnchor = hashIndex !== -1;
86718671
if (hasAnchor) startHeadingId = url.substring(hashIndex + 1);
86728672
loadContentFromUrl(url, newContent => {
8673+
var content = "";
86738674
var startHeading = newContent.querySelector("#" + startHeadingId);
86748675
if (startHeading) {
8675-
var content = "";
86768676
var heading = startHeading.outerHTML;
8677-
var nextSibling = startHeading.nextElementSibling;
8678-
if (nextSibling == null && false == hasAnchor) {
8679-
startHeading = newContent.querySelector(".page__content");
8680-
nextSibling = startHeading.firstElementChild;
8681-
if (nextSibling && nextSibling.classList.contains("sidebar__right")) nextSibling = nextSibling.nextElementSibling;
8682-
}
8683-
while (nextSibling && nextSibling.tagName !== "H1" && nextSibling.tagName !== "H2" && nextSibling.tagName !== "H3" && nextSibling.tagName !== "H4" && nextSibling.tagName !== "H5" && nextSibling.tagName !== "H6") {
8684-
content += nextSibling.outerHTML;
8685-
nextSibling = nextSibling.nextElementSibling;
8677+
var description = newContent.querySelector("#page-description");
8678+
if (description && description.outerHTML.length > 0) {
8679+
content = description.outerHTML;
8680+
} else {
8681+
var nextSibling = startHeading.nextElementSibling;
8682+
if (nextSibling == null && false == hasAnchor) {
8683+
startHeading = newContent.querySelector(".page__content");
8684+
nextSibling = startHeading.firstElementChild;
8685+
if (nextSibling && nextSibling.classList.contains("sidebar__right")) nextSibling = nextSibling.nextElementSibling;
8686+
}
8687+
while (nextSibling && nextSibling.tagName !== "H1" && nextSibling.tagName !== "H2" && nextSibling.tagName !== "H3" && nextSibling.tagName !== "H4" && nextSibling.tagName !== "H5" && nextSibling.tagName !== "H6") {
8688+
content += nextSibling.outerHTML;
8689+
nextSibling = nextSibling.nextElementSibling;
8690+
}
86868691
}
86878692
if (content.length != 0 || hasAnchor) content = heading + content;
86888693
onSuccess(content);
@@ -8726,6 +8731,7 @@ $(function() {
87268731
contentTooltip.style.top = contentTooltipTop + "px";
87278732
}
87288733
function getRealZIndex(element) {
8734+
if (element == null) return null;
87298735
var zIndex = getComputedStyle(element).zIndex;
87308736
if (zIndex !== "auto") return parseInt(zIndex); else {
87318737
var parent = element.parentElement;

_layouts/single.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ <h1 id="page-title" class="page__title p-name page__title_decoration" itemprop="
3737
</h1>
3838
{% endif %}
3939
{% comment %}<!--
40-
DO NOT ADD page.subtitle and page.description !!!
41-
It will be added by aour Jekyll plugin as simple text paragraph right at the beggining of the content, before the first heading instead, that will be used as description in the tooltips.
40+
DO NOT ADD page.subtitle and page.description, as that is handled specially by Jekyll, and will not be rendered almost perfectly with markdown or liquid contents in every cases !!!
41+
It will be added by our Jekyll plugin as simple text paragraph right at the beggining of the content, before the first heading instead, that will be used as description in the tooltips.
4242
This eliminates markdown and liquid rendering differences in the description part.
4343
4444
{% if page.subtitle or page.description %}

_plugins/generate_tooltips.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
446446
end
447447
end
448448
if description
449-
page.content = description + "\n" + page.content
449+
page.content = JekyllTooltipGen_description_start_tag + description + "\n" + JekyllTooltipGen_description_end_tag + page.content
450450
end
451451
end
452452

@@ -461,6 +461,8 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
461461
JekyllTooltipGen_link_aliases_yaml = '_data/link_aliases.yml'
462462
JekyllTooltipGen_excluded_yaml = '_data/excluded_titles.yml'
463463
JekyllTooltipGen_external_yaml = '_data/external_links.yml'
464+
JekyllTooltipGen_description_start_tag = '<---description_start--->'
465+
JekyllTooltipGen_description_end_tag = '<---description_end--->'
464466

465467
$JekyllTooltipGen_markdown_extensions = nil
466468
$JekyllTooltipGen_page_links = nil
@@ -545,4 +547,7 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
545547
page.content = template.render!(payload, info)
546548

547549
Jekyll::TooltipGen.generate_tooltips(page, $JekyllTooltipGen_should_build_persistent_tooltips)
550+
551+
page.content = page.content.gsub(JekyllTooltipGen_description_start_tag, '<p id="page-description">')
552+
page.content = page.content.gsub(JekyllTooltipGen_description_end_tag, '</p>')
548553
end

0 commit comments

Comments
 (0)