Skip to content

fix: build anchor macros for section titles containing special charac… - #1193

Open
blackduckx wants to merge 1 commit into
sphinx-contrib:mainfrom
blackduckx:fix/title-ampersand-anchor-macro
Open

fix: build anchor macros for section titles containing special charac…#1193
blackduckx wants to merge 1 commit into
sphinx-contrib:mainfrom
blackduckx:fix/title-ampersand-anchor-macro

Conversation

@blackduckx

Copy link
Copy Markdown

…ters

Section titles containing characters flagged as "unsafe" by ascii_quote() (e.g. &) never received an <ac:structured-macro ac:name="anchor"> in the generated storage-format output, even though the heading text itself rendered correctly.

The anchor target for a title is registered in
_register_doctree_targets (builder.py) keyed on the section's ids (docutils' ascii-safe, punctuation-stripped slug), unconditionally. But visit_title (storage/translator.py) looked that target up using a different key, rebuilt from the section's names (which keeps punctuation like &). For titles with no special characters both keys happened to match; for a title like "Foo & Bar" they diverged ("foo-bar" vs "foo-&-bar"), the lookup silently returned nothing, and the anchor macro was skipped entirely.

The fix makes visit_title key its lookup off the section's ids directly, matching exactly what is registered, so the anchor is always found regardless of the title's characters.

Adds a regression test (dataset + test case) covering a section title with an ampersand, including its local table-of-contents backlink.

…ters

Section titles containing characters flagged as "unsafe" by ascii_quote()
(e.g. `&`) never received an `<ac:structured-macro ac:name="anchor">` in
the generated storage-format output, even though the heading text itself
rendered correctly.

The anchor target for a title is registered in
`_register_doctree_targets` (builder.py) keyed on the section's `ids`
(docutils' ascii-safe, punctuation-stripped slug), unconditionally. But
`visit_title` (storage/translator.py) looked that target up using a
different key, rebuilt from the section's `names` (which keeps
punctuation like `&`). For titles with no special characters both keys
happened to match; for a title like "Foo & Bar" they diverged
("foo-bar" vs "foo-&-bar"), the lookup silently returned nothing, and
the anchor macro was skipped entirely.

The fix makes `visit_title` key its lookup off the section's `ids`
directly, matching exactly what is registered, so the anchor is always
found regardless of the title's characters.

Adds a regression test (dataset + test case) covering a section title
with an ampersand, including its local table-of-contents backlink.

Signed-off-by: Frédéric Brin <duckx@mezimail.com>
@blackduckx
blackduckx force-pushed the fix/title-ampersand-anchor-macro branch from ea5a5dd to 33819e2 Compare August 4, 2026 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant