Skip to content

Build your personal knowledge base with Trilium Notes

fix(link previews): stop turning Wikipedia's icon into a black square

Elian Doran3519c6235560
diff --git a/apps/server/src/routes/api/link_embed.spec.ts b/apps/server/src/routes/api/link_embed.spec.ts
index 90cf7cf030..afd3d4a4b0 100644
--- a/apps/server/src/routes/api/link_embed.spec.ts
+++ b/apps/server/src/routes/api/link_embed.spec.ts
@@ -550,10 +550,10 @@ describe("link-embed getMetadata", () => {
+ `<link rel="ICON SHORTCUT" sizes="16x16" href="/tiny.png">`
+ `<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/touch.png">`
+ `<link rel="icon" sizes="48x48" href="/right.png">`;
- const serve = (extra = "") => {
+ const serve = (declared = head) => {
safeFetch.mockReset();
safeFetch.mockImplementation(async (url: string) => url.endsWith("/page")
- ? fakeResponse(`<html><head><title>T</title>${extra}${head}</head></html>`, { contentType: "text/html" })
+ ? fakeResponse(`<html><head><title>T</title>${declared}</head></html>`, { contentType: "text/html" })
: fakeResponse("", { ok: false }));
};
const iconsRequested = () => safeFetch.mock.calls
@@ -562,21 +562,29 @@ describe("link-embed getMetadata", () => {
serve();
expect((await linkEmbedRoute.getMetadata(req("https://example.com/page"))).favicon).toBeUndefined();
- // The smallest that still covers a 3x display, then the larger one, then the one that would
- // have to be upscaled — and /favicon.ico last, which no site has to declare. The mask-icon
- // falls past the cap: a silhouette meant to be tinted draws as a black blob.
+ // The smallest that still covers a 3x display, then the one that would have to be upscaled,
+ // then the home-screen icon — which is a picture of the site drawn for a different purpose,
+ // on the opaque tile the platform requires — and /favicon.ico last, which no site has to
+ // declare. The mask-icon falls past the cap: a silhouette meant to be tinted is a black blob.
expect(iconsRequested().slice(0, 4)).toEqual([
"https://example.com/right.png",
- "https://example.com/touch.png",
"https://example.com/tiny.png",
+ "https://example.com/touch.png",
"https://example.com/favicon.ico"
]);
// An SVG — named by its type here, by its extension elsewhere — draws at every size for a
// few hundred bytes, so it outranks anything a size can be declared for.
- serve(`<link rel="icon" type="image/svg+xml" href="/scalable.svg">`);
+ serve(`<link rel="icon" type="image/svg+xml" href="/scalable.svg">${head}`);
await linkEmbedRoute.getMetadata(req("https://example.com/page"));
expect(iconsRequested()[0]).toBe("https://example.com/scalable.svg");
+
+ // Wikipedia's head exactly: a home-screen icon named first, the site's own icon second,
+ // neither declaring a size. Taking the first was how a preview ended up storing a white
+ // tile with a thin mark on it, which the theme then inverted into a black square.
+ serve(`<link rel="apple-touch-icon" href="/touch.png"><link rel="icon" href="/fav.ico">`);
+ await linkEmbedRoute.getMetadata(req("https://example.com/page"));
+ expect(iconsRequested()[0]).toBe("https://example.com/fav.ico");
});
it("keeps trying after an icon that fails to arrive, down to the conventional path", async () => {
diff --git a/apps/server/src/routes/api/link_embed.ts b/apps/server/src/routes/api/link_embed.ts
index 60b3a4653d..d82ca79c2d 100644
--- a/apps/server/src/routes/api/link_embed.ts
+++ b/apps/server/src/routes/api/link_embed.ts
@@ -458,7 +458,8 @@ const TIER_SCALABLE = 0;
const TIER_BIG_ENOUGH = 1;
const TIER_UNDECLARED = 2;
const TIER_TOO_SMALL = 3;
-const TIER_MASK = 4;
+const TIER_HOME_SCREEN = 4;
+const TIER_MASK = 5;
/**
* How promising an icon link is as a favicon, best first.
@@ -467,9 +468,16 @@ const TIER_MASK = 4;
* plain `favicon.ico` carries, and that is usually an icon directory of several sizes, out of which
* {@link downloadFavicon} keeps the right one. Ranking it below what a page says is big enough, and
* above what a page says is too small, is exactly what it is worth.
+ *
+ * The two icons at the bottom are there for being the wrong picture rather than the wrong size. A
+ * home-screen icon is composited onto an opaque tile by the platform it is for, so sites draw one:
+ * Wikipedia's apple-touch icon is a thin dark W on a white square, where its favicon is the same
+ * mark drawn to be seen at 16 pixels. Both would download; only one is the site's icon. And a
+ * Safari `mask-icon` is a silhouette meant to be tinted, which draws as a black blob when it is not.
*/
function faviconTier(rel: string, href: string, type: string | undefined, declared: number): number {
if (rel.includes("mask-icon")) return TIER_MASK;
+ if (rel.includes("apple-touch-icon")) return TIER_HOME_SCREEN;
const scalable = declared === Number.MAX_SAFE_INTEGER
|| (type || "").toLowerCase().includes("svg")
diff --git a/packages/commons/src/lib/favicon_contrast.spec.ts b/packages/commons/src/lib/favicon_contrast.spec.ts
index f7def7bca5..3badd18490 100644
--- a/packages/commons/src/lib/favicon_contrast.spec.ts
+++ b/packages/commons/src/lib/favicon_contrast.spec.ts
@@ -80,11 +80,28 @@ describe("classifying a favicon", () => {
expect(classifyFaviconContrast(visibility)).toBe("neutral");
});
+ it("leaves a tile alone however thin its mark is, since inverting one only moves the problem", () => {
+ // Wikipedia's home-screen icon: a white tile with a thin dark W over a sixteenth of it. The
+ // tile is the surface's own colour on a light theme, so it counts for nothing here — but
+ // that is what makes the icon look right, and inverting it yields a black square with a
+ // white W, which is what a reader saw before this share was lowered.
+ const lightTile = summarizeFaviconVisibility(bitmap([255, 255, 255, 255, 94], [0x20, 0x20, 0x20, 255, 6]));
+
+ expect(lightTile.onLight).toBeCloseTo(0.06, 5);
+ expect(classifyFaviconContrast(lightTile)).toBe("neutral");
+
+ // The same icon drawn the other way round, which a dark theme has to leave alone for the
+ // same reason.
+ const darkTile = summarizeFaviconVisibility(bitmap([0, 0, 0, 255, 94], [255, 255, 255, 255, 6]));
+
+ expect(classifyFaviconContrast(darkTile)).toBe("neutral");
+ });
+
it("weighs each pixel by how opaque it is, so a faint edge does not count as a whole mark", () => {
// A black glyph whose only light pixels are half-transparent: not enough to save it.
const visibility = summarizeFaviconVisibility(bitmap([0, 0, 0, 255, 90], [255, 255, 255, 26, 10]));
- expect(visibility.onDark).toBeLessThan(0.15);
+ expect(visibility.onDark).toBeLessThan(0.02);
expect(classifyFaviconContrast(visibility)).toBe("dark");
});
diff --git a/packages/commons/src/lib/favicon_contrast.ts b/packages/commons/src/lib/favicon_contrast.ts
index b6014b2fd3..6fe4b2dfc4 100644
--- a/packages/commons/src/lib/favicon_contrast.ts
+++ b/packages/commons/src/lib/favicon_contrast.ts
@@ -119,11 +119,17 @@ export type FaviconContrast = "dark" | "light" | "neutral";
* How much of an icon has to survive a surface for it to be left alone there.
*
* Not zero, because an icon is allowed a few stray pixels that happen to clear the threshold — a
- * black glyph with an anti-aliased edge or a single light speck is still a black glyph. A seventh of
- * the icon is enough to read as a shape, and is roughly the size of the white mark inside a filled
- * tile, which is the case this is really here to spare.
+ * black glyph with an anti-aliased edge or a single light speck is still a black glyph. But barely
+ * above it, because the share is measured over the whole icon, and an icon that carries its own
+ * background spends most of itself on that background: where the background is the surface's own
+ * colour it counts as invisible here, though it is precisely what makes such an icon look right.
+ *
+ * Wikipedia's home-screen icon is the case that fixed the number — a thin dark W on a white tile,
+ * of which the mark is 6% of what is drawn. It reads perfectly on a light theme, and at the seventh
+ * this used to ask for it was condemned and inverted into a black square. An icon that genuinely
+ * vanishes has no share at all to speak of: GitHub's octocat on a dark surface measures zero.
*/
-const MIN_VISIBLE_SHARE = 0.15;
+const MIN_VISIBLE_SHARE = 0.04;
/** Reads {@link summarizeFaviconVisibility}'s measurement as the one decision the stylesheets need. */
export function classifyFaviconContrast(visibility: FaviconVisibility): FaviconContrast {