github / cmark-gfm

GitHub's fork of cmark, a CommonMark parsing and rendering library and program in C
Other
909 stars 175 forks source link

Fix quadratic list indexing in commonmark/man/plaintext output formats #322

Closed kevinbackhouse closed 1 year ago

kevinbackhouse commented 1 year ago

Fixes: #321

This adds some code to cmark_render() to calculate the correct list item index and store it on the node, so that output formats like commonmark don't have to calculate it themselves.

Nodes of type CMARK_NODE_ITEM store the same kind of data as CMARK_NODE_LIST, as can be seen here:

https://github.com/github/cmark-gfm/blob/c4de25253b4621bef631fef3092c7d2ebd601830/src/blocks.c#L1371-L1376

So I decided to use the start field to store the index.

gravypod commented 1 year ago

@kevinbackhouse, for what it's worth this passes our internal test suite. I am not a huge fan of how list numbers are calculated in the main render loop, and not at parse time, but I think that can be improved in future PRs.