Skip to content

Build your personal knowledge base with Trilium Notes

link previews: handle images with a relative path as well

Adorian Doran95d9cea98771
diff --git a/apps/server/src/routes/api/link_embed.spec.ts b/apps/server/src/routes/api/link_embed.spec.ts
index eff9ac9845..236d4d1301 100644
--- a/apps/server/src/routes/api/link_embed.spec.ts
+++ b/apps/server/src/routes/api/link_embed.spec.ts
@@ -86,6 +86,25 @@ describe("link-embed getMetadata", () => {
expect(result.title).toBe("OG Title");
expect(result.description).toBe("OG Desc");
expect(result.siteName).toBe("Example");
+ expect(result.image).toBe("https://site/img.png");
+ });
+
+ it("resolves a relative og:image against the page URL", async () => {
+ // A relative or protocol-relative og:image would otherwise be resolved against Trilium's own
+ // origin when the card renders, silently yielding a broken image.
+ const page = (image: string) => `<html><head><title>T</title><meta property="og:image" content="${image}"></head></html>`;
+ const imageFor = async (image: string) => {
+ safeFetch.mockResolvedValue(fakeResponse(page(image), { contentType: "text/html" }));
+ const result = await linkEmbedRoute.getMetadata(req("https://example.com/blog/post"));
+ return result.image;
+ };
+
+ expect(await imageFor("/img/cover.png")).toBe("https://example.com/img/cover.png");
+ expect(await imageFor("cover.png")).toBe("https://example.com/blog/cover.png");
+ expect(await imageFor("//cdn.example.com/cover.png")).toBe("https://cdn.example.com/cover.png");
+ expect(await imageFor("https://cdn.example.com/cover.png")).toBe("https://cdn.example.com/cover.png");
+ // Malformed enough that it cannot form a URL even against a valid base.
+ expect(await imageFor("http://[")).toBeUndefined();
});
it("falls back to the hostname when the fetch fails, flagging the result unresolved", async () => {
diff --git a/apps/server/src/routes/api/link_embed.ts b/apps/server/src/routes/api/link_embed.ts
index 04bfcaf004..549ba082b1 100644
--- a/apps/server/src/routes/api/link_embed.ts
+++ b/apps/server/src/routes/api/link_embed.ts
@@ -75,6 +75,21 @@ async function downloadFaviconAsDataUri(faviconUrl: string): Promise<string | un
}
}
+/**
+ * Resolves a URL found in the page (`og:image` content, favicon `href`, …) against the page's own
+ * address, so a root-relative or relative one — both legal and common — becomes absolute. Without
+ * this the value would later be resolved against Trilium's origin instead of the site's.
+ * Returns undefined when the value cannot form a URL at all.
+ */
+function toAbsoluteUrl(href: string | undefined, pageUrl: string): string | undefined {
+ if (!href) return undefined;
+ try {
+ return new URL(href, pageUrl).toString();
+ } catch {
+ return undefined;
+ }
+}
+
/**
* Resolves a favicon URL from the parsed HTML, then downloads it as a data URI.
*/
@@ -83,15 +98,9 @@ async function resolveFavicon(document: ReturnType<typeof parse>, pageUrl: strin
|| document.querySelector('link[rel="shortcut icon"]')
|| document.querySelector('link[rel="apple-touch-icon"]');
- let faviconUrl: string | undefined;
- if (faviconEl) {
- const href = faviconEl.getAttribute("href");
- if (href) {
- try { faviconUrl = new URL(href, pageUrl).toString(); } catch { /* ignore */ }
- }
- }
+ let faviconUrl = toAbsoluteUrl(faviconEl?.getAttribute("href"), pageUrl);
if (!faviconUrl) {
- try { faviconUrl = `${new URL(pageUrl).origin}/favicon.ico`; } catch { /* ignore */ }
+ faviconUrl = toAbsoluteUrl("/favicon.ico", pageUrl);
}
if (!faviconUrl) return undefined;
@@ -163,7 +172,7 @@ async function fetchOpenGraphData(url: string) {
return {
title: getMeta("og:title") || document.querySelector("title")?.textContent?.trim() || undefined,
description: getMeta("og:description") || getMeta("description") || undefined,
- image: getMeta("og:image") || undefined,
+ image: toAbsoluteUrl(getMeta("og:image"), url),
siteName: getMeta("og:site_name") || undefined,
favicon
};