Skip to content

Build your personal knowledge base with Trilium Notes

link previews: add a "Plain link" display mode

Elian Doran6b48f125f9c0
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 1c3e212992..062349d482 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2458,7 +2458,8 @@
"display": "Display",
"mode_inline": "Inline",
"mode_card": "Card",
- "mode_embed": "Embed"
+ "mode_embed": "Embed",
+ "mode_plain": "Plain link"
},
"link_context_menu": {
"open_note_in_new_tab": "Open note in a new tab",
diff --git a/packages/ckeditor5/src/plugins/linkembed.spec.ts b/packages/ckeditor5/src/plugins/linkembed.spec.ts
index 160123f4f6..2e69498c93 100644
--- a/packages/ckeditor5/src/plugins/linkembed.spec.ts
+++ b/packages/ckeditor5/src/plugins/linkembed.spec.ts
@@ -127,6 +127,18 @@ describe("LinkEmbed", () => {
expect(findElement(editor, "linkEmbed")?.getAttribute("embedType")).toBe("youtube");
});
+ it("inserts an ordinary link for the 'plain' mode, without fetching any metadata", async () => {
+ const url = "https://example.com/article";
+ setModelData(editor.model, "<paragraph>[]</paragraph>");
+
+ await editor.execute(LINK_EMBED_COMMAND, { url, mode: "plain" });
+
+ expect(fetchLinkMetadata).not.toHaveBeenCalled();
+ expect(findElement(editor, "linkMention")).toBeUndefined();
+ expect(findElement(editor, "linkEmbed")).toBeUndefined();
+ expect(editor.getData()).toBe(`<p><a href="${url}">${url}</a></p>`);
+ });
+
it("is enabled in a paragraph and disabled when read-only", () => {
setModelData(editor.model, "<paragraph>foo[]bar</paragraph>");
const command = editor.commands.get(LINK_EMBED_COMMAND);
@@ -454,6 +466,35 @@ describe("LinkEmbed", () => {
expect(mention?.getAttribute("title")).toBe("T");
});
+ it("converts a link widget to a plain, ordinary link — and never re-converts it", async () => {
+ setModelData(editor.model, '<paragraph>[<linkMention url="https://e.com/" title="T"></linkMention>]</paragraph>');
+
+ editor.execute(CHANGE_LINK_DISPLAY_COMMAND, { value: "plain" });
+
+ expect(findElement(editor, "linkMention")).toBeUndefined();
+ expect(editor.getData()).toBe('<p><a href="https://e.com/">https://e.com/</a></p>');
+
+ // The linked text arrives as an insertion, not a linkHref attribute change, so
+ // AutoLinkToMention must not have kicked in and fetched the preview straight back.
+ await Promise.resolve();
+ expect(fetchLinkMetadata).not.toHaveBeenCalled();
+
+ // A block preview converts the same way, gaining a wrapping paragraph.
+ setModelData(editor.model, '[<linkEmbed url="https://e.com/" embedType="opengraph"></linkEmbed>]');
+ editor.execute(CHANGE_LINK_DISPLAY_COMMAND, { value: "plain" });
+ expect(editor.getData()).toBe('<p><a href="https://e.com/">https://e.com/</a></p>');
+ });
+
+ it("degrades a non-http(s) URL to bare text when converting to a plain link", () => {
+ // A data-url the sanitizers pass through verbatim must not become a live href
+ // (same rule as the toolbar's open-link button; see isHttpUrl).
+ setModelData(editor.model, '[<linkEmbed url="javascript:alert(1)" embedType="opengraph"></linkEmbed>]');
+
+ editor.execute(CHANGE_LINK_DISPLAY_COMMAND, { value: "plain" });
+
+ expect(editor.getData()).toBe("<p>javascript:alert(1)</p>");
+ });
+
it("does nothing when the target mode equals the current mode", () => {
setModelData(editor.model, '[<linkEmbed url="https://e.com/" embedType="opengraph"></linkEmbed>]');
const before = editor.getData();
diff --git a/packages/ckeditor5/src/plugins/linkembed.ts b/packages/ckeditor5/src/plugins/linkembed.ts
index 61b1b3cccc..0fe56c8bde 100644
--- a/packages/ckeditor5/src/plugins/linkembed.ts
+++ b/packages/ckeditor5/src/plugins/linkembed.ts
@@ -12,7 +12,10 @@ export const REMOVE_LINK_EMBED_COMMAND = 'removeLinkEmbed';
export const LINK_DISPLAY_MODES = [
{ value: 'inline', label: 'Inline', labelKey: 'link_embed.mode_inline' },
{ value: 'card', label: 'Card', labelKey: 'link_embed.mode_card' },
- { value: 'embed', label: 'Embed', labelKey: 'link_embed.mode_embed' }
+ { value: 'embed', label: 'Embed', labelKey: 'link_embed.mode_embed' },
+ // The way out of the preview system: an ordinary <a> link, no widget. Offered in the same
+ // dropdowns as the real modes so "back to a normal URL" is found where the user looks for it.
+ { value: 'plain', label: 'Plain link', labelKey: 'link_embed.mode_plain' }
] as const;
export type LinkDisplayMode = typeof LINK_DISPLAY_MODES[number]['value'];
@@ -360,6 +363,16 @@ class LinkEmbedEditing extends Plugin {
class InsertLinkEmbedCommand extends Command {
override async execute({ url, mode }: { url: string; mode: LinkDisplayMode }) {
const editor = this.editor;
+
+ // A plain link is not a preview at all, so no metadata is fetched: the URL becomes an
+ // ordinary link, exactly as the editor's own link balloon would have inserted it.
+ if (mode === 'plain') {
+ editor.model.change((writer) => {
+ editor.model.insertContent(writer.createText(url, { linkHref: url }));
+ });
+ return;
+ }
+
const editorEl = editor.editing.view.getDomRoot();
const component = glob.getComponentByEl<EditorComponent>(editorEl);
@@ -467,8 +480,21 @@ class ChangeLinkDisplayCommand extends Command {
if (val != null) attrs[key] = val;
}
- // Detect the actual embed type from the URL via the client service.
const url = attrs.url as string;
+
+ // "Plain link" leaves the preview system: the widget becomes an ordinary link whose text is
+ // the URL. Inserted as new content (not an attribute change), so AutoLinkToMention does not
+ // see it and immediately convert it back. A URL we would never link to (see the isHttpUrl
+ // note in refresh()) degrades to bare text instead, as the unlink button does.
+ if (targetMode === 'plain') {
+ model.change(writer => {
+ const text = isHttpUrl(url) ? writer.createText(url, { linkHref: url }) : writer.createText(url);
+ model.insertContent(text, writer.createRangeOn(selected));
+ });
+ return;
+ }
+
+ // Detect the actual embed type from the URL via the client service.
const detectedType = this._detectEmbedType(url);
model.change(writer => {