import type {
MetadataOutput,
ComponentDoc,
HookDoc,
TypeDoc,
OtherDoc,
MemberDoc,
ParameterDoc,
BaseSymbolDoc,
ExternalPackageRef,
} from './types';
import { groupByAnnotation, type AnnotationGroup } from './annotation-groups';
/**
* Format MetadataOutput as a self-contained HTML document.
*/
export function formatMetadataAsHtml(data: MetadataOutput): string {
const { package: pkg, legend, categories, externalReferences } = data;
return `
API Metadata: ${esc(pkg.name)}
${renderStyles()}
API Metadata: ${esc(pkg.name)} v${esc(pkg.version)}
${renderLegend(legend)}
${renderSummary(categories)}
${renderComponents(categories.components)}
${renderHooks(categories.hooks)}
${renderTypes(categories.types)}
${renderOthers(categories.others)}
${renderExternalReferences(externalReferences)}
`;
}
// ---------------------------------------------------------------------------
// Sections
// ---------------------------------------------------------------------------
function renderLegend(legend: MetadataOutput['legend']): string {
const rows = Object.values(legend)
.map(e => `| ${esc(e.name)} | ${esc(e.description)} |
`)
.join('\n');
return `
Legend
| Category | Description |
${rows}
`;
}
function renderSummary(categories: MetadataOutput['categories']): string {
const counts = [
['Components', 'cat-components', Object.keys(categories.components).length],
['Hooks', 'cat-hooks', Object.keys(categories.hooks).length],
['Types', 'cat-types', Object.keys(categories.types).length],
['Others', 'cat-others', Object.keys(categories.others).length],
] as const;
const rows = counts
.map(([name, anchor, count]) => `| ${name} | ${count} |
`)
.join('\n');
return ``;
}
function renderComponents(components: Record): string {
const all = Object.values(components);
if (all.length === 0) {
return '';
}
const groups = groupByAnnotation(all);
return `
Components
(${all.length})
${renderAnnotationGroups(groups, renderComponentItem)}
`;
}
function renderComponentItem(comp: ComponentDoc): string {
const propsRef = comp.propsType ? `Props: ${renderRef(comp.propsType)}
` : '';
return `
${esc(comp.name)}
${comp.description ? `
${esc(comp.description)}
` : ''}
Type: ${esc(comp.typeSignature)}
${propsRef}
${renderTagsBadges(comp.tags)}
`;
}
function renderHooks(hooks: Record): string {
const all = Object.values(hooks);
if (all.length === 0) {
return '';
}
const groups = groupByAnnotation(all);
return `
Hooks
(${all.length})
${renderAnnotationGroups(groups, renderHookItem)}
`;
}
function renderHookItem(hook: HookDoc): string {
return `
${esc(hook.name)}
${hook.description ? `
${esc(hook.description)}
` : ''}
Signature: ${esc(hook.typeSignature)}
${hook.parameters.length > 0 ? renderParametersTable(hook.parameters, 'Arguments') : ''}
Returns: ${esc(hook.returnType)}
${renderTagsBadges(hook.tags)}
`;
}
function renderTypes(types: Record): string {
const all = Object.values(types);
if (all.length === 0) {
return '';
}
const groups = groupByAnnotation(all);
return `
Types
(${all.length})
${renderAnnotationGroups(groups, renderTypeItem)}
`;
}
function renderTypeItem(type: TypeDoc): string {
const memberEntries = Object.values(type.members);
return `
${esc(type.name)} ${esc(type.kind)}
${type.description ? `
${esc(type.description)}
` : ''}
Type: ${esc(type.typeSignature)}
${memberEntries.length > 0 ? renderMembersTable(memberEntries) : ''}
${renderTagsBadges(type.tags)}
`;
}
function renderOthers(others: Record): string {
const all = Object.values(others);
if (all.length === 0) {
return '';
}
const groups = groupByAnnotation(all);
return `
Others
(${all.length})
${renderAnnotationGroups(groups, renderOtherItem)}
`;
}
function renderOtherItem(other: OtherDoc): string {
return `
${esc(other.name)} ${esc(other.kind)}
${other.description ? `
${esc(other.description)}
` : ''}
Type: ${esc(other.typeSignature)}
${other.parameters && other.parameters.length > 0 ? renderParametersTable(other.parameters, 'Arguments') : ''}
${other.returnType ? `
Returns: ${esc(other.returnType)}
` : ''}
${renderTagsBadges(other.tags)}
`;
}
function renderExternalReferences(externalReferences?: Record): string {
if (!externalReferences || Object.keys(externalReferences).length === 0) {
return '';
}
const totalSymbols = Object.values(externalReferences).reduce((sum, pkg) => sum + Object.keys(pkg.symbols).length, 0);
const packages = Object.entries(externalReferences)
.sort(([a], [b]) => a.localeCompare(b))
.map(([pkgSpec, pkgRef]) => {
const symbolRows = Object.entries(pkgRef.symbols)
.sort(([a], [b]) => a.localeCompare(b))
.map(([name, ref]) => {
const refDisplay =
'$ref' in ref
? `${esc(ref.$ref)}`
: `${esc(ref.inline)}`;
return `${esc(name)} | ${refDisplay} |
`;
})
.join('\n');
return `
${esc(pkgSpec)} (${Object.keys(pkgRef.symbols).length} symbols)
metadata: ${esc(pkgRef.metadataRef)}
| Symbol | Reference |
${symbolRows}
`;
})
.join('\n');
return `
External References
(${totalSymbols} symbols from ${
Object.keys(externalReferences).length
} packages)
${packages}
`;
}
// ---------------------------------------------------------------------------
// Annotation sub-group renderer
// ---------------------------------------------------------------------------
function renderAnnotationGroups(
groups: AnnotationGroup[],
renderItem: (item: T) => string,
): string {
// When there's only one group (all stable), skip the sub-group wrapper
if (groups.length === 1 && groups[0].key === 'stable') {
return groups[0].items.map(renderItem).join('\n');
}
return groups
.map(g => {
const items = g.items.map(renderItem).join('\n');
return `
${esc(g.label)} (${g.items.length})
${items}
`;
})
.join('\n');
}
// ---------------------------------------------------------------------------
// Shared renderers
// ---------------------------------------------------------------------------
function renderParametersTable(params: ParameterDoc[], title: string): string {
const rows = params
.map(
p =>
`${esc(p.name)} | ${esc(p.type)} | ${
p.required ? 'Yes' : 'No'
} | ${esc(p.description)} |
`,
)
.join('\n');
return `${esc(title)}:
| Name | Type | Required | Description |
${rows}
`;
}
function renderMembersTable(members: MemberDoc[]): string {
const rows = members
.map(
m =>
`${esc(m.name)} | ${esc(m.type)} | ${
m.required ? 'Yes' : 'No'
} | ${m.defaultValue ? `${esc(m.defaultValue)}` : '—'} | ${esc(
m.description,
)} |
`,
)
.join('\n');
return `Members (${members.length})
| Name | Type | Required | Default | Description |
${rows}
`;
}
function renderTagsBadges(tags: Record): string {
const entries = Object.entries(tags);
if (entries.length === 0) {
return '';
}
const badges = entries.map(([key, val]) => `@${esc(key)}${val ? ` ${esc(val)}` : ''}`);
return `${badges.join(' ')}
`;
}
/**
* Render a $ref or inline type as HTML.
* Local refs (e.g. `#/categories/types/ButtonProps`) become clickable anchor links.
* Cross-package refs (e.g. `@fluentui/react-utilities#/...`) are shown as non-linked code.
*/
function renderRef(ref: { $ref: string } | { inline: string }): string {
if ('inline' in ref) {
return `${esc(ref.inline)}`;
}
const refValue = ref.$ref;
// Local ref: #/categories//
if (refValue.startsWith('#/')) {
const symbolName = refValue.split('/').pop()!;
return `${esc(symbolName)}`;
}
// Cross-package ref: @scope/pkg#/categories//
const hashIdx = refValue.indexOf('#/');
if (hashIdx > 0) {
const pkgName = refValue.substring(0, hashIdx);
const symbolName = refValue.split('/').pop()!;
return `${esc(pkgName)} → ${esc(symbolName)}`;
}
return `${esc(refValue)}`;
}
// ---------------------------------------------------------------------------
// Utilities
// ---------------------------------------------------------------------------
function esc(text: string): string {
return text.replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"');
}
function toAnchor(name: string): string {
return 'sym-' + name.replace(/[^a-zA-Z0-9_-]/g, '-');
}
function renderStyles(): string {
return ``;
}