Skip to content

Build your personal knowledge base with Trilium Notes

fix(notes): strip stale external srcset from localized images

Elian Doranc0a0079c5e5a
diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.ts
index 36f5c9c935..5366611ed9 100644
--- a/packages/trilium-core/src/services/notes.spec.ts
+++ b/packages/trilium-core/src/services/notes.spec.ts
@@ -318,6 +318,44 @@ describe("notes service (real DB)", () => {
expect(code.note.getRelations().some((r) => r.name === "internalLink")).toBe(false);
});
+ it("strips a stale external srcset from an image already pointing at a local attachment (#srcset)", () => {
+ const source = createNote("root", { title: "spec-srcset" });
+
+ // Real-world paste: the image was saved as a local attachment (src rewritten),
+ // but the copied HTML still carries a srcset of external URLs. Browsers prefer
+ // srcset over src, so once upstream removes those URLs the image vanishes even
+ // though the local attachment is still valid.
+ const content =
+ `<figure class="image image_resized" style="width:49.35%;">` +
+ `<img style="aspect-ratio:1290/238;" src="api/attachments/gbsXLfqQwo4a/image/asas.png" alt="" ` +
+ `srcset="https://example.com/wp-content/uploads/2025/02/asas.png 1290w, ` +
+ `https://example.com/wp-content/uploads/2025/02/asas-300x55.png 300w" ` +
+ `sizes="100vw" width="1290" height="238"></figure>`;
+
+ const { content: newContent } = getContext().init(() => saveLinks(source.note, content));
+
+ // The local src is preserved…
+ expect(newContent).toContain(`src="api/attachments/gbsXLfqQwo4a/image/asas.png"`);
+ // …but the external srcset/sizes are removed so the browser falls back to it.
+ expect(newContent).not.toContain("srcset=");
+ expect(newContent).not.toContain("example.com");
+ expect(newContent).not.toContain("sizes=");
+ });
+
+ it("keeps the srcset on an image whose src is still an external URL", () => {
+ const source = createNote("root", { title: "spec-srcset-external" });
+
+ // Nothing was localized here (e.g. downloadImagesAutomatically off): the src is
+ // still external, so the srcset is the legitimate/only source and must survive.
+ const content =
+ `<img src="https://example.com/a.png" ` +
+ `srcset="https://example.com/a.png 1290w, https://example.com/a-300.png 300w" sizes="100vw">`;
+
+ const { content: newContent } = getContext().init(() => saveLinks(source.note, content));
+
+ expect(newContent).toContain("srcset=");
+ });
+
it("extracts an inline base64 attachment, deriving its title via prepareTitle", () => {
const source = createNote("root", { title: "spec-inline-attachment" });
diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.ts
index fdc05a3ecb..c5b954e281 100644
--- a/packages/trilium-core/src/services/notes.ts
+++ b/packages/trilium-core/src/services/notes.ts
@@ -963,9 +963,31 @@ function saveAttachments(note: BNote, content: string) {
// we also omit / at the beginning to keep the paths relative
content = content.replace(/src="[^"]*\/api\/attachments\//g, 'src="api/attachments/');
+ content = stripStaleSrcset(content);
+
return content;
}
+/**
+ * When an image is pasted from the web its `src` is localized to a Trilium
+ * attachment, but the copied HTML often still carries the original `srcset`
+ * (and its companion `sizes`) pointing at external URLs. Browsers prefer
+ * `srcset` over `src`, so once those external URLs disappear the image breaks
+ * even though the local attachment is intact. Drop `srcset`/`sizes` from any
+ * `<img>` already backed by a local attachment so the valid `src` is used.
+ * Images whose `src` is still external keep their `srcset` — there it is the
+ * legitimate (and only) source.
+ */
+function stripStaleSrcset(content: string): string {
+ return content.replace(/<img\b[^>]*>/gi, (imgTag) => {
+ if (!/\ssrc=["']api\/attachments\/[^"']+["']/i.test(imgTag)) {
+ return imgTag;
+ }
+
+ return imgTag.replace(/\s(?:srcset|sizes)=["'][^"']*["']/gi, "");
+ });
+}
+
export function saveLinks(note: BNote, content: string | Uint8Array) {
if ((note.type !== "text" && note.type !== "relationMap" && note.type !== "llmChat" && note.type !== "spreadsheet" && note.type !== "canvas" && !note.isMarkdown()) || (note.isProtected && !protectedSessionService.isProtectedSessionAvailable())) {