Same header names generate identical HTML anchors
JCGoran opened this issue · 1 comments
Describe the bug
This is a report from downstream, see mitmproxy/pdoc#534 and mitmproxy/pdoc#497 for more details (was told to report it here).
To Reproduce
Long story short, when using markdown2
as part of pdoc
to generate documentation of a Python project, and one has two identically-named headers in different docstrings (but the same module/file), they will generate an identical anchor.
Expected behavior
Different headers with same names should have different anchors so they can be properly referenced.
Additional context
I haven't found anything in the Markdown spec that mentions what should be done in case of identical headers (or how the anchors should be generated in the first place), so technically this is not a bug, but an issue with the spec being undefined (or implementation-defined?). Nevertheless, it would be nice if there was an option in this library to disambiguate headers with the same name via some option.
The header-ids
extra does generate unique anchors for identical headers:
# abc
# abc
# abc
<h1 id="abc">abc</h1>
<h1 id="abc-2">abc</h1>
<h1 id="abc-3">abc</h1>
I think the problem is that the way pdoc
converts docstrings is by passing each one as a markdown snippet, rather than a single document. Every time pdoc
calls Markdown.convert
, the counter that keeps track of header IDs is reset as part of the _setup_extras
function
The solution here would be to either persist the header count across resets or provide some way of preventing the reset between conversions.