{
"$schema": "https://ui.shadcn.com/schema/registry-item.json",
"name": "markdown",
"type": "registry:ui",
"title": "Markdown",
"description": "Themed Markdown renderer for repository documents, set in Prose with safe links, safe images and syntax-highlighted code.",
"dependencies": [
"@fungi.computer/stipe",
"@tanstack/highlight",
"@tanstack/markdown",
"class-variance-authority",
"cn",
"react"
],
"registryDependencies": ["@shiit/prose"],
"files": [
{
"path": "../src/components/markdown.tsx",
"type": "registry:ui",
"target": "@ui/markdown.tsx",
"content": "import type {\n MarkdownComponentProps,\n MarkdownComponents,\n} from \"@tanstack/markdown/react\";\nimport { Markdown as TanStackMarkdown } from \"@tanstack/markdown/react\";\nimport { parseMarkdown } from \"@tanstack/markdown/parser\";\nimport { createElement, useMemo, type ReactNode } from \"react\";\n\nimport { cn } from \"@/lib/cn\";\nimport { highlightMarkdownCode } from \"@/lib/tanstack-highlight\";\nimport { Prose } from \"@/components/ui/prose\";\n\n/** Props accepted by Markdown. */\nexport type MarkdownProps = {\n /** The Markdown source, including an unfinished streaming response. */\n children: string;\n /** Extra classes merged onto the rendered container. */\n className?: string;\n /** Element treatments; links use the shared policy, images are filtered first. */\n components?: MarkdownComponents;\n /** Strip a leading YAML frontmatter block before rendering. Defaults to true. */\n frontmatter?: boolean;\n /** Visitor content allows only absolute HTTP(S) links and never loads images. */\n policy?: \"default\" | \"visitor\";\n /** Use Caps Prose, or inherit typography from the containing surface. */\n typography?: \"prose\" | \"inherit\";\n};\n\n/** The protocol of an absolute URL, or `null` when the value is relative. */\nfunction absoluteProtocol(value: string): string | null {\n return URL.parse(value)?.protocol ?? null;\n}\n\n/** Whether an href is safe to render live: http(s), mailto, or relative/hash. */\nfunction isSafeHref(href: string): boolean {\n if (href === \"\" || href.startsWith(\"//\")) return false;\n const protocol = absoluteProtocol(href);\n return (\n protocol === null ||\n protocol === \"http:\" ||\n protocol === \"https:\" ||\n protocol === \"mailto:\"\n );\n}\n\n/** Whether an href leaves the current document and should open in a new tab. */\nfunction isExternalHref(href: string): boolean {\n const protocol = absoluteProtocol(href);\n return protocol === \"http:\" || protocol === \"https:\";\n}\n\n/** Whether an image src is safe to render: http(s) or relative. */\nfunction isSafeImageSrc(src: string): boolean {\n if (src === \"\" || src.startsWith(\"//\")) return false;\n const protocol = absoluteProtocol(src);\n return protocol === null || protocol === \"http:\" || protocol === \"https:\";\n}\n\n/**\n * Shared Markdown renderer with safe links, images and highlighted code.\n * Documents use Caps Prose; containing surfaces can supply their own typography\n * and element treatments. Replacing the source renders streaming partial Markdown.\n */\nexport function Markdown({\n children,\n className,\n components,\n frontmatter = true,\n policy = \"default\",\n typography = \"prose\",\n}: MarkdownProps): ReactNode {\n const document = useMemo(\n () => parseMarkdown(children, { allowHtml: false, frontmatter }),\n [children, frontmatter],\n );\n const safeComponents = useMemo(\n () =>\n ({\n ...components,\n a(props: MarkdownComponentProps<\"a\">) {\n const href = props.href ?? \"\";\n if (policy === \"visitor\") {\n const destination = URL.parse(href);\n if (destination === null || !isExternalHref(destination.href))\n return {props.children};\n return (\n \n {props.children}\n \n );\n }\n if (!isSafeHref(href)) return <>{props.children}>;\n const external = isExternalHref(href);\n return (\n \n );\n },\n img(props: MarkdownComponentProps<\"img\">) {\n if (policy === \"visitor\")\n return Image removed;\n const source = props.src ?? \"\";\n if (!isSafeImageSrc(source)) return <>{props.alt ?? \"\"}>;\n if (components?.img) return createElement(components.img, props);\n return (\n \n );\n },\n }) satisfies MarkdownComponents,\n [components, policy],\n );\n const content = (\n