Skip to content

Add Opt-In GitHub-Compatible Heading ID Generation to TOC Extension #1562

@dhruvjain1512

Description

@dhruvjain1512

Problem feature would solve:
Users want Markdown heading ids that match GitHub-style anchors when using the toc extension. Today toc uses its default slugify which differs on underscores, punctuation, Unicode normalization, and duplicate handling. Add an opt-in mode so inputs like "A_B C-D!" yield ids identical to GitHub such as "a-b-c-d", with stable numeric suffixes for duplicates.

Proposed solution:
Add an optional config key for the toc extension: id_strategy with values "default" (current behavior) or "github". When "github", generate ids with a GitHub-like slugifier: lowercase; Unicode NFKD; drop combining marks; remove non-alphanumerics except hyphen; map spaces and underscores to hyphen; collapse repeated hyphens; trim leading and trailing hyphens. Handle duplicates by suffixing "-1", "-2", etc., in document order. Precedence rules: an explicit id on the heading wins; if a user supplies a slugify callable, it overrides id_strategy; otherwise use the selected strategy. Only ids and matching internal href targets change; permalink HTML and list structure remain unchanged. If id_strategy is provided with an unsupported value, raise ValueError and include "id_strategy" in the message.

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