UNPKG

svem

Version:

Svelte in Markdown preprocessor

61 lines (60 loc) 1.84 kB
import {} from "./code-highlight.js"; import { visit } from "unist-util-visit"; import { createTab } from "./tab.js"; const remarkCodePreview = () => { return (tree) => { visit(tree, "directive", (node) => { if (node.name !== "code-preview") { return; } if (!node.children) { node.children = []; } const preview = node.children.filter((child) => { return child.type !== "code" && !child.attributes?.hidden && !child.attributes?.excluded && !child.attributes?.norender; }).map((child) => { if (child.children?.length) { return child.children.map((child2) => child2.value ?? "").join(""); } return child.value ?? ""; }).filter(Boolean).join("\n"); const code = node.children.map((child) => { if (child.children?.length) { return child.children.map((child2) => child2.value ?? "").join(""); } return child.value ?? ""; }).filter(Boolean).join("\n\n"); const nodes = [ { type: "html", attributes: { id: "preview", label: "Preview", class: ["preview-panel"], active: node.attributes?.active === "preview" }, value: preview }, { type: "code", lang: node.attributes?.lang ?? "html", value: code, attributes: { id: "code", label: "Code", class: ["code-panel"], active: node.attributes?.active === "code" } } ]; if (nodes.every((node2) => node2.attributes?.active !== true)) { nodes[0].attributes.active = true; } const tab = createTab(nodes, node.attributes); Object.assign(node, tab); }); }; }; export { remarkCodePreview };