Skip to content

Commit

Permalink
Create anchors in website generator
Browse files Browse the repository at this point in the history
Moves it out of JS into the html directly
  • Loading branch information
hendricius committed Nov 8, 2023
1 parent 1a5d184 commit 60c5f14
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
10 changes: 0 additions & 10 deletions website/assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
15 changes: 15 additions & 0 deletions website/modify_build.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def modify_file(filename)
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 }
Expand Down Expand Up @@ -561,6 +562,20 @@ def fix_js_dependency_link(text)
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{<a href="#{anchor}" class="permalink">🔗</a>}
el.inner_html = "#{el.inner_html}#{a_link}"
end
doc.to_html
end
end

ModifyBuild.build

0 comments on commit 60c5f14

Please sign in to comment.