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
link previews: add an option to let the user control whether link previews are automatically generated for autodetected links
Adorian Dorande9729d85617
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 79900ec82a..16810a828b 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2590,7 +2590,9 @@"slash_commands_enabled": "Slash commands","slash_commands_description": "Editing commands such as inserting line breaks or headings can be toggled by typing `/`.","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."+ "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."},"table_view": {"new-row": "New row",diff --git a/apps/client/src/widgets/type_widgets/options/text_notes.tsx b/apps/client/src/widgets/type_widgets/options/text_notes.tsxindex 6c6d828f73..b8acbc679e 100644--- a/apps/client/src/widgets/type_widgets/options/text_notes.tsx+++ b/apps/client/src/widgets/type_widgets/options/text_notes.tsx@@ -130,6 +130,7 @@ function EditorFeatures() {const [noteCompletionEnabled, setNoteCompletionEnabled] = useTriliumOptionBool("textNoteCompletionEnabled");const [slashCommandsEnabled, setSlashCommandsEnabled] = useTriliumOptionBool("textNoteSlashCommandsEnabled");const [contentHintsEnabled, setContentHintsEnabled] = useTriliumOptionBool("textNoteContentHintsEnabled");+ const [autoLinkPreviewsEnabled, setAutoLinkPreviewsEnabled] = useTriliumOptionBool("textNoteAutoLinkPreviewsEnabled");return (<OptionsSection title={t("editorfeatures.title")}>@@ -141,6 +142,14 @@ function EditorFeatures() {onChange={setEmojiCompletionEnabled}/>+ <OptionsRowWithToggle+ name="auto-link-previews-enabled"+ label={t("editorfeatures.auto_link_previews_enabled")}+ description={t("editorfeatures.auto_link_previews_description")}+ currentValue={autoLinkPreviewsEnabled}+ onChange={setAutoLinkPreviewsEnabled}+ />+<OptionsRowWithTogglename="note-completion-enabled"label={t("editorfeatures.note_completion_enabled")}diff --git a/apps/client/src/widgets/type_widgets/text/config.ts b/apps/client/src/widgets/type_widgets/text/config.tsindex 3fca387cb1..c3e491d354 100644--- a/apps/client/src/widgets/type_widgets/text/config.ts+++ b/apps/client/src/widgets/type_widgets/text/config.ts@@ -198,6 +198,12 @@ export async function buildConfig(opts: BuildEditorOptions): Promise<EditorConfi// managers when it's false.(config as Record<string, unknown>).contentHintsEnabled = options.get("textNoteContentHintsEnabled") === "true";+ // Whether a URL typed or pasted into the note is auto-detected and turned into a link preview.+ // A getter rather than a boolean: the LinkEmbed plugin calls it each time a URL is detected, so+ // toggling the option applies to already-open editors instead of only to ones created afterwards.+ // Only the auto-detection is gated — inserting a preview from the toolbar dialog always works.+ (config as Record<string, unknown>).autoLinkPreviewsEnabled = () => options.get("textNoteAutoLinkPreviewsEnabled") === "true";+// Image toolbar actions (copy / download), handled by the ImageActions plugin. The copy// button is only added where copying the raw image is supported (Electron or a secure// context); elsewhere the browser's own context menu still offers a "Copy image" entry.diff --git a/packages/ckeditor5/src/plugins/linkembed.spec.ts b/packages/ckeditor5/src/plugins/linkembed.spec.tsindex 64ca1dc055..a5168f957b 100644--- a/packages/ckeditor5/src/plugins/linkembed.spec.ts+++ b/packages/ckeditor5/src/plugins/linkembed.spec.ts@@ -1,3 +1,4 @@+import type { LinkEmbedMetadata } from "@triliumnext/commons";import {_getViewData as getViewData,_setModelData as setModelData,@@ -26,7 +27,7 @@ const META = {favicon: "https://example.com/favicon.ico",siteName: "Example",image: "https://example.com/image.png"-};+} satisfies LinkEmbedMetadata;describe("LinkEmbed", () => {let editor: ClassicEditor;@@ -69,7 +70,7 @@ describe("LinkEmbed", () => {});it("binds the button to the insert command and executes it on click", () => {- const view = editor.ui.componentFactory.create("linkEmbed") as {+ const view = editor.ui.componentFactory.create("linkEmbed") as unknown as {isOn: boolean;isEnabled: boolean;fire(name: string): void;@@ -416,6 +417,44 @@ describe("LinkEmbed", () => {warn.mockRestore();});+ it("does not auto-convert when autoLinkPreviewsEnabled is false, but still offers the insert command", async () => {+ const url = "https://example.com/article";+ editor = await createTestEditor(+ [Essentials, Paragraph, BlockQuote, Link, Undo, LinkEmbed],+ { autoLinkPreviewsEnabled: false }+ );++ addLinkedText(editor, url);+ await flushFetch();++ // The URL is not even looked up: the check runs before the metadata request.+ expect(fetchLinkMetadata).not.toHaveBeenCalled();+ expect(findElement(editor, "linkMention")).toBeUndefined();+ expect(editor.getData()).toContain(`<a href="${url}">${url}</a>`);++ // The dialog route is unaffected by the option.+ editor.execute(LINK_EMBED_COMMAND);+ expect(triggerCommand).toHaveBeenCalledWith("addLinkEmbedToText");+ });++ it("re-reads a getter on every detected URL, so toggling the option needs no new editor", async () => {+ let enabled = false;+ editor = await createTestEditor(+ [Essentials, Paragraph, BlockQuote, Link, Undo, LinkEmbed],+ { autoLinkPreviewsEnabled: () => enabled }+ );++ addLinkedText(editor, "https://example.com/off");+ await flushFetch();+ expect(findElement(editor, "linkMention")).toBeUndefined();++ // Flip the option on the same editor instance — the next detected URL converts.+ enabled = true;+ addLinkedText(editor, "https://example.com/on");+ await flushFetch();+ expect(findElement(editor, "linkMention")).toBeDefined();+ });+it("ignores a labeled link whose text differs from the href", async () => {addLinkedText(editor, "https://example.com/article", "click here");diff --git a/packages/ckeditor5/src/plugins/linkembed.ts b/packages/ckeditor5/src/plugins/linkembed.tsindex 54c8b8afc2..aaf70ebadd 100644--- a/packages/ckeditor5/src/plugins/linkembed.ts+++ b/packages/ckeditor5/src/plugins/linkembed.ts@@ -383,6 +383,7 @@ class AutoLinkToMention extends Plugin {editor.model.document.on('change:data', (_evt, batch) => {if (this._converting) return;+ if (!this._autoDetectEnabled()) return;// When the user undoes our conversion, the undo batch re-inserts// the original linked text. Record its URL so we don't re-convert.@@ -425,6 +426,19 @@ class AutoLinkToMention extends Plugin {});}+ /**+ * Whether auto-detection is on (Options → Text Notes → Features). Consulted on every detected+ * URL rather than once at startup, so the host can supply a getter and have the option apply to+ * already-open editors. A plain boolean is honoured too, and an absent config means enabled.+ *+ * Only auto-detection is gated: inserting a preview from the toolbar dialog goes through+ * {@link InsertLinkEmbedCommand} and stays available either way.+ */+ private _autoDetectEnabled(): boolean {+ const setting = this.editor.config.get('autoLinkPreviewsEnabled') as boolean | (() => boolean) | undefined;+ return (typeof setting === 'function' ? setting() : setting) !== false;+ }+private _replaceWithPreview(url: string, parentPath: number[]) {const editor = this.editor;const editorEl = editor.editing.view.getDomRoot();diff --git a/packages/commons/src/lib/options_interface.ts b/packages/commons/src/lib/options_interface.tsindex 3bc4e1a051..acf5677d91 100644--- a/packages/commons/src/lib/options_interface.ts+++ b/packages/commons/src/lib/options_interface.ts@@ -189,6 +189,8 @@ export interface OptionDefinitions extends KeyboardShortcutsOptions<KeyboardActitextNoteSlashCommandsEnabled: boolean;/** Whether the editor surfaces content-area hints (bottom-corner popups that document how to interact with the element under the caret or pointer, e.g. task-state cycle, collapsible-summary shortcut, drag-handle label). */textNoteContentHintsEnabled: boolean;+ /** Whether a URL typed or pasted into a text note is automatically turned into a link preview. The "Link preview" dialog is unaffected and always inserts one on request. */+ textNoteAutoLinkPreviewsEnabled: boolean;backgroundEffects: boolean;newLayout: boolean;diff --git a/packages/trilium-core/src/routes/api/options.ts b/packages/trilium-core/src/routes/api/options.tsindex d7cbb52a0f..0948274c71 100644--- a/packages/trilium-core/src/routes/api/options.ts+++ b/packages/trilium-core/src/routes/api/options.ts@@ -112,6 +112,7 @@ const ALLOWED_OPTIONS = new Set<OptionNames>(["textNoteCompletionEnabled","textNoteSlashCommandsEnabled","textNoteContentHintsEnabled",+ "textNoteAutoLinkPreviewsEnabled","includeNoteDefaultBoxSize","layoutOrientation","backgroundEffects",diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.tsindex f2f9c68c79..cb78a96af7 100644--- a/packages/trilium-core/src/services/options_init.ts+++ b/packages/trilium-core/src/services/options_init.ts@@ -249,6 +249,7 @@ const defaultOptions: DefaultOption[] = [{ name: "textNoteCompletionEnabled", value: "true", isSynced: true },{ name: "textNoteSlashCommandsEnabled", value: "true", isSynced: true },{ name: "textNoteContentHintsEnabled", value: "true", isSynced: true },+ { name: "textNoteAutoLinkPreviewsEnabled", value: "true", isSynced: true },{ name: "includeNoteDefaultBoxSize", value: "medium", isSynced: true },// HTML import configuration