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
feat(clipper): save browser tabs as link previews
Elian Doranf69fe74c2dd9
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex f092897dd4..fa8edf8683 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3443,7 +3443,7 @@"content_hints_enabled": "Content hints","content_hints_description": "Show short hints in the bottom corner of the editor that explain how to interact with the element under the caret or pointer.","auto_link_previews_enabled": "Autogenerate link previews",- "auto_link_previews_description": "Automatically turn a detected link into a preview showing the title, description and image of the web page. Inserting a preview from the toolbar always works, regardless of this setting.",+ "auto_link_previews_description": "Automatically turn a detected link into a preview showing the title, description and image of the web page, and save the tabs sent by the web clipper as inline previews. Inserting a preview from the toolbar always works, regardless of this setting.","html_support_enabled": "Preserve unsupported HTML tags","html_support_description": "Keep the HTML tags that the editor has no feature of its own for, as listed under Options → Other → HTML import tags. They appear as blocks that cannot be edited directly, and an unrecognised tag may not be visible at all while editing. With this off, such tags are dropped from a note the first time it is edited and saved; importing is unaffected either way."},diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.htmlindex 7299973ef3..0c13b60332 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Installation & Setup/Web Clipper.html@@ -83,8 +83,10 @@<p>Below the preview, a row of buttons offers the other actions: <em>Crop</em> (ascreenshot of an area you select), <em>Screenshot</em> (of the visible partof the page) and <em>Tabs</em> (saves the links of every tab in the current- window as a list). Hovering over a button shows its full name and, if it- has one, its keyboard shortcut.</p>+ window as a list of <a class="reference-link" href="#root/_help_UmgZAfcltxkS">Link Previews</a>,+ or of plain links when <em>Autogenerate link previews</em> is turned off).+ Hovering over a button shows its full name and, if it has one, its keyboard+ shortcut.</p></li><li><p>If the current page was already clipped, a notice at the top offers todiff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Link Previews.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Link Previews.htmlindex 88ee398837..816c911312 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Link Previews.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Link Previews.html@@ -52,7 +52,7 @@</table></figure><h2>Inserting a link preview</h2>-<p>There are two ways to create one:</p>+<p>There are three ways to create one:</p><h3>1. By pasting a URL</h3><p>A link preview is automatically created:</p><ul>@@ -79,6 +79,14 @@ class="admonition note"><h3>2. By using the "Link preview" toolbar button</h3><p>Click <strong>Link preview</strong> in the formatting toolbar to open the <em>Link preview</em> popup.Paste the URL and choose the type of preview.</p>+<h3>3. By saving browser tabs from the Web Clipper</h3>+<p>When the <a class="reference-link" href="#root/_help_MtPxeAWVAzMg">Web Clipper</a> saves+ the tabs of a window, each tab becomes an <em>inline</em> link preview showing+ the title and favicon the browser displays for it. Because this data comes+ from the browser, pages behind a login keep their real title, and the Trilium+ server does not contact the saved pages.</p>+<p>When <em>Autogenerate link previews</em> is turned off, the tabs are saved+ as a list of plain links instead.</p><h2>Altering an existing link preview</h2><p>Click on an inserted link preview to select it, and a small toolbar willappear with the following options:</p>diff --git a/apps/server/src/routes/api/clipper.spec.ts b/apps/server/src/routes/api/clipper.spec.tsindex acb79b6ef7..7619c80f3d 100644--- a/apps/server/src/routes/api/clipper.spec.ts+++ b/apps/server/src/routes/api/clipper.spec.ts@@ -1,4 +1,4 @@-import { BBranch, becca, becca_easy_mocking, BNote, branches, cls } from "@triliumnext/core";+import { BBranch, becca, becca_easy_mocking, BNote, branches, cls, options } from "@triliumnext/core";import type { Request } from "express";import { beforeAll, describe, expect, it, vi } from "vitest";@@ -7,10 +7,12 @@ import clipperRoute, { processContent } from "./clipper";const { buildNote } = becca_easy_mocking;const resolveYouTubePreview = vi.hoisted(() => vi.fn());+const storeFavicon = vi.hoisted(() => vi.fn());vi.mock("@triliumnext/core", async (importOriginal) => ({...await importOriginal<typeof import("@triliumnext/core")>(),- resolveYouTubePreview+ resolveYouTubePreview,+ storeFavicon}));vi.mock("../../services/image.js", () => ({@@ -171,6 +173,58 @@ describe("clipper route handlers", () => {expect(clipping.getParentNotes().map((p) => p.noteId)).toEqual([ folder.noteId ]);});+ describe("saving tabs", () => {+ const PLAIN_LIST = `<ul><li><a href="https://a.com/">A</a></li></ul>`;+ const FAVICON = "data:image/png;base64,iVBORw0KGgo=";++ function saveTabs(tabs: unknown) {+ return cls.init(async () => {+ const { noteId } = await clipperRoute.createNote({+ body: { title: "2 browser tabs", content: PLAIN_LIST, clipType: "tabs", tabs }+ } as unknown as Request);+ const note = becca.getNoteOrThrow(noteId);+ expect(note.hasLabel("pageUrl")).toBe(false);+ return note.getContent();+ });+ }++ it("saves the tabs as link mentions with their favicons", async () => {+ cls.init(() => options.setOption("textNoteAutoLinkPreviewsEnabled", "true"));+ storeFavicon.mockReset();+ storeFavicon.mockImplementation(async (_noteId: string, pageUrl: string) =>+ (pageUrl.startsWith("https://a.com") ? "api/attachments/fav/image/a.com.png" : undefined));++ const content = await saveTabs([+ { url: "https://a.com/1", title: "Tom & \"Jerry\"", favicon: FAVICON },+ { url: "https://www.youtube.com/watch?v=dQw4w9WgXcQ", title: "Video", favicon: "chrome://favicon" },+ { url: "https://b.com/", favicon: FAVICON },+ { title: "No URL" },+ null+ ]);++ expect(content).toBe("<ul>"+ + `<li><span class="link-mention" data-url="https://a.com/1" data-embed-type="opengraph"`+ + ` data-title="Tom & "Jerry""`+ + ` data-favicon="api/attachments/fav/image/a.com.png"></span></li>`+ + `<li><span class="link-mention" data-url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"`+ + ` data-embed-type="youtube" data-title="Video"></span></li>`+ + `<li><span class="link-mention" data-url="https://b.com/" data-embed-type="opengraph"></span></li>`+ + "</ul>");+ const stored = storeFavicon.mock.calls.map(([ , pageUrl, bytes ]) =>+ [ pageUrl, Buffer.from(bytes).toString("base64") ]);+ expect(stored).toEqual([ [ "https://a.com/1", "iVBORw0KGgo=" ], [ "https://b.com/", "iVBORw0KGgo=" ] ]);+ });++ it("keeps the plain list when previews are off or the extension sent no usable tabs", async () => {+ cls.init(() => options.setOption("textNoteAutoLinkPreviewsEnabled", "true"));+ expect(await saveTabs(undefined)).toBe(PLAIN_LIST);+ expect(await saveTabs([ { title: "No URL" } ])).toBe(PLAIN_LIST);++ cls.init(() => options.setOption("textNoteAutoLinkPreviewsEnabled", "false"));+ expect(await saveTabs([ { url: "https://a.com/", title: "A" } ])).toBe(PLAIN_LIST);+ });+ });+it("creates a standalone note with labels", async () => {const result = await cls.init(() => clipperRoute.createNote({body: {diff --git a/apps/server/src/routes/api/clipper.ts b/apps/server/src/routes/api/clipper.tsindex b7012fab1c..2cef1ceeb1 100644--- a/apps/server/src/routes/api/clipper.ts+++ b/apps/server/src/routes/api/clipper.ts@@ -1,5 +1,5 @@import { escapeHtml, extractYouTubeVideoId, type LinkEmbedMetadata } from "@triliumnext/commons";-import { app_info as appInfo, attribute_formatter as attributeFormatter, attributes as attributeService, becca, type BNote, date_notes as dateNoteService, date_utils as dateUtils, getLog, note_service as noteService, resolveYouTubePreview, sanitize, search as searchService, ValidationError, ws } from "@triliumnext/core";+import { app_info as appInfo, attribute_formatter as attributeFormatter, attributes as attributeService, becca, type BNote, date_notes as dateNoteService, date_utils as dateUtils, getLog, note_service as noteService, options, resolveYouTubePreview, sanitize, search as searchService, storeFavicon, ValidationError, ws } from "@triliumnext/core";import type { Request } from "express";import { parse } from "node-html-parser";import path from "path";@@ -7,6 +7,13 @@ import path from "path";import imageService from "../../services/image.js";import utils from "../../services/utils.js";+/** A browser tab as the extension sends it when saving the tabs of a window. */+interface ClippedTab {+ url?: unknown;+ title?: unknown;+ favicon?: unknown;+}+interface Image {src: string;dataUrl: string;@@ -121,7 +128,10 @@ async function createNote(req: Request) {if (typeof existingContent !== "string") {throw new ValidationError("Invalid note content type.");}- const rewrittenContent = await processContent(images, note, content);+ const tabMentions = clipType === "tabs"+ ? await renderTabMentions(note, req.body.tabs)+ : undefined;+ const rewrittenContent = await processContent(images, note, tabMentions ?? content);const newContent = `${existingContent}${existingContent.trim() ? "<br/>" : ""}${rewrittenContent}`;note.setContent(newContent);@@ -132,6 +142,46 @@ async function createNote(req: Request) {};}+/**+ * Renders saved browser tabs as a list of link mentions, each with the tab's favicon stored as an+ * attachment of `note`. Returns undefined when automatic link previews are turned off or the+ * extension sent no tabs, so the plain list of links in `content` is kept.+ */+async function renderTabMentions(note: BNote, tabs: unknown) {+ if (!options.getOptionBool("textNoteAutoLinkPreviewsEnabled") || !Array.isArray(tabs)) {+ return undefined;+ }++ const items: string[] = [];+ for (const tab of tabs as (ClippedTab | null)[]) {+ if (typeof tab?.url !== "string") {+ continue;+ }++ const url = sanitize.sanitizeUrl(tab.url);+ const favicon = await storeTabFavicon(note, url, tab.favicon);+ const mention = renderLinkPreview("span", "link-mention", {+ url,+ embedType: extractYouTubeVideoId(url) ? "youtube" : "opengraph",+ title: typeof tab.title === "string" ? tab.title : undefined,+ favicon+ });+ items.push(`<li>${mention}</li>`);+ }++ return items.length ? `<ul>${items.join("")}</ul>` : undefined;+}++/** Stores a tab's favicon, sent as a base64 `data:image/…` URL, and returns its attachment URL. */+async function storeTabFavicon(note: BNote, pageUrl: string, dataUrl: unknown) {+ if (typeof dataUrl !== "string" || !/^data:image\/[^,]*;base64,/.test(dataUrl)) {+ return undefined;+ }++ const bytes = Buffer.from(dataUrl.substring(dataUrl.indexOf(",") + 1), "base64");+ return await storeFavicon(note.noteId, pageUrl, bytes);+}+/** Sets each label the extension sends, replacing the value a previous clipping set. */function setLabels(note: BNote, labels: Record<string, string> | undefined) {for (const labelName in labels) {@@ -219,14 +269,18 @@ async function replaceYouTubePlayers(note: BNote, content: string) {getLog().info(`Could not resolve the preview of a clipped YouTube video: ${e}`);metadata = { url, embedType: "youtube" };}- iframe.replaceWith(renderLinkEmbed(metadata));+ iframe.replaceWith(renderLinkPreview("section", "link-embed", metadata));}));return dom.toString();}-/** The markup the text editor stores for a block link preview. */-function renderLinkEmbed(metadata: LinkEmbedMetadata) {+/** The markup the text editor stores for a link preview, a block `section` or an inline `span`. */+function renderLinkPreview(+ tagName: "section" | "span",+ className: string,+ metadata: LinkEmbedMetadata+) {const attributes = ([[ "data-url", metadata.url ],[ "data-embed-type", metadata.embedType ],@@ -239,7 +293,7 @@ function renderLinkEmbed(metadata: LinkEmbedMetadata) {.filter(([ , value ]) => value).map(([ name, value ]) => ` ${name}="${escapeHtml(value ?? "")}"`).join("");- return `<section class="link-embed"${attributes}></section>`;+ return `<${tagName} class="${className}"${attributes}></${tagName}>`;}function openNote(req: Request<{ noteId: string }>) {diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.tsindex 93f7995c88..0e17f9c81f 100644--- a/apps/web-clipper/entrypoints/background/index.spec.ts+++ b/apps/web-clipper/entrypoints/background/index.spec.ts@@ -31,7 +31,7 @@ vi.mock("./trilium_server_facade", () => ({import background from "./index";-type Tab = { id?: number, title?: string, url?: string };+type Tab = { id?: number, title?: string, url?: string, favIconUrl?: string };type ContextMenuClick = {menuItemId: string,pageUrl?: string,@@ -66,9 +66,10 @@ const createDocument = vi.fn(async () => {});const runtimeSendMessage = vi.fn(async (_message: unknown): Promise<unknown> => PNG);/** Answers a picture for a web address, and decodes a `data:` URL as the browser does. */const browserFetch = globalThis.fetch;-const fetchMock = vi.fn(async (url: string) => (url.startsWith("data:")+const fetchMockDefault = async (url: string) => (url.startsWith("data:")? browserFetch(url)- : new Response(new Blob([ "img" ], { type: "image/png" }))));+ : new Response(new Blob([ "img" ], { type: "image/png" })));+const fetchMock = vi.fn(fetchMockDefault);const SVG = "<svg xmlns='http://www.w3.org/2000/svg'></svg>";describe("background", () => {@@ -623,7 +624,12 @@ describe("background", () => {+ "<li><a href=\"https://c.com/1\">C1</a></li>"+ "<li><a href=\"https://d.com/1\">D1</a></li>"+ "</ul>",- clipType: "tabs"+ clipType: "tabs",+ tabs: [ "a.com/1", "b.com/1", "b.com/2", "c.com/1", "d.com/1" ].map((path) => ({+ url: `https://${path}`,+ title: path.replace(/^(\w)\.com\//, (_, letter: string) => letter.toUpperCase()),+ favicon: undefined+ }))});expect(await lastToast()).toMatchObject({ noteId: "saved", tabIds: [ 1, 2, 4, 5 ] });});@@ -642,7 +648,11 @@ describe("background", () => {content: "<ul><li><a href=\"https://a.com/?x=1&y="2"\">"+ "<b>Bold</b> & "quoted"</a></li>"+ "<li><a href=\"https://a.com/untitled\">https://a.com/untitled</a></li></ul>",- clipType: "tabs"+ clipType: "tabs",+ tabs: [+ { url: "https://a.com/?x=1&y=\"2\"", title: "<b>Bold</b> & \"quoted\"" },+ { url: "https://a.com/untitled", title: "https://a.com/untitled" }+ ]});expect(await lastToast()).toMatchObject({message: "2 links have been saved to Trilium.",@@ -650,6 +660,31 @@ describe("background", () => {});});+ it("sends each favicon it can read as a data URL", async () => {+ tabs = [+ { id: 1, title: "Missing", url: "https://a.com/", favIconUrl: "https://a.com/missing.ico" },+ { id: 2, title: "Fetched", url: "https://b.com/", favIconUrl: "https://b.com/favicon.png" },+ { id: 3, title: "Inline", url: "https://c.com/", favIconUrl: "data:image/svg+xml,<svg/>" },+ { id: 4, title: "Browser page", url: "chrome://newtab/", favIconUrl: "chrome://theme/icon" }+ ];+ // The favicons are fetched in tab order; the second and third get the default answer.+ fetchMock.mockImplementationOnce(async () => new Response("Not found", { status: 404 }));+ fetchMock.mockImplementationOnce(fetchMockDefault);+ fetchMock.mockImplementationOnce(fetchMockDefault);+ fetchMock.mockImplementationOnce(async () => {+ throw new TypeError("Failed to fetch");+ });++ await onCommand("saveTabs");++ expect((lastPayload() as { tabs: unknown }).tabs).toEqual([+ { url: "https://a.com/", title: "Missing", favicon: undefined },+ { url: "https://b.com/", title: "Fetched", favicon: "data:image/png;base64,aW1n" },+ { url: "https://c.com/", title: "Inline", favicon: "data:image/svg+xml;base64,PHN2Zy8+" },+ { url: "chrome://newtab/", title: "Browser page", favicon: undefined }+ ]);+ });+it("adds no ellipsis for up to three tabs and shows why saving failed", async () => {tabs = [{ id: 1, title: "A", url: "https://a.com/" },diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.tsindex fd2f42c541..4883beac39 100644--- a/apps/web-clipper/entrypoints/background/index.ts+++ b/apps/web-clipper/entrypoints/background/index.ts@@ -398,13 +398,30 @@ export default defineBackground(() => {if (tabs.length > 3) { topDomains += '...'; }+ // Trilium builds link mentions from `tabs` when link previews are turned on; older versions+ // ignore it and save `content`.return {title: `${tabs.length} browser tabs: ${topDomains}`,content,- clipType: 'tabs'+ clipType: 'tabs',+ tabs: await Promise.all(tabs.map(async tab => ({+ url: tab.url,+ title: tab.title ?? tab.url,+ favicon: await fetchFavicon(tab.favIconUrl)+ })))};}+ /** The tab's favicon as a base64 data URL, or `undefined` if there is none it can read. */+ async function fetchFavicon(url: string | undefined) {+ if (!url) return undefined;+ try {+ return await fetchImage(url);+ } catch {+ return undefined;+ }+ }+async function saveTabs() {const tabs = (await getWindowTabs()).filter((tab): tab is Browser.tabs.Tab & { url: string } => !!tab.url);diff --git a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.mdindex babdf4ae64..8369587f32 100644--- a/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md+++ b/docs/User Guide/User Guide/Installation & Setup/Web Clipper.md@@ -39,7 +39,7 @@ Clicking the extension's button in the browser toolbar opens its popup:The popup opens on the selection if there is one, otherwise on _Page_, showing the article, or the whole page when the page has no article. On a page that shows nothing to save, it opens on _Bookmark_ and says so. The keyboard shortcuts and the context menu still save right away, without a preview: saving the whole page that way saves the article, or the whole page when the page has no article.* Pages the extension cannot access (the browser's own pages, the extension stores, and pages that were open before the extension was installed until they are reloaded) cannot be saved or captured, so the popup says so in place of the preview, and only _Tabs_ remains available there.-* Below the preview, a row of buttons offers the other actions: _Crop_ (a screenshot of an area you select), _Screenshot_ (of the visible part of the page) and _Tabs_ (saves the links of every tab in the current window as a list). Hovering over a button shows its full name and, if it has one, its keyboard shortcut.+* Below the preview, a row of buttons offers the other actions: _Crop_ (a screenshot of an area you select), _Screenshot_ (of the visible part of the page) and _Tabs_ (saves the links of every tab in the current window as a list of <a class="reference-link" href="../Note%20Types/Text/Link%20Previews.md">Link Previews</a>, or of plain links when _Autogenerate link previews_ is turned off). Hovering over a button shows its full name and, if it has one, its keyboard shortcut.* If the current page was already clipped, a notice at the top offers to open the note in Trilium.* If Trilium cannot be found, the popup explains why instead of showing the actions, with buttons to look for Trilium again and to open the options.* The bottom of the popup shows whether the extension is connected to Trilium, as a colored dot (green when connected, amber when the versions are not compatible, red when Trilium was not found) next to a description of the connection, followed by the buttons for the extension's options and for this help page. A button to check the connection again appears next to them when hovering over the bar, and stays visible while Trilium cannot be reached or is not compatible.diff --git a/docs/User Guide/User Guide/Note Types/Text/Link Previews.md b/docs/User Guide/User Guide/Note Types/Text/Link Previews.mdindex 6665510e73..1d52a403f1 100644--- a/docs/User Guide/User Guide/Note Types/Text/Link Previews.md+++ b/docs/User Guide/User Guide/Note Types/Text/Link Previews.md@@ -28,7 +28,7 @@ A link preview can be displayed in one of three modes:## Inserting a link preview-There are two ways to create one:+There are three ways to create one:### 1\. By pasting a URL@@ -50,6 +50,12 @@ If the link is not accessible to the Trilium server (or the network request failClick **Link preview** in the formatting toolbar to open the _Link preview_ popup. Paste the URL and choose the type of preview.+### 3\. By saving browser tabs from the Web Clipper++When the <a class="reference-link" href="../../Installation%20%26%20Setup/Web%20Clipper.md">Web Clipper</a> saves the tabs of a window, each tab becomes an _inline_ link preview showing the title and favicon the browser displays for it. Because this data comes from the browser, pages behind a login keep their real title, and the Trilium server does not contact the saved pages.++When _Autogenerate link previews_ is turned off, the tabs are saved as a list of plain links instead.+## Altering an existing link previewClick on an inserted link preview to select it, and a small toolbar will appear with the following options:diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.tsindex 75f41beb0a..9760b9b0b2 100644--- a/packages/trilium-core/src/index.ts+++ b/packages/trilium-core/src/index.ts@@ -157,7 +157,7 @@ export type { RequestProvider, ExecOpts, CookieJar, FetchApiOpts, FetchResourceOexport type * from "./meta";export type { QueryString, QueryValue, Request, RequestHeaders, Response } from "./http_interface";export * as routeHelpers from "./routes/helpers";-export { resolveYouTubePreview } from "./routes/api/link_embed";+export { resolveYouTubePreview, storeFavicon } from "./routes/api/link_embed";export { getZipProvider, type ZipArchive, type ZipProvider } from "./services/zip_provider";export { default as zipImportService } from "./services/import/zip";diff --git a/packages/trilium-core/src/routes/api/link_embed.spec.ts b/packages/trilium-core/src/routes/api/link_embed.spec.tsindex d2ee7bf804..85e4f3cf19 100644--- a/packages/trilium-core/src/routes/api/link_embed.spec.ts+++ b/packages/trilium-core/src/routes/api/link_embed.spec.ts@@ -13,7 +13,7 @@ const safeFetch = vi.hoisted(() => vi.fn());const saveImageToAttachment = vi.hoisted(() => vi.fn());const awaitImageWrite = vi.hoisted(() => vi.fn(async () => {}));-import linkEmbedRoute from "./link_embed.js";+import linkEmbedRoute, { storeFavicon } from "./link_embed.js";let attachmentCounter = 0;@@ -912,3 +912,29 @@ describe("link-embed getMetadata", () => {expect(result.favicon).toBeUndefined();});});++describe("storeFavicon", () => {+ it("stores a favicon the caller holds, trimmed and named after the site", async () => {+ const fat = makeMultiSizeIco([+ { edge: 16, bytes: 1128 },+ { edge: 48, bytes: 9640 },+ { edge: 256, bytes: 70000 }+ ]);++ const url = await storeFavicon("note1", "https://www.example.com/page", fat);++ expect(url).toMatch(/^api\/attachments\/att\d+\/image\/www\.example\.com\.ico$/);+ expect(saveImageToAttachment.mock.calls[0]?.[0]).toBe("note1");+ expect(stored("favicon")?.buffer.byteLength).toBe(22 + 9640);+ expect(awaitImageWrite).toHaveBeenCalledWith("att1");+ });++ it("stores nothing for bytes that are not a picture or too large", async () => {+ const huge = makeMultiSizeIco([ { edge: 256, bytes: 100 * 1024 } ]);++ for (const bytes of [ Buffer.from("<html>Not found</html>"), huge ]) {+ expect(await storeFavicon("note1", "https://example.com/", bytes)).toBeUndefined();+ }+ expect(saveImageToAttachment).not.toHaveBeenCalled();+ });+});diff --git a/packages/trilium-core/src/routes/api/link_embed.ts b/packages/trilium-core/src/routes/api/link_embed.tsindex 2a154a3f58..96387bfa9c 100644--- a/packages/trilium-core/src/routes/api/link_embed.ts+++ b/packages/trilium-core/src/routes/api/link_embed.ts@@ -200,12 +200,18 @@ async function downloadFavicon(faviconUrl: string): Promise<DownloadedPicture |return undefined;}- // An icon directory gives up the sizes nothing will look at; anything else is kept as it came.- const bytes = trimIcoToSmallestEntry(downloaded.bytes, FAVICON_TARGET_EDGE) ?? downloaded.bytes;+ return asFavicon(downloaded.bytes);+}++/**+ * Keeps the part of a favicon a preview draws: the smallest entry of an icon directory, any other+ * picture as it is. Returns undefined if the bytes are not a picture or what is kept is too large.+ */+function asFavicon(bytes: Uint8Array): DownloadedPicture | undefined {+ const kept = trimIcoToSmallestEntry(bytes, FAVICON_TARGET_EDGE) ?? bytes;- // The ceiling is on what is kept, not on what arrived: a file that is one large picture has- // nothing to give up, and is the case this refuses.- return bytes.byteLength <= MAX_FAVICON_SIZE ? asPicture(bytes) : undefined;+ // The limit applies to what is kept, so an icon directory with one large entry is refused.+ return kept.byteLength <= MAX_FAVICON_SIZE ? asPicture(kept) : undefined;}/**@@ -625,6 +631,14 @@ export async function resolveYouTubePreview(noteId: string, url: string, videoIdreturn await storePictures(noteId, url, await fetchYouTubeMetadata(url, videoId));}+/**+ * Stores a favicon the caller already holds as an attachment of the note `noteId`, named after the+ * site of `pageUrl`, and returns its URL. Returns undefined if the bytes are not a usable favicon.+ */+export async function storeFavicon(noteId: string, pageUrl: string, bytes: Uint8Array) {+ return await storePicture(noteId, asFavicon(bytes), "favicon", safeHostname(pageUrl));+}+/*** The degraded result for a URL whose page told us nothing: everything the caller gets back is* derived from the URL itself. Flagged so the caller can keep a plain link instead of rendering a