Skip to main content

Syntax

turndownService.escape(string)

Description

Escapes Markdown syntax characters in a string by adding backslashes. This ensures that special characters are not interpreted as Markdown when the output is compiled back to HTML. The method is used internally during conversion but can also be called directly.

Parameters

string
string
required
The string containing text that may have Markdown syntax characters

Returns

escaped
string
A string with Markdown syntax characters escaped using backslashes

Escaped Characters

The following patterns are escaped:
PatternEscaped AsExample
\\\foo\barfoo\\bar
*\*foo*barfoo\*bar
^- (line start)\--foo\-foo
^+ (line start)\+ + foo\+ foo
^=+ (line start)\====\===
^#{1,6} (line start)\### foo\## foo
`\`foo`barfoo\`bar
^~~~ (line start)\~~~~~~foo\~~~foo
[\[foo[bar]foo\[bar]
]\]foo[bar]foo\[bar\]
^> (line start)\>>quote\>quote
_\_foo_barfoo\_bar
^\d+. (line start)\d+\. 1. item1\. item

Examples

Basic Escaping

var result = turndownService.escape('Hello *world*')
// Result: 'Hello \*world\*'

Escape Multiple Characters

var result = turndownService.escape('Check out [this](link) and `code`')
// Result: 'Check out \[this\](link) and \`code\`'

Escape Heading Markers

var result = turndownService.escape('## Not a heading')
// Result: '\## Not a heading'

Escape List Markers

var result = turndownService.escape('1. This is not a list')
// Result: '1\. This is not a list'

Escape Backslashes

var result = turndownService.escape('Path: C:\\Users\\Documents')
// Result: 'Path: C:\\\\Users\\\\Documents'

Using in Custom Rules

turndownService.addRule('customText', {
  filter: 'span',
  replacement: function (content, node, options) {
    // Manually escape content if needed
    var escaped = turndownService.escape(node.textContent)
    return escaped
  }
})

Internal Usage

The escape method is called automatically during conversion for text nodes (but not for code elements):
// From turndown.js source (simplified)
if (node.nodeType === 3) {
  replacement = node.isCode ? node.nodeValue : self.escape(node.nodeValue)
}

Customizing Escape Behavior

You can override the escape method to customize escaping behavior:
TurndownService.prototype.escape = function (string) {
  // Custom escape logic
  return string.replace(/\*/g, '\\*')
}

Example: Less Aggressive Escaping

var turndownService = new TurndownService()

// Override with custom escape that only escapes asterisks
turndownService.escape = function (string) {
  return string.replace(/\*/g, '\\*')
}

Notes

  • Text in code elements is never passed to escape
  • The escaping rules are intentionally aggressive to prevent false positives
  • Only line-start patterns (like ^-, ^>, ^#{1,6}) are escaped when at the beginning of a line
  • The method uses an array of regex patterns applied sequentially
  • You can override TurndownService.prototype.escape for custom escaping behavior
  • Be careful when customizing: too little escaping may cause parsing issues when converting back to HTML

Build docs developers (and LLMs) love