import ReactMarkdown, { defaultUrlTransform } from "react-markdown"; import rehypeRaw from "rehype-raw"; import rehypeSanitize, { defaultSchema } from "rehype-sanitize"; import remarkGfm from "remark-gfm"; // 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"], }, }; /** Renders a repo's README like the Hub: relative links/images point at the pinned commit. */ export function ModelCard({ markdown, repoId, sha }: { markdown: string; repoId: string; sha: string }) { const base = `https://huggingface.co/${repoId}`; 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. 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}
); }