AST API

Traverse and modify the MarkdownDocument AST after parsing using the visit() utility.

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: The MarkdownDocument to traverse
  • checker: A predicate (node: Node) => boolean that returns true for nodes to visit
  • visitor: A transform (node: Node) => Node | false | void. See Node
  • options (optional): { skip?: number }. skip is the number of top-level nodes (tree.nodes) to skip. Skipped nodes and their descendants are never passed to checker or visitor. Defaults to 0

Returns: void (mutations are applied in place)

The visitor return value controls what happens to the node:

Return valueEffect
voidLeave the node unchanged
NodeReplace the node with the returned value
falseRemove 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
    )
  },
}))
© 2026 Vercel, Inc.