Skip to content

Build your personal knowledge base with Trilium Notes

fix(markdown): error due to slash commands registration

Elian Doran15173559dded
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index f5998c8cf3..b98c4ceef9 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -448,10 +448,7 @@ function useSlashCommands(parentComponent: TypeWidgetProps["parentComponent"], e
useEffect(() => {
if (!editorView) return;
- Promise.all([
- import("@codemirror/autocomplete"),
- import("@codemirror/state")
- ]).then(([{ autocompletion }, { StateEffect }]) => {
+ import("@codemirror/autocomplete").then(({ autocompletion }) => {
const ext = autocompletion({
override: [(ctx) => {
const match = ctx.matchBefore(/(?:^|(?<=\s))\/\w*/);
@@ -578,7 +575,7 @@ function useSlashCommands(parentComponent: TypeWidgetProps["parentComponent"], e
activateOnTyping: true
});
- editorView.dispatch({ effects: StateEffect.appendConfig.of(ext) });
+ editorView.setNamedExtension("slashCommands", ext);
});
}, [editorView, parentComponent, note.noteId]);
}
diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.ts
index de4f9f7a58..3fbb6cf4f9 100644
--- a/packages/codemirror/src/index.ts
+++ b/packages/codemirror/src/index.ts
@@ -1,7 +1,7 @@
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";
import { EditorView, highlightActiveLine, keymap, lineNumbers, placeholder, ViewPlugin, ViewUpdate, type EditorViewConfig, KeyBinding } from "@codemirror/view";
import { defaultHighlightStyle, StreamLanguage, syntaxHighlighting, indentUnit, bracketMatching, foldGutter, codeFolding } from "@codemirror/language";
-import { Compartment, EditorSelection, EditorState, type Extension } from "@codemirror/state";
+import { Compartment, EditorSelection, EditorState, StateEffect, type Extension } from "@codemirror/state";
import { highlightSelectionMatches } from "@codemirror/search";
import { vim } from "@replit/codemirror-vim";
import { indentationMarkers } from "@replit/codemirror-indentation-markers";
@@ -55,6 +55,7 @@ export default class CodeMirror extends EditorView {
private indentUnitCompartment: Compartment;
private searchHighlightCompartment: Compartment;
private searchPlugin?: SearchHighlighter | null;
+ private namedCompartments = new Map<string, Compartment>();
constructor(config: EditorConfig) {
const languageCompartment = new Compartment();
@@ -278,6 +279,22 @@ export default class CodeMirror extends EditorView {
}
}
+ /**
+ * Adds or reconfigures a named extension. If the extension has already been
+ * added under this name, it is reconfigured in place (no duplicate config error).
+ * This is safe to call repeatedly (e.g. from React effects or during hot-reload).
+ */
+ setNamedExtension(name: string, ext: Extension) {
+ let compartment = this.namedCompartments.get(name);
+ if (compartment) {
+ this.dispatch({ effects: compartment.reconfigure(ext) });
+ } else {
+ compartment = new Compartment();
+ this.namedCompartments.set(name, compartment);
+ this.dispatch({ effects: StateEffect.appendConfig.of(compartment.of(ext)) });
+ }
+ }
+
async setMimeType(mime: string) {
let newExtension: Extension[] = [];