Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: show relative and blob Hugging Face images in Hub model cards (#14602)

Michael Han114ee5fc76ac
diff --git a/studio/frontend/src/features/hub/catalog/model-readme.tsx b/studio/frontend/src/features/hub/catalog/model-readme.tsx
index 34a585bfcd2..d1590545054 100644
--- a/studio/frontend/src/features/hub/catalog/model-readme.tsx
+++ b/studio/frontend/src/features/hub/catalog/model-readme.tsx
@@ -40,6 +40,7 @@ import {
stripChromeHeadings,
stripFrontmatter,
} from "../lib/hf-readme";
+import { rehypeReadmeUrls } from "../lib/rehype-readme-urls";
type ReadmePlugins = NonNullable<ComponentProps<typeof Streamdown>["plugins"]>;
@@ -73,7 +74,6 @@ const README_ALLOWED_TAGS: NonNullable<
source: ["src", "type", "media"],
track: ["src", "kind", "srclang", "label", "default"],
};
-const README_REHYPE_PLUGINS = withLiteralUnknownTags(README_ALLOWED_TAGS);
const README_RENDER_CHAR_LIMIT = 120_000;
const README_CACHE_TTL_MS = 60_000;
@@ -410,6 +410,16 @@ export function ModelReadme({
current.baseUrl ? createReadmeUrlTransform(current.baseUrl) : undefined,
[current.baseUrl],
);
+ const rehypePlugins = useMemo(
+ () =>
+ withLiteralUnknownTags(
+ README_ALLOWED_TAGS,
+ current.baseUrl
+ ? [[rehypeReadmeUrls, { baseUrl: current.baseUrl }]]
+ : [],
+ ),
+ [current.baseUrl],
+ );
useEffect(() => {
let canceled = false;
@@ -542,7 +552,7 @@ export function ModelReadme({
components={README_COMPONENTS}
icons={MARKDOWN_TABLE_ICONS}
allowedTags={README_ALLOWED_TAGS}
- rehypePlugins={README_REHYPE_PLUGINS}
+ rehypePlugins={rehypePlugins}
urlTransform={urlTransform}
>
{current.body}
diff --git a/studio/frontend/src/features/hub/lib/rehype-readme-urls.ts b/studio/frontend/src/features/hub/lib/rehype-readme-urls.ts
new file mode 100644
index 00000000000..352a8f880ed
--- /dev/null
+++ b/studio/frontend/src/features/hub/lib/rehype-readme-urls.ts
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import type { Root, RootContent } from "hast";
+import type { Plugin } from "unified";
+
+const MEDIA_TAGS = new Set(["img", "video", "audio", "source", "track"]);
+// `/{datasets|spaces}/owner/repo/blob/rev/...` is the HTML file viewer; `resolve` serves the bytes.
+const HF_BLOB_PATH_RE = /^(\/(?:datasets\/|spaces\/)?[^/]+\/[^/]+\/)blob\//;
+const HF_HOSTS = new Set(["huggingface.co", "www.huggingface.co", "hf.co"]);
+
+export function resolveReadmeUrl(
+ url: string,
+ baseUrl: string,
+ media: boolean,
+): string {
+ const trimmed = url.trim();
+ if (!trimmed || /^(?:#|data:|mailto:|tel:)/i.test(trimmed)) return url;
+ let parsed: URL;
+ try {
+ parsed = new URL(trimmed, baseUrl);
+ } catch {
+ return url;
+ }
+ // Other schemes pass through untouched for harden to judge.
+ if (!/^https?:$/.test(parsed.protocol)) return url;
+ const base = new URL(baseUrl);
+ if (
+ media &&
+ (parsed.origin === base.origin || HF_HOSTS.has(parsed.hostname)) &&
+ HF_BLOB_PATH_RE.test(parsed.pathname)
+ ) {
+ parsed.pathname = parsed.pathname.replace(HF_BLOB_PATH_RE, "$1resolve/");
+ }
+ return parsed.toString();
+}
+
+/**
+ * Harden blocks a relative URL unless it starts with `/`, `./` or `../`, so a card's
+ * `<img src="assets/plot.png">` showed "[Image blocked]": `urlTransform` resolves it only after
+ * harden has run. This resolves against the repo first; harden still decides every URL.
+ */
+export const rehypeReadmeUrls: Plugin<[{ baseUrl: string }], Root> =
+ function rehypeReadmeUrls({ baseUrl }) {
+ return (tree) => {
+ function walk(node: Root | RootContent): void {
+ if (node.type === "element") {
+ const media = MEDIA_TAGS.has(node.tagName);
+ for (const key of ["src", "href", "poster"]) {
+ const value = node.properties[key];
+ if (typeof value === "string") {
+ node.properties[key] = resolveReadmeUrl(
+ value,
+ baseUrl,
+ media || key === "poster",
+ );
+ }
+ }
+ }
+ if ("children" in node) node.children.forEach(walk);
+ }
+ walk(tree);
+ };
+ };
diff --git a/studio/frontend/src/lib/markdown-data-images.ts b/studio/frontend/src/lib/markdown-data-images.ts
index bf16b08b910..08b8f2dd28a 100644
--- a/studio/frontend/src/lib/markdown-data-images.ts
+++ b/studio/frontend/src/lib/markdown-data-images.ts
@@ -165,8 +165,9 @@ function literalTagPipeline(
* formatting tags used as markup are unwrapped. */
export function withLiteralUnknownTags(
allowedTags: Record<string, string[]> = {},
+ beforeHarden: Pluggable[] = [],
): Pluggable[] {
- return literalTagPipeline(allowedTags, [], [], true);
+ return literalTagPipeline(allowedTags, [], beforeHarden, true);
}
/** Keep data images, keep tags outside the schema as text, resolve sandbox paths before URL hardening. */
diff --git a/studio/frontend/tests/hub-readme-images.test.ts b/studio/frontend/tests/hub-readme-images.test.ts
new file mode 100644
index 00000000000..c04fce15b5a
--- /dev/null
+++ b/studio/frontend/tests/hub-readme-images.test.ts
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+import { createElement } from "react";
+import { renderToStaticMarkup } from "react-dom/server";
+import { Streamdown } from "streamdown";
+
+import {
+ rehypeReadmeUrls,
+ resolveReadmeUrl,
+} from "../src/features/hub/lib/rehype-readme-urls.ts";
+import { withLiteralUnknownTags } from "../src/lib/markdown-data-images.ts";
+
+const BASE = "https://huggingface.co/unsloth/flint-GGUF/resolve/main/";
+const ASSETS = `${BASE}assets`;
+
+function render(markdown: string, baseUrl: string | null = BASE) {
+ return renderToStaticMarkup(
+ createElement(
+ Streamdown,
+ {
+ mode: "static",
+ rehypePlugins: withLiteralUnknownTags(
+ {},
+ baseUrl ? [[rehypeReadmeUrls, { baseUrl }]] : [],
+ ),
+ },
+ markdown,
+ ),
+ );
+}
+
+test("without the plugin a bare relative card image is blocked", () => {
+ assert.match(
+ render('<img src="assets/flint-logo.png" alt="Flint logo">', null),
+ /Image blocked: Flint logo/,
+ );
+});
+
+test("relative card images resolve against the repo", () => {
+ for (const [markdown, src] of [
+ [
+ '<img src="assets/size_vs_kld.png" alt="KLD">',
+ `${ASSETS}/size_vs_kld.png`,
+ ],
+ [
+ '<p align="center"><img src="assets/flint-logo.png" width="160" alt="Flint logo"></p>',
+ `${ASSETS}/flint-logo.png`,
+ ],
+ ["![KLD](assets/size_vs_kld.png)", `${ASSETS}/size_vs_kld.png`],
+ ["![KLD](./assets/size_vs_kld.png)", `${ASSETS}/size_vs_kld.png`],
+ [
+ "![root](/unsloth/flint-GGUF/resolve/main/a.png)",
+ "https://huggingface.co/unsloth/flint-GGUF/resolve/main/a.png",
+ ],
+ ]) {
+ const html = render(markdown);
+ assert.ok(!html.includes("Image blocked"), html);
+ assert.ok(html.includes(`src="${src}"`), html);
+ }
+});
+
+test("Hugging Face blob image URLs load through resolve", () => {
+ for (const [src, expected] of [
+ [
+ "https://huggingface.co/RecursiveSI/flint/blob/main/assets/x.png",
+ "https://huggingface.co/RecursiveSI/flint/resolve/main/assets/x.png",
+ ],
+ [
+ "https://huggingface.co/datasets/o/d/blob/main/x.png",
+ "https://huggingface.co/datasets/o/d/resolve/main/x.png",
+ ],
+ ["blob/main/x.png", `${BASE}blob/main/x.png`],
+ ]) {
+ const html = render(`![x](${src})`);
+ assert.ok(html.includes(`src="${expected}"`), html);
+ }
+ // Links keep the file viewer; other hosts are left alone.
+ assert.equal(
+ resolveReadmeUrl("https://huggingface.co/o/r/blob/main/x.py", BASE, false),
+ "https://huggingface.co/o/r/blob/main/x.py",
+ );
+ assert.equal(resolveReadmeUrl("LICENSE", BASE, false), `${BASE}LICENSE`);
+ assert.ok(
+ render("![x](https://example.com/o/r/blob/main/x.png)").includes(
+ 'src="https://example.com/o/r/blob/main/x.png"',
+ ),
+ );
+});
+
+test("unsafe schemes stay blocked", () => {
+ const html = render(
+ '![x](javascript:alert(1)) <img src="data:text/html,x" alt="d">',
+ );
+ assert.ok(!html.includes("javascript:"), html);
+ assert.ok(!html.includes("data:text/html"), html);
+});
diff --git a/studio/frontend/tests/markdown-preview-literal-tags.test.ts b/studio/frontend/tests/markdown-preview-literal-tags.test.ts
index 8e1576d05ce..52007e11cb2 100644
--- a/studio/frontend/tests/markdown-preview-literal-tags.test.ts
+++ b/studio/frontend/tests/markdown-preview-literal-tags.test.ts
@@ -197,8 +197,8 @@ test("Hub model cards are wired to the literal-tag pipeline", () => {
ts.forEachChild(node, visit);
})(source);
assert.ok(rehypePlugins, `${path}: Streamdown has no rehypePlugins`);
- assert.equal(
+ assert.match(
initializers.get(rehypePlugins) ?? rehypePlugins,
- "withLiteralUnknownTags(README_ALLOWED_TAGS)",
+ /withLiteralUnknownTags\(\s*README_ALLOWED_TAGS,/,
);
});