Skip to content

Build your personal knowledge base with Trilium Notes

refactor(link previews): render a preview's pictures through one decision each side

Elian Doranaeb85a7e4068
diff --git a/apps/client/src/services/link_embed.tsx b/apps/client/src/services/link_embed.tsx
index 882f9f3084..88f8eafed1 100644
--- a/apps/client/src/services/link_embed.tsx
+++ b/apps/client/src/services/link_embed.tsx
@@ -1,7 +1,7 @@
import "../widgets/type_widgets/text/LinkEmbed.css";
import { extractYouTubeVideoId, type LinkEmbedMetadata, safeHostname, safeLinkPreviewHref, safeLinkPreviewImageSrc, YOUTUBE_REGEX } from "@triliumnext/commons";
-import { render } from "preact";
+import { render, type VNode } from "preact";
import { useState } from "preact/hooks";
import { t } from "./i18n.js";
@@ -82,22 +82,39 @@ export function unresolvedMetadata(url: string): EmbedMetadata {
// notes, markdown preview).
// ---------------------------------------------------------------------------
-function Favicon({ src }: { src?: string }) {
- const [failed, setFailed] = useState(false);
- // See safeLinkPreviewImageSrc: only an inline image or an attachment of this instance, so that
- // opening a note never announces the reader to a third party.
+/**
+ * One of a preview's pictures, or the placeholder standing 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.
+ */
+function PreviewPicture({ src, className, placeholder, size }: {
+ src?: string | null;
+ className: string;
+ placeholder: VNode;
+ /** For a picture drawn at a fixed size; the card image is sized by CSS instead. */
+ size?: number;
+}) {
+ const [ failed, setFailed ] = useState(false);
const safeSrc = safeLinkPreviewImageSrc(src);
if (!safeSrc || failed) {
- return <span className="link-embed-mention-dot" />;
+ return placeholder;
}
return (
<img
- className="link-embed-mention-favicon"
+ className={className}
src={safeSrc}
- width={16}
- height={16}
+ alt=""
+ width={size}
+ height={size}
+ loading="lazy"
+ draggable={false}
onError={() => setFailed(true)}
/>
);
@@ -107,26 +124,21 @@ function ImagePlaceholder() {
return <div className="link-embed-card-image-placeholder">&#128279;</div>;
}
-function CardImage({ src }: { src?: string }) {
- const [failed, setFailed] = useState(false);
- const safeSrc = safeLinkPreviewImageSrc(src);
-
- if (!safeSrc || failed) {
- return <ImagePlaceholder />;
- }
-
+function Favicon({ src }: { src?: string }) {
return (
- <img
- className="link-embed-card-image"
- src={safeSrc}
- alt=""
- loading="lazy"
- draggable={false}
- onError={() => setFailed(true)}
+ <PreviewPicture
+ src={src}
+ className="link-embed-mention-favicon"
+ size={16}
+ placeholder={<span className="link-embed-mention-dot" />}
/>
);
}
+function CardImage({ src }: { src?: string }) {
+ return <PreviewPicture src={src} className="link-embed-card-image" placeholder={<ImagePlaceholder />} />;
+}
+
/**
* A YouTube player that only contacts YouTube once the user asks it to.
*
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.ts
index 8318de331b..e34c0bdabc 100644
--- a/apps/server/src/share/content_renderer.spec.ts
+++ b/apps/server/src/share/content_renderer.spec.ts
@@ -298,7 +298,7 @@ describe("content_renderer", () => {
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">`
+ + `<img class="link-embed-mention-favicon" src="${FAVICON}" alt="" loading="lazy" width="16" height="16">`
+ `<span>Example</span></div>`);
});
@@ -386,7 +386,7 @@ describe("content_renderer", () => {
});
const content = String(getContent(note).content);
- expect(content).toContain(`<img class="link-embed-mention-favicon" src="${FAVICON}" width="16" height="16">`);
+ expect(content).toContain(`<img class="link-embed-mention-favicon" src="${FAVICON}" alt="" loading="lazy" width="16" height="16">`);
expect(content).toContain(`<span class="link-embed-mention-title">A title</span>`);
});
});
diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.ts
index c231d91f0e..cc34b4b6e8 100644
--- a/apps/server/src/share/content_renderer.ts
+++ b/apps/server/src/share/content_renderer.ts
@@ -355,17 +355,33 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt
};
const document = parse(result.content || "", parseOpts);
+ // One of a preview's pictures, or what stands in for it. Every picture on a shared page makes
+ // the same decision, so it is made once: safeLinkPreviewImageSrc() keeps the placeholder for
+ // anything but an inline image or an attachment of this instance, because an <img> fires on
+ // load — a remote URL here would have every visitor to the shared page announce itself to a
+ // third party without so much as a click.
+ const renderPicture = (
+ src: string | undefined | null,
+ { className, placeholder, size }: { className: string; placeholder: string; size?: number }
+ ) => {
+ const safeSrc = safeLinkPreviewImageSrc(src);
+
+ if (!safeSrc) {
+ return placeholder;
+ }
+
+ const sizeAttrs = size ? ` width="${size}" height="${size}"` : "";
+
+ return `<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.
- // safeLinkPreviewImageSrc() keeps the dot for anything but an inline image or an attachment of
- // this instance: an <img> fires on load, so a remote URL here would have every visitor to the
- // shared page announce itself to a third party without so much as a click.
- const renderFavicon = (favicon: string | undefined | null) => {
- const src = safeLinkPreviewImageSrc(favicon);
- return src
- ? `<img class="link-embed-mention-favicon" src="${escapeHtml(src)}" width="16" height="16">`
- : `<span class="link-embed-mention-dot"></span>`;
- };
+ const renderFavicon = (favicon: string | undefined | null) => renderPicture(favicon, {
+ className: "link-embed-mention-favicon",
+ size: 16,
+ placeholder: `<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")) {
@@ -393,10 +409,11 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt
// loads YouTube's player once a visitor asks for it, so simply reading the page does
// not hand every visitor's IP to Google. The swap is done by the share theme's
// video_facade script, which reads data-video-id.
- const image = safeLinkPreviewImageSrc(embedEl.getAttribute("data-image"));
- const thumbnailHtml = image
- ? `<img class="link-embed-video-thumbnail" src="${escapeHtml(image)}" alt="" loading="lazy">`
- : "";
+ // No placeholder: the play button carries the facade on its own.
+ const thumbnailHtml = renderPicture(embedEl.getAttribute("data-image"), {
+ className: "link-embed-video-thumbnail",
+ placeholder: ""
+ });
embedEl.innerHTML = `<div class="link-embed-video">`
+ `<button type="button" class="link-embed-video-facade" data-video-id="${escapeHtml(videoId)}" aria-label="Play video" title="Play video">`
+ thumbnailHtml
@@ -406,12 +423,16 @@ function renderText(result: Result, note: SNote | BNote, options: ShareRenderOpt
} else {
const title = embedEl.getAttribute("data-title") || safeHostnameForShare(url);
const description = embedEl.getAttribute("data-description");
- const image = safeLinkPreviewImageSrc(embedEl.getAttribute("data-image"));
const siteName = embedEl.getAttribute("data-site-name") || safeHostnameForShare(url);
- const imageHtml = image
- ? `<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">&#128279;</div></div>`;
+ // The wrapper is there either way: it is what gives the card's left column its size, so
+ // a card without a picture keeps the same shape as one with it.
+ const imageHtml = `<div class="link-embed-card-image-wrapper">`
+ + renderPicture(embedEl.getAttribute("data-image"), {
+ className: "link-embed-card-image",
+ placeholder: `<div class="link-embed-card-image-placeholder">&#128279;</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"))