{ "$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 {props.alt\n );\n },\n }) satisfies MarkdownComponents,\n [components, policy],\n );\n const content = (\n \n {document}\n \n );\n const containerClass = cn(\"min-w-0 break-words\", className);\n return typography === \"prose\" ? (\n {content}\n ) : (\n
{content}
\n );\n}\n" }, { "path": "../src/lib/tanstack-highlight.ts", "type": "registry:lib", "target": "@lib/tanstack-highlight.ts", "content": "import { createHighlighter } from \"@tanstack/highlight/core\";\nimport { css } from \"@tanstack/highlight/languages/css\";\nimport { dockerfile } from \"@tanstack/highlight/languages/dockerfile\";\nimport { html } from \"@tanstack/highlight/languages/html\";\nimport { js } from \"@tanstack/highlight/languages/js\";\nimport { json } from \"@tanstack/highlight/languages/json\";\nimport { jsx } from \"@tanstack/highlight/languages/jsx\";\nimport { markdown } from \"@tanstack/highlight/languages/markdown\";\nimport { plaintext } from \"@tanstack/highlight/languages/plaintext\";\nimport { python } from \"@tanstack/highlight/languages/python\";\nimport { shell } from \"@tanstack/highlight/languages/shell\";\nimport { sql } from \"@tanstack/highlight/languages/sql\";\nimport { ts } from \"@tanstack/highlight/languages/ts\";\nimport { tsx } from \"@tanstack/highlight/languages/tsx\";\nimport { yaml } from \"@tanstack/highlight/languages/yaml\";\nimport { createTanStackMarkdownHighlighter } from \"@tanstack/highlight/markdown\";\n\nconst highlighter = createHighlighter({\n fallbackLanguage: \"plaintext\",\n languages: [\n css,\n dockerfile,\n html,\n js,\n json,\n jsx,\n markdown,\n plaintext,\n python,\n shell,\n sql,\n ts,\n tsx,\n yaml,\n ],\n});\n\n/** Highlights Markdown fences with the shared chat and document language set. */\nexport const highlightMarkdownCode =\n createTanStackMarkdownHighlighter(highlighter);\n\n/** Converts source code to escaped token spans for a code display. */\nexport function highlightToHtml(code: string, lang?: string): string {\n return highlighter.highlightToHtml(code, lang ? { lang } : {});\n}\n" }, { "path": "../src/lib/cn.ts", "type": "registry:lib", "target": "@lib/cn.ts", "content": "import { STIPE_SPACING_NAMES } from \"@fungi.computer/stipe\";\nimport { cva } from \"class-variance-authority\";\nimport { createCn } from \"cn/config\";\n\n/** A conditional class value accepted by cn. */\nexport type { ClassValue } from \"cn\";\nexport { cva };\n\n/** Merge conditional classes with Tailwind rules and Stipe's named spacing. */\nexport const cn = createCn({\n extend: { theme: { spacing: [...STIPE_SPACING_NAMES] } },\n});\n\n/** Resolve a typed React class-name prop before merging its classes. */\nexport function resolveClassName(\n className: string | ((state: State) => string | undefined) | undefined,\n state: State,\n): string | undefined {\n return typeof className === \"function\" ? className(state) : className;\n}\n" } ], "css": { "@import \"@fungi.computer/stipe/styles.css\"": {} } }