visitHast()
Walk and transform a HAST tree using a visitor function.
import { visitHast } from "@unifast/core";Signature
function visitHast(node: HastNode, visitor: (node: HastNode) => HastNode | void): HastNodeParameters
node
| Property | Type | Default | Description |
|---|---|---|---|
type | string | — | The node type ("root", "element", "text", etc.) |
children | HastNode[] | — | Child nodes (for "root" and "element" types) |
visitor
| Property | Type | Default | Description |
|---|---|---|---|
visitor | (node: HastNode) => HastNode | void | — | A function called for each node; return a node to replace the original, or void to keep it |
Returns
HastNode — A new tree with any transformations applied by the visitor function.
Usage
import { visitHast } from "@unifast/core";
import type { HastNode, HastRoot } from "@unifast/core";
const tree: HastRoot = {
type: "root",
children: [
{
type: "element",
tagName: "p",
properties: {},
children: [{ type: "text", value: "Hello world" }],
},
],
};
const result = visitHast(tree, (node) => {
if (node.type === "text") {
return { type: "text", value: node.value.toUpperCase() };
}
});
console.log(result);
// Tree with text "HELLO WORLD"Examples
Add a class to all paragraphs
import { visitHast } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const tree: HastRoot = {
type: "root",
children: [
{
type: "element",
tagName: "p",
properties: {},
children: [{ type: "text", value: "First paragraph." }],
},
{
type: "element",
tagName: "p",
properties: {},
children: [{ type: "text", value: "Second paragraph." }],
},
],
};
const result = visitHast(tree, (node) => {
if (node.type === "element" && node.tagName === "p") {
return {
...node,
properties: { ...node.properties, className: ["prose"] },
};
}
});
// Both <p> elements now have className: ["prose"]Remove all images
import { visitHast } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const tree: HastRoot = {
type: "root",
children: [
{
type: "element",
tagName: "p",
properties: {},
children: [
{ type: "text", value: "See the photo: " },
{
type: "element",
tagName: "img",
properties: { src: "photo.jpg", alt: "A photo" },
children: [],
},
],
},
],
};
const result = visitHast(tree, (node) => {
if (node.type === "element" && (node.tagName === "p" || node.tagName === "div")) {
return {
...node,
children: node.children.filter(
(child) => !(child.type === "element" && child.tagName === "img"),
),
};
}
});
// The <img> element has been removed from the treeCollect all links
import { visitHast } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const tree: HastRoot = {
type: "root",
children: [
{
type: "element",
tagName: "p",
properties: {},
children: [
{ type: "text", value: "Visit " },
{
type: "element",
tagName: "a",
properties: { href: "https://example.com" },
children: [{ type: "text", value: "Example" }],
},
{ type: "text", value: " and " },
{
type: "element",
tagName: "a",
properties: { href: "https://docs.example.com" },
children: [{ type: "text", value: "Docs" }],
},
],
},
],
};
const links: string[] = [];
visitHast(tree, (node) => {
if (node.type === "element" && node.tagName === "a") {
const href = node.properties.href;
if (typeof href === "string") {
links.push(href);
}
}
});
console.log(links);
// ["https://example.com", "https://docs.example.com"]Wrap code blocks in a container
import { visitHast } from "@unifast/core";
import type { HastNode, HastRoot } from "@unifast/core";
const tree: HastRoot = {
type: "root",
children: [
{
type: "element",
tagName: "pre",
properties: {},
children: [
{
type: "element",
tagName: "code",
properties: { className: ["language-js"] },
children: [{ type: "text", value: "const x = 1;" }],
},
],
},
],
};
const result = visitHast(tree, (node) => {
if (node.type === "element" && node.tagName === "pre") {
return {
type: "element",
tagName: "div",
properties: { className: ["code-block"] },
children: [node],
} as HastNode;
}
});
// <pre> is now wrapped inside <div class="code-block">Behavior
Immutable: Returns a new tree; the original tree is not modified
Top-down traversal: The visitor is called on the parent before its children are visited
Replacement: If the visitor returns a node, it replaces the current node before children are traversed
No-op: If the visitor returns
void(orundefined), the original node is keptRecursive: Children of
"root"and"element"nodes are visited recursively