mirror of
https://github.com/hendricius/the-sourdough-framework
synced 2025-11-08 20:21:12 -06:00
Create anchors in website generator (#274)
Moves it out of JS into the html directly
This commit is contained in:
committed by
GitHub
parent
60c5c4211a
commit
b91f00b103
@@ -8,14 +8,4 @@ document.addEventListener('DOMContentLoaded', function() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add permalinks to headers
|
|
||||||
var heads = document.querySelectorAll('.sectionHead');
|
|
||||||
heads.forEach(function (head) {
|
|
||||||
let permalink = document.createElement("a");
|
|
||||||
permalink.href = '#' + head.id;
|
|
||||||
permalink.classList.add('permalink');
|
|
||||||
permalink.append('🔗');
|
|
||||||
head.append(permalink);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ class ModifyBuild
|
|||||||
text = add_text_to_coverpage(text, extract_file_from_path(filename))
|
text = add_text_to_coverpage(text, extract_file_from_path(filename))
|
||||||
text = fix_js_dependency_link(text)
|
text = fix_js_dependency_link(text)
|
||||||
text = fix_list_of_tables_figures_duplicates(text)
|
text = fix_list_of_tables_figures_duplicates(text)
|
||||||
|
text = add_anchors_to_headers(text)
|
||||||
text = fix_menus_list_figures_tables(text) if is_list_figures_tables?(filename)
|
text = fix_menus_list_figures_tables(text) if is_list_figures_tables?(filename)
|
||||||
text = fix_list_of_figures_tables_display(text) if is_list_figures_tables?(filename)
|
text = fix_list_of_figures_tables_display(text) if is_list_figures_tables?(filename)
|
||||||
File.open(filename, "w") {|file| file.puts text }
|
File.open(filename, "w") {|file| file.puts text }
|
||||||
@@ -370,15 +371,17 @@ class ModifyBuild
|
|||||||
|
|
||||||
def mark_menu_as_selected_if_on_page(text, filename)
|
def mark_menu_as_selected_if_on_page(text, filename)
|
||||||
doc = build_doc(text)
|
doc = build_doc(text)
|
||||||
|
return doc.to_html
|
||||||
|
|
||||||
selected = doc.css(".menu-items .chapterToc > a").find do |el|
|
selected = doc.css(".menu-items .chapterToc > a").find do |el|
|
||||||
el["href"] == ""
|
el["href"] == ""
|
||||||
end
|
end
|
||||||
|
|
||||||
# Special case for index page
|
# Special case for index page
|
||||||
if ["index.html", "book.html"].include?(filename)
|
#if ["index.html", "book.html"].include?(filename)
|
||||||
doc.css(".menu-items .chapterToc.home-link")[0].add_class("selected")
|
# doc.css(".menu-items .chapterToc.home-link")[0].add_class("selected")
|
||||||
return doc.to_html
|
# return doc.to_html
|
||||||
end
|
#end
|
||||||
|
|
||||||
# Special case for the flowcharts page which is added by us to the menu.
|
# Special case for the flowcharts page which is added by us to the menu.
|
||||||
# This needs to be done for future manually added pages too
|
# This needs to be done for future manually added pages too
|
||||||
@@ -561,6 +564,20 @@ class ModifyBuild
|
|||||||
def build_doc(text)
|
def build_doc(text)
|
||||||
Nokogiri::HTML(text)
|
Nokogiri::HTML(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def add_anchors_to_headers(text)
|
||||||
|
doc = build_doc(text)
|
||||||
|
content = doc.css(".sectionHead, .subsectionHead")
|
||||||
|
content.each do |el|
|
||||||
|
anchor = el.attribute("id").value
|
||||||
|
# No anchor for whatever reason
|
||||||
|
next unless anchor
|
||||||
|
|
||||||
|
copy_link = %Q{<a href="##{anchor}" class="permalink">🔗</a>}
|
||||||
|
el.inner_html = "#{el.inner_html}#{copy_link}"
|
||||||
|
end
|
||||||
|
doc.to_html
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ModifyBuild.build
|
ModifyBuild.build
|
||||||
|
|||||||
Reference in New Issue
Block a user