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
chrisjsewell added a commit that referenced this pull request Nov 10, 2026
Brings in #148, #138, #153, #157, #149, #152, #154, #158, #159, #161, #162.

Conflicts:
- attrs/index.py: #153 merges inline classes from the opening token
  (attr_token), #150 passes state.md to _add_attrs; both kept.
- tests/test_attrs.py, tests/test_texmath.py: both sides' tests kept.

Adds test_class_accumulation_is_filtered: classes merged across inline
groups (onto the opening token) and across stacked block attributes still
pass through the on*, URL and allowed filters.
@chrisjsewell

Copy link
Copy Markdown
Member

Merged in c1835a6, thanks @muneikh, this was a clear fix and matches what markdown-it-anchor does. Before merging I pushed a few commits on top of yours:

  • merges of master;
  • the CHANGELOG entry dropped: the release PR writes every entry, with credit;
  • an empty explicit id ({id=""}) now counts as no id, so the heading gets a generated slug instead of id="";
  • tests pinning the edge cases: an explicit id outside min_level/max_level is still reserved, duplicate explicit ids are kept as written, and the result does not depend on plugin load order.

A separate change of ours went in with it: the default slugify no longer strips the title first, so it is identical to MyST-Parser's github_slugify. That 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) now gives foo-), and the release notes will say so.

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