Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
link previews: add site's favicon to the card view
Adorian Doran4f4daf15d555
diff --git a/apps/client/src/services/link_embed.spec.ts b/apps/client/src/services/link_embed.spec.tsindex 9e4f476aa5..bfd893a95a 100644--- a/apps/client/src/services/link_embed.spec.ts+++ b/apps/client/src/services/link_embed.spec.ts@@ -145,6 +145,31 @@ describe("renderEmbedPreview", () => {expect(root.querySelector(".link-embed-card-image-placeholder")).toBeNull();});+ it("shows the stored favicon beside the site name, falling back to a dot without one", () => {+ const root = makeContainer();+ const meta = {+ url: "https://example.com/page",+ embedType: "opengraph",+ siteName: "Example",+ favicon: "data:image/png;base64,AAA"+ };++ renderEmbedPreview(root, meta);++ // The card reuses the favicon the inline mention shows, straight from the stored metadata.+ const urlLine = root.querySelector(".link-embed-card-url")!;+ const favicon = urlLine.querySelector("img.link-embed-mention-favicon")!;+ expect(favicon.getAttribute("src")).toBe(meta.favicon);+ expect(urlLine.textContent).toBe("Example");+ // The favicon leads the line, so it renders to the left of the site name.+ expect(urlLine.firstElementChild).toBe(favicon);++ const withoutFavicon = document.createElement("div");+ renderEmbedPreview(withoutFavicon, { ...meta, favicon: undefined });+ expect(withoutFavicon.querySelector(".link-embed-card-url .link-embed-mention-dot")).not.toBeNull();+ expect(withoutFavicon.querySelector(".link-embed-card-url img")).toBeNull();+ });+it("omits optional fields and falls back to hostname, and drops target when editable", () => {const root = makeContainer();renderEmbedPreview(diff --git a/apps/client/src/services/link_embed.tsx b/apps/client/src/services/link_embed.tsxindex dae6d3e7b6..6c54499cec 100644--- a/apps/client/src/services/link_embed.tsx+++ b/apps/client/src/services/link_embed.tsx@@ -145,7 +145,12 @@ function EmbedPreview({ meta, editable }: { meta: EmbedMetadata; editable?: bool<div className="link-embed-card-content">{meta.title && <div className="link-embed-card-title">{meta.title}</div>}{meta.description && <div className="link-embed-card-description">{meta.description}</div>}- <div className="link-embed-card-url">{meta.siteName || safeHostname(meta.url)}</div>+ <div className="link-embed-card-url">+ {/* The same favicon the inline mention shows, read from the metadata already stored+ on the element — the data URI is not duplicated. */}+ <Favicon src={meta.favicon} />+ <span>{meta.siteName || safeHostname(meta.url)}</span>+ </div></div></a>);diff --git a/apps/client/src/widgets/type_widgets/text/LinkEmbed.css b/apps/client/src/widgets/type_widgets/text/LinkEmbed.cssindex c4bcc60c42..bd4256d64f 100644--- a/apps/client/src/widgets/type_widgets/text/LinkEmbed.css+++ b/apps/client/src/widgets/type_widgets/text/LinkEmbed.css@@ -246,10 +246,29 @@}.link-embed-card-url {+ display: flex;+ align-items: center;+ gap: 5px;font-size: 0.78em;color: var(--muted-text-color, #999);- overflow: hidden;- text-overflow: ellipsis;- white-space: nowrap;+ min-width: 0;margin-top: auto;++ /* The favicon keeps its size; only the site name is truncated. */+ & > span {+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }++ /* Scaled down from the inline mention's 16px: it sits next to 0.78em text here. */+ & .link-embed-mention-favicon {+ width: 14px;+ height: 14px;+ }++ & .link-embed-mention-dot {+ width: 6px;+ height: 6px;+ }}diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.tsindex f8c44b44b8..7e2d4a3df7 100644--- a/apps/server/src/share/content_renderer.spec.ts+++ b/apps/server/src/share/content_renderer.spec.ts@@ -283,6 +283,42 @@ describe("content_renderer", () => {});});+ describe("Link previews", () => {+ const FAVICON = "data:image/png;base64,AAA";++ it("renders a card, showing the stored favicon beside the site name", () => {+ const note = buildShareNote({+ content: `<section class="link-embed" data-url="https://example.com/page" data-embed-type="opengraph"`+ + ` data-title="A title" data-description="A description" data-site-name="Example"`+ + ` data-favicon="${FAVICON}" data-image="data:image/jpeg;base64,BBB"></section>`+ });++ const content = String(getContent(note).content);+ expect(content).toContain(`<div class="link-embed-card-url">`+ + `<img class="link-embed-mention-favicon" src="${FAVICON}" width="16" height="16">`+ + `<span>Example</span></div>`);+ });++ it("falls back to a dot when the site has no favicon", () => {+ const note = buildShareNote({+ content: `<section class="link-embed" data-url="https://example.com/page" data-embed-type="opengraph" data-title="A title"></section>`+ });++ const content = String(getContent(note).content);+ expect(content).toContain(`<div class="link-embed-card-url"><span class="link-embed-mention-dot"></span><span>example.com</span></div>`);+ });++ it("renders an inline mention with the same favicon markup", () => {+ const note = buildShareNote({+ content: `<p><span class="link-mention" data-url="https://example.com/page" data-title="A title" data-favicon="${FAVICON}"></span></p>`+ });++ const content = String(getContent(note).content);+ expect(content).toContain(`<img class="link-embed-mention-favicon" src="${FAVICON}" width="16" height="16">`);+ expect(content).toContain(`<span class="link-embed-mention-title">A title</span>`);+ });+ });+describe("renderCode", () => {it("identifies empty content", () => {const emptyResult: Result = {diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.tsindex 33ee378bc1..db0197a64d 100644--- a/apps/server/src/share/content_renderer.ts+++ b/apps/server/src/share/content_renderer.ts@@ -355,17 +355,19 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt};const document = parse(result.content || "", parseOpts);+ // The site's favicon, or a neutral dot when it has none — shown by both the inline mention and+ // the card's URL line, from the one `data-favicon` the element already carries.+ const renderFavicon = (favicon: string | undefined) => (favicon+ ? `<img class="link-embed-mention-favicon" src="${escapeHtml(favicon)}" width="16" height="16">`+ : `<span class="link-embed-mention-dot"></span>`);+// Process link mentions (inline) — metadata is stored in data attributes.for (const mentionEl of document.querySelectorAll("span.link-mention")) {const url = mentionEl.getAttribute("data-url");if (!url) continue;const title = mentionEl.getAttribute("data-title") || safeHostnameForShare(url);- const favicon = mentionEl.getAttribute("data-favicon");- const faviconHtml = favicon- ? `<img class="link-embed-mention-favicon" src="${escapeHtml(favicon)}" width="16" height="16">`- : `<span class="link-embed-mention-dot"></span>`;mentionEl.innerHTML = `<a class="link-embed-mention" href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">` +- faviconHtml ++ renderFavicon(mentionEl.getAttribute("data-favicon")) +`<span class="link-embed-mention-title">${escapeHtml(title)}</span></a>`;}@@ -390,10 +392,13 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt? `<div class="link-embed-card-image-wrapper"><img class="link-embed-card-image" src="${escapeHtml(image)}" alt="" loading="lazy"></div>`: `<div class="link-embed-card-image-wrapper"><div class="link-embed-card-image-placeholder">🔗</div></div>`;const descHtml = description ? `<div class="link-embed-card-description">${escapeHtml(description)}</div>` : "";+ const urlHtml = `<div class="link-embed-card-url">`+ + renderFavicon(embedEl.getAttribute("data-favicon"))+ + `<span>${escapeHtml(siteName)}</span></div>`;embedEl.innerHTML = `<a class="link-embed-card" href="${escapeHtml(url)}" target="_blank" rel="noopener noreferrer">` +imageHtml +- `<div class="link-embed-card-content"><div class="link-embed-card-title">${escapeHtml(title)}</div>${descHtml}<div class="link-embed-card-url">${escapeHtml(siteName)}</div></div></a>`;+ `<div class="link-embed-card-content"><div class="link-embed-card-title">${escapeHtml(title)}</div>${descHtml}${urlHtml}</div></a>`;}}diff --git a/packages/share-theme/src/styles/link-embed.css b/packages/share-theme/src/styles/link-embed.cssindex da3fe491e7..6a0ea7f03b 100644--- a/packages/share-theme/src/styles/link-embed.css+++ b/packages/share-theme/src/styles/link-embed.css@@ -146,10 +146,29 @@}.link-embed-card-url {+ display: flex;+ align-items: center;+ gap: 5px;font-size: 0.78em;color: var(--text-menu);- overflow: hidden;- text-overflow: ellipsis;- white-space: nowrap;+ min-width: 0;margin-top: auto;++ /* The favicon keeps its size; only the site name is truncated. */+ & > span {+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }++ /* Scaled down from the inline mention's 16px: it sits next to 0.78em text here. */+ & .link-embed-mention-favicon {+ width: 14px;+ height: 14px;+ }++ & .link-embed-mention-dot {+ width: 6px;+ height: 6px;+ }}