mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-15 03:20:33 +08:00
Render GitHub blob image URLs
Image URLs pointing at a GitHub "blob" page, such as https://github.com/<owner>/<repo>/blob/<ref>/<path>, serve an HTML page rather than the raw image bytes, so they render as broken images in markdown. Add a canonicalizeGithubImageUrl helper that appends ?raw=true to such URLs, which makes GitHub redirect to the raw content. URLs already pointing at the raw content, via ?raw=true or raw.githubusercontent.com, are left unchanged, as are non-GitHub and non-URL sources. Wire the helper into the markdown image loop so external image URLs are canonicalized while relative paths keep resolving against the repo's raw endpoint.
This commit is contained in:
@@ -37,6 +37,7 @@
|
||||
twemoji,
|
||||
scrollIntoView,
|
||||
canonicalize,
|
||||
canonicalizeGithubImageUrl,
|
||||
isCommit,
|
||||
} from "@app/lib/utils";
|
||||
import { Renderer, markdown, sanitizeConfig } from "@app/lib/markdown";
|
||||
@@ -226,10 +227,14 @@
|
||||
const imagePath = i.getAttribute("src");
|
||||
const imageClass = i.getAttribute("class");
|
||||
|
||||
// Make sure the source isn't a URL before trying to fetch it from the repo
|
||||
const emoji = imageClass && imageClass === "txt-emoji";
|
||||
if (imagePath && !isUrl(imagePath) && !emoji) {
|
||||
i.setAttribute("src", `${rawPath}/${canonicalize(imagePath, path)}`);
|
||||
if (imagePath && !emoji) {
|
||||
if (isUrl(imagePath)) {
|
||||
// Rewrite GitHub "blob" URLs so the raw image content is rendered.
|
||||
i.setAttribute("src", canonicalizeGithubImageUrl(imagePath));
|
||||
} else {
|
||||
i.setAttribute("src", `${rawPath}/${canonicalize(imagePath, path)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user