Skip to content

Commit f98dc8b

Browse files
committed
add anchors to headers
1 parent 3436d0f commit f98dc8b

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ dependencies = [
1515
"gunicorn",
1616
"lxml",
1717
"markdown-it-py",
18+
"mdit-py-plugins",
1819
"pygments",
1920
]
2021

requirements/base.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,15 @@ lxml==5.3.0
3333
# feedgen
3434
# pallets-web (../pyproject.toml)
3535
markdown-it-py==3.0.0
36-
# via pallets-web (../pyproject.toml)
36+
# via
37+
# mdit-py-plugins
38+
# pallets-web (../pyproject.toml)
3739
markupsafe==2.1.5
3840
# via
3941
# jinja2
4042
# werkzeug
43+
mdit-py-plugins==0.4.2
44+
# via pallets-web (../pyproject.toml)
4145
mdurl==0.1.2
4246
# via markdown-it-py
4347
packaging==24.1

requirements/dev.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ lxml==5.3.0
6464
# -r base.txt
6565
# feedgen
6666
markdown-it-py==3.0.0
67-
# via -r base.txt
67+
# via
68+
# -r base.txt
69+
# mdit-py-plugins
6870
markupsafe==2.1.5
6971
# via
7072
# -r base.txt
7173
# jinja2
7274
# werkzeug
75+
mdit-py-plugins==0.4.2
76+
# via -r base.txt
7377
mdurl==0.1.2
7478
# via
7579
# -r base.txt

src/pallets/markdown.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from markdown_it.renderer import RendererHTML
1010
from markdown_it.token import Token
1111
from markdown_it.utils import OptionsDict
12+
from mdit_py_plugins.anchors import anchors_plugin
1213
from pygments.formatters.html import HtmlFormatter
1314
from pygments.lexers import get_lexer_by_name
1415

@@ -110,7 +111,7 @@ def _rewrite_link(
110111
if ref is None:
111112
return None
112113

113-
if ref and ref[0] != "/" and ":" not in ref:
114+
if ref and ref[0] not in "/#" and ":" not in ref:
114115
# rewrite relative links to be served by app views
115116
if (
116117
allow_page
@@ -132,6 +133,7 @@ def _rewrite_link(
132133

133134

134135
md = MarkdownIt(options_update={"highlight": highlight})
136+
md.use(anchors_plugin, min_level=2, max_level=6, permalink=True)
135137
md.add_render_rule("fence", render_fence)
136138
md.add_render_rule("link_open", render_link_open)
137139
md.add_render_rule("image", render_image)

src/pallets/static/style.css

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@ footer ul li {
4646
white-space: nowrap;
4747
border: 0;
4848
}
49+
50+
a.header-anchor {
51+
text-decoration: none;
52+
}
53+
54+
a.header-anchor > p {
55+
display: none;
56+
}
57+
58+
:is(h1, h2, h3, h4, h5, h6):hover > a.header-anchor > p {
59+
display: inline
60+
}

0 commit comments

Comments
 (0)