Skip to content

Add heading id attributes via MD_FLAG_HEADINGIDS - #432

Draft
rnkn wants to merge 1 commit into
mity:masterfrom
rnkn:master
Draft

rnkn wants to merge 1 commit into
mity:masterfrom
rnkn:master

Conversation

@rnkn

@rnkn rnkn commented Sep 17, 2026

Copy link
Copy Markdown

This is a LLM-generated patch.

This adds an id attribute to h1-h6 elements, enabled with the MD_FLAG_HEADINGIDS extension. The id is derived from the heading text: ASCII letters are lowercased, digits are kept, and every other character is replaced with a dash. Only the first 64 bytes of heading text contribute to the id. Deduplication is left to the caller.

Implementation lives in md4c-html.c: while a heading is open, rendered content is diverted into a growable buffer and the heading text is mirrored into a fixed-size buffer. On leave, the opening tag (with id), buffered content and closing tag are emitted in order. No changes to md4c.c.

Adds a --fheading-ids option to md2html.

I had a few go arounds with the ideal implementation and this seemed the only viable option. The introduction of a buffer means md4c would no longer be stack-only, so there's that. It introduces a dependency on stdlib.h and bloats MD_HTML_tag struct considerably.

It works during my testing but given this is LLM-generated code and my understanding of C is limited I can perfectly understand if you reject it outright. Maybe it could be worth some ideas.

Add an id attribute to every HTML heading (h1-h6), enabled with the
MD_FLAG_HEADINGIDS extension flag. The id is derived from the heading
text: ASCII letters are lowercased, digits are kept, and every run of
other characters is replaced with a single dash (trailing dash trimmed).
Only the first 64 bytes of heading text contribute to the id; the full
heading is still rendered. Deduplication is left to the caller.

Implementation lives entirely in the HTML renderer (md4c-html.c): while
a heading is open, rendered content is diverted into a growable buffer
and the heading text is mirrored into a fixed-size slug buffer. On
leave, the opening tag (with id), buffered content and closing tag are
emitted in order. No changes to the parser (md4c.c) or the public detail
structs. Adds a --fheading-ids option to md2html.

Co-authored-by: rnkn <rnkn@users.noreply.github.com>
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