You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
toc: Unusual characters in heading ids not well supported #1493
I noticed that toc encodes characters like * as \x0242\x03, 42 being the index of * in the ASCII table. This causes a discrepancy between the permalink of a heading and the link in the table of contents.
mkdir /tmp/toc
cd /tmp/toc
python -m venv .venv
. .venv/bin/activate
python -m pip install mkdocs
python -m mkdocs new .
index file:
# Welcome
Demonstrating an issue with HTML ids and `toc`.
## `*Foo*` { id="\*Foo\*" }- Click on `*Foo*`'s permalink: `#*Foo*` in the URL.
- Click on `*Foo*` in the table of contents: `#%0242%03Foo%0242%03` in the URL.
mkdocs config:
site_name: My Docsmarkdown_extensions:
- attr_list
- toc:
permalink: true
Serve and observe the behavior described in the index page.
I'm not saying this is a bug. I'm just curious if this is expected, and whether there would be a way improve support for headings with such "unusual" ids. This would help for the work I'm doing with mkdocstrings, where we try to expand our languages support, and some languages might use uncommon characters in object identifiers. Not only toc would have to work, but also mkdocs-autorefs, which picks up ids from the table of contents when registering URLs and anchors to objects.
I believe HTML5 supports any kind of characters in ids. Some of them just cause a bit of pain, like . or #, because they then need to be escaped in CSS selectors.
That is possible. As explained in that discussion, the various parts of the parser rely on each other and require that the parts run in a certain order. I know you are often recalling parts of the parser in an unusual order, which could result in some unescaping being missed or some similar issue. That's why I ask for input, actual output and expected output.
So, here's how we render HTML headings within mkdocstrings:
{# just an example #}{%filterheading(
heading_level + 1,
role="module",
id="*foo*",
class="doc doc-heading",
toc_label="*foo*",
) %}
Hello from Foo!
{%endfilter%}
And here's the final output (heading then TOC):
<h2id="*foo*" class="doc doc-heading"> Hello from Foo!
<ahref="#*foo*" class="headerlink" title="Permanent link">¤</a></h2>
Call unescape on the value assigned to the toc_token['id'] (toc.py#L394).
Option 1 fixes the instance issue while option 2 ensures any future issues are addressed as well. However, option 2 would be redundant in the exiting test. My inclination is to go with option 1 and expect each individual extension to properly unescape its own output.
Activity
pawamoy commentedon Dec 10, 2024
Might be related to #864.
waylan commentedon Dec 12, 2024
I'm not sure I follow. Could you please provide sample input, actual output and expected output as 3 separate code blocks?
That is possible. As explained in that discussion, the various parts of the parser rely on each other and require that the parts run in a certain order. I know you are often recalling parts of the parser in an unusual order, which could result in some unescaping being missed or some similar issue. That's why I ask for input, actual output and expected output.
pawamoy commentedon Dec 12, 2024
Of course, here are some sample inputs/outputs 🙂
Input
## `*Foo*` { id="\*Foo\*" }Actual heading output
Actual table of contents output
Expected table of contents output (only a supposition)
pawamoy commentedon Dec 12, 2024
The reason why I'm escaping
*is that otherwiseattr_listfails to parse it, outputting this HTML:pawamoy commentedon Dec 12, 2024
This issue might actually not arise from inside mkdocstrings, since we output HTML directly (so
attr_listisn't involved). I'll check and report back.waylan commentedon Dec 12, 2024
I can confirm that this is indeed a bug.
Presumably, this requires a similar fix to #864 when building the
toc_tokens.pawamoy commentedon Dec 12, 2024
So, here's how we render HTML headings within mkdocstrings:
And here's the final output (heading then TOC):
(TOC styled by Material for MkDocs)
As we can see mkdocstrings doesn't actually suffer from this, since we bypass
attr_listprobably, or override some parts oftoc.Anyway, glad you were able to reproduce the issue!
Fixing it will at least make it much easier for mkdocs-autorefs to test its upcoming features 👍
waylan commentedon Dec 12, 2024
So, it appears this is an issue with attr_lists. If the id is defined outside of an attr_list, then it is fine.
In fact, we already have a test for that.
There are 2 ways we could fix this.
toc_token['id'](toc.py#L394).Option 1 fixes the instance issue while option 2 ensures any future issues are addressed as well. However, option 2 would be redundant in the exiting test. My inclination is to go with option 1 and expect each individual extension to properly unescape its own output.
pawamoy commentedon Dec 12, 2024
Option 1 sounds reasonable. I don't expect a lot of extensions to be affected by this.
Backslash Unescape IDs set via `attr_list` on `toc`
waylan commentedon Dec 12, 2024
Actually, I couldn't create a test that failed on the
attr_listextension alone. So I went with option 2.Backslash Unescape IDs set via `attr_list` on `toc`