What Are Rules?
A rule is a JavaScript object with two properties:filter- Selects which HTML elements the rule applies toreplacement- A function that converts the element to Markdown
Basic Rule Structure
<p> elements and wraps their content with blank lines.
Adding Custom Rules
Use theaddRule() method to add a custom rule:
string
required
A unique identifier for the rule. Used for reference and debugging.
object
required
An object with
filter and replacement properties.addRule() returns the TurndownService instance, allowing you to chain multiple calls.The Filter Property
The filter determines which elements the rule applies to. It can be a string, array, or function.String Filter
Matches elements by tag name (case-insensitive):Array Filter
Matches any of the specified tag names:Function Filter
Provides complete control over element matching:node- The DOM node being evaluatedoptions- The TurndownService options object
true to apply the rule to the node.
The Replacement Function
The replacement function converts the matched element to Markdown. Function signature:content- The Markdown content of the element’s childrennode- The DOM node being convertedoptions- The TurndownService options object
Replacement Examples
Here are real examples from Turndown’s built-in CommonMark rules:Emphasis (Italic) - src/commonmark-rules.js:208
Emphasis (Italic) - src/commonmark-rules.js:208
<em>text</em> to _text_ (or *text* depending on options).Strong (Bold) - src/commonmark-rules.js:217
Strong (Bold) - src/commonmark-rules.js:217
<strong>text</strong> to **text**.Headings - src/commonmark-rules.js:21
Headings - src/commonmark-rules.js:21
Inline Links - src/commonmark-rules.js:143
Inline Links - src/commonmark-rules.js:143
<a href="url" title="title">text</a> to [text](url "title").Blockquotes - src/commonmark-rules.js:38
Blockquotes - src/commonmark-rules.js:38
> to create a blockquote.Inline Code - src/commonmark-rules.js:226
Inline Code - src/commonmark-rules.js:226
Rule Precedence
When converting an element, Turndown selects the first matching rule in this order:- Blank rule - Elements containing only whitespace
- Added rules - Custom rules added with
addRule() - CommonMark rules - Built-in rules for standard Markdown elements
- Keep rules - Elements marked with
keep() - Remove rules - Elements marked with
remove() - Default rule - Fallback for unrecognized elements
Rules added with
addRule() take precedence over CommonMark rules, allowing you to override default behavior.Special Rules
Turndown has several special built-in rules:Blank Rule
Handles elements that contain only whitespace (except<a>, <td>, <th>, and void elements).
Customize with the blankReplacement option:
Keep Rules
Elements marked withkeep() are rendered as HTML in the Markdown output:
keepReplacement option:
Remove Rules
Elements marked withremove() are completely removed from output:
Default Rule
Handles elements not matched by any other rule. Outputs the element’s text content. Customize with thedefaultReplacement option:
Advanced Examples
Handling Custom HTML Elements
Preserving Data Attributes
Conditional Conversion
Related
- Conversion Options - Configure global conversion behavior
- Plugins - Package multiple rules as reusable plugins
- API Reference - Detailed addRule() documentation