Skip to content

Build your personal knowledge base with Trilium Notes

feat(markdown): avoid slash commands within code blocks

Elian Doran48ea2db8e08d
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index 65c0892113..ab8c53e1b4 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -468,13 +468,20 @@ function useSlashCommands(parentComponent: TypeWidgetProps["parentComponent"], e
Promise.all([
import("@codemirror/autocomplete"),
- import("@codemirror/state")
- ]).then(([{ autocompletion }, { StateEffect }]) => {
+ import("@codemirror/state"),
+ import("@codemirror/language")
+ ]).then(([{ autocompletion }, { StateEffect }, { syntaxTree }]) => {
const ext = autocompletion({
override: [(ctx) => {
const match = ctx.matchBefore(/(?:^|(?<=\s))\/\w*/);
if (!match) return null;
+ // Suppress slash menu inside fenced/indented code blocks and inline code spans —
+ // a leading `/` there is part of the code, not a command trigger.
+ for (let node: import("@lezer/common").SyntaxNode | null = syntaxTree(ctx.state).resolveInner(ctx.pos, -1); node; node = node.parent) {
+ if (/Code(Block|Text)|FencedCode|InlineCode/.test(node.name)) return null;
+ }
+
return {
from: match.from,
options: [