Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

toc_tokens escaping is inconsistant #906

Closed

Description

@waylan

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:

  1. 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.
  2. 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.

Activity

  1. added
    bug
    confirmed
    extension
    on Feb 7, 2020
  2. jimporter commented on Feb 7, 2020

    @jimporter
    Contributor

    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 &.

  3. added a commit that references this issue on Mar 30, 2026
    74c0166
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugconfirmedextension

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions