Skip to content

Build your personal knowledge base with Trilium Notes

fix(ckeditor): keep a plain <kbd> instead of dropping it on the next save

Elian Doran6f9676dd31e7
diff --git a/packages/ckeditor5/src/plugins/keyboard_marker/README.md b/packages/ckeditor5/src/plugins/keyboard_marker/README.md
index 1c9265d9ac..a3129ae7b0 100644
--- a/packages/ckeditor5/src/plugins/keyboard_marker/README.md
+++ b/packages/ckeditor5/src/plugins/keyboard_marker/README.md
@@ -9,10 +9,16 @@ Derived from [mlewand/ckeditor5-keyboard-marker](https://github.com/mlewand/cked
by Marek Lewandowski, licensed **GPL-3.0** — see [`LICENSE`](./LICENSE) next to this file.
The source was ported to TypeScript and to current CKEditor 5 APIs, and has since diverged from
-upstream (two-step caret movement, `spellcheck="false"` on the downcast element, and
+upstream (two-step caret movement, `spellcheck="false"` on the editing-view element only, and
`copyOnEnter: false` so the formatting does not carry onto the next paragraph). Upstream targets a
long-obsolete CKEditor 5 version and is not tracked; treat this as Trilium-maintained code.
+The conversion is split rather than two-way: the upcast matches any `<kbd>`, the data downcast emits
+a plain one, and only the editing downcast carries `spellcheck="false"`. A two-way converter keyed on
+that attribute left a plain `<kbd>` — what `docs/` and every Markdown import hold, since
+`sanitizeHtml` allows `spellcheck` on `code` but not on `kbd` — to General HTML Support, which drops
+it under the shipped empty allow-list.
+
It lived at `packages/ckeditor5-keyboard-marker` until it was folded into this package — it had no
consumers outside `@triliumnext/ckeditor5`, was never published, and carried a full
`ckeditor5-package-generator` scaffold (sample pages, its own ESLint/Stylelint/vitest configs) that
diff --git a/packages/ckeditor5/src/plugins/keyboard_marker/keyboard_marker.spec.ts b/packages/ckeditor5/src/plugins/keyboard_marker/keyboard_marker.spec.ts
index 3c19d1f6b8..7dcaa90c42 100644
--- a/packages/ckeditor5/src/plugins/keyboard_marker/keyboard_marker.spec.ts
+++ b/packages/ckeditor5/src/plugins/keyboard_marker/keyboard_marker.spec.ts
@@ -1,4 +1,4 @@
-import { _setModelData as setModelData, ButtonView, ClassicEditor, Paragraph } from "ckeditor5";
+import { _getViewData as getViewData, _setModelData as setModelData, ButtonView, ClassicEditor, GeneralHtmlSupport, Paragraph } from "ckeditor5";
import { beforeEach, describe, expect, it } from "vitest";
import { createTestEditor } from "../../../test/editor-kit.js";
@@ -31,18 +31,23 @@ describe("Kbd", () => {
});
});
- it("downcasts the kbd attribute to a <kbd> element with spellcheck disabled", () => {
+ it("downcasts the kbd attribute to a plain <kbd> in the data and disables spellcheck only while editing", () => {
setModelData(editor.model, "<paragraph>[Ctrl]</paragraph>");
editor.execute("kbd");
- expect(editor.getData()).toBe(`<p><kbd spellcheck="false">Ctrl</kbd></p>`);
+ expect(editor.getData()).toBe("<p><kbd>Ctrl</kbd></p>");
+ expect(getViewData(editor.editing.view, { withoutSelection: true }))
+ .toBe(`<p><kbd spellcheck="false">Ctrl</kbd></p>`);
});
- it("upcasts a <kbd> element back to the kbd attribute", () => {
- editor.setData(`<p>Press <kbd spellcheck="false">Ctrl</kbd>.</p>`);
+ it("upcasts a <kbd> element back to the kbd attribute, with or without spellcheck", () => {
+ editor.setData("<p>Press <kbd>Ctrl</kbd>.</p>");
+ expect(editor.getData()).toBe("<p>Press <kbd>Ctrl</kbd>.</p>");
- expect(editor.getData()).toBe(`<p>Press <kbd spellcheck="false">Ctrl</kbd>.</p>`);
+ // Notes saved before the attribute moved to the editing view normalize to a plain <kbd>.
+ editor.setData(`<p>Press <kbd spellcheck="false">Ctrl</kbd>.</p>`);
+ expect(editor.getData()).toBe("<p>Press <kbd>Ctrl</kbd>.</p>");
});
it("registers the toolbar button bound to the command", () => {
@@ -57,3 +62,18 @@ describe("Kbd", () => {
expect(command?.value).toBe(true);
});
});
+
+describe("Kbd under the shipped General HTML Support configuration", () => {
+ // `textNoteHtmlSupportEnabled` ships off, which leaves GHS with an empty allow-list. A plain
+ // <kbd> has to be claimed by the plugin itself, or nothing models it and the element is lost on
+ // the note's next save — the Markdown docs under `docs/` write <kbd> without any attribute.
+ it("keeps a plain <kbd> the docs store", async () => {
+ const editor = await createTestEditor([Paragraph, Kbd, GeneralHtmlSupport], {
+ htmlSupport: { allow: [] }
+ });
+
+ editor.setData("<p>Press <kbd>Ctrl</kbd> + <kbd>R</kbd>.</p>");
+
+ expect(editor.getData()).toBe("<p>Press <kbd>Ctrl</kbd> + <kbd>R</kbd>.</p>");
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/keyboard_marker/keyboard_marker_editing.ts b/packages/ckeditor5/src/plugins/keyboard_marker/keyboard_marker_editing.ts
index 8d6cb7dc2c..d3e8e02767 100644
--- a/packages/ckeditor5/src/plugins/keyboard_marker/keyboard_marker_editing.ts
+++ b/packages/ckeditor5/src/plugins/keyboard_marker/keyboard_marker_editing.ts
@@ -34,7 +34,23 @@ export default class KbdEditing extends Plugin {
// Enable two-step caret movement so the user can arrow out of the kbd element.
editor.plugins.get(TwoStepCaretMovement).registerAttribute(KBD);
- editor.conversion.attributeToElement({
+ // Match every <kbd>, whatever attributes it carries. A pattern that demanded
+ // spellcheck="false" left a plain <kbd> to General HTML Support, which drops it when the
+ // allow-list is empty — the shipped default.
+ editor.conversion.for("upcast").elementToAttribute({
+ model: KBD,
+ view: KBD
+ });
+
+ editor.conversion.for("dataDowncast").attributeToElement({
+ model: KBD,
+ view: KBD
+ });
+
+ // The browser spellchecker underlines key names, so switch it off where the user types.
+ // Keeping spellcheck out of the data means the attribute never reaches stored content,
+ // where `sanitizeHtml` strips it off <kbd> on every import.
+ editor.conversion.for("editingDowncast").attributeToElement({
model: KBD,
view: {
name: KBD,