The worker reads README.md when it scans the models folder (the web app can't see it) and stores it on the folder and, after import, on the model; every scan refreshes it for local models. Relative images and links are dropped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
5.1 KiB
TypeScript
131 lines
5.1 KiB
TypeScript
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 (
|
|
<p className="text-sm text-muted-foreground">
|
|
{card === null ? "This repository has no model card." : "The model card could not be loaded."}{" "}
|
|
<a href={hub} target="_blank" rel="noreferrer" className="underline">
|
|
Open on Hugging Face
|
|
</a>
|
|
</p>
|
|
);
|
|
}
|
|
return (
|
|
<>
|
|
<ModelCard markdown={card} repoId={repoId} sha={sha} />
|
|
<div className="mt-6 flex justify-end">
|
|
<a
|
|
href={`${hub}/blob/${sha}/README.md`}
|
|
target="_blank"
|
|
rel="noreferrer"
|
|
className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground"
|
|
>
|
|
README.md @ {shortSha(sha)}
|
|
<ExternalLinkIcon className="size-3" />
|
|
</a>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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 (
|
|
<p className="text-sm text-muted-foreground">
|
|
This folder has no README.md. Add one to <code className="font-mono">{localPath}</code> and it shows up here after the
|
|
next scan of the models folder.
|
|
</p>
|
|
);
|
|
}
|
|
return (
|
|
<>
|
|
<ModelCard markdown={markdown} />
|
|
<p className="mt-6 text-right text-xs text-muted-foreground">
|
|
README.md in <code className="font-mono">{localPath}</code>, as of the last scan
|
|
</p>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function ModelCardSkeleton() {
|
|
return (
|
|
<div className="flex flex-col gap-3" aria-busy="true" aria-label="Loading the model card">
|
|
<Skeleton className="h-7 w-2/5" />
|
|
<Skeleton className="h-4 w-full" />
|
|
<Skeleton className="h-4 w-11/12" />
|
|
<Skeleton className="h-4 w-4/5" />
|
|
<Skeleton className="mt-3 h-5 w-1/4" />
|
|
<Skeleton className="h-4 w-full" />
|
|
<Skeleton className="h-4 w-3/4" />
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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 (
|
|
<article className="prose prose-sm max-w-none dark:prose-invert prose-headings:scroll-mt-20 prose-img:inline-block prose-img:my-1 prose-pre:bg-muted prose-pre:text-foreground prose-table:text-xs">
|
|
<ReactMarkdown
|
|
remarkPlugins={[remarkGfm]}
|
|
rehypePlugins={[rehypeRaw, [rehypeSanitize, schema]]}
|
|
urlTransform={(url, key) => {
|
|
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 }) => (
|
|
<a href={href} {...props} target={href?.startsWith("#") ? undefined : "_blank"} rel="noreferrer">
|
|
{children}
|
|
</a>
|
|
),
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
img: ({ alt, ...props }) => <img alt={alt ?? ""} loading="lazy" referrerPolicy="no-referrer" {...props} />,
|
|
}}
|
|
>
|
|
{markdown}
|
|
</ReactMarkdown>
|
|
</article>
|
|
);
|
|
}
|