// Default inline-level renderers: text, emphasis, strong, delete, inlineCode, // link, image, break, html, and reference variants. import { text, em, strong, code, a, img, span, br, el, unsafeHtml } from '@llui/dom' import type { ChildNode, Mountable } from '@llui/dom' import type { Text, Emphasis, Strong, Delete, InlineCode, Link, Image, Break, Html, LinkReference, ImageReference, } from 'mdast' import type { NodeRenderer, RenderContext } from '../types.js' import type { Renderable } from '@llui/dom' import { resolveUrl } from '../security.js' /** Generic element helper for tags @llui/dom doesn't export by name (e.g. `del`). */ function tag(name: string) { return (props: Record, children: readonly ChildNode[] = []): Mountable => el(name, props, children) } const del = tag('del') const renderText: NodeRenderer = (node) => [text(node.value)] const renderEmphasis: NodeRenderer = (node, ctx) => [em(ctx.renderChildren(node))] const renderStrong: NodeRenderer = (node, ctx) => [strong(ctx.renderChildren(node))] const renderDelete: NodeRenderer = (node, ctx) => [del({}, ctx.renderChildren(node))] const renderInlineCode: NodeRenderer = (node) => [code([text(node.value)])] const renderBreak: NodeRenderer = () => [br([])] const renderLink: NodeRenderer = (node, ctx) => { const href = resolveUrl(node.url, node, ctx.options) const children = ctx.renderChildren(node) if (href === null) return [span(children)] // neutralized → keep the visible text const props: Record = { href } if (node.title) props.title = node.title return [a(props, children)] } const renderImage: NodeRenderer = (node, ctx) => { const src = resolveUrl(node.url, node, ctx.options) if (src === null) return [] // blocked → drop the image const props: Record = { src, alt: node.alt ?? '' } if (node.title) props.title = node.title return [img(props)] } /** Render one already-joined run of raw HTML (see {@link renderHtmlRun}). * Raw HTML is dropped by default (safe for untrusted/LLM content). It renders * only when the consumer supplies a `sanitizeHtml` hook, which sees the raw * string and returns the safe HTML to inject — there is no unsanitized * passthrough. */ export function renderHtmlRun(html: string, options: RenderContext['options']): Renderable { const sanitize = options.sanitizeHtml if (!sanitize) return [] return [unsafeHtml(sanitize(html))] } const renderHtml: NodeRenderer = (node, ctx) => renderHtmlRun(node.value, ctx.options) const renderLinkReference: NodeRenderer = (node, ctx) => { const def = ctx.definitions.get(node.identifier.toLowerCase()) const children = ctx.renderChildren(node) if (!def) return children // unresolved reference → render its label text // Route through the SAME policy (transformLink → sanitize) as inline links, // so a consumer's transformLink governs reference-style links too. const href = resolveUrl(def.url, node, ctx.options) if (href === null) return [span(children)] const props: Record = { href } if (def.title) props.title = def.title return [a(props, children)] } const renderImageReference: NodeRenderer = (node, ctx) => { const def = ctx.definitions.get(node.identifier.toLowerCase()) if (!def) return [text(node.alt ?? '')] const src = resolveUrl(def.url, node, ctx.options) if (src === null) return [] const props: Record = { src, alt: node.alt ?? '' } if (def.title) props.title = def.title return [img(props)] } export const inlineRenderers = { text: renderText, emphasis: renderEmphasis, strong: renderStrong, delete: renderDelete, inlineCode: renderInlineCode, break: renderBreak, link: renderLink, image: renderImage, html: renderHtml, linkReference: renderLinkReference, imageReference: renderImageReference, }