mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-29 18:20:30 +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:
@@ -82,6 +82,57 @@ describe("String Assertions", () => {
|
||||
])("isUrl $url => $expected", ({ url, expected }) => {
|
||||
expect(utils.isUrl(url)).toBe(expected);
|
||||
});
|
||||
|
||||
test.each([
|
||||
{
|
||||
url: "https://github.com/radicle-dev/radicle-explorer/blob/master/img.png",
|
||||
expected:
|
||||
"https://github.com/radicle-dev/radicle-explorer/blob/master/img.png?raw=true",
|
||||
},
|
||||
{
|
||||
// Already has ?raw=true, leave it untouched.
|
||||
url: "https://github.com/radicle-dev/radicle-explorer/blob/master/img.png?raw=true",
|
||||
expected:
|
||||
"https://github.com/radicle-dev/radicle-explorer/blob/master/img.png?raw=true",
|
||||
},
|
||||
{
|
||||
// Raw content host is already correct, leave it untouched.
|
||||
url: "https://raw.githubusercontent.com/radicle-dev/radicle-explorer/master/img.png",
|
||||
expected:
|
||||
"https://raw.githubusercontent.com/radicle-dev/radicle-explorer/master/img.png",
|
||||
},
|
||||
{
|
||||
// An existing query string is preserved.
|
||||
url: "https://github.com/radicle-dev/radicle-explorer/blob/master/img.png?token=abc",
|
||||
expected:
|
||||
"https://github.com/radicle-dev/radicle-explorer/blob/master/img.png?token=abc&raw=true",
|
||||
},
|
||||
{
|
||||
// The www subdomain is handled too.
|
||||
url: "https://www.github.com/radicle-dev/radicle-explorer/blob/master/img.png",
|
||||
expected:
|
||||
"https://www.github.com/radicle-dev/radicle-explorer/blob/master/img.png?raw=true",
|
||||
},
|
||||
{
|
||||
// A "blob" segment that isn't in the <owner>/<repo>/blob/ position is
|
||||
// left untouched.
|
||||
url: "https://github.com/radicle-dev/radicle-explorer/raw/master/blob/img.png",
|
||||
expected:
|
||||
"https://github.com/radicle-dev/radicle-explorer/raw/master/blob/img.png",
|
||||
},
|
||||
{
|
||||
// Non-GitHub URLs are returned unchanged.
|
||||
url: "https://example.com/img.png",
|
||||
expected: "https://example.com/img.png",
|
||||
},
|
||||
{
|
||||
// Not a valid URL, returned unchanged.
|
||||
url: "not a url",
|
||||
expected: "not a url",
|
||||
},
|
||||
])("canonicalizeGithubImageUrl $url => $expected", ({ url, expected }) => {
|
||||
expect(utils.canonicalizeGithubImageUrl(url)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Parse Functions", () => {
|
||||
|
||||
Reference in New Issue
Block a user