Skip to content

Commit e03dce1

Browse files
authored
Merge pull request #167 from danog/header-anchors
A # link after each heading of the docs and articles
2 parents b3ce880 + 5ae2879 commit e03dce1

6 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎assets/css/mkdocs_material_extra.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎assets/css/site.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎assets/sass/mkdocs_material_extra.scss‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ p, ul, table {
149149
}
150150
}
151151

152+
// The # after each heading (the toc permalink of mkdocs.yml) stays visible, not only on hover, and turns red like
153+
// links when hovered or focused. html body and [id] make these rules more specific than the theme's.
154+
html body .md-typeset .headerlink {
155+
opacity: 1;
156+
transform: none;
157+
}
158+
159+
html body .md-typeset [id] > .headerlink:hover,
160+
html body .md-typeset [id] > .headerlink:focus {
161+
color: #943b3a;
162+
}
163+
152164
.md-typeset {
153165
h1, h2, h3 {
154166
font-family: "National 2", "Helvetica Neue", "Helvetica", sans-serif;

‎assets/sass/post.scss‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ div.post {
3434
padding-right: 20px;
3535
margin: 1.5em 0 .5em;
3636
font-family: "National 2", "Helvetica Neue", "Helvetica", sans-serif;
37+
38+
a.headerlink {
39+
margin-left: .4em;
40+
color: rgba(0, 0, 0, .26);
41+
text-decoration: none;
42+
43+
&:hover,
44+
&:focus {
45+
color: #a75251;
46+
}
47+
}
3748
}
3849

3950
h2 {

‎mkdocs.yml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ theme:
1010
custom_dir: 'theme'
1111
markdown_extensions:
1212
- codehilite
13+
# A # after each heading links to it
14+
- toc:
15+
permalink: '#'
1316
extra:
1417
# The logo links to the main site
1518
homepage: https://psalm.dev/
1619
extra_css:
17-
- '../assets/css/mkdocs_material_extra.css?v=11'
20+
- '../assets/css/mkdocs_material_extra.css?v=12'

‎views/article.php‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@
5757
// The library builds the description from rendered HTML, so it holds entities like &
5858
$description = trim(preg_replace('/\s+/u', ' ', html_entity_decode($article->description, ENT_QUOTES | ENT_HTML5, 'UTF-8')));
5959
$meta_description = $description;
60+
// A # after each heading links to it, as in the docs
61+
$article_html = preg_replace(
62+
'~<(h[2-6]) id="([^"]+)">(.*?)</\\1>~s',
63+
'<$1 id="$2">$3<a class="headerlink" href="#$2" title="Permanent link">#</a></$1>',
64+
$article->html
65+
) ?? $article->html;
66+
6067
$timestamp = strtotime($article->date);
6168
$published = $timestamp === false ? null : date('c', $timestamp);
6269

@@ -126,9 +133,9 @@
126133
<?php endif ?>
127134
<?php require_once('../includes/subscribe.php'); ?>
128135
<?php // An article can add a subscription box in its text with <!-- newsletter --> ?>
129-
<?= str_contains($article->html, '<!-- newsletter -->')
130-
? str_replace('<!-- newsletter -->', NewsletterForm::render('box'), $article->html)
131-
: $article->html ?>
136+
<?= str_contains($article_html, '<!-- newsletter -->')
137+
? str_replace('<!-- newsletter -->', NewsletterForm::render('box'), $article_html)
138+
: $article_html ?>
132139
<?= NewsletterForm::render('box') ?>
133140
</div>
134141
<?php require('../includes/footer.php'); ?>

0 commit comments

Comments
 (0)