import { mergeInlineNodes, Node } from "@dbushell/hyperless"; import { renderNode } from "../render.ts"; import type { BlockPlugin, HmmBlock, HmmOptions } from "../types.ts"; import { parentTags } from "../html.ts"; import { hmmarkdown } from "../mod.ts"; import { parseHTML } from "../utils.ts"; /** @todo Configurable option? */ const tab = " "; const plugin: BlockPlugin = { type: "paragraph", multiline: true, matchStart: () => false, render: (block: HmmBlock, options: HmmOptions) => { if (block.lines.length === 1 && block.lines[0] === "") { return ""; } // Parse content as HTML and merge inline elements. // e.g. "Start middle end." becomes one text node. const content = block.lines.join("\n"); const root = parseHTML(content, options); mergeInlineNodes(root, options.inlineTags); const render = (node: Node) => { for (const child of node.children) { if (child.type === "TEXT") { let { raw } = child; // Pass nested text through the block level parser if (node.depth) { const indent = new RegExp(`^${tab.repeat(node.depth)}`, "gm"); raw = raw.replace(indent, ""); child.raw = hmmarkdown(raw); continue; } // Render inline markdown only child.raw = renderNode(raw, options, "html"); continue; } // Recurse special elements if (parentTags.has(child.tag)) { render(child); continue; } // For other elements render inline markdown const text = renderNode(child.toString(), options, "html"); const textNode = new Node(null, "TEXT", text); child.replace(textNode); } }; render(root); block.render = root.toString(); return block.render; }, }; export default plugin;