Skip to content

Commit

Permalink
add anchors to headers w/ smooth scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
stebunovd committed Jan 6, 2024
1 parent a255108 commit 225f914
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions layouts/_default/_markup/render-heading.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h{{ .Level }} id="{{ .Anchor | safeURL }}">{{ .Text | safeHTML }}</h{{ .Level }}>
1 change: 1 addition & 0 deletions layouts/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
<a href="https://creativecommons.org/licenses/by/4.0/">CC BY 4.0</a>
</div>
</footer>
<script src="/prettify.js"></script>
22 changes: 22 additions & 0 deletions static/main.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
html {
scroll-behavior: smooth;
}

body {
-webkit-font-smoothing: antialiased;
color: rgb(36, 36, 36);
Expand Down Expand Up @@ -28,11 +32,29 @@ article > h1 {
margin: 0.67em 0;
}

article > h2 {
margin: -0.83em 0 0.83em;
padding: 0.83em 0 0;
}

a {
color: inherit;
cursor: pointer;
}

a.anchor {
text-decoration: none;
}

a.anchor:hover {
text-decoration: underline;
}

a.anchor:hover:after {
color: #c0c0c0;
content: ' ¶';
}

p, ul, ol {
margin: 0 0 2em;
}
Expand Down
20 changes: 20 additions & 0 deletions static/prettify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

function activateAnchors() {
var article = document.querySelector('article');
if (!article) {
return;
}
var headings = document.querySelectorAll('h2').forEach(function (h) {
var id = h.id;
if (id) {
var a = document.createElement('a');
a.href = '#' + id;
a.className = 'anchor';
a.text = h.innerHTML;
h.innerHTML = a.outerHTML;
}
});
}

activateAnchors();

0 comments on commit 225f914

Please sign in to comment.