Skip to content

Build your personal knowledge base with Trilium Notes

feat(text): drop a web page's theme styles from pasted text

Elian Doran91aadd183d8d
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/General formatting.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/General formatting.html
index cdf2ae3c58..203a490df3 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/General formatting.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/General formatting.html
@@ -84,6 +84,20 @@
were picked, until the note is next edited.</p>
<p>Older browsers that can't adapt colors show them exactly as they were
picked.</p>
+<p>When you paste text copied from a web page in Chrome, Edge or a Chromium-based
+ app (such as Visual Studio Code), Trilium removes the page's styling:</p>
+<ul>
+ <li>Text and background colors.</li>
+ <li>Fonts and font sizes.</li>
+ <li>Table borders, backgrounds and spacing.</li>
+</ul>
+<p>The following formatting is preserved:</p>
+<ul>
+ <li>Bold, italic, underline and strikethrough.</li>
+ <li>Subscript and superscript.</li>
+ <li>Links, code and tables.</li>
+</ul>
+<p>Colors are preserved when pasting from Word, Google Docs or another note.</p>
<p>To remove either the background or foreground color of a text, select
the corresponding formatting button and press <em>Remove color</em> or use
the <em>Remove formatting</em> toolbar item.</p>
diff --git a/docs/User Guide/User Guide/Note Types/Text/General formatting.md b/docs/User Guide/User Guide/Note Types/Text/General formatting.md
index f620ff40fc..ecc8cbb0a8 100644
--- a/docs/User Guide/User Guide/Note Types/Text/General formatting.md
+++ b/docs/User Guide/User Guide/Note Types/Text/General formatting.md
@@ -71,6 +71,20 @@ Colors applied with older versions of Trilium are shown exactly as they were pic
Older browsers that can't adapt colors show them exactly as they were picked.
+When you paste text copied from a web page in Chrome, Edge or a Chromium-based app (such as Visual Studio Code), Trilium removes the page's styling:
+
+* Text and background colors.
+* Fonts and font sizes.
+* Table borders, backgrounds and spacing.
+
+The following formatting is preserved:
+
+* Bold, italic, underline and strikethrough.
+* Subscript and superscript.
+* Links, code and tables.
+
+Colors are preserved when pasting from Word, Google Docs or another note.
+
To remove either the background or foreground color of a text, select the corresponding formatting button and press _Remove color_ or use the _Remove formatting_ toolbar item.
## Remove formatting
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index f651e8be93..76616959e5 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -38,6 +38,7 @@ import CopyLinkUrlButton from "./plugins/copy_link_url.js";
import ImageActions from "./plugins/image_actions.js";
import ClipboardBareImage from "./plugins/clipboard_bare_image.js";
import ClipboardImageEmbed from "./plugins/clipboard_image_embed.js";
+import ClipboardPageStyles from "./plugins/clipboard_page_styles.js";
import TriliumSnippets from "./plugins/snippets/snippets.js";
import TriliumAiAssistant from "./plugins/ai_assistant/ai_assistant.js";
@@ -113,6 +114,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [
ImageActions,
ClipboardImageEmbed,
ClipboardBareImage,
+ ClipboardPageStyles,
AdaptiveColors,
TriliumSnippets,
TriliumAiAssistant,
diff --git a/packages/ckeditor5/src/plugins/clipboard_page_styles.spec.ts b/packages/ckeditor5/src/plugins/clipboard_page_styles.spec.ts
new file mode 100644
index 0000000000..f2172de859
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/clipboard_page_styles.spec.ts
@@ -0,0 +1,151 @@
+import { Bold, ClassicEditor, ClipboardPipeline, Code, Essentials, Font, GeneralHtmlSupport, Italic, Link, Paragraph, Strikethrough, Subscript, Superscript, Table, TableCellProperties, TableProperties, Underline } from "ckeditor5";
+import type { ViewDocumentFragment, ViewElement } from "ckeditor5";
+import { beforeEach, describe, expect, it } from "vitest";
+
+import { createTestEditor } from "../../test/editor-kit.js";
+import ClipboardPageStyles, { isComputedStyleDump, stripPageStyles } from "./clipboard_page_styles.js";
+
+/** A selection copied out of the Claude Code panel in VS Code under a dark theme, verbatim. */
+const VS_CODE_SELECTION = `<meta charset='utf-8'><span style="color: rgb(191, 191, 191); font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Roboto, sans-serif; font-size: 13px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(25, 26, 27); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">I'd start with (1).</span>`;
+
+/**
+ * A heading and a table copied out of the same panel, verbatim but cut after the first body row.
+ * Each top-level element carries a dump; the cells and the code span carry only the styles that the
+ * page's stylesheet matched.
+ */
+const VS_CODE_TABLE = `<meta charset='utf-8'><p style="white-space: pre-wrap; margin-top: 0.1em; margin-bottom: 0.2em; unicode-bidi: plaintext; color: rgb(191, 191, 191); font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Roboto, sans-serif; font-size: 13px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(25, 26, 27); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><strong>What competitors do:</strong></p><table style="border-collapse: collapse; border-color: rgb(42, 43, 44); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; color: rgb(191, 191, 191); font-family: -apple-system, &quot;system-ui&quot;, &quot;Segoe UI&quot;, Roboto, sans-serif; font-size: 13px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(25, 26, 27); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><thead><tr><th style="unicode-bidi: plaintext; border-color: rgb(42, 43, 44); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; padding: 2px;">App</th><th style="unicode-bidi: plaintext; border-color: rgb(42, 43, 44); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; padding: 2px;">Behaviour</th></tr></thead><tbody><tr><td style="unicode-bidi: plaintext; border-color: rgb(42, 43, 44); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; padding: 2px;"><strong>Tiptap / ProseMirror editors</strong><span> </span>(and products built on them)</td><td style="unicode-bidi: plaintext; border-color: rgb(42, 43, 44); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; padding: 2px;">Tiptap's code-block extension reads<span> </span><code style="font-family: monospace; color: rgb(140, 140, 140); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(38, 38, 38); padding: 2px 4px; border-radius: 3px; word-break: break-word; font-size: 0.9em;">vscode-editor-data</code>: the paste becomes a real code block in the right language, and the colors are thrown away. That's from my memory of its source; I didn't verify it today.</td></tr></tbody></table>`;
+
+/** The computed-style dump that Chromium writes on the element wrapping a copied selection. */
+const DUMP = "color: rgb(191, 191, 191); background-color: rgb(25, 26, 27); font-family: Roboto, sans-serif; font-size: 13px; "
+ + "font-style: normal; font-weight: 400; orphans: 2; widows: 2; -webkit-text-stroke-width: 0px; "
+ + "font-variant-ligatures: normal; font-variant-caps: normal; text-decoration-thickness: initial;";
+
+/**
+ * A selection spanning formatted text: inside the dump, Chromium writes only the styles that the
+ * page's stylesheet matched on each element.
+ */
+const FORMATTED_SELECTION = `<p style="${DUMP}">`
+ + `<code style="background-color: rgb(40, 40, 40); color: rgb(230, 120, 120); font-family: monospace; padding: 2px;">npm</code> `
+ + `<span style="font-weight: 700; color: rgb(255, 255, 255);">bold</span> `
+ + `<span style="font-style: italic;">italic</span> `
+ + `<span style="text-decoration: underline;">under</span> `
+ + `<span style="vertical-align: super;">sup</span> `
+ + `<a href="https://example.com" style="color: rgb(120, 160, 255);">link</a>`
+ + `</p>`;
+
+describe("ClipboardPageStyles", () => {
+ let editor: ClassicEditor;
+
+ beforeEach(async () => {
+ editor = await createTestEditor(
+ [
+ Essentials, Paragraph, Bold, Italic, Underline, Strikethrough, Subscript, Superscript, Code, Link, Font,
+ Table, TableProperties, TableCellProperties, GeneralHtmlSupport, ClipboardPageStyles
+ ],
+ {
+ // As in Trilium with HTML support on: every tag with every style, so whatever the
+ // plugin leaves behind would reach the note.
+ htmlSupport: { allow: [{ name: /^(p|span|code|a|table|thead|tbody|tr|th|td)$/, attributes: true, classes: true, styles: true }] }
+ }
+ );
+ editor.setData("<p></p>");
+ });
+
+ function toView(html: string) {
+ return editor.data.processor.toView(html) as ViewDocumentFragment;
+ }
+
+ function firstElement(html: string) {
+ const fragment = toView(html);
+ for (const { item } of editor.editing.view.createRangeIn(fragment)) {
+ if (item.is("element")) {
+ return item as ViewElement;
+ }
+ }
+ throw new Error("No element in fixture");
+ }
+
+ function paste(html: string) {
+ editor.plugins.get(ClipboardPipeline).fire("inputTransformation", {
+ content: toView(html),
+ dataTransfer: { getData: () => "", setData: () => {} },
+ method: "paste"
+ });
+
+ return editor.getData();
+ }
+
+ describe("isComputedStyleDump", () => {
+ it("recognizes the dump that Chromium writes on a copied selection", () => {
+ expect(isComputedStyleDump(firstElement(VS_CODE_SELECTION))).toBe(true);
+ expect(isComputedStyleDump(firstElement(`<span style="${DUMP}">x</span>`))).toBe(true);
+ });
+
+ it("rejects styles that an application or an author wrote", () => {
+ expect(isComputedStyleDump(firstElement(`<span style="color: red; background-color: yellow;">x</span>`))).toBe(false);
+ expect(isComputedStyleDump(firstElement(`<span>x</span>`))).toBe(false);
+ // Under the threshold: a print stylesheet can set `orphans` and `widows` on purpose.
+ expect(isComputedStyleDump(firstElement(`<p style="orphans: 3; widows: 3; color: red;">x</p>`))).toBe(false);
+ });
+ });
+
+ describe("stripPageStyles", () => {
+ it("reports whether the fragment was a dump", () => {
+ const view = editor.editing.view;
+
+ expect(stripPageStyles(view, toView(VS_CODE_SELECTION))).toBe(true);
+ expect(stripPageStyles(view, toView(`<p><span style="color: red;">x</span></p>`))).toBe(false);
+ });
+
+ it("drops a style attribute with nothing left to keep", () => {
+ const fragment = toView(`<p style="${DUMP}">x</p>`);
+
+ stripPageStyles(editor.editing.view, fragment);
+
+ const paragraph = fragment.getChild(0) as ViewElement;
+ expect(paragraph.is("element", "p")).toBe(true);
+ expect(paragraph.hasAttribute("style")).toBe(false);
+ });
+
+ it("unwraps spans that are left with no attributes, and keeps the rest", () => {
+ const fragment = toView(`<p style="${DUMP}"><span style="color: red;">a</span><span class="x">b</span></p>`);
+
+ stripPageStyles(editor.editing.view, fragment);
+
+ const paragraph = fragment.getChild(0) as ViewElement;
+ const children = [...paragraph.getChildren()];
+ expect(children[0].is("$text") && children[0].data).toBe("a");
+ expect(children[1].is("element", "span")).toBe(true);
+ });
+ });
+
+ describe("on paste", () => {
+ it("pastes the VS Code selection as plain text", () => {
+ expect(paste(VS_CODE_SELECTION)).toBe("<p>I'd start with (1).</p>");
+ });
+
+ it("keeps the formatting of a dump and drops the page's colors and fonts", () => {
+ const data = paste(FORMATTED_SELECTION);
+
+ expect(data).toBe("<p><code>npm</code> <strong>bold</strong> <i>italic</i> <u>under</u> <sup>sup</sup> "
+ + "<a href=\"https://example.com\">link</a></p>");
+ });
+
+ it("pastes a table without the page's borders, backgrounds and padding", () => {
+ const data = paste(VS_CODE_TABLE);
+
+ expect(data).toContain("<p><strong>What competitors do:</strong></p>");
+ expect(data).toContain("<th>App</th>");
+ expect(data).toContain("<code>vscode-editor-data</code>");
+ expect(data).not.toContain("style=");
+ expect(data).not.toContain("<span");
+ });
+
+ it("leaves colors alone when the HTML is not a dump", () => {
+ const data = paste(`<p><span style="color: rgb(255, 0, 0);">red</span> <span style="background-color: rgb(255, 255, 0);">marked</span></p>`);
+
+ expect(data).toContain("<span style=\"color:rgb(255,0,0);\">red</span>");
+ expect(data).toContain("<span style=\"background-color:rgb(255,255,0);\">marked</span>");
+ });
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/clipboard_page_styles.ts b/packages/ckeditor5/src/plugins/clipboard_page_styles.ts
new file mode 100644
index 0000000000..bfad590740
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/clipboard_page_styles.ts
@@ -0,0 +1,137 @@
+import { ClipboardPipeline, Plugin, UpcastWriter } from "ckeditor5";
+import type { ClipboardInputTransformationEvent, EditingView, ViewDocumentFragment, ViewElement } from "ckeditor5";
+
+/**
+ * Properties that Chromium and WebKit write into every computed-style dump and that nobody sets by
+ * hand on inline content. `orphans` and `widows` only affect paged media.
+ */
+const DUMP_FINGERPRINT = [
+ "orphans",
+ "widows",
+ "-webkit-text-stroke-width",
+ "font-variant-ligatures",
+ "font-variant-caps",
+ "text-decoration-thickness"
+];
+
+/** How many {@link DUMP_FINGERPRINT} properties an element must carry to count as a dump. */
+const FINGERPRINT_THRESHOLD = 3;
+
+/**
+ * The styles in a dump that carry meaning rather than the source page's theme, each with the
+ * values that are only the default and are therefore dropped too.
+ */
+const KEPT_STYLES: Record<string, (value: string) => boolean> = {
+ "font-weight": (value) => value === "bold" || value === "bolder" || Number(value) >= 600,
+ "font-style": (value) => value === "italic" || value.startsWith("oblique"),
+ "text-decoration": hasTextDecoration,
+ "text-decoration-line": hasTextDecoration,
+ "vertical-align": (value) => value === "sub" || value === "super"
+};
+
+/**
+ * Removes the source page's theme from HTML that a Chromium or WebKit browser copied out of a
+ * rendered page.
+ *
+ * When copying a selection, Chromium, WebKit and every Electron app write the computed styles of the
+ * source page inline (`color`, `background-color`, `font-family`, `font-size`, …). Pasted into a
+ * note, text copied from a dark-themed page keeps its light-grey text on a near-black background.
+ *
+ * The plugin recognizes such a paste by {@link DUMP_FINGERPRINT}, then strips every inline style in
+ * the fragment except bold, italic, underline, strikethrough, subscript and superscript. Elements,
+ * links and classes are kept. Clipboard HTML that applications build themselves (Word, Google Docs,
+ * a copy out of another note) carries no fingerprint and is left alone.
+ */
+export default class ClipboardPageStyles extends Plugin {
+
+ static get requires() {
+ return [ClipboardPipeline] as const;
+ }
+
+ static get pluginName() {
+ return "ClipboardPageStyles" as const;
+ }
+
+ init() {
+ const view = this.editor.editing.view;
+
+ // `high`, so that the converters behind the normal-priority insertion see the cleaned
+ // styles.
+ this.listenTo<ClipboardInputTransformationEvent>(
+ this.editor.plugins.get(ClipboardPipeline),
+ "inputTransformation",
+ (_evt, data) => stripPageStyles(view, data.content),
+ { priority: "high" }
+ );
+ }
+}
+
+/**
+ * Strips the page styles from every element of `fragment` if any of its elements is a computed-style
+ * dump. Inner elements of a dump carry only the styles that the page's stylesheet matched (an inline
+ * code span's background, for example), so they are cleaned as well, and spans left with no
+ * attributes are unwrapped. Returns whether the fragment was a dump.
+ */
+export function stripPageStyles(view: EditingView, fragment: ViewDocumentFragment): boolean {
+ const elements: ViewElement[] = [];
+ for (const { item } of view.createRangeIn(fragment)) {
+ if (item.is("element")) {
+ elements.push(item);
+ }
+ }
+
+ if (!elements.some(isComputedStyleDump)) {
+ return false;
+ }
+
+ const writer = new UpcastWriter(view.document);
+ for (const element of elements) {
+ if (element.hasAttribute("style")) {
+ const kept = keptStyles(element);
+ writer.removeAttribute("style", element);
+ if (Object.keys(kept).length) {
+ writer.setStyle(kept, element);
+ }
+ }
+
+ // A span with no attributes carries nothing, but General HTML Support would keep it.
+ if (element.is("element", "span") && !hasAttributes(element)) {
+ writer.unwrapElement(element);
+ }
+ }
+
+ return true;
+}
+
+/** Whether the element's inline style is a browser's computed-style dump. */
+export function isComputedStyleDump(element: ViewElement): boolean {
+ let matches = 0;
+ for (const property of DUMP_FINGERPRINT) {
+ if (element.hasStyle(property)) {
+ matches++;
+ }
+ }
+
+ return matches >= FINGERPRINT_THRESHOLD;
+}
+
+/** The element's styles from {@link KEPT_STYLES} whose values are not the default. */
+function keptStyles(element: ViewElement): Record<string, string> {
+ const kept: Record<string, string> = {};
+ for (const [property, isMeaningful] of Object.entries(KEPT_STYLES)) {
+ const value = element.getStyle(property)?.trim().toLowerCase();
+ if (value && isMeaningful(value)) {
+ kept[property] = value;
+ }
+ }
+
+ return kept;
+}
+
+function hasAttributes(element: ViewElement) {
+ return !element.getAttributeKeys().next().done;
+}
+
+function hasTextDecoration(value: string) {
+ return value.includes("underline") || value.includes("line-through");
+}