import React, { useMemo } from 'react'; import { fromMarkdown } from 'mdast-util-from-markdown'; import type { RootContent, Root } from 'mdast'; function renderNode(node: Root | RootContent, key: number, images: Record, depth = 0): React.ReactNode { if (depth > 24) return null; const children = 'children' in node ? node.children.map((child, index) => renderNode(child as RootContent, index, images, depth + 1)) : null; switch (node.type) { case 'root': return {children}; case 'heading': return

{children}

; case 'paragraph': return

{children}

; case 'text': return node.value; case 'strong': return {children}; case 'emphasis': return {children}; case 'inlineCode': return {node.value}; case 'code': return
{node.value}
; case 'list': return ; case 'listItem': return
  • {children}
  • ; case 'blockquote': return
    {children}
    ; case 'break': return
    ; case 'thematicBreak': return
    ; case 'link': { let safe = false; try { const url = new URL(node.url); safe = url.protocol === 'https:' && !url.username && !url.password; } catch { /* relative and unsafe links stay text */ } return safe ? {children} : {children}; } case 'image': { const src = Object.hasOwn(images, node.url) ? images[node.url] : undefined; return src && /^data:image\/png;base64,[A-Za-z0-9+/]+=*$/.test(src) ? {node.alt : [{node.alt ?? 'image'}]; } case 'imageReference': return [{node.alt ?? 'image'}]; case 'html': return {node.value}; default: return {children}; } } export function PluginReadme({ markdown, images = {} }: { markdown: string; images?: Record }) { const tree = useMemo(() => fromMarkdown(markdown.slice(0, 256 * 1024)), [markdown]); return
    {renderNode(tree, 0, images)}
    ; }