Skip to content

Build your personal knowledge base with Trilium Notes

fix(link previews): spell a site the same way under both of its pictures

Elian Doran24ad1ccb0ec3
diff --git a/apps/client/src/services/link_embed.spec.ts b/apps/client/src/services/link_embed.spec.ts
index 476381d09b..32eea2374d 100644
--- a/apps/client/src/services/link_embed.spec.ts
+++ b/apps/client/src/services/link_embed.spec.ts
@@ -144,7 +144,7 @@ describe("fetchMetadata", () => {
"note1",
"data:image/jpeg;base64,IMG",
"coverImage",
- expect.stringMatching(/^example-com-[0-9a-f]{8}$/)
+ expect.stringMatching(/^example\.com-[0-9a-f]{8}$/)
);
// Only the attachment URLs land in the note content — inlined base64 counts against the
// auto-read-only size threshold, and a favicon pays that cost once per link rather than
diff --git a/packages/commons/src/lib/link_embed.spec.ts b/packages/commons/src/lib/link_embed.spec.ts
index 640c78e365..150016986a 100644
--- a/packages/commons/src/lib/link_embed.spec.ts
+++ b/packages/commons/src/lib/link_embed.spec.ts
@@ -118,7 +118,14 @@ describe("linkPreviewImageName", () => {
it("names the page it is a picture of, readably", () => {
// The attachment list shows this, where several rows of "image.jpeg" say nothing about
// which is which.
- expect(linkPreviewImageName(wiki)).toMatch(/^en-wikipedia-org-Russo-Japanese-War-[0-9a-f]{8}$/);
+ expect(linkPreviewImageName(wiki)).toMatch(/^en\.wikipedia\.org-Russo-Japanese-War-[0-9a-f]{8}$/);
+ });
+
+ it("spells the site the same way its favicon does", () => {
+ // A favicon is titled by the bare hostname, so reducing the hostname here would put the
+ // two pictures of one site under two different-looking names in the same list.
+ expect(linkPreviewImageName(wiki).startsWith("en.wikipedia.org")).toBe(true);
+ expect(linkPreviewImageName("https://example.com/")).toMatch(/^example\.com-[0-9a-f]{8}$/);
});
it("gives the same URL the same name, which is what makes pasting it twice reuse one picture", () => {
@@ -157,10 +164,11 @@ describe("linkPreviewImageName", () => {
];
for (const name of names) {
- // Nothing a file system, a URL or an export archive would have to escape.
- expect(name, name).toMatch(/^[a-zA-Z0-9-]+$/);
- expect(name.startsWith("-"), name).toBe(false);
- expect(name.endsWith("-"), name).toBe(false);
+ // Nothing a file system, a URL or an export archive would have to escape. Dots are in,
+ // being what a hostname is written with.
+ expect(name, name).toMatch(/^[a-zA-Z0-9.-]+$/);
+ expect(name.startsWith("-") || name.startsWith("."), name).toBe(false);
+ expect(name.endsWith("-") || name.endsWith("."), name).toBe(false);
// Bounded, so a long path cannot crowd the attachment list out.
expect(name.length, name).toBeLessThanOrEqual(70);
}
diff --git a/packages/commons/src/lib/link_embed.ts b/packages/commons/src/lib/link_embed.ts
index 2d29347f85..91052699fb 100644
--- a/packages/commons/src/lib/link_embed.ts
+++ b/packages/commons/src/lib/link_embed.ts
@@ -102,31 +102,39 @@ export function safeLinkPreviewImageSrc(src: string | undefined | null): string
*/
export function linkPreviewImageName(url: string): string {
let canonical = url;
- let readable = url;
+ let readable = toFileNamePart(url);
try {
const parsed = new URL(url);
parsed.hash = "";
canonical = parsed.toString();
- const lastSegment = decodeURIComponent(parsed.pathname).split("/").filter(Boolean).pop() ?? "";
- readable = `${parsed.hostname} ${lastSegment}`;
+ const lastSegment = toFileNamePart(decodeURIComponent(parsed.pathname).split("/").filter(Boolean).pop() ?? "");
+
+ // The hostname is kept as it is written. It is already a file name — letters, digits, dots
+ // and dashes, an international one having been punycoded by the parse — so reducing it too
+ // would only spell the same site one way here and another under its favicon, which is
+ // titled by the bare hostname.
+ readable = lastSegment ? `${parsed.hostname}-${lastSegment}` : parsed.hostname;
} catch {
// Not an address that can be taken apart — rare, since a preview only ever has an http(s)
// URL. The digest identifies it regardless; only the readable part suffers.
}
- return `${toFileNamePart(readable) || "image"}-${shortDigest(canonical)}`;
+ return `${capReadable(readable) || "image"}-${shortDigest(canonical)}`;
}
/** How much of the readable part to keep, so a long path does not crowd out the attachment list. */
const PREVIEW_NAME_MAX_READABLE = 60;
+/** Reduces a part of the address that is not already a file name — a path segment, or the lot. */
function toFileNamePart(value: string): string {
- return value
- .replace(/[^a-zA-Z0-9]+/g, "-")
- .slice(0, PREVIEW_NAME_MAX_READABLE)
- .replace(/^-+|-+$/g, "");
+ return value.replace(/[^a-zA-Z0-9]+/g, "-").replace(/^-+|-+$/g, "");
+}
+
+/** Bounds the readable part, and never lets it end on the punctuation a cut leaves behind. */
+function capReadable(value: string): string {
+ return value.slice(0, PREVIEW_NAME_MAX_READABLE).replace(/^[-.]+|[-.]+$/g, "");
}
/**
diff --git a/packages/trilium-core/src/routes/api/attachments.spec.ts b/packages/trilium-core/src/routes/api/attachments.spec.ts
index ae29ce2d8f..04a40813f2 100644
--- a/packages/trilium-core/src/routes/api/attachments.spec.ts
+++ b/packages/trilium-core/src/routes/api/attachments.spec.ts
@@ -282,9 +282,9 @@ describe("Attachments API (core)", () => {
{ file: { originalname: title, mimetype: "image/jpeg", buffer: PIXEL_PNG } }
);
- const first = await upload("en-wikipedia-org-Russo-Japanese-War-1a2b3c4d.jpeg");
- const again = await upload("en-wikipedia-org-Russo-Japanese-War-1a2b3c4d.jpeg");
- await upload("en-wikipedia-org-Crimean-War-5e6f7a8b.jpeg");
+ const first = await upload("en.wikipedia.org-Russo-Japanese-War-1a2b3c4d.jpeg");
+ const again = await upload("en.wikipedia.org-Russo-Japanese-War-1a2b3c4d.jpeg");
+ await upload("en.wikipedia.org-Crimean-War-5e6f7a8b.jpeg");
expect(again.body.url).toBe(first.body.url);
expect((await api.get<AttachmentPojo[]>(`/api/notes/${noteId}/attachments`)).body).toHaveLength(2);