import type { NodeHandler, State } from "comark"; import type { IconData } from "comark-icon"; import { iconHtml, iconStyles } from "comark-icon/html"; import { external, rel } from "./client.ts"; import { css } from "./styles.ts"; import { attr } from "../../../shared/html.ts"; export { colors, default, variants } from "./index.ts"; export type { CardAttributes, CardColor, CardVariant, CardsMeta, CardsOptions, ColumnsAttributes, } from "./index.ts"; // Each render gets a fresh `state.context`, so a document carries the styles once. const styled = new WeakSet(); const styles = (state: State) => { if (styled.has(state.context)) return ""; styled.add(state.context); return ``; }; const card: NodeHandler = async (node, state) => { const [, attrs] = node; // SAFETY: the parse plugin writes `target` as a string. const target = attrs.target ? (attrs.target as string) : undefined; const link = attrs.to ? `` : ""; // SAFETY: the parse plugin writes `icon` as comark-icon's `IconData`. const icon = attrs.icon ? iconHtml(attrs.icon as IconData) : ""; const arrow = attrs.external ? `` : ""; return ( `${styles(state)}${attrs.icon ? iconStyles(state) : ""}
` + `${link}${icon}${arrow}${await state.flow(node, state)}
` ); }; const title: NodeHandler = async (node, state) => `

${await state.flow(node, state)}

`; const group: NodeHandler = async (node, state) => `${styles(state)}
${await state.flow(node, state)}
`; const columns: NodeHandler = async (node, state) => `${styles(state)}
${await state.flow(node, state)}
`; const column: NodeHandler = async (node, state) => `
${await state.flow(node, state)}
`; /** The HTML renderer for `::card-group`, `::card`, and `::columns`. Pass it as `components`. */ export const components: Record< "card" | "card-title" | "card-group" | "columns" | "column", NodeHandler > = { card, "card-title": title, "card-group": group, columns, column, };