Avoiding double escape of html #602

Open
opened 2026-01-29 14:40:49 +00:00 by claunia · 2 comments
Owner

Originally created by @magom001 on GitHub (May 2, 2023).

I am having trouble figuring out how to disable escaping of certain characters.

Example: https://dotnetfiddle.net/Jx9koN

It is being replaced in WriteEscapeSlow with no way to turn if off.

What would be the correct approach to fix it?

Originally created by @magom001 on GitHub (May 2, 2023). I am having trouble figuring out how to disable escaping of certain characters. Example: https://dotnetfiddle.net/Jx9koN It is being replaced in [WriteEscapeSlow](https://github.com/xoofx/markdig/blob/b7cb169fd3a924e1e0d4e2e487a22c51caaedd10/src/Markdig/Renderers/HtmlRenderer.cs#L192) with no way to turn if off. What would be the correct approach to fix it?
claunia added the wontfix label 2026-01-29 14:40:49 +00:00
Author
Owner

@xoofx commented on GitHub (May 3, 2023):

It is by design (and afaik, a requirement for HTML) and can't be turned off. If there is an entity like ', even within backsticks, it will have to escape the & in the resulting HTML. All (CommonMark) Markdown implementations are respecting this as you can see here

@xoofx commented on GitHub (May 3, 2023): It is by design (and afaik, a requirement for HTML) and can't be turned off. If there is an entity like `'`, even within backsticks, it will have to escape the `&` in the resulting HTML. All (CommonMark) Markdown implementations are respecting this as you can see [here](https://babelmark.github.io/?text=Here+is+some+text+with+words+wrapped+into+single+quotes%3A%0A%0A%60She+said+%26apos%3Bhello%26apos%3B+to+the+%27stranger%27+and+then+walked+away.%60)
Author
Owner

@magom001 commented on GitHub (May 4, 2023):

For anyone who might face this issue, I solved it by creating an extension which replaces certain rerenders with my owns.

By default Markdig html renderers will use HtmlRenderer.WriteEscape method, which will escape & and some other characters. I simply copied the implementation of those renerers and replaces WriteEscape with Write.

You should not normally do any of that, but for some technical reasons, in our code the markdown string has to be escaped before it is fed into the Markdown.ToHtml method.

@magom001 commented on GitHub (May 4, 2023): For anyone who might face this issue, I solved it by creating an extension which replaces certain rerenders with my owns. By default Markdig html renderers will use HtmlRenderer.WriteEscape method, which will escape & and some other characters. I simply copied the implementation of those renerers and replaces WriteEscape with Write. You should not normally do any of that, but for some technical reasons, in our code the markdown string has to be escaped before it is fed into the Markdown.ToHtml method.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#602