Browse Source

fix(export): normalize all site-relative markdown image urls

Made-with: Cursor
main
npmrun 2 weeks ago
parent
commit
d8c6a4093d
  1. 7
      app/utils/markdown-export.test.ts
  2. 4
      app/utils/markdown-export.ts

7
app/utils/markdown-export.test.ts

@ -13,6 +13,13 @@ describe("normalizeMarkdownImageUrls", () => {
expect(result).toBe("![cover](https://example.com/public/assets/posts/cover.png)");
});
test("converts other site-relative image links to absolute URLs", () => {
const markdown = "![hero](/images/a.png)";
const result = normalizeMarkdownImageUrls(markdown, "https://example.com");
expect(result).toBe("![hero](https://example.com/images/a.png)");
});
test("keeps absolute http/https image links unchanged", () => {
const markdown = [
"![a](http://cdn.example.com/a.png)",

4
app/utils/markdown-export.ts

@ -3,8 +3,6 @@ type MarkdownExportFileNameInput = {
id: number;
};
const PUBLIC_ASSETS_PREFIX = "/public/assets/";
function isAbsoluteOrSpecialUrl(url: string): boolean {
return /^(https?:)?\/\//i.test(url) || /^data:/i.test(url);
}
@ -17,7 +15,7 @@ export function normalizeMarkdownImageUrls(markdown: string, origin: string): st
return full;
}
if (!rawUrl.startsWith(PUBLIC_ASSETS_PREFIX)) {
if (!rawUrl.startsWith("/")) {
return full;
}

Loading…
Cancel
Save