{
  "url": "https://unifast.dev/de/docs/packages/core/visit-hast/",
  "locale": "de",
  "title": "visitHast()",
  "description": "Durchläuft und transformiert einen HAST-Baum mithilfe einer Visitor-Funktion.",
  "section": "packages",
  "body": "```ts\nimport { visitHast } from \"@unifast/core\";\n```\n\n## Signatur\n\n```ts\nfunction visitHast(node: HastNode, visitor: (node: HastNode) => HastNode | void): HastNode\n```\n\n## Parameter\n\n### node\n\n| Eigenschaft | Typ | Standard | Beschreibung |\n|----------|------|---------|-------------|\n| `type` | `string` | — | Der Knotentyp (`\"root\"`, `\"element\"`, `\"text\"` usw.) |\n| `children` | `HastNode[]` | — | Kindknoten (für die Typen `\"root\"` und `\"element\"`) |\n\n### visitor\n\n| Eigenschaft | Typ | Standard | Beschreibung |\n|----------|------|---------|-------------|\n| `visitor` | `(node: HastNode) => HastNode \\| void` | — | Eine Funktion, die für jeden Knoten aufgerufen wird; geben Sie einen Knoten zurück, um den Originalknoten zu ersetzen, oder `void`, um ihn beizubehalten |\n\n## Rückgabewert\n\n`HastNode` – Ein neuer Baum mit allen durch die Visitor-Funktion angewendeten Transformationen.\n\n## Verwendung\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## Beispiele\n\n### Eine Klasse zu allen Absätzen hinzufügen\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### Alle Bilder entfernen\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### Alle Links sammeln\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### Code-Blöcke in einen Container einwickeln\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## Verhalten\n\n- **Unveränderlich:** Gibt einen neuen Baum zurück; der ursprüngliche Baum wird nicht verändert\n- **Top-down-Traversierung:** Der Visitor wird auf dem Elternknoten aufgerufen, bevor seine Kinder besucht werden\n- **Ersetzung:** Gibt der Visitor einen Knoten zurück, ersetzt dieser den aktuellen Knoten, bevor die Kinder durchlaufen werden\n- **No-op:** Gibt der Visitor `void` (oder `undefined`) zurück, bleibt der ursprüngliche Knoten erhalten\n- **Rekursiv:** Die Kinder von `\"root\"`- und `\"element\"`-Knoten werden rekursiv besucht",
  "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"
    }
  ]
}
