How rules work in Turndown
masterTurndown uses rules to transform HTML elements into Markdown. A rule consists of two parts:
filter: Determines which elements are selected. It can be a string (tag name), an array of strings, or a function that returns a boolean based on the node and current options.replacement: A function that determines the Markdown output. It receives the node'scontent, thenodeitself, and theoptionsobject.
Rule Precedence
Turndown picks the first matching rule based on this order:
- Blank rule
- Added rules (via
addRule) - Commonmark rules
- Keep rules
- Remove rules
- Default rule
// Example of a custom rule
{
filter: ['em', 'i'],
replacement: function (content, node, options) {
return options.emDelimiter + content + options.emDelimiter
}
}