Skip to content

Commit 9025dcc

Browse files
committed
Anchor links on post headings
1 parent ca3a8eb commit 9025dcc

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

_layouts/post.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ <h2 class='text-l news-title no-border'>{{ page.title }}</h2>
5555
<div class='text-xxs content-text generated-content'>
5656
{{ content }}
5757
</div>
58+
<script>
59+
document.querySelectorAll('.generated-content h1[id], .generated-content h2[id], .generated-content h3[id], .generated-content h4[id]').forEach(function(heading) {
60+
var link = document.createElement('a');
61+
link.className = 'heading-anchor';
62+
link.href = '#' + heading.id;
63+
link.innerHTML = '<svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor"><path d="M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z"></path></svg>';
64+
heading.appendChild(link);
65+
});
66+
</script>
5867
{% if content contains 'class="mermaid"' %}
5968
<script src="{{ site.mermaid.src | default: 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js' }}"></script>
6069
<script>mermaid.initialize({startOnLoad: true});</script>

_sass/pages/post.scss

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,26 @@
251251
line-height: 1.4;
252252
}
253253

254+
.heading-anchor {
255+
opacity: 0;
256+
margin-left: 0.4em;
257+
color: #aab;
258+
text-decoration: none;
259+
transition: opacity 0.15s;
260+
vertical-align: middle;
261+
}
262+
263+
h1:hover .heading-anchor,
264+
h2:hover .heading-anchor,
265+
h3:hover .heading-anchor,
266+
h4:hover .heading-anchor {
267+
opacity: 1;
268+
}
269+
270+
.heading-anchor:hover {
271+
color: #557;
272+
}
273+
254274
.admonition {
255275
margin: 1.5rem 0;
256276
padding: 1rem 1.25rem;

0 commit comments

Comments
 (0)