Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] Provide Escaping Options in Lexer #3491

Closed
Bistard opened this issue Oct 14, 2024 · 4 comments · Fixed by #3495
Closed

[Suggestion] Provide Escaping Options in Lexer #3491

Bistard opened this issue Oct 14, 2024 · 4 comments · Fixed by #3495
Labels

Comments

@Bistard
Copy link

Bistard commented Oct 14, 2024

Describe the feature
Is it possible to provide an option in the lexer, like enableEscaping: boolean; that controls the lexer should turn any characters (e.g. &, <, > etc) into escaping characters (e.g. &amp;, &lt;, &gt; etc).

const tokens = marked.lexer(text, {
    enableEscaping: false,
});

Why is this feature necessary?

  1. For example, when I get a token like {type: 'codespan', raw: '<h1>', text: '&lt;h1&gt;'}. I wish to display the text normally. I have to use extra calculations like regular expressions to turn &lt;h1&gt; back to <h1>.
  2. But this is essentially just undoing the process during the first time (default tokenizer in marked) which caused meaningless extra calculations.
  3. So my suggestion is to provide an option that prevents the escaping process in the first place inside the default lexer.

Describe alternatives you've considered
I saw the issue answered in 2022 said I have to override the tokenizer to achieve this.

@UziTech
Copy link
Member

UziTech commented Oct 15, 2024

I noticed this a little bit ago. I am working on moving the escaping to the renderers instead of the tokenizers.

The Tokenizers should just pull the information out of the markdown and create tokens without changing it, then the renderers should handle any changes like HTML escaping.

@Bistard
Copy link
Author

Bistard commented Oct 15, 2024

Your proposal seems better and more logical.

Even though I’m only using the tokenizer and not the renderer in my case, do you still plan to make HTML escaping optional inside the renderer?

@UziTech
Copy link
Member

UziTech commented Oct 15, 2024

No. The default renderers will always escape since that follows the CommonMark spec but they can be overridden

@Bistard
Copy link
Author

Bistard commented Oct 15, 2024

Got it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@UziTech @Bistard and others