Skip to content

Build your personal knowledge base with Trilium Notes

fix(ckeditor): keep spellcheck off inline code out of the note's content

Elian Doran3a11204634e4
diff --git a/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.spec.ts b/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.spec.ts
index 791abf1a88..0bca3bffb3 100644
--- a/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.spec.ts
+++ b/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.spec.ts
@@ -1,4 +1,4 @@
-import { _setModelData as setModelData, _getViewData as getViewData, ClassicEditor, Code, Essentials, Paragraph } from "ckeditor5";
+import { _getViewData as getViewData, _setModelData as setModelData, ClassicEditor, Code, Essentials, Paragraph } from "ckeditor5";
import { beforeEach, describe, expect, it } from "vitest";
import { createTestEditor } from "../../test/editor-kit.js";
@@ -15,39 +15,37 @@ describe("InlineCodeNoSpellcheck", () => {
expect(editor.plugins.get(InlineCodeNoSpellcheck)).toBeInstanceOf(InlineCodeNoSpellcheck);
});
- it("renders inline code with spellcheck=false in the editing view", () => {
- setModelData(editor.model, "<paragraph>foo<$text code=\"true\">bar</$text>baz</paragraph>");
- const viewData = getViewData(editor.editing.view);
- expect(viewData).toContain("spellcheck=\"false\"");
- expect(viewData).toContain("<code");
+ it("disables spellcheck on inline code in the editing view but not in the data", () => {
+ setModelData(editor.model, `<paragraph>foo<$text code="true">bar</$text>baz</paragraph>`);
+
+ expect(getViewData(editor.editing.view, { withoutSelection: true }))
+ .toBe(`<p>foo<code spellcheck="false">bar</code>baz</p>`);
+ expect(editor.getData()).toBe("<p>foo<code>bar</code>baz</p>");
});
- it("renders inline code with spellcheck=false in the output data", () => {
- setModelData(editor.model, "<paragraph>foo<$text code=\"true\">bar</$text>baz</paragraph>");
- const data = editor.getData();
- expect(data).toContain("spellcheck=\"false\"");
- expect(data).toContain("<code");
+ it("marks every run of inline code, and nothing else", () => {
+ setModelData(editor.model,
+ `<paragraph><$text code="true">start</$text> middle <$text code="true">end</$text></paragraph>`);
+
+ expect(getViewData(editor.editing.view, { withoutSelection: true }))
+ .toBe(`<p><code spellcheck="false">start</code> middle <code spellcheck="false">end</code></p>`);
+ expect(editor.getData()).toBe("<p><code>start</code> middle <code>end</code></p>");
});
- it("does not add spellcheck=false to non-code text", () => {
+ it("does not add spellcheck to plain text", () => {
setModelData(editor.model, "<paragraph>plain[]text</paragraph>");
- const viewData = getViewData(editor.editing.view);
- expect(viewData).not.toContain("spellcheck");
+
+ expect(getViewData(editor.editing.view)).not.toContain("spellcheck");
});
- it("round-trips spellcheck=false through setData/getData", () => {
+ // Notes saved before the attribute moved to the editing view, and Markdown that keeps a table
+ // as raw HTML, both hand the editor a <code> that may or may not carry spellcheck. Either way
+ // the data comes back plain, so re-saving a note stops rewriting its every inline code.
+ it("normalizes both spellings of <code> to a plain one in the data", () => {
editor.setData("<p>hello <code>world</code></p>");
- const data = editor.getData();
- expect(data).toContain("spellcheck=\"false\"");
- expect(data).toContain("<code");
- });
+ expect(editor.getData()).toBe("<p>hello <code>world</code></p>");
- it("applies spellcheck=false to code text adjacent to plain text", () => {
- setModelData(editor.model,
- "<paragraph><$text code=\"true\">start</$text> middle <$text code=\"true\">end</$text></paragraph>");
- const data = editor.getData();
- const matches = data.match(/spellcheck="false"/g);
- expect(matches).not.toBeNull();
- expect(matches?.length).toBeGreaterThanOrEqual(1);
+ editor.setData(`<p>hello <code spellcheck="false">world</code></p>`);
+ expect(editor.getData()).toBe("<p>hello <code>world</code></p>");
});
});
diff --git a/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.ts b/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.ts
index 49d81195a4..304ae7e507 100644
--- a/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.ts
+++ b/packages/ckeditor5/src/plugins/inline_code_no_spellcheck.ts
@@ -1,17 +1,25 @@
import { Plugin } from "ckeditor5";
+/**
+ * Stops the browser spellchecker underlining inline code, which is identifiers rather than prose.
+ *
+ * Only the editing view carries `spellcheck`, so the attribute never reaches the note's stored
+ * content. Emitting it into the data instead makes a note's HTML depend on whether the editor has
+ * opened it: `sanitizeHtml` passes a raw `<code>` through untouched, so the same note round-trips
+ * as `<code>` until an edit rewrites every one of them.
+ */
export default class InlineCodeNoSpellcheck extends Plugin {
init() {
- this.editor.conversion.for('downcast').attributeToElement({
- model: 'code',
+ this.editor.conversion.for("editingDowncast").attributeToElement({
+ model: "code",
view: (modelAttributeValue, conversionApi) => {
const { writer } = conversionApi;
- return writer.createAttributeElement('code', {
- spellcheck: 'false'
+ return writer.createAttributeElement("code", {
+ spellcheck: "false"
});
},
- converterPriority: 'high'
+ converterPriority: "high"
});
}
diff --git a/packages/commons/src/lib/markdown_renderer.spec.ts b/packages/commons/src/lib/markdown_renderer.spec.ts
index 4f806c9a67..a90cbe7a5b 100644
--- a/packages/commons/src/lib/markdown_renderer.spec.ts
+++ b/packages/commons/src/lib/markdown_renderer.spec.ts
@@ -209,9 +209,9 @@ describe("renderToHtml", () => {
});
describe("inline code (codespan)", () => {
- it("renders inline code with spellcheck disabled and escaped content", () => {
+ it("renders inline code with escaped content", () => {
const html = render("use `foo > bar` here");
- expect(html).toBe('<p>use <code spellcheck="false">foo &gt; bar</code> here</p>');
+ expect(html).toBe('<p>use <code>foo &gt; bar</code> here</p>');
});
});
@@ -565,7 +565,7 @@ describe("renderToHtml", () => {
it("does not convert a formula-like sequence inside inline code (codeMap path)", () => {
const html = render("use `$x$` and real $y$");
// The `$x$` inside the codespan stays literal; only the bare $y$ becomes a formula.
- expect(html).toContain('<code spellcheck="false">$x$</code>');
+ expect(html).toContain('<code>$x$</code>');
expect(html).toContain('<span class="math-tex">\\(y\\)</span>');
});
@@ -637,7 +637,7 @@ describe("renderToHtml", () => {
});
it("does not highlight == inside code", () => {
- expect(render("`==x==`")).toBe('<p><code spellcheck="false">==x==</code></p>');
+ expect(render("`==x==`")).toBe('<p><code>==x==</code></p>');
expect(render("```\na ==x== b\n```")).toContain("a ==x== b");
});
diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.ts
index 907012527b..1c61eba0c5 100644
--- a/packages/commons/src/lib/markdown_renderer.ts
+++ b/packages/commons/src/lib/markdown_renderer.ts
@@ -593,7 +593,7 @@ export class CustomMarkdownRenderer extends Renderer {
}
override codespan({ text }: Tokens.Codespan): string {
- return `<code spellcheck="false">${escapeHtml(text)}</code>`;
+ return `<code>${escapeHtml(text)}</code>`;
}
}
diff --git a/packages/trilium-core/src/services/import/markdown.spec.ts b/packages/trilium-core/src/services/import/markdown.spec.ts
index 798a07f558..993327c6fc 100644
--- a/packages/trilium-core/src/services/import/markdown.spec.ts
+++ b/packages/trilium-core/src/services/import/markdown.spec.ts
@@ -484,9 +484,9 @@ $$`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
- it("adds spellcheck=false to inline code", () => {
+ it("renders inline code as a plain <code> element", () => {
const input = `This is some inline code: \`const x = 10;\``;
- const expected = /*html*/`<p>This is some inline code: <code spellcheck="false">const x = 10;</code></p>`;
+ const expected = /*html*/`<p>This is some inline code: <code>const x = 10;</code></p>`;
expect(markdownService.renderToHtml(input, "Title")).toStrictEqual(expected);
});
diff --git a/packages/trilium-core/src/services/sanitizer.ts b/packages/trilium-core/src/services/sanitizer.ts
index 5ba4adc0ab..2692880cb2 100644
--- a/packages/trilium-core/src/services/sanitizer.ts
+++ b/packages/trilium-core/src/services/sanitizer.ts
@@ -52,8 +52,7 @@ export function sanitizeHtml(dirtyHtml: string) {
input: ["type", "checked"],
img: ["width", "height"],
td: ["colspan", "rowspan"],
- th: ["colspan", "rowspan", "scope"],
- code: [ "spellcheck" ]
+ th: ["colspan", "rowspan", "scope"]
},
allowedStyles: {
"*": {