import "server-only"; import { ExternalLinkIcon } from "lucide-react"; import ReactMarkdown, { defaultUrlTransform } from "react-markdown"; import rehypeRaw from "rehype-raw"; import rehypeSanitize, { defaultSchema } from "rehype-sanitize"; import remarkGfm from "remark-gfm"; import { Skeleton } from "@/components/ui/skeleton"; import { shortSha } from "@/lib/format"; import { fetchModelCard } from "@/lib/hub/client"; // Model cards mix Markdown and raw HTML. Parse the HTML (rehype-raw), then sanitize it: no scripts, // styles or event handlers, only the tags/attributes GitHub-style READMEs normally use. const schema = { ...defaultSchema, attributes: { ...defaultSchema.attributes, img: [...(defaultSchema.attributes?.img ?? []), "width", "height", "align", "loading"], div: [...(defaultSchema.attributes?.div ?? []), "align"], p: [...(defaultSchema.attributes?.p ?? []), "align"], td: [...(defaultSchema.attributes?.td ?? []), "align"], th: [...(defaultSchema.attributes?.th ?? []), "align"], }, }; /** * The Model card tab: the README at the pinned commit, fetched from the Hub (cached per commit). * Rendered inside a Suspense boundary, so the rest of the page doesn't wait for the Hub. */ export async function ModelCardSection({ repoId, sha }: { repoId: string; sha: string }) { const hub = `https://huggingface.co/${repoId}`; const card = await fetchModelCard(repoId, sha).catch(() => undefined); if (!card) { return (

{card === null ? "This repository has no model card." : "The model card could not be loaded."}{" "} Open on Hugging Face

); } return ( <>
README.md @ {shortSha(sha)}
); } /** * The Model card tab of a local model: the folder's README.md, as the worker last read it. Relative * images and links are dropped: the web app can't serve files from the models folder. */ export function LocalModelCardSection({ markdown, localPath }: { markdown: string | null; localPath: string }) { if (!markdown) { return (

This folder has no README.md. Add one to {localPath} and it shows up here after the next scan of the models folder.

); } return ( <>

README.md in {localPath}, as of the last scan

); } export function ModelCardSkeleton() { return (
); } /** * Renders a repo's README like the Hub: relative links/images point at the pinned commit. Without a * repo (a local model's README) they are dropped. */ export function ModelCard({ markdown, repoId, sha }: { markdown: string; repoId?: string; sha?: string }) { const base = repoId ? `https://huggingface.co/${repoId}` : null; return (
{ const safe = defaultUrlTransform(url); if (!safe || safe.startsWith("#") || /^[a-z][a-z0-9+.-]*:|^\/\//i.test(safe)) return safe; // Relative path inside the repo: images from /resolve, links to /blob, both at the pinned SHA. if (!base) return null; const path = safe.replace(/^\.?\//, ""); return key === "src" ? `${base}/resolve/${sha}/${path}` : `${base}/blob/${sha}/${path}`; }} components={{ a: ({ href, children, ...props }) => ( {children} ), // eslint-disable-next-line @next/next/no-img-element img: ({ alt, ...props }) => {alt, }} > {markdown}
); }