Skip to content

Commit

Permalink
Added a dedicated description paragraph to be able to parse the descr…
Browse files Browse the repository at this point in the history
…iption solely as tooltip if there is no other heading in the page at all (syslog-ng#103)

Signed-off-by: Hofi <hofione@gmail.com>
  • Loading branch information
HofiOne authored Jun 29, 2024
2 parents 753458c + 640d534 commit 42151f5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
46 changes: 28 additions & 18 deletions _js/custom/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,27 +379,35 @@ $(function () {
loadContentFromUrl(
url,
newContent => {
var content = '';
var startHeading = newContent.querySelector('#' + startHeadingId);

if (startHeading) {
var content = '';
var heading = startHeading.outerHTML; // Include the starting <h> element itself
var nextSibling = startHeading.nextElementSibling;

// 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)
// 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.
// If not presented, drop the whole content, return an empty one (a.k.a. do not show title only tooltips)
if (nextSibling == null && false == hasAnchor) {
startHeading = newContent.querySelector('.page__content');
nextSibling = startHeading.firstElementChild;
// First element is the TOC, skip it to be able to produce an empty content
if (nextSibling && nextSibling.classList.contains('sidebar__right'))
nextSibling = nextSibling.nextElementSibling;
var description = newContent.querySelector('#page-description');

if (description && description.outerHTML.length > 0) {
content = description.outerHTML;
}
// Collect all siblings until the next heading or the end of the initial content
// FIXME: This magic 6 must be maintained together now with generate_links.rb (and other places ?!), eliminate it!
while (nextSibling && nextSibling.tagName !== 'H1' && nextSibling.tagName !== 'H2' && nextSibling.tagName !== 'H3' && nextSibling.tagName !== 'H4' && nextSibling.tagName !== 'H5' && nextSibling.tagName !== 'H6') {
content += nextSibling.outerHTML;
nextSibling = nextSibling.nextElementSibling;
else {
var nextSibling = startHeading.nextElementSibling;

// 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)
// 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.
// If not presented, drop the whole content, return an empty one (a.k.a. do not show title only tooltips)
if (nextSibling == null && false == hasAnchor) {
startHeading = newContent.querySelector('.page__content');
nextSibling = startHeading.firstElementChild;
// First element is the TOC, skip it to be able to produce an empty content
if (nextSibling && nextSibling.classList.contains('sidebar__right'))
nextSibling = nextSibling.nextElementSibling;
}
// Collect all siblings until the next heading or the end of the initial content
// FIXME: This magic 6 must be maintained together now with generate_links.rb (and other places ?!), eliminate it!
while (nextSibling && nextSibling.tagName !== 'H1' && nextSibling.tagName !== 'H2' && nextSibling.tagName !== 'H3' && nextSibling.tagName !== 'H4' && nextSibling.tagName !== 'H5' && nextSibling.tagName !== 'H6') {
content += nextSibling.outerHTML;
nextSibling = nextSibling.nextElementSibling;
}
}

if (content.length != 0 || hasAnchor)
Expand Down Expand Up @@ -468,8 +476,10 @@ $(function () {
}

function getRealZIndex(element) {
var zIndex = getComputedStyle(element).zIndex;
if (element == null)
return null;

var zIndex = getComputedStyle(element).zIndex;
// If the element's z-index is not auto, return it
if (zIndex !== "auto")
return parseInt(zIndex);
Expand Down
26 changes: 16 additions & 10 deletions _js/main.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -8670,19 +8670,24 @@ $(function() {
var hasAnchor = hashIndex !== -1;
if (hasAnchor) startHeadingId = url.substring(hashIndex + 1);
loadContentFromUrl(url, newContent => {
var content = "";
var startHeading = newContent.querySelector("#" + startHeadingId);
if (startHeading) {
var content = "";
var heading = startHeading.outerHTML;
var nextSibling = startHeading.nextElementSibling;
if (nextSibling == null && false == hasAnchor) {
startHeading = newContent.querySelector(".page__content");
nextSibling = startHeading.firstElementChild;
if (nextSibling && nextSibling.classList.contains("sidebar__right")) nextSibling = nextSibling.nextElementSibling;
}
while (nextSibling && nextSibling.tagName !== "H1" && nextSibling.tagName !== "H2" && nextSibling.tagName !== "H3" && nextSibling.tagName !== "H4" && nextSibling.tagName !== "H5" && nextSibling.tagName !== "H6") {
content += nextSibling.outerHTML;
nextSibling = nextSibling.nextElementSibling;
var description = newContent.querySelector("#page-description");
if (description && description.outerHTML.length > 0) {
content = description.outerHTML;
} else {
var nextSibling = startHeading.nextElementSibling;
if (nextSibling == null && false == hasAnchor) {
startHeading = newContent.querySelector(".page__content");
nextSibling = startHeading.firstElementChild;
if (nextSibling && nextSibling.classList.contains("sidebar__right")) nextSibling = nextSibling.nextElementSibling;
}
while (nextSibling && nextSibling.tagName !== "H1" && nextSibling.tagName !== "H2" && nextSibling.tagName !== "H3" && nextSibling.tagName !== "H4" && nextSibling.tagName !== "H5" && nextSibling.tagName !== "H6") {
content += nextSibling.outerHTML;
nextSibling = nextSibling.nextElementSibling;
}
}
if (content.length != 0 || hasAnchor) content = heading + content;
onSuccess(content);
Expand Down Expand Up @@ -8726,6 +8731,7 @@ $(function() {
contentTooltip.style.top = contentTooltipTop + "px";
}
function getRealZIndex(element) {
if (element == null) return null;
var zIndex = getComputedStyle(element).zIndex;
if (zIndex !== "auto") return parseInt(zIndex); else {
var parent = element.parentElement;
Expand Down
4 changes: 2 additions & 2 deletions _layouts/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ <h1 id="page-title" class="page__title p-name page__title_decoration" itemprop="
</h1>
{% endif %}
{% comment %}<!--
DO NOT ADD page.subtitle and page.description !!!
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.
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 !!!
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.
This eliminates markdown and liquid rendering differences in the description part.
{% if page.subtitle or page.description %}
Expand Down
7 changes: 6 additions & 1 deletion _plugins/generate_tooltips.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
end
end
if description
page.content = description + "\n" + page.content
page.content = JekyllTooltipGen_description_start_tag + description + "\n" + JekyllTooltipGen_description_end_tag + page.content
end
end

Expand All @@ -461,6 +461,8 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
JekyllTooltipGen_link_aliases_yaml = '_data/link_aliases.yml'
JekyllTooltipGen_excluded_yaml = '_data/excluded_titles.yml'
JekyllTooltipGen_external_yaml = '_data/external_links.yml'
JekyllTooltipGen_description_start_tag = '<---description_start--->'
JekyllTooltipGen_description_end_tag = '<---description_end--->'

$JekyllTooltipGen_markdown_extensions = nil
$JekyllTooltipGen_page_links = nil
Expand Down Expand Up @@ -545,4 +547,7 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
page.content = template.render!(payload, info)

Jekyll::TooltipGen.generate_tooltips(page, $JekyllTooltipGen_should_build_persistent_tooltips)

page.content = page.content.gsub(JekyllTooltipGen_description_start_tag, '<p id="page-description">')
page.content = page.content.gsub(JekyllTooltipGen_description_end_tag, '</p>')
end

0 comments on commit 42151f5

Please sign in to comment.