Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(editor): skip automatic language detection when switching notes (#702)

tianma-if74abc86bd2f8
diff --git a/apps/web/src/lib/code-block-highlighting.test.mjs b/apps/web/src/lib/code-block-highlighting.test.mjs
new file mode 100644
index 00000000..30e1808c
--- /dev/null
+++ b/apps/web/src/lib/code-block-highlighting.test.mjs
@@ -0,0 +1,55 @@
+import { describe, expect, test } from "bun:test";
+import { createCodeBlockLowlight } from "./code-block-highlighting";
+import { Editor } from "@tiptap/core";
+import StarterKit from "@tiptap/starter-kit";
+import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
+import { EditorState } from "@tiptap/pm/state";
+import { FRONT_MATTER_LANGUAGE } from "@edgeever/shared";
+
+const textOf = (node) => node.type === "text" ? node.value : (node.children ?? []).map(textOf).join("");
+const elementsOf = (node) => (node.type === "element" ? 1 : 0) + (node.children ?? []).reduce((n, child) => n + elementsOf(child), 0);
+
+describe("editor code block highlighting", () => {
+ test("unknown and render-only languages keep exact text without guessed syntax", () => {
+ const lowlight = createCodeBlockLowlight();
+ // JavaScript-looking content would receive decorations under auto detection.
+ const source = 'const value = { name: "edgeever", count: 123 };\n'.repeat(200);
+ for (const language of ["unknown-702", "mermaid", FRONT_MATTER_LANGUAGE]) {
+ expect(lowlight.registered(language)).toBe(false);
+ const tree = lowlight.highlightAuto(source);
+ expect(textOf(tree)).toBe(source);
+ expect(elementsOf(tree)).toBe(0);
+ }
+ });
+
+ test("explicit languages and aliases retain syntax highlighting", () => {
+ const lowlight = createCodeBlockLowlight();
+ for (const language of ["javascript", "js"]) {
+ const source = 'const value = "hello";';
+ const tree = lowlight.highlight(language, source);
+ expect(textOf(tree)).toBe(source);
+ expect(elementsOf(tree)).toBeGreaterThan(0);
+ }
+ expect(lowlight.listLanguages()).toContain("yaml");
+ expect(textOf(lowlight.highlightAuto(""))).toBe("");
+ });
+});
+
+// Exercise the plugin initialization used by resetEditorDocument, rather than
+// just the adapter: unsupported language attributes must survive loading.
+test("document switching initializes decorations without guessing or changing content", () => {
+ const editor = new Editor({ content: { type: "doc", content: [{ type: "paragraph" }] }, extensions: [
+ StarterKit.configure({ codeBlock: false }),
+ CodeBlockLowlight.configure({ lowlight: createCodeBlockLowlight(), defaultLanguage: "plaintext" }),
+ ] });
+ try {
+ for (const language of ["mermaid", FRONT_MATTER_LANGUAGE, "unknown-702", "javascript"]) {
+ const json = { type: "doc", content: [{ type: "codeBlock", attrs: { language }, content: [{ type: "text", text: 'const value = "hello";' }] }] };
+ const state = EditorState.create({ schema: editor.schema, doc: editor.schema.nodeFromJSON(json), plugins: editor.extensionManager.plugins });
+ expect(state.doc.toJSON()).toEqual(json);
+ const decorations = state.plugins.flatMap(plugin => plugin.props.decorations?.(state)?.find() ?? []);
+ if (language === "javascript") expect(decorations.length).toBeGreaterThan(0);
+ else expect(decorations).toEqual([]);
+ }
+ } finally { editor.destroy(); }
+});
diff --git a/apps/web/src/lib/code-block-highlighting.ts b/apps/web/src/lib/code-block-highlighting.ts
new file mode 100644
index 00000000..14724e14
--- /dev/null
+++ b/apps/web/src/lib/code-block-highlighting.ts
@@ -0,0 +1,14 @@
+import { common, createLowlight } from "lowlight";
+
+export const createCodeBlockLowlight = () => {
+ const lowlight = createLowlight(common);
+
+ return {
+ ...lowlight,
+ // TipTap calls highlightAuto for unregistered languages, including Mermaid
+ // and our front-matter node. Guessing across every grammar blocks note
+ // switching on the main thread. Keep their text and language untouched;
+ // only omit syntax decorations when the language is not supported.
+ highlightAuto: (value: string) => lowlight.highlight("plaintext", value),
+ };
+};
diff --git a/apps/web/src/lib/code-block.ts b/apps/web/src/lib/code-block.ts
index de295e77..403fddfc 100644
--- a/apps/web/src/lib/code-block.ts
+++ b/apps/web/src/lib/code-block.ts
@@ -1,4 +1,4 @@
-import { common, createLowlight } from "lowlight";
+import { createCodeBlockLowlight } from "./code-block-highlighting";
import CodeBlockLowlight from "@tiptap/extension-code-block-lowlight";
import type { Editor } from "@tiptap/core";
import { ReactNodeViewRenderer } from "@tiptap/react";
@@ -7,7 +7,7 @@ import { NoteProperties } from "@/components/editor/NoteProperties";
import { FRONT_MATTER_LANGUAGE } from "@edgeever/shared";
import { createElement } from "react";
-export const codeBlockLowlight = createLowlight(common);
+export const codeBlockLowlight = createCodeBlockLowlight();
export const selectCurrentCodeBlockContent = (editor: Editor) => {
const { $from, $to, from, to } = editor.state.selection;