Why Escaping Is Needed
Markdown uses special characters to define formatting. When converting HTML to Markdown, these characters in the original content need to be escaped to preserve their literal meaning. For example, without escaping:Escaped Characters
Turndown escapes the following Markdown special characters:Escaped to:
\\Backslashes themselves need escaping since they’re the escape character.Escaped to:
\*Prevents interpretation as emphasis, strong text, or list markers.Escaped to:
\-Prevents interpretation as list markers or setext heading underlines.Only escaped when it appears at the start of a line.Escaped to:
\+Prevents interpretation as a list marker.Pattern: ^+ (plus at start of line followed by space)Escaped to:
\=Prevents interpretation as setext heading underlines.Pattern: ^(=+) (one or more equals signs at line start)Escaped to:
\#Prevents interpretation as ATX heading markers.Pattern: ^(#{1,6}) (1-6 hashes at start of line followed by space)Escaped to:
\`Prevents interpretation as inline code or code block delimiters.Escaped to:
\~~~Prevents interpretation as fenced code block delimiters.Pattern: ^~~~ (three tildes at start of line)Escaped to:
\[Prevents interpretation as link or image syntax.Escaped to:
\]Prevents interpretation as link or image syntax.Escaped to:
\>Prevents interpretation as blockquote markers.Only escaped when it appears at the start of a line.Escaped to:
\_Prevents interpretation as emphasis markers.Escaped to:
1\.Prevents interpretation as ordered list items.Pattern: ^(\d+). (digits followed by period and space at line start)Escape Patterns
The escape patterns are defined in the source code at src/turndown.js:7-21:String.prototype.replace() method.
How Escaping Works
Turndown escapes text in two stages:1. During Node Processing
When processing DOM nodes, text nodes are escaped unless they’re inside code elements (src/turndown.js:164):Text inside
<code> and <pre> elements is never escaped because code should be displayed literally.2. The Escape Method
Theescape() method applies all escape patterns sequentially (src/turndown.js:142-146):
Examples
Escaping List Markers
Escaping Heading Markers
Escaping Emphasis
Escaping Links
Code Is Not Escaped
Customizing Escape Behavior
You can customize the escape behavior by overriding theescape method on the TurndownService prototype.
Custom Escape Method
Less Aggressive Escaping
The default escaping can be aggressive. If you’re confident your content won’t create ambiguous Markdown:Context-Aware Escaping
For more sophisticated escaping based on context:Escaping Performance
Turndown’s escaping approach is optimized for correctness rather than performance:- Regular expression based - Uses regex patterns for pattern matching
- Sequential application - Applies each escape pattern in order
- Applied to all text nodes - Escapes every text node except code
To avoid the complexity and the performance implications of parsing the content of every HTML element as Markdown, Turndown uses a group of regular expressions to escape potential Markdown syntax.As a result:
The escaping rules can be quite aggressive. Some text may be escaped even when it wouldn’t actually be interpreted as Markdown in practice.
Related
- Rules - Learn about custom conversion rules
- Conversion Options - Configure conversion behavior
- API Reference - Detailed escape() method documentation