{
  "url": "https://unifast.dev/id/docs/packages/core/visit-hast/",
  "locale": "id",
  "title": "visitHast()",
  "description": "Berjalan dan mentransformasi pohon HAST menggunakan fungsi visitor.",
  "section": "packages",
  "body": "```ts\nimport { visitHast } from \"@unifast/core\";\n```\n\n## Signature\n\n```ts\nfunction visitHast(node: HastNode, visitor: (node: HastNode) => HastNode | void): HastNode\n```\n\n## Parameter\n\n### node\n\n| Properti | Tipe | Default | Deskripsi |\n|----------|------|---------|-------------|\n| `type` | `string` | — | Tipe node (`\"root\"`, `\"element\"`, `\"text\"`, dll.) |\n| `children` | `HastNode[]` | — | Node anak (untuk tipe `\"root\"` dan `\"element\"`) |\n\n### visitor\n\n| Properti | Tipe | Default | Deskripsi |\n|----------|------|---------|-------------|\n| `visitor` | `(node: HastNode) => HastNode \\| void` | — | Sebuah fungsi yang dipanggil untuk setiap node; kembalikan sebuah node untuk menggantikan yang asli, atau `void` untuk membiarkannya |\n\n## Return\n\n`HastNode` — Sebuah pohon baru dengan transformasi yang diterapkan oleh fungsi visitor.\n\n## Penggunaan\n\n```ts\nimport { visitHast } from \"@unifast/core\";\nimport type { HastNode, HastRoot } from \"@unifast/core\";\n\nconst tree: HastRoot = {\n  type: \"root\",\n  children: [\n    {\n      type: \"element\",\n      tagName: \"p\",\n      properties: {},\n      children: [{ type: \"text\", value: \"Hello world\" }],\n    },\n  ],\n};\n\nconst result = visitHast(tree, (node) => {\n  if (node.type === \"text\") {\n    return { type: \"text\", value: node.value.toUpperCase() };\n  }\n});\n\nconsole.log(result);\n// Tree with text \"HELLO WORLD\"\n```\n\n## Contoh\n\n### Menambahkan class ke semua paragraf\n\n```ts\nimport { visitHast } from \"@unifast/core\";\nimport type { HastRoot } from \"@unifast/core\";\n\nconst tree: HastRoot = {\n  type: \"root\",\n  children: [\n    {\n      type: \"element\",\n      tagName: \"p\",\n      properties: {},\n      children: [{ type: \"text\", value: \"First paragraph.\" }],\n    },\n    {\n      type: \"element\",\n      tagName: \"p\",\n      properties: {},\n      children: [{ type: \"text\", value: \"Second paragraph.\" }],\n    },\n  ],\n};\n\nconst result = visitHast(tree, (node) => {\n  if (node.type === \"element\" && node.tagName === \"p\") {\n    return {\n      ...node,\n      properties: { ...node.properties, className: [\"prose\"] },\n    };\n  }\n});\n\n// Both <p> elements now have className: [\"prose\"]\n```\n\n### Menghapus semua gambar\n\n```ts\nimport { visitHast } from \"@unifast/core\";\nimport type { HastRoot } from \"@unifast/core\";\n\nconst tree: HastRoot = {\n  type: \"root\",\n  children: [\n    {\n      type: \"element\",\n      tagName: \"p\",\n      properties: {},\n      children: [\n        { type: \"text\", value: \"See the photo: \" },\n        {\n          type: \"element\",\n          tagName: \"img\",\n          properties: { src: \"photo.jpg\", alt: \"A photo\" },\n          children: [],\n        },\n      ],\n    },\n  ],\n};\n\nconst result = visitHast(tree, (node) => {\n  if (node.type === \"element\" && (node.tagName === \"p\" || node.tagName === \"div\")) {\n    return {\n      ...node,\n      children: node.children.filter(\n        (child) => !(child.type === \"element\" && child.tagName === \"img\"),\n      ),\n    };\n  }\n});\n\n// The <img> element has been removed from the tree\n```\n\n### Mengumpulkan semua link\n\n```ts\nimport { visitHast } from \"@unifast/core\";\nimport type { HastRoot } from \"@unifast/core\";\n\nconst tree: HastRoot = {\n  type: \"root\",\n  children: [\n    {\n      type: \"element\",\n      tagName: \"p\",\n      properties: {},\n      children: [\n        { type: \"text\", value: \"Visit \" },\n        {\n          type: \"element\",\n          tagName: \"a\",\n          properties: { href: \"https://example.com\" },\n          children: [{ type: \"text\", value: \"Example\" }],\n        },\n        { type: \"text\", value: \" and \" },\n        {\n          type: \"element\",\n          tagName: \"a\",\n          properties: { href: \"https://docs.example.com\" },\n          children: [{ type: \"text\", value: \"Docs\" }],\n        },\n      ],\n    },\n  ],\n};\n\nconst links: string[] = [];\n\nvisitHast(tree, (node) => {\n  if (node.type === \"element\" && node.tagName === \"a\") {\n    const href = node.properties.href;\n    if (typeof href === \"string\") {\n      links.push(href);\n    }\n  }\n});\n\nconsole.log(links);\n// [\"https://example.com\", \"https://docs.example.com\"]\n```\n\n### Membungkus code block dalam sebuah container\n\n```ts\nimport { visitHast } from \"@unifast/core\";\nimport type { HastNode, HastRoot } from \"@unifast/core\";\n\nconst tree: HastRoot = {\n  type: \"root\",\n  children: [\n    {\n      type: \"element\",\n      tagName: \"pre\",\n      properties: {},\n      children: [\n        {\n          type: \"element\",\n          tagName: \"code\",\n          properties: { className: [\"language-js\"] },\n          children: [{ type: \"text\", value: \"const x = 1;\" }],\n        },\n      ],\n    },\n  ],\n};\n\nconst result = visitHast(tree, (node) => {\n  if (node.type === \"element\" && node.tagName === \"pre\") {\n    return {\n      type: \"element\",\n      tagName: \"div\",\n      properties: { className: [\"code-block\"] },\n      children: [node],\n    } as HastNode;\n  }\n});\n\n// <pre> is now wrapped inside <div class=\"code-block\">\n```\n\n## Perilaku\n\n- **Imutabel:** Mengembalikan pohon baru; pohon asli tidak dimodifikasi\n- **Penjelajahan top-down:** Visitor dipanggil pada parent sebelum anak-anaknya dikunjungi\n- **Penggantian:** Jika visitor mengembalikan sebuah node, node tersebut menggantikan node saat ini sebelum anak-anaknya dijelajahi\n- **No-op:** Jika visitor mengembalikan `void` (atau `undefined`), node asli dipertahankan\n- **Rekursif:** Anak-anak dari node `\"root\"` dan `\"element\"` dikunjungi secara rekursif",
  "alternates": [
    {
      "locale": "en",
      "url": "https://unifast.dev/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "ja",
      "url": "https://unifast.dev/ja/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/ja/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "zh-CN",
      "url": "https://unifast.dev/zh-CN/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/zh-CN/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "zh-TW",
      "url": "https://unifast.dev/zh-TW/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/zh-TW/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "ko",
      "url": "https://unifast.dev/ko/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/ko/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "fr",
      "url": "https://unifast.dev/fr/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/fr/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "it",
      "url": "https://unifast.dev/it/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/it/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "es",
      "url": "https://unifast.dev/es/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/es/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "pt-BR",
      "url": "https://unifast.dev/pt-BR/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/pt-BR/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "de",
      "url": "https://unifast.dev/de/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/de/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "ru",
      "url": "https://unifast.dev/ru/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/ru/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "hi",
      "url": "https://unifast.dev/hi/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/hi/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "id",
      "url": "https://unifast.dev/id/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/id/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "tr",
      "url": "https://unifast.dev/tr/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/tr/docs/packages/core/visit-hast.json"
    },
    {
      "locale": "vi",
      "url": "https://unifast.dev/vi/docs/packages/core/visit-hast/",
      "api": "https://unifast.dev//api/vi/docs/packages/core/visit-hast.json"
    }
  ]
}
