{
  "url": "https://unifast.dev/ko/docs/packages/core/visit-hast/",
  "locale": "ko",
  "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// 텍스트가 \"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// 두 <p> 요소 모두 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// 트리에서 <img> 요소가 제거됩니다\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>가 <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"
    }
  ]
}
