Skip to content

fix: give a heading with no slug-able characters no anchor at all - #176

Open
rahul05ranjan wants to merge 2 commits into
executablebooks:masterfrom
rahul05ranjan:fix/anchors-empty-slug
Open

rahul05ranjan wants to merge 2 commits into
executablebooks:masterfrom
rahul05ranjan:fix/anchors-empty-slug

Conversation

@rahul05ranjan

Copy link
Copy Markdown

Fixes #175

What's wrong

anchors_plugin generates an empty id attribute for any selected-level heading whose title has no slug-able characters (## ???, ## ***, ## $$$ …), which is invalid HTML, and a second such heading gets the odd id="-1":

md.render("## ???\n\n## ???\n")
# master: '<h2 id="">???</h2>\n<h2 id="-1">???</h2>\n'

With permalink=True it also emits a permalink pointing at href="#" — the top of the page, not the heading.

The plugin already documents this doctrine for the explicit-ID path ("an empty explicit ID counts as no ID"; test_explicit_heading_id_edge_cases asserts 'id=""' not in rendered), but the generated-slug path never checked for an empty slugify() result.

Fix

Treat a generated empty slug exactly like an empty explicit ID: the heading gets no id attribute and no permalink. "" is also no longer added to the slug set (which is what produced id="-1" for a second such heading).

Tests

New test_empty_generated_slug_gets_no_id (3 cases: no slug-able chars, twice, followed by a real heading):

  • all 3 fail on master (fde61f4) with 'id=""' in rendered / id="-1",
  • pass with this fix, and the following heading keeps a clean id="real" (no real-1),
  • pytest tests/test_anchors.py: 28 passed (25 + 3 new),
  • full suite: 637 passed, 3 skipped, 1 xfailed (634 + 3 new), plus the same 16 pre-existing data_regression fixture env errors that occur on a clean checkout (markdown-it-py 3.0.0 here, the repo's declared floor).

rahul05ranjan and others added 2 commits November 13, 2026 07:29
A generated empty slug produced an invalid empty id="" attribute
(and a permalink pointing at "#"), while an empty *explicit* ID is
already treated as absent. Extend the same doctrine to generated
slugs: no id, no permalink, and no "" reservation in the slug set.

Fixes executablebooks#175

Co-authored-by: rahul05ranjan <[email protected]>

This branch has not been deployed

No deployments
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.

anchors_plugin: a heading with no slug-able characters gets an empty id="" attribute

1 participant