// Renders the bundled docs (see markdown.ts) as React text nodes; nothing is parsed as HTML.
// Links to the other bundled doc switch docs, `#anchors` scroll, and web links open in the
// system browser.
import type { ReactNode } from 'react';
import { anchorOf, inlineTokens, parseMarkdown, resolveLink, type DocLink } from './markdown.ts';
function inline(text: string, onLink: (link: DocLink) => void, docs: readonly string[]): ReactNode[] {
return inlineTokens(text).map((token, index) => {
if (token.kind === 'text') return token.text;
if (token.kind === 'code') return {token.text};
if (token.kind === 'bold') return {token.text};
const link = resolveLink(token.href, docs);
return link
?
: {token.text};
});
}
export function MarkdownDoc({ source, docs, onLink }: { source: string; docs: readonly string[]; onLink(link: DocLink): void }) {
const blocks = parseMarkdown(source);
return
{inline(block.text, onLink, docs)}
; case 'list': { const List = block.ordered ? 'ol' : 'ul'; return| {inline(cell, onLink, docs)} | )}
|---|
| {inline(cell, onLink, docs)} | )}
{block.text};
}
})}