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
fix(link previews): let a missing favicon be missing
Elian Doran6d8fca19f70e
diff --git a/apps/client/src/services/link_embed.spec.ts b/apps/client/src/services/link_embed.spec.tsindex 90b1adc96a..00b2484691 100644--- a/apps/client/src/services/link_embed.spec.ts+++ b/apps/client/src/services/link_embed.spec.ts@@ -239,7 +239,6 @@ describe("renderEmbedPreview", () => {expect(card.querySelector("img.link-embed-card-image")).toBeNull();expect(card.querySelector(".link-embed-card-image-placeholder")).not.toBeNull();expect(card.querySelector("img.link-embed-mention-favicon")).toBeNull();- expect(card.querySelector(".link-embed-mention-dot")).not.toBeNull();const video = makeContainer();renderEmbedPreview(video, {@@ -251,11 +250,10 @@ describe("renderEmbedPreview", () => {const mention = makeContainer();renderMentionPreview(mention, { url: "https://example.com", favicon: "http://169.254.169.254/latest/meta-data/" });- expect(mention.querySelector("img.link-embed-mention-favicon")).toBeNull();- expect(mention.querySelector(".link-embed-mention-dot")).not.toBeNull();+ expect(mention.querySelector("img")).toBeNull();});- it("shows the stored favicon beside the site name, falling back to a dot without one", () => {+ it("shows the stored favicon beside the site name, and nothing at all without one", () => {const root = makeContainer();const meta = {url: "https://example.com/page",@@ -274,10 +272,12 @@ describe("renderEmbedPreview", () => {// The favicon leads the line, so it renders to the left of the site name.expect(urlLine.firstElementChild).toBe(favicon);+ // Nothing stands in for an icon that could not be had: the URL line is the site name alone.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();+ const urlLineWithout = withoutFavicon.querySelector(".link-embed-card-url");+ expect(urlLineWithout?.querySelector("img")).toBeNull();+ expect(urlLineWithout?.children.length).toBe(1);});it("omits optional fields and falls back to hostname, and drops target when editable", () => {@@ -332,11 +332,10 @@ describe("renderMentionPreview", () => {const anchor = root.querySelector("a.link-embed-mention")!;expect(anchor.getAttribute("target")).toBe("_blank");expect(anchor.querySelector("img.link-embed-mention-favicon")).not.toBeNull();- expect(root.querySelector(".link-embed-mention-dot")).toBeNull();expect(anchor.querySelector(".link-embed-mention-title")!.textContent).toBe("Example site");});- it("falls back to a dot favicon and hostname title; drops target when editable", () => {+ it("shows the title alone without a favicon, and drops target when editable", () => {const root = makeContainer();renderMentionPreview(root,@@ -346,12 +345,13 @@ describe("renderMentionPreview", () => {const anchor = root.querySelector("a.link-embed-mention")!;expect(anchor.getAttribute("target")).toBeNull();- expect(root.querySelector("img.link-embed-mention-favicon")).toBeNull();- expect(root.querySelector(".link-embed-mention-dot")).not.toBeNull();+ expect(root.querySelector("img")).toBeNull();+ // The title is the whole mention: nothing is drawn where the icon would have been.+ expect(anchor.children.length).toBe(1);expect(anchor.querySelector(".link-embed-mention-title")!.textContent).toBe("fallback.example.com");});- it("replaces a broken favicon with the dot on error", async () => {+ it("drops a broken favicon on error, leaving the title alone", async () => {const root = makeContainer();renderMentionPreview(root, {url: "https://example.com",@@ -363,7 +363,7 @@ describe("renderMentionPreview", () => {await fireError(img);expect(root.querySelector("img.link-embed-mention-favicon")).toBeNull();- expect(root.querySelector(".link-embed-mention-dot")).not.toBeNull();+ expect(root.querySelector("a.link-embed-mention")?.children.length).toBe(1);});});diff --git a/apps/client/src/services/link_embed.tsx b/apps/client/src/services/link_embed.tsxindex 2ef3d1b7a6..f88586fc2b 100644--- a/apps/client/src/services/link_embed.tsx+++ b/apps/client/src/services/link_embed.tsx@@ -84,19 +84,22 @@ export function unresolvedMetadata(url: string): EmbedMetadata {// ---------------------------------------------------------------------------/**- * One of a preview's pictures, or the placeholder standing in for it.+ * One of a preview's pictures, or what stands in for it.** Both pictures make the same two decisions, so they make them in one place: load only what* {@link safeLinkPreviewImageSrc} allows — an inline image or an attachment of this instance, so- * that opening a note never announces the reader to a third party — and show the placeholder both- * when there is nothing to load and when loading fails. The failure half matters as much as the- * check: an attachment can be erased out from under a preview that still references it, and a- * broken-image glyph reads as a bug where the placeholder reads as an absence.+ * that opening a note never announces the reader to a third party — and fall back both when there+ * is nothing to load and when loading fails. The failure half matters as much as the check: an+ * attachment can be erased out from under a preview that still references it, and a broken-image+ * glyph reads as a bug where an absence reads as an absence.+ *+ * `placeholder` is what a card's missing cover image gets, having a hole to fill; a missing favicon+ * omits it, since nothing drawn in its place says as much as the title already does.*/function PreviewPicture({ src, className, placeholder, size }: {src?: string | null;className: string;- placeholder: VNode;+ placeholder?: VNode;/** For a picture drawn at a fixed size; the card image is sized by CSS instead. */size?: number;}) {@@ -104,7 +107,7 @@ function PreviewPicture({ src, className, placeholder, size }: {const safeSrc = safeLinkPreviewImageSrc(src);if (!safeSrc || failed) {- return placeholder;+ return placeholder ?? null;}return (@@ -136,7 +139,6 @@ function Favicon({ src }: { src?: string }) {src={src}className={contrastClass ? `link-embed-mention-favicon ${contrastClass}` : "link-embed-mention-favicon"}size={16}- placeholder={<span className="link-embed-mention-dot" />}/>);}diff --git a/apps/client/src/widgets/type_widgets/text/LinkEmbed.css b/apps/client/src/widgets/type_widgets/text/LinkEmbed.cssindex aa1c5fee2d..fc9365a4e9 100644--- a/apps/client/src/widgets/type_widgets/text/LinkEmbed.css+++ b/apps/client/src/widgets/type_widgets/text/LinkEmbed.css@@ -86,19 +86,9 @@ body.light-theme .link-embed-favicon-light {display: unset;}-.link-embed-mention-dot {- display: inline-block;- width: 8px;- height: 8px;- border-radius: 50%;- background: var(--muted-text-color, #ccc);- flex-shrink: 0;-}-/* Sits the favicon before the link text and spaces it off the title. Scoped to the mention soit doesn't disturb the same favicon reused in the card's flex URL row. */-.link-embed-mention > .link-embed-mention-favicon,-.link-embed-mention > .link-embed-mention-dot {+.link-embed-mention > .link-embed-mention-favicon {vertical-align: -0.2em;margin-right: 4px;}@@ -367,9 +357,4 @@ section.link-embed:has(+ section.link-embed) {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 e34c0bdabc..bd1b6bb63a 100644--- a/apps/server/src/share/content_renderer.spec.ts+++ b/apps/server/src/share/content_renderer.spec.ts@@ -302,13 +302,13 @@ describe("content_renderer", () => {+ `<span>Example</span></div>`);});- it("falls back to a dot when the site has no favicon", () => {+ it("shows the site name alone 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>`);+ expect(content).toContain(`<div class="link-embed-card-url"><span>example.com</span></div>`);});it("renders a video as a click-to-play facade, without contacting YouTube", () => {@@ -374,8 +374,11 @@ describe("content_renderer", () => {// fetches a `data-favicon` — but nothing reaches an `src`, which is what would be fetched.expect(content).not.toContain(`src="http://169.254.169.254`);expect(content).not.toContain(`src="https://tracker.test`);- // Each sink degrades to the placeholder it already shows for a preview with no image.- expect(content).toContain(`<span class="link-embed-mention-dot"></span>`);+ // Each sink degrades to what it already shows for a preview that has no such picture:+ // the card keeps a placeholder in the hole its cover would fill, the favicon simply goes.+ expect(content).not.toContain(`class="link-embed-mention-favicon"`);+ expect(content).toContain(`<a class="link-embed-mention" href="https://example.com/page" target="_blank" rel="noopener noreferrer">`+ + `<span class="link-embed-mention-title">A title</span></a>`);expect(content).toContain(`<div class="link-embed-card-image-placeholder">`);expect(content).not.toContain(`class="link-embed-video-thumbnail"`);});diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.tsindex cc34b4b6e8..0233fef4bc 100644--- a/apps/server/src/share/content_renderer.ts+++ b/apps/server/src/share/content_renderer.ts@@ -375,12 +375,14 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOptreturn `<img class="${className}" src="${escapeHtml(safeSrc)}" alt="" loading="lazy"${sizeAttrs}>`;};- // 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.+ // The site's favicon — shown by both the inline mention and the card's URL line, from the one+ // `data-favicon` the element already carries. A site whose icon could not be had shows nothing+ // in its place: unlike a card's missing cover there is no hole to fill, and anything stood there+ // instead was read as a mark of its own rather than as an absent icon.const renderFavicon = (favicon: string | undefined | null) => renderPicture(favicon, {className: "link-embed-mention-favicon",size: 16,- placeholder: `<span class="link-embed-mention-dot"></span>`+ placeholder: ""});// Process link mentions (inline) — metadata is stored in data attributes.diff --git a/packages/share-theme/src/styles/link-embed.css b/packages/share-theme/src/styles/link-embed.cssindex 159910f2d8..2b8ec3bb53 100644--- a/packages/share-theme/src/styles/link-embed.css+++ b/packages/share-theme/src/styles/link-embed.css@@ -46,19 +46,9 @@ html.theme-light .link-embed-favicon-light {filter: hue-rotate(180deg) invert(1);}-.link-embed-mention-dot {- display: inline-block;- width: 8px;- height: 8px;- border-radius: 50%;- background: var(--text-menu);- flex-shrink: 0;-}-/* Sits the favicon before the link text and spaces it off the title. Scoped to the mention soit doesn't disturb the same favicon reused in the card's flex URL row. */-.link-embed-mention > .link-embed-mention-favicon,-.link-embed-mention > .link-embed-mention-dot {+.link-embed-mention > .link-embed-mention-favicon {vertical-align: -0.2em;margin-right: 4px;}@@ -241,9 +231,4 @@ html.theme-light .link-embed-favicon-light {width: 14px;height: 14px;}-- & .link-embed-mention-dot {- width: 6px;- height: 6px;- }}