export type ApiReferenceSymbol = { aliases: string[]; documentation: string; entrypoint: string; kind: string; name: string; runtime: boolean; signature: string; source: string | null; typeParameters: string[]; }; export type ApiReferenceManifest = { packages: { platejs: { symbols: Record; }; }; }; export type ExtractedProp = { default?: string; deprecated?: boolean; description?: string; experimental?: boolean; name: string; required: boolean; type: string; }; export type ExtractedDataAttribute = { name: string; values?: string[]; }; export type ExtractedComponentExport = { dataAttributes?: ExtractedDataAttribute[]; description?: string; extends?: string[]; name: string; props: ExtractedProp[]; source: string; }; export type ComponentPropsManifest = { components: Record; schemaVersion: 1; }; export type SpecEvidenceItem = { id: string; text: string; }; export type SpecEvidenceGroup = { items: SpecEvidenceItem[]; title: string; }; export type SpecEvidenceDomain = { count: number; groups: SpecEvidenceGroup[]; }; export type SpecEvidenceManifest = { schemaVersion: 1; specs: Record; }; export type ApiReferenceQuery = { entrypoint?: string; kind?: string; prefix?: string; runtimeOnly?: boolean; symbols?: string[]; }; export type ToolbarButtonCatalogItem = { description?: string; name: string; title?: string; }; const PIPE_REGEX = /\|/g; const NEWLINE_REGEX = /\r?\n/g; const BACKTICK_RUN_REGEX = /`+/g; const DEPRECATED_TAG_REGEX = /@deprecated\b/i; const EXPERIMENTAL_TAG_REGEX = /@experimental\b/i; export function escapeMarkdownCell(value: string | undefined) { return String(value ?? '') .replace(PIPE_REGEX, '\\|') .replace(NEWLINE_REGEX, ' ') .trim(); } export function formatInlineCode(value: string) { let maxRun = 0; for (const match of value.matchAll(BACKTICK_RUN_REGEX)) { if (match[0].length > maxRun) { maxRun = match[0].length; } } const fence = '`'.repeat(maxRun + 1); const pad = value.startsWith('`') || value.endsWith('`') || (value.startsWith(' ') && value.endsWith(' ') && value.trim().length > 0) ? ' ' : ''; return `${fence}${pad}${value}${pad}${fence}`; } export function getApiSymbolFlags(symbol: ApiReferenceSymbol) { return { deprecated: DEPRECATED_TAG_REGEX.test(symbol.documentation), experimental: EXPERIMENTAL_TAG_REGEX.test(symbol.documentation), }; } export function queryApiReferenceSymbolsFromList( allSymbols: ApiReferenceSymbol[], { entrypoint, kind, prefix, runtimeOnly = false, symbols }: ApiReferenceQuery ): ApiReferenceSymbol[] { const symbolSet = symbols?.length ? new Set(symbols) : null; const filtered = allSymbols.filter((entry) => { if (entrypoint && entry.entrypoint !== entrypoint) return false; if (kind && entry.kind !== kind) return false; if (runtimeOnly && !entry.runtime) return false; if (prefix && !entry.name.startsWith(prefix)) return false; if (symbolSet && !symbolSet.has(entry.name)) return false; return true; }); if (symbols?.length) { const order = new Map(symbols.map((name, index) => [name, index])); return filtered.sort((a, b) => { const diff = (order.get(a.name) ?? 0) - (order.get(b.name) ?? 0); return diff !== 0 ? diff : a.entrypoint.localeCompare(b.entrypoint); }); } return filtered.sort((a, b) => { if (a.runtime !== b.runtime) return a.runtime ? -1 : 1; return a.name.localeCompare(b.name); }); } export function renderApiReferenceMarkdownFromSymbols( allSymbols: ApiReferenceSymbol[], query: ApiReferenceQuery ): string { const entries = queryApiReferenceSymbolsFromList(allSymbols, query); if (entries.length === 0) return ''; const rows = [ '| Symbol | Kind | Signature | Description |', '| --- | --- | --- | --- |', ]; for (const entry of entries) { const flags = getApiSymbolFlags(entry); const badges = [ flags.deprecated ? '_(deprecated)_' : '', flags.experimental ? '_(experimental)_' : '', ] .filter(Boolean) .join(' '); const nameCell = badges ? `${formatInlineCode(entry.name)} ${badges}` : formatInlineCode(entry.name); const signature = entry.signature.split('\n')[0]; const desc = escapeMarkdownCell(entry.documentation || '-'); rows.push( `| ${nameCell} | ${formatInlineCode(entry.kind)} | ${formatInlineCode(escapeMarkdownCell(signature))} | ${desc} |` ); } const header = query.entrypoint ? `Exports from \`${query.entrypoint}\`:\n\n` : ''; return `${header}${rows.join('\n')}`; } export function renderComponentPropsMarkdownFromExports( exportsList: ExtractedComponentExport[] ): string { if (exportsList.length === 0) return ''; const blocks: string[] = ['## Props']; for (const exp of exportsList) { const lines: string[] = [`### ${exp.name}`]; if (exp.description) { lines.push('', exp.description); } if (exp.extends?.length) { lines.push( '', `Extends: ${exp.extends.map((item) => formatInlineCode(item)).join(', ')}` ); } if (exp.props.length > 0) { lines.push( '', '| Prop | Type | Default | Description |', '| --- | --- | --- | --- |' ); for (const prop of exp.props) { const badges = [ prop.required ? '_(required)_' : '', prop.deprecated ? '_(deprecated)_' : '', prop.experimental ? '_(experimental)_' : '', ] .filter(Boolean) .join(' '); const propCell = badges ? `${formatInlineCode(prop.name)} ${badges}` : formatInlineCode(prop.name); const typeCell = formatInlineCode(escapeMarkdownCell(prop.type)); const defaultCell = prop.default ? formatInlineCode(escapeMarkdownCell(prop.default)) : '-'; const descCell = escapeMarkdownCell(prop.description || '-'); lines.push( `| ${propCell} | ${typeCell} | ${defaultCell} | ${descCell} |` ); } } if (exp.dataAttributes?.length) { lines.push('', '| Data attribute | Values |', '| --- | --- |'); for (const attr of exp.dataAttributes) { const valuesCell = attr.values?.length ? attr.values .map((v) => formatInlineCode(escapeMarkdownCell(v))) .join(' \\| ') : '_present / dynamic_'; lines.push(`| ${formatInlineCode(`[${attr.name}]`)} | ${valuesCell} |`); } } blocks.push(lines.join('\n')); } return blocks.join('\n\n'); } export function renderSpecListMarkdownFromDomain( domain: SpecEvidenceDomain | undefined ): string { if (!domain || domain.groups.length === 0) return ''; const sections: string[] = [ `_Verified by ${domain.count} automated test${domain.count === 1 ? '' : 's'}._`, ]; for (const group of domain.groups) { const items = group.items.map( (item) => `- [x] ${item.text} (\`${item.id}\`)` ); sections.push(`### ${group.title}\n\n${items.join('\n')}`); } return sections.join('\n\n'); } export function renderToolbarButtonsCatalogMarkdownFromItems( items: ToolbarButtonCatalogItem[], { linkToComponentDocs = false }: { linkToComponentDocs?: boolean } = {} ): string { const rows = items .filter((item) => item.name.endsWith('-toolbar-button')) .sort((a, b) => (a.title || a.name).localeCompare(b.title || b.name)) .map((item) => { const title = escapeMarkdownCell(item.title || item.name); const buttonCell = linkToComponentDocs ? `[${title}](/docs/components/${item.name}.md)` : title; return `| ${buttonCell} | ${escapeMarkdownCell(item.description)} | \`npx shadcn@latest add @plate/${item.name}\` |`; }); if (rows.length === 0) return ''; return [ '| Button | Purpose | Install |', '| --- | --- | --- |', ...rows, ].join('\n'); }