Skip to content

Commit

Permalink
👌 Improve default slug generation for heading anchors (#753)
Browse files Browse the repository at this point in the history
This commit makes the slug generation closer to GitHub,
in that, starting/ending whitespace will not be stripped.

For example, ``#  ` a` b `c ` `` will now correctly create the slug
`-a-b-c-` and not `a-b-c`

Co-authored-by: Chris Sewell <chrisj_sewell@hotmail.com>
  • Loading branch information
Cimbali and chrisjsewell authored Jun 13, 2023
1 parent 80b892f commit 28a9df3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion myst_parser/mdit_to_docutils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1974,7 +1974,7 @@ def default_slugify(title: str) -> str:
- https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
- https://gist.github.com/asabaylus/3071099
"""
return _SLUGIFY_CLEAN_REGEX.sub("", title.strip().lower().replace(" ", "-"))
return _SLUGIFY_CLEAN_REGEX.sub("", title.lower().replace(" ", "-"))


def compute_unique_slug(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_sphinx/sourcedirs/references/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ Known explicit [**hallo**](#paragraph-target)
Known with title [](#title-target)

Ambiguous [](#duplicate)

# Image in title ![badge](https://shields.io/or/something.svg)

[link up](#image-in-title-)
14 changes: 14 additions & 0 deletions tests/test_sphinx/test_sphinx_builds/test_references.html
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ <h1>
</a>
</p>
</section>
<section class="tex2jax_ignore mathjax_ignore" id="image-in-title">
<h1>
Image in title
<img alt="badge" src="https://shields.io/or/something.svg"/>
<a class="headerlink" href="#image-in-title" title="Permalink to this heading">
</a>
</h1>
<p>
<a class="reference internal" href="#image-in-title">
link up
</a>
</p>
</section>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<document source="index.md">
<target refid="title">
<section classes="tex2jax_ignore mathjax_ignore" ids="title-with-nested-a-1 title" names="title\ with\ nested\ a=1 title" slug="title-with-nested">
<section classes="tex2jax_ignore mathjax_ignore" ids="title-with-nested-a-1 title" names="title\ with\ nested\ a=1 title" slug="title-with-nested-">
<title>
Title with
<strong>
Expand Down Expand Up @@ -150,3 +150,10 @@
<reference internal="False" reftitle="Example 0.0.1" refuri="https://example.com/index.html#module-duplicate">
<literal classes="iref myst">
duplicate
<section classes="tex2jax_ignore mathjax_ignore" ids="image-in-title" names="image\ in\ title" slug="image-in-title-">
<title>
Image in title
<image alt="badge" candidates="{'?': 'https://shields.io/or/something.svg'}" uri="https://shields.io/or/something.svg">
<paragraph>
<reference id_link="True" refid="image-in-title">
link up
9 changes: 8 additions & 1 deletion tests/test_sphinx/test_sphinx_builds/test_references.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<document source="index.md">
<target refid="title">
<section classes="tex2jax_ignore mathjax_ignore" ids="title-with-nested-a-1 title" names="title\ with\ nested\ a=1 title" slug="title-with-nested">
<section classes="tex2jax_ignore mathjax_ignore" ids="title-with-nested-a-1 title" names="title\ with\ nested\ a=1 title" slug="title-with-nested-">
<title>
Title with
<strong>
Expand Down Expand Up @@ -128,3 +128,10 @@
Ambiguous
<pending_xref refdoc="index" refdomain="True" refexplicit="False" reftarget="duplicate" reftype="myst">
<inline classes="xref myst">
<section classes="tex2jax_ignore mathjax_ignore" ids="image-in-title" names="image\ in\ title" slug="image-in-title-">
<title>
Image in title
<image alt="badge" candidates="{'?': 'https://shields.io/or/something.svg'}" uri="https://shields.io/or/something.svg">
<paragraph>
<reference id_link="True" refid="image-in-title">
link up

0 comments on commit 28a9df3

Please sign in to comment.