From b91f00b103dbab66df3f85de45093c2c3856b211 Mon Sep 17 00:00:00 2001 From: Hendrik Kleinwaechter Date: Sun, 12 Nov 2023 23:01:53 +0100 Subject: [PATCH] Create anchors in website generator (#274) Moves it out of JS into the html directly --- website/assets/script.js | 10 ---------- website/modify_build.rb | 25 +++++++++++++++++++++---- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/website/assets/script.js b/website/assets/script.js index e7127e5..69c4906 100644 --- a/website/assets/script.js +++ b/website/assets/script.js @@ -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); - }); }); diff --git a/website/modify_build.rb b/website/modify_build.rb index ce2f697..e0d251f 100644 --- a/website/modify_build.rb +++ b/website/modify_build.rb @@ -58,6 +58,7 @@ class ModifyBuild text = add_text_to_coverpage(text, extract_file_from_path(filename)) text = fix_js_dependency_link(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_list_of_figures_tables_display(text) if is_list_figures_tables?(filename) File.open(filename, "w") {|file| file.puts text } @@ -370,15 +371,17 @@ class ModifyBuild def mark_menu_as_selected_if_on_page(text, filename) doc = build_doc(text) + return doc.to_html + selected = doc.css(".menu-items .chapterToc > a").find do |el| el["href"] == "" end # Special case for index page - if ["index.html", "book.html"].include?(filename) - doc.css(".menu-items .chapterToc.home-link")[0].add_class("selected") - return doc.to_html - end + #if ["index.html", "book.html"].include?(filename) + # doc.css(".menu-items .chapterToc.home-link")[0].add_class("selected") + # return doc.to_html + #end # 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 @@ -561,6 +564,20 @@ class ModifyBuild def build_doc(text) Nokogiri::HTML(text) 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{} + el.inner_html = "#{el.inner_html}#{copy_link}" + end + doc.to_html + end end ModifyBuild.build