Skip to content

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

Open

Description

@rahul05ranjan

What's wrong

anchors_plugin generates an empty id attribute for any selected-level heading whose title has no slug-able characters, e.g. ## ???:

from markdown_it import MarkdownIt
from mdit_py_plugins.anchors import anchors_plugin

md = MarkdownIt().use(anchors_plugin)
md.render("## ???\n")
# '<h2 id="">???</h2>\n'

An empty id is invalid HTML (the value must not be the empty string), and a second such heading gets the odd id="-1" (the empty string deduplicated):

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

With permalink=True it also emits a link that points nowhere:

md = MarkdownIt().use(anchors_plugin, permalink=True)
md.render("## ???\n")
# '<h2 id="">??? <a class="header-anchor" href="#">¶</a></h2>\n'

The plugin already documents the right doctrine for the explicit-ID path — "an empty explicit ID counts as no ID" (test_explicit_heading_id_edge_cases even asserts 'id=""' not in rendered and 'href="#"' not in rendered) — but the generated-slug path never checks for an empty result, so the same invalid HTML slips in from slugify() (a title of only non-word characters like ???, ***, or $$$ slugifies to "").

Expected

A heading whose generated slug is empty should be treated the same way as an empty explicit ID: no id attribute at all, and no permalink for it (href="#" would point at the top of the page).

Environment: mdit-py-plugins 0.7.0 (master fde61f4), markdown-it-py 3.0.0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions