hastToHtml()
Serialisiert einen HAST-Wurzelknoten in einen HTML-String.
import { hastToHtml } from "@unifast/core";Signatur
function hastToHtml(hast: HastRoot): stringParameter
hast
| Eigenschaft | Typ | Standard | Beschreibung |
|---|---|---|---|
type | "root" | — | Bezeichner des Knotentyps |
children | HastNode[] | — | Kindknoten des Baums |
Rückgabewert
string – Der serialisierte HTML-String.
Verwendung
import { hastToHtml } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const hast: HastRoot = {
type: "root",
children: [
{
type: "element",
tagName: "h1",
properties: { id: "hello", className: ["title", "main"] },
children: [
{ type: "text", value: "Hello " },
{
type: "element",
tagName: "strong",
properties: {},
children: [{ type: "text", value: "world" }],
},
],
},
],
};
const html = hastToHtml(hast);
console.log(html);
// <h1 class="title main" id="hello">Hello <strong>world</strong></h1>Beispiele
Grundlegende Serialisierung
import { hastToHtml } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const hast: HastRoot = {
type: "root",
children: [
{
type: "element",
tagName: "p",
properties: {},
children: [
{ type: "text", value: "This is " },
{
type: "element",
tagName: "strong",
properties: {},
children: [{ type: "text", value: "bold" }],
},
{ type: "text", value: " text." },
],
},
],
};
console.log(hastToHtml(hast));
// <p>This is <strong>bold</strong> text.</p>Void-Elemente
Void-Elemente (<br>, <img>, <hr> usw.) werden automatisch selbstschließend ausgegeben:
import { hastToHtml } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const hast: HastRoot = {
type: "root",
children: [
{
type: "element",
tagName: "img",
properties: { src: "photo.jpg", alt: "A photo" },
children: [],
},
],
};
console.log(hastToHtml(hast));
// <img alt="A photo" src="photo.jpg" />Mit compile()-Ausgabe
import { compile } from "@unifast/node";
import { hastToHtml } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const result = compile("**bold text**", { outputKind: "hast" });
const hast: HastRoot = JSON.parse(result.output as string);
console.log(hastToHtml(hast));
// <p><strong>bold text</strong></p>Roh-HTML durchreichen
import { hastToHtml } from "@unifast/core";
import type { HastRoot } from "@unifast/core";
const hast: HastRoot = {
type: "root",
children: [
{ type: "raw", value: "<div class=\"custom\">Raw HTML</div>" },
],
};
console.log(hastToHtml(hast));
// <div class="custom">Raw HTML</div>Verhalten
HTML-Maskierung: Textinhalte werden maskiert (
&,<,>,")Void-Elemente:
area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbrwerden selbstschließend ausgegebenAttribute: Alphabetisch sortiert;
className-Arrays werden mit Leerzeichen verbunden und alsclassgerendert; der boolesche Werttruewird als bloßes Attribut gerendert;false/null/undefinedwerden ausgelassenKommentare: Werden als
<!--value-->gerendertDoctype: Wird als
<!DOCTYPE html>gerendertRaw-Knoten: Werden unverändert und ohne Maskierung ausgegeben