AST API
The comark/utils module exports visit, a tree traversal utility for transforming AST nodes inside a post hook.
visit(tree, checker, visitor, options?)
Traverses all nodes in a MarkdownDocument, calling visitor on every node that passes checker.
Parameters:
tree: TheMarkdownDocumentto traversechecker: A predicate(node: Node) => booleanthat returnstruefor nodes to visitvisitor: A transform(node: Node) => Node | false | void. SeeNodeoptions(optional):{ skip?: number }.skipis the number of top-level nodes (tree.nodes) to skip. Skipped nodes and their descendants are never passed tocheckerorvisitor. Defaults to0
Returns: void (mutations are applied in place)
The visitor return value controls what happens to the node:
| Return value | Effect |
|---|---|
void | Leave the node unchanged |
Node | Replace the node with the returned value |
false | Remove the node from the tree |
Example:
import { defineComarkPlugin } from 'comark'
import { visit } from 'comark/utils'
export default defineComarkPlugin(() => ({
name: 'word-count',
post(state) {
let count = 0
visit(state.tree,
(node) => typeof node === 'string',
(node) => { count += (node as string).split(/\s+/).filter(Boolean).length }
)
state.tree.meta.wordCount = count
},
}))Skipping already-processed nodes
In streaming mode, state.reusableNodes holds the leading nodes that were already processed on an earlier parse. Pass its length as skip to avoid walking them again. Removals and replacements after the skipped nodes are still applied to tree.nodes.
import { defineComarkPlugin } from 'comark'
import { visit } from 'comark/utils'
export default defineComarkPlugin(() => ({
name: 'mark-quotes',
post(state) {
visit(state.tree,
(node) => Array.isArray(node) && node[0] === 'blockquote',
(node) => { (node as any)[1].quote = true },
{ skip: state.reusableNodes?.length ?? 0 }
)
},
}))visitAsync accepts the same options argument.
Use cases
Replacing nodes
Return a new node to replace the matched one. The following example wraps bare URL text nodes in anchor elements:
import type { Node } from 'comark'
import { defineComarkPlugin } from 'comark'
import { visit } from 'comark/utils'
export default defineComarkPlugin(() => ({
name: 'auto-link',
post(state) {
const urlPattern = /https?:\/\/[^\s]+/g
visit(state.tree,
(node) => typeof node === 'string',
(node) => {
const text = node as string
if (!urlPattern.test(text)) return
return ['a', { href: text }, text] as Node
}
)
},
}))Mutating attributes
Return void and mutate the node in place when you only need to update attributes:
import { defineComarkPlugin } from 'comark'
import { visit } from 'comark/utils'
export default defineComarkPlugin(() => ({
name: 'styled-tables',
post(state) {
visit(state.tree,
(node) => Array.isArray(node) && node[0] === 'table',
(node) => {
const el = node as [string, Record<string, any>, ...any[]]
el[1].class = [el[1].class, 'styled-table'].filter(Boolean).join(' ')
}
)
},
}))Removing nodes
Return false to remove matched nodes entirely:
import { defineComarkPlugin } from 'comark'
import { visit } from 'comark/utils'
export default defineComarkPlugin(() => ({
name: 'strip-html-comments',
post(state) {
visit(state.tree,
(node) => Array.isArray(node) && node[0] === null,
() => false
)
},
}))