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
refactor(ckeditor5): replace the code-block patch with a downcast converter
Elian Doran52d6f8ba2454
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.tsindex 8d1f9db070..3eb2b0bd50 100644--- a/packages/ckeditor5/src/plugins.ts+++ b/packages/ckeditor5/src/plugins.ts@@ -34,6 +34,7 @@ import ImageActions from "./plugins/image_actions.js";import CodeBlockToolbar from "./plugins/code_block_toolbar.js";import CodeBlockLanguageDropdown from "./plugins/code_block_language_dropdown.js";import CodeBlockInsertParagraph from "./plugins/code_block_insert_paragraph.js";+import CodeBlockHljsClass from "./plugins/code_block_hljs_class.js";import MoveBlockUpDownPlugin from "./plugins/move_block_updown.js";import ScrollOnUndoRedoPlugin from "./plugins/scroll_on_undo_redo.js"import InlineCodeNoSpellcheck from "./plugins/inline_code_no_spellcheck.js";@@ -68,6 +69,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [CodeBlockLanguageDropdown,CodeBlockToolbar,CodeBlockInsertParagraph,+ CodeBlockHljsClass,MoveBlockUpDownPlugin,ScrollOnUndoRedoPlugin,InlineCodeNoSpellcheck,diff --git a/packages/ckeditor5/src/plugins/code_block_hljs_class.spec.ts b/packages/ckeditor5/src/plugins/code_block_hljs_class.spec.tsnew file mode 100644index 0000000000..a8be48cb0a--- /dev/null+++ b/packages/ckeditor5/src/plugins/code_block_hljs_class.spec.ts@@ -0,0 +1,59 @@+import { ClassicEditor, CodeBlock, Essentials, Paragraph, _setModelData as setModelData } from "ckeditor5";+import { beforeEach, describe, expect, it } from "vitest";++import { createTestEditor } from "../../test/editor-kit.js";+import CodeBlockHljsClass from "./code_block_hljs_class.js";++describe("CodeBlockHljsClass", () => {+ let editor: ClassicEditor;++ beforeEach(async () => {+ editor = await createTestEditor([Essentials, Paragraph, CodeBlock, CodeBlockHljsClass]);+ });++ /** Render a model fragment to the editing DOM and return the contenteditable root. */+ function render(content: string): Element {+ setModelData(editor.model, content);+ editor.editing.view.forceRender();+ const root = editor.editing.view.getDomRoot();+ if (!root) {+ throw new Error("Editing view has no DOM root.");+ }+ return root;+ }++ it("loads the plugin and exposes its name", () => {+ expect(editor.plugins.get(CodeBlockHljsClass)).toBeInstanceOf(CodeBlockHljsClass);+ expect(CodeBlockHljsClass.pluginName).toBe("CodeBlockHljsClass");+ });++ it("tags the editing-view <pre> so highlight.js themes can target it", () => {+ const root = render("<codeBlock language=\"plaintext\">foo[]</codeBlock>");+ const pre = root.querySelector("pre");++ // The theme rewrite in packages/highlightjs targets `.ck-content pre.hljs`, so the class+ // has to land on the <pre>, not on the inner <code>.+ expect(pre?.classList.contains("hljs")).toBe(true);+ expect(pre?.querySelector("code")?.classList.contains("hljs")).toBe(false);+ });++ it("keeps the class out of the stored data", () => {+ setModelData(editor.model, "<codeBlock language=\"plaintext\">foo[]</codeBlock>");++ // Only the editing downcast is tagged — note content must stay free of the editor-only class.+ expect(editor.getData()).not.toContain("hljs");+ });++ it("tags every code block, including after a language change", () => {+ const root = render("<codeBlock language=\"plaintext\">one</codeBlock><codeBlock language=\"plaintext\">two[]</codeBlock>");+ expect([...root.querySelectorAll("pre")].every((pre) => pre.classList.contains("hljs"))).toBe(true);++ // forceValue keeps it a code block — a plain execute would toggle it back to a paragraph.+ editor.execute("codeBlock", { language: "css", forceValue: true });+ editor.editing.view.forceRender();++ const changed = root.querySelectorAll("pre");+ expect(changed.length).toBe(2);+ expect([...changed].every((pre) => pre.classList.contains("hljs"))).toBe(true);+ });+});diff --git a/packages/ckeditor5/src/plugins/code_block_hljs_class.ts b/packages/ckeditor5/src/plugins/code_block_hljs_class.tsnew file mode 100644index 0000000000..d8ad51fc56--- /dev/null+++ b/packages/ckeditor5/src/plugins/code_block_hljs_class.ts@@ -0,0 +1,42 @@+import { type DowncastConversionApi, type DowncastInsertEvent, Plugin } from "ckeditor5";++/**+ * Tags the editing-view `<pre>` of every code block with `hljs`.+ *+ * The highlight.js themes Trilium injects are rewritten by `normalizeThemeCss` (see+ * `packages/highlightjs`) to target `.hljs, .ck-content pre.hljs`, whose specificity outranks+ * CKEditor's own `.ck-content pre` colours. Without this class the editor would keep CKEditor's+ * grey code block instead of the theme the user picked.+ *+ * Only the editing view is tagged — the data downcast is untouched, so the class never reaches+ * stored note content. This replaces a patch of `@ckeditor/ckeditor5-code-block` that set the+ * same attribute inline, and which broke on every release that rebuilt the bundled dist.+ */+export default class CodeBlockHljsClass extends Plugin {++ static get pluginName() {+ return "CodeBlockHljsClass" as const;+ }++ init() {+ this.editor.editing.downcastDispatcher.on<DowncastInsertEvent>("insert:codeBlock", (evt, data, conversionApi: DowncastConversionApi) => {+ /* v8 ignore next 3 -- the "insert:codeBlock" event only fires for code blocks; the check narrows the model item's type */+ if (!data.item.is("element", "codeBlock")) {+ return;+ }++ // CodeBlockEditing binds the model element to the inner <code>, so the <pre> we want+ // is its parent. "low" priority runs us once that element exists.+ const viewCode = conversionApi.mapper.toViewElement(data.item);+ const viewPre = viewCode?.parent;++ /* v8 ignore next 3 -- defensive: the code block is always converted to <pre><code> before this runs */+ if (!viewPre?.is("element", "pre")) {+ return;+ }++ conversionApi.writer.addClass("hljs", viewPre);+ }, { priority: "low" });+ }++}diff --git a/patches/@ckeditor__ckeditor5-code-block.patch b/patches/@ckeditor__ckeditor5-code-block.patchdeleted file mode 100644index 4a2007e0bb..0000000000--- a/patches/@ckeditor__ckeditor5-code-block.patch+++ /dev/null@@ -1,12 +0,0 @@-diff --git a/dist/index.js b/dist/index.js-index 055bdaeb3665fd469b59e09ec1111fc4da20be6c..b405e7c2b76c33e6a41d221389ffff3c2d791300 100644---- a/dist/index.js-+++ b/dist/index.js-@@ -540,6 +540,7 @@ function modelToViewCodeBlockInsertion(model, languageDefs, useLabels = false) {- const preAttributes = {};- if (useLabels) {- preAttributes["data-language"] = languagesToLabels[codeBlockLanguage];-+ preAttributes.class = "hljs";- preAttributes.spellcheck = "false";- }- const codeAttributes = languagesToClasses[codeBlockLanguage] ? { class: languagesToClasses[codeBlockLanguage] } : void 0;diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex c5a1fb9096..f452b4f170 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -82,7 +82,6 @@ overrides:extract-zip>yauzl: 3.4.0patchedDependencies:- '@ckeditor/ckeditor5-code-block': 34e1e0b5d23dc9072aba2893a309957255ac1afb90eb07bff913fa3030def91a'@ckeditor/ckeditor5-mention': 91c41ee286960cf306640d976142aa98e286415096d5731828d796ac574fdbe8'@excalidraw/[email protected]': 859531a8ff622291b895d36bcd33e07ced6fcb3bcc90631ccaf776789b03f0c2'@maplibre/[email protected]': c30b921f19820021e9c5c23d64378cbdf3dfba379cd4b5daff8a3f1f8b3578d5@@ -14291,7 +14290,7 @@ snapshots:'@ckeditor/ckeditor5-upload': 48.3.1'@ckeditor/ckeditor5-utils': 48.3.1- '@ckeditor/[email protected](patch_hash=34e1e0b5d23dc9072aba2893a309957255ac1afb90eb07bff913fa3030def91a)':+ '@ckeditor/[email protected]':dependencies:'@ckeditor/ckeditor5-clipboard': 48.3.1'@ckeditor/ckeditor5-core': 48.3.1@@ -14921,7 +14920,7 @@ snapshots:'@ckeditor/[email protected]':dependencies:'@ckeditor/ckeditor5-clipboard': 48.3.1- '@ckeditor/ckeditor5-code-block': 48.3.1(patch_hash=34e1e0b5d23dc9072aba2893a309957255ac1afb90eb07bff913fa3030def91a)+ '@ckeditor/ckeditor5-code-block': 48.3.1'@ckeditor/ckeditor5-collaboration-core': 48.3.1'@ckeditor/ckeditor5-comments': 48.3.1'@ckeditor/ckeditor5-core': 48.3.1@@ -21546,7 +21545,7 @@ snapshots:'@ckeditor/ckeditor5-ckfinder': 48.3.1'@ckeditor/ckeditor5-clipboard': 48.3.1'@ckeditor/ckeditor5-cloud-services': 48.3.1- '@ckeditor/ckeditor5-code-block': 48.3.1(patch_hash=34e1e0b5d23dc9072aba2893a309957255ac1afb90eb07bff913fa3030def91a)+ '@ckeditor/ckeditor5-code-block': 48.3.1'@ckeditor/ckeditor5-core': 48.3.1'@ckeditor/ckeditor5-easy-image': 48.3.1'@ckeditor/ckeditor5-editor-balloon': 48.3.1diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yamlindex f6e6040e57..58b00e128e 100644--- a/pnpm-workspace.yaml+++ b/pnpm-workspace.yaml@@ -111,7 +111,6 @@ ignoredOptionalDependencies:patchedDependencies:"@ckeditor/ckeditor5-mention": patches/@ckeditor__ckeditor5-mention.patch- "@ckeditor/ckeditor5-code-block": patches/@ckeditor__ckeditor5-code-block.patch'@maplibre/[email protected]': patches/@[email protected][email protected]: patches/[email protected]'@excalidraw/[email protected]': patches/@[email protected]