Skip to content

Build your personal knowledge base with Trilium Notes

client/snippets: add support for generic code snippets

Adorian Doran1366d00cb93a
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index e904737d95..e1cf7e1eb1 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2549,6 +2549,7 @@
"snippet_badge": {
"label": "{{type}} snippet",
"type_text": "Text",
+ "type_code": "Code",
"tooltip": "A reusable piece of text or code, meant to be inserted into the editor (from the snippet menu in the toolbar, or via a slash command)."
},
"active_content_badges": {
diff --git a/apps/client/src/widgets/layout/SnippetBadge.tsx b/apps/client/src/widgets/layout/SnippetBadge.tsx
index 2133393a19..9e51c5ea63 100644
--- a/apps/client/src/widgets/layout/SnippetBadge.tsx
+++ b/apps/client/src/widgets/layout/SnippetBadge.tsx
@@ -49,11 +49,15 @@ function useSnippetInfo(note: FNote | null | undefined) {
return;
}
+ // A generic (plain-text) or unrecognized code snippet reads as "Code" — clearer than "Plain
+ // text" (which collides with "Text") and matching the "Code snippet" template. A recognized
+ // language keeps its own name and icon (e.g. "CSS snippet").
const mimeType = mimeTypesService.getMimeTypes().find((mt) => mt.mime === note.mime);
- setInfo({
- typeName: mimeType?.title ?? note.mime,
- icon: mimeType?.icon ?? "bx bx-code"
- });
+ if (!mimeType || note.mime === "text/plain") {
+ setInfo({ typeName: t("snippet_badge.type_code"), icon: "bx bx-code" });
+ return;
+ }
+ setInfo({ typeName: mimeType.title, icon: mimeType.icon ?? "bx bx-code" });
}
useEffect(refresh, [ note, noteType, noteMime ]);
diff --git a/apps/client/src/widgets/type_widgets/code/Code.tsx b/apps/client/src/widgets/type_widgets/code/Code.tsx
index 5cfaebbb85..74427f81c9 100644
--- a/apps/client/src/widgets/type_widgets/code/Code.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Code.tsx
@@ -133,7 +133,7 @@ export function EditableCode({ note, ntxId, noteContext, debounceUpdate, parentC
// Disabled for Markdown notes, whose editor provides its own combined slash-command menu.
useSnippetSlashCommands(
editorView,
- (candidate) => candidate.type === "code" && candidate.mime === note.mime,
+ (candidate) => candidate.type === "code" && (candidate.mime === note.mime || candidate.mime === "text/plain"),
note.mime,
!note.isMarkdown(),
note.noteId
diff --git a/apps/client/src/widgets/type_widgets/code/Markdown.tsx b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
index 772b4fd11a..a8a6d02455 100644
--- a/apps/client/src/widgets/type_widgets/code/Markdown.tsx
+++ b/apps/client/src/widgets/type_widgets/code/Markdown.tsx
@@ -509,9 +509,12 @@ function useSlashCommands(parentComponent: TypeWidgetProps["parentComponent"], e
// The user-configured todo task states (from the `_taskStates` subtree), loaded once.
// Read inside the autocomplete closure, so `/todo:*` commands reflect the current config.
const taskStatesRef = useRef<TaskStateDef[]>([]);
- // Markdown snippets (#snippet code notes with a markdown MIME), inserted via `/snippet:<name>`.
- // useCodeSnippets keeps the ref fresh so the slash menu reads the latest list when it opens.
- const snippetsRef = useCodeSnippets((candidate) => candidate.isMarkdown(), "markdown");
+ // Markdown snippets (#snippet code notes with a markdown MIME) plus generic plain-text snippets,
+ // inserted via `/snippet:<name>`. useCodeSnippets keeps the ref fresh so the menu reads the latest.
+ const snippetsRef = useCodeSnippets(
+ (candidate) => candidate.isMarkdown() || (candidate.type === "code" && candidate.mime === "text/plain"),
+ "markdown"
+ );
useEffect(() => { noteRef.current = note; }, [note]);
useEffect(() => { parentRef.current = parentComponent; }, [parentComponent]);
useEffect(() => { void getTaskStateDefinitions().then((states) => { taskStatesRef.current = states; }); }, []);