{
  "url": "https://unifast.dev/ja/docs/packages/core/visit-hast/",
  "locale": "ja",
  "title": "visitHast()",
  "description": "ビジター関数を使って HAST ツリーを走査・変換します。",
  "section": "packages",
  "body": "```ts\nimport { visitHast } from \"@unifast/core\";\n```\n\n## シグネチャ\n\n```ts\nfunction visitHast(node: HastNode, visitor: (node: HastNode) => HastNode | void): HastNode\n```\n\n## パラメータ\n\n### node\n\n| プロパティ | 型 | デフォルト | 説明 |\n|----------|------|---------|-------------|\n| `type` | `string` | — | ノードの種別 (`\"root\"`、`\"element\"`、`\"text\"` など) |\n| `children` | `HastNode[]` | — | 子ノード (`\"root\"` と `\"element\"` 型の場合) |\n\n### visitor\n\n| プロパティ | 型 | デフォルト | 説明 |\n|----------|------|---------|-------------|\n| `visitor` | `(node: HastNode) => HastNode \\| void` | — | 各ノードに対して呼ばれる関数。元のノードを置き換えたい場合はノードを返し、そのままにしたい場合は `void` を返します |\n\n## 戻り値\n\n`HastNode` — ビジター関数による変換が適用された新しいツリーです。\n\n## 使い方\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## 使用例\n\n### すべての段落にクラスを追加する\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### すべての画像を削除する\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### すべてのリンクを収集する\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### コードブロックをコンテナでラップする\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## 動作\n\n- **イミュータブル:** 新しいツリーを返し、元のツリーは変更しない\n- **トップダウンの走査:** 子ノードを訪問する前に、親ノードに対してビジターが呼ばれる\n- **置き換え:** ビジターがノードを返した場合、子ノードを走査する前に現在のノードが置き換えられる\n- **ノーオペレーション:** ビジターが `void` (または `undefined`) を返した場合、元のノードがそのまま残る\n- **再帰的:** `\"root\"` と `\"element\"` ノードの子は再帰的に訪問される",
  "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"
    }
  ]
}
