Skip to content

Commit

Permalink
add anchors to headers
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism committed Dec 29, 2024
1 parent 3436d0f commit f98dc8b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies = [
"gunicorn",
"lxml",
"markdown-it-py",
"mdit-py-plugins",
"pygments",
]

Expand Down
6 changes: 5 additions & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ lxml==5.3.0
# feedgen
# pallets-web (../pyproject.toml)
markdown-it-py==3.0.0
# via pallets-web (../pyproject.toml)
# via
# mdit-py-plugins
# pallets-web (../pyproject.toml)
markupsafe==2.1.5
# via
# jinja2
# werkzeug
mdit-py-plugins==0.4.2
# via pallets-web (../pyproject.toml)
mdurl==0.1.2
# via markdown-it-py
packaging==24.1
Expand Down
6 changes: 5 additions & 1 deletion requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ lxml==5.3.0
# -r base.txt
# feedgen
markdown-it-py==3.0.0
# via -r base.txt
# via
# -r base.txt
# mdit-py-plugins
markupsafe==2.1.5
# via
# -r base.txt
# jinja2
# werkzeug
mdit-py-plugins==0.4.2
# via -r base.txt
mdurl==0.1.2
# via
# -r base.txt
Expand Down
4 changes: 3 additions & 1 deletion src/pallets/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from markdown_it.renderer import RendererHTML
from markdown_it.token import Token
from markdown_it.utils import OptionsDict
from mdit_py_plugins.anchors import anchors_plugin
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name

Expand Down Expand Up @@ -110,7 +111,7 @@ def _rewrite_link(
if ref is None:
return None

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


md = MarkdownIt(options_update={"highlight": highlight})
md.use(anchors_plugin, min_level=2, max_level=6, permalink=True)
md.add_render_rule("fence", render_fence)
md.add_render_rule("link_open", render_link_open)
md.add_render_rule("image", render_image)
Expand Down
12 changes: 12 additions & 0 deletions src/pallets/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,15 @@ footer ul li {
white-space: nowrap;
border: 0;
}

a.header-anchor {
text-decoration: none;
}

a.header-anchor > p {
display: none;
}

:is(h1, h2, h3, h4, h5, h6):hover > a.header-anchor > p {
display: inline
}

0 comments on commit f98dc8b

Please sign in to comment.