Skip to content

Commit

Permalink
JS script to transform h2 in anchor links in blog posts.
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenchio committed Dec 19, 2024
1 parent 30c6644 commit 4d7589d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions layouts/blog/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
{{ end }}

{{- define "main" }}
<script type="module">
import '/js/prettify.js';
</script>
<div class="page article">
<div class="article--back">
<a class="article--back-link" href="/blog/">
Expand Down
16 changes: 16 additions & 0 deletions static/js/prettify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

function activateAnchors() {
var article = document.querySelector('article');
if (!article) {
return;
}
document.querySelectorAll('h2').forEach(function (h) {
var id = h.id;
if (id) {
h.innerHTML = '<a href="#' + id + '" class="anchor">' + h.innerHTML + '</a>';
}
});
}

activateAnchors();

0 comments on commit 4d7589d

Please sign in to comment.