import type { NodeHandler, State } from "comark"; import { css } from "./styles.ts"; import { attr } from "../../../shared/html.ts"; export { default, PROGRESS_TAG, STATS_TILES_TAG } from "./index.ts"; export type { DeltaAttributes, MetricsMeta, ProgressAttributes } from "./index.ts"; // Each render gets a fresh `state.context`, so a document carries the styles once. const styled = new WeakSet(); const styles = ({ context }: State) => { if (styled.has(context)) return ""; styled.add(context); return ``; }; const delta: NodeHandler = ([, attrs], state) => { const { from, to, change, direction, verdict } = attrs; if (!change) return ""; return ( `${styles(state)}` + (from && to ? `${attr(from)} → ${attr(to)} ` : "") + `${attr(change)}` ); }; const progress: NodeHandler = ([, attrs], state) => { const { value, max, percent, label } = attrs; if (!percent) return ""; return ( `${styles(state)}` + (label ? `${attr(label)}` : "") + `${attr(percent)}` + `${attr(percent)}` ); }; const tiles: NodeHandler = async (node, state) => `${styles(state)}
${await state.flow(node, state)}
`; /** The HTML renderer for `:delta`, `:progress`, and `::stats`. Pass it as `components`; it inlines the styles once per document. */ export const components: Record<"delta" | "progress-bar" | "stat-tiles", NodeHandler> = { delta, "progress-bar": progress, "stat-tiles": tiles, };