Skip to content

Build your personal knowledge base with Trilium Notes

client/snippets: exclude the current snippet from being listed in the slash commands when editing it

Adorian Doran2797c4d3b156
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index 62ebb3d5f0..5cfaebbb85 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -135,7 +135,8 @@ export function EditableCode({ note, ntxId, noteContext, debounceUpdate, parentC
editorView,
(candidate) => candidate.type === "code" && candidate.mime === note.mime,
note.mime,
- !note.isMarkdown()
+ !note.isMarkdown(),
+ note.noteId
);
return (
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index 9c19582c88..772b4fd11a 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -680,7 +680,7 @@ function useSlashCommands(parentComponent: TypeWidgetProps["parentComponent"], e
}
};
}),
- ...buildSnippetCompletions(snippetsRef.current)
+ ...buildSnippetCompletions(snippetsRef.current.filter((snippet) => snippet.noteId !== noteRef.current.noteId))
]
};
}],
diff --git a/apps/client/src/widgets/type_widgets/code/snippets.ts b/apps/client/src/widgets/type_widgets/code/snippets.ts
index 524213e21a..c7415fd22a 100644
--- a/apps/client/src/widgets/type_widgets/code/snippets.ts
+++ b/apps/client/src/widgets/type_widgets/code/snippets.ts
@@ -120,8 +120,12 @@ export function useCodeSnippets(matches: (note: FNote) => boolean, reloadKey: st
* content at the cursor. No-op when `enabled` is false (e.g. the Markdown editor, which builds its
* own combined slash-command menu instead).
*/
-export function useSnippetSlashCommands(editorView: VanillaCodeMirror | null, matches: (note: FNote) => boolean, reloadKey: string, enabled: boolean) {
+export function useSnippetSlashCommands(editorView: VanillaCodeMirror | null, matches: (note: FNote) => boolean, reloadKey: string, enabled: boolean, currentNoteId: string) {
const snippetsRef = useCodeSnippets(matches, reloadKey, enabled);
+ // The note being edited, kept in a ref so the once-registered completion source always excludes
+ // the current snippet from its own menu — even when the editor is reused for a different note.
+ const currentNoteIdRef = useRef(currentNoteId);
+ useEffect(() => { currentNoteIdRef.current = currentNoteId; });
useEffect(() => {
if (!editorView || !enabled) return;
@@ -130,9 +134,11 @@ export function useSnippetSlashCommands(editorView: VanillaCodeMirror | null, ma
override: [(context: CompletionContext) => {
const match = context.matchBefore(SLASH_COMMAND_REGEX);
if (!match) return null;
- const options = buildSnippetCompletions(snippetsRef.current);
- // No matching snippets (e.g. none for this MIME, or disabled) → no source, so an
- // empty completion popup never appears when the user types "/".
+ const options = buildSnippetCompletions(
+ snippetsRef.current.filter((snippet) => snippet.noteId !== currentNoteIdRef.current)
+ );
+ // No matching snippets (e.g. none for this MIME, or only the current note) → no source,
+ // so an empty completion popup never appears when the user types "/".
return options.length ? { from: match.from, options } : null;
}],
activateOnTyping: true