Overview
Thefilter property of a rule determines which HTML elements the rule applies to. Filters can be strings, arrays, or functions, providing flexibility for simple and complex matching logic.
Filter Types
String Filter
Matches a single HTML element by tag name (case-insensitive). Example - Paragraph:<p> elements.
Example - Blockquote:
<blockquote> elements.
Example - Line Break:
<br> elements.
Array Filter
Matches multiple HTML elements by tag names. Example - Emphasis:<em> and <i> elements.
Example - Strong:
<strong> and <b> elements.
Example - Headings:
<h1> through <h6>.
Example - Lists:
<ul>) and ordered (<ol>) lists.
Function Filter
A function that receives a DOM node and options object, and returnstrue if the rule should apply to that node.
Returns: boolean - true if the rule should apply to this node
Example: Inline Code
Matches<code> elements that are NOT inside <pre> blocks:
- Checks if the node is a
<code>element - Determines if it’s inside a
<pre>block without siblings (which would be a code block) - Returns
trueonly for inline code, not code blocks
Example: Indented Code Block
Matches code blocks when thecodeBlockStyle option is set to 'indented':
- Checks the
codeBlockStyleoption - Verifies the node is a
<pre>element - Confirms it contains a
<code>child element
Example: Fenced Code Block
Matches code blocks when thecodeBlockStyle option is set to 'fenced':
Example: Inline Links
Matches links when thelinkStyle option is set to 'inlined':
- Checks if inline link style is enabled
- Verifies the node is an
<a>element - Ensures the link has an
hrefattribute
Example: Reference Links
Matches links when thelinkStyle option is set to 'referenced':
Filter Behavior
Case Sensitivity
Tag names in string and array filters are case-insensitive. The filter automatically converts them to lowercase for comparison:Node Names
When checkingnode.nodeName in function filters, it returns the tag name in uppercase. Always compare using uppercase or convert to lowercase:
Filter Evaluation Order
Filters are evaluated in the order of rule precedence. The first matching filter’s replacement function is used.Common Patterns
Checking Parent Elements
Checking Attributes
Checking Child Elements
Combining with Options
Usage with keep() and remove()
Thekeep() and remove() methods also accept filters: