Skip to content

🐛 FIX: Preserve explicit heading anchor IDs - #159

Merged
chrisjsewell merged 7 commits into
executablebooks:masterfrom
muneikh:fix-explicit-heading-anchor-ids
Nov 9, 2026
Merged

chrisjsewell merged 7 commits into
executablebooks:masterfrom
muneikh:fix-explicit-heading-anchor-ids

Conversation

@muneikh

@muneikh muneikh commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

When attrs_block_plugin assigns a heading ID, the anchors plugin overwrites it with a slug of the title. This breaks explicit links and calls custom slug functions unnecessarily.

Preserve existing heading IDs and use them for permalinks. Reserve explicit IDs before generating slugs so automatic headings avoid collisions regardless of their order in the document. Update the public docstring and changelog.

Four regressions fail before the change: an explicit ID, collisions before and after an explicit heading, and a custom slug function that must not run for an explicit ID.

Validation: the prescribed Tox Python 3.13 session passes all 515 tests. All repository pre-commit hooks pass, including pinned Ruff checks and strict Mypy.

muneikh and others added 7 commits October 11, 2026 13:19
An empty `{id=""}` is invalid HTML and gave a `href="#"` permalink;
it is now neither reserved nor kept, and the heading gets a generated id.
Tests pin the out-of-level reservation, duplicate explicit ids being kept
(no error), the empty id, and plugin load-order independence.
The default slug function no longer strips leading/trailing whitespace,
so it is now identical to MyST-Parser's `github_slugify`
(which keeps it deliberately, see its v0.18.1 changelog).

This changes generated ids where the title text starts or ends with a
space next to an image, raw HTML or an entity,
e.g. `# Foo ![img](x.png)` gives `foo-` (was `foo`),
and `# &executablebooks#32;Foo` gives `-foo` (was `foo`).
@chrisjsewell
chrisjsewell merged commit c1835a6 into executablebooks:master Nov 9, 2026
11 checks passed
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.

2 participants