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
As reported in mkdocs/mkdocs#1970, a header like # foo > " results in the text 'foo > "' in the .toc_tokens attribute, while the the HTML in .toc would be foo > ". If a user was to use the token text in an HTML context, they would understandably need foo > ", but in any other context they probably actually would prefer foo > ".
I see two possible ways to address this:
Assume toc_tokens does not contain HTML and therefore should not contain HTML entities. Therefore, the text should be foo > ", which users can then HTML escape if they are using the text in an HTML context.
Assume toc_tokens does contain HTML, in which case we should run it through the same HTML escape function that the content of toc is run through. Namely markdown.serializers._escape_cdata, which is smart enough not to escape already existing HTML entities. The problem is that that function is private (name starts with an underscore) and should not be relied on outside of the serializer, especially in an extension.
Either way, users in one of the two contexts (HTML or not HTML) will need to convert from whichever we use to the other. However, Markdown is primarily intended to be used in contexts which output HTML, so I'm inclined to go with option 2. Besides, that would be the least disruptive change as only standalone angle brackets in header text would be affected, which presumably, is not very common.
Assume toc_tokens does contain HTML, in which case we should run it through the same HTML escape function that the content of toc is run through. Namely markdown.serializers._escape_cdata, which is smart enough not to escape already existing HTML entities. The problem is that that function is private (name starts with an underscore) and should not be relied on outside of the serializer, especially in an extension.
This is roughly what I went with in #907, though I just used the simple util.code_escape function. This ends up working fine, since I escape HTML special chars (<, >, &) before unstashing the HTML entities. Then there's no need to be smart about when to escape &.
Activity
jimporter commentedon Feb 7, 2020
This is roughly what I went with in #907, though I just used the simple
util.code_escapefunction. This ends up working fine, since I escape HTML special chars (<,>,&) before unstashing the HTML entities. Then there's no need to be smart about when to escape&.Fix escaping of HTML special chars (<, >, &) in `.toc_tokens`