Skip to content

Build your personal knowledge base with Trilium Notes

feat(link previews): read a Twitter card's description where there is no OpenGraph one

Elian Doran1c6563b97ccd
diff --git a/apps/server/src/routes/api/link_embed.spec.ts b/apps/server/src/routes/api/link_embed.spec.ts
index d4449d9c4f..4ed106c19f 100644
--- a/apps/server/src/routes/api/link_embed.spec.ts
+++ b/apps/server/src/routes/api/link_embed.spec.ts
@@ -616,6 +616,35 @@ describe("link-embed getMetadata", () => {
expect(result.description).toBeUndefined();
});
+ it("takes a Twitter card's description where a page offers no OpenGraph one", async () => {
+ const describedBy = async (tags: string) => {
+ safeFetch.mockResolvedValue(fakeResponse(
+ `<html><head><title>T</title>${tags}</head></html>`,
+ { contentType: "text/html" }
+ ));
+ return (await linkEmbedRoute.getMetadata(req("https://example.com/page"))).description;
+ };
+
+ // The card spec says `name`; plenty of sites write `property` regardless, and both are read.
+ expect(await describedBy(`<meta name="twitter:description" content="From the card">`)).toBe("From the card");
+ expect(await describedBy(`<meta property="twitter:description" content="From the card">`)).toBe("From the card");
+
+ // Both of the first two are written to be read on a card, so they come before the one
+ // written for a search result.
+ expect(await describedBy(
+ `<meta property="og:description" content="From OpenGraph">`
+ + `<meta name="twitter:description" content="From the card">`
+ + `<meta name="description" content="For a search result">`
+ )).toBe("From OpenGraph");
+ expect(await describedBy(
+ `<meta name="twitter:description" content="From the card">`
+ + `<meta name="description" content="For a search result">`
+ )).toBe("From the card");
+
+ // And a page carrying none of them still says nothing rather than something invented.
+ expect(await describedBy("")).toBeUndefined();
+ });
+
it("ignores a favicon that advertises a size over the limit", async () => {
const html = `<html><head><title>Plain</title><link rel="icon" href="/big.ico"></head></html>`;
safeFetch.mockImplementation(async (url: string) => {
diff --git a/apps/server/src/routes/api/link_embed.ts b/apps/server/src/routes/api/link_embed.ts
index c0ad365d28..aef843976a 100644
--- a/apps/server/src/routes/api/link_embed.ts
+++ b/apps/server/src/routes/api/link_embed.ts
@@ -469,7 +469,13 @@ async function fetchOpenGraphData(url: string) {
return {
title: getMeta("og:title") || document.querySelector("title")?.textContent?.trim() || undefined,
- description: getMeta("og:description") || getMeta("description") || undefined,
+ // A Twitter card's description before the generic one: both of the first two are written to
+ // be read on a card, which is what this is, whereas `name="description"` is written for a
+ // search result and is often keyword-stuffed or cut off mid-sentence. Sites that ship only
+ // Twitter tags are common enough to be worth the extra look. `getMeta` tries `property=`
+ // and `name=` in turn, so it finds the tag under either spelling — the card spec says
+ // `name`, and plenty of sites write `property` regardless.
+ description: getMeta("og:description") || getMeta("twitter:description") || getMeta("description") || undefined,
siteName: getMeta("og:site_name") || undefined,
image,
favicon