Skip to main content
Rules define how HTML elements are converted to Markdown. Turndown includes built-in CommonMark rules and allows you to add custom rules to handle additional elements or override default behavior.

What Are Rules?

A rule is a JavaScript object with two properties:
  • filter - Selects which HTML elements the rule applies to
  • replacement - A function that converts the element to Markdown

Basic Rule Structure

This rule selects <p> elements and wraps their content with blank lines.

Adding Custom Rules

Use the addRule() 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:
The function receives:
  • node - The DOM node being evaluated
  • options - The TurndownService options object
Return true to apply the rule to the node.
Tag names in the filter must be lowercase, even if the HTML uses uppercase tags.

The Replacement Function

The replacement function converts the matched element to Markdown. Function signature:
Parameters:
  • content - The Markdown content of the element’s children
  • node - The DOM node being converted
  • options - The TurndownService options object
Returns: A Markdown string

Replacement Examples

Here are real examples from Turndown’s built-in CommonMark rules:
Converts <em>text</em> to _text_ (or *text* depending on options).
Converts <strong>text</strong> to **text**.
Converts headings to either setext or atx style based on options.
Prefixes each line with > to create a blockquote.
Handles backtick escaping and edge cases for inline code.

Rule Precedence

When converting an element, Turndown selects the first matching rule in this order:
  1. Blank rule - Elements containing only whitespace
  2. Added rules - Custom rules added with addRule()
  3. CommonMark rules - Built-in rules for standard Markdown elements
  4. Keep rules - Elements marked with keep()
  5. Remove rules - Elements marked with remove()
  6. 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 with keep() are rendered as HTML in the Markdown output:
Customize with the keepReplacement option:

Remove Rules

Elements marked with remove() are completely removed from output:

Default Rule

Handles elements not matched by any other rule. Outputs the element’s text content. Customize with the defaultReplacement option:

Advanced Examples

Handling Custom HTML Elements

Preserving Data Attributes

Conditional Conversion