commonmark/cmark

Automatically generate ids for headers

Opened this issue · 3 comments

I know that this issue was not yet resolved in the spec, but it could potentially be done in an implementation-specific way that does not violate the spec. This is a very useful feature and has been requested both on the forum and elsewhere (see e.g.: github#186).

Would it be in scope to add such an extension to cmark? Is this better sent to a forked implementation like cmark-gfm?

jgm commented

I'm not dead-set against adding extensions here; after all, we have smart quotes.

This is not a show-stopper in my use case, but I feel it would be useful to allow elements to have id's since both HTML and XML allow any element to have an id.

Rather than generating these automatically, how about adding APIs to cmark_node to get or explicitly set an id and modifying the respective rendering code to add id="name" attributes to affected nodes? For example:

  • const char *cmark_node_get_id(cmark_node *node);
  • int cmark_node_set_id(cmark_node *node, const char *id);

Then it would be simple for an application to traverse the parse tree prior to rendering, say, to generate a table of contents, looking for interesting nodes to reference, not necessarily headings. Furthermore using this approach, the ids can be generated by and meaningful to the appplication.

I have forked a proof of concept at https://github.com/iarthair/cmark on the branch node-id-api.

I'll post a simple program that uses it later, my test case was a hack to a larger application. I have an equivalent patch for cmark-gfm which I'll deal with in due course.

There's a few issues to sort, mainly it's only done for HTML and the id attribute is written out in the "wrong" function.

Perhaps this is the way to go?