Skip to content

Build your personal knowledge base with Trilium Notes

client/snippets: handle protected notes

Adorian Doran31448c79faa7
diff --git a/apps/client/src/widgets/type_widgets/code/snippets.ts b/apps/client/src/widgets/type_widgets/code/snippets.ts
index c7415fd22a..f5aed94391 100644
--- a/apps/client/src/widgets/type_widgets/code/snippets.ts
+++ b/apps/client/src/widgets/type_widgets/code/snippets.ts
@@ -25,15 +25,22 @@ export interface CodeSnippet {
* Markdown editor, or a MIME comparison for a code editor).
*/
export async function getCodeSnippets(matches: (note: FNote) => boolean): Promise<CodeSnippet[]> {
- // The `search` route includes archived notes, so drop archived snippets explicitly.
- const notes = (await search.searchForNotes("#snippet")).filter((note) => matches(note) && !note.isArchived);
-
- return Promise.all(notes.map(async (note) => ({
- noteId: note.noteId,
- title: note.title,
- description: note.getLabelValue("snippetDescription") ?? undefined,
- content: (await note.getContent()) ?? ""
- })));
+ try {
+ // The `search` route includes archived notes, so drop archived ones; also skip protected
+ // snippets whose content isn't available (no active protected session) — they can't be inserted.
+ const notes = (await search.searchForNotes("#snippet"))
+ .filter((note) => matches(note) && !note.isArchived && note.isContentAvailable());
+
+ return await Promise.all(notes.map(async (note) => ({
+ noteId: note.noteId,
+ title: note.title,
+ description: note.getLabelValue("snippetDescription") ?? undefined,
+ content: (await note.getContent()) ?? ""
+ })));
+ } catch (e) {
+ logError("Error while building code snippets: ", e);
+ return [];
+ }
}
/**
@@ -146,4 +153,4 @@ export function useSnippetSlashCommands(editorView: VanillaCodeMirror | null, ma
editorView.setNamedExtension("snippetCommands", extension);
}, [editorView, enabled]);
-}
+}
\ No newline at end of file
diff --git a/apps/client/src/widgets/type_widgets/text/snippets.ts b/apps/client/src/widgets/type_widgets/text/snippets.ts
index b2baa9b10e..001407d1bb 100644
--- a/apps/client/src/widgets/type_widgets/text/snippets.ts
+++ b/apps/client/src/widgets/type_widgets/text/snippets.ts
@@ -28,7 +28,7 @@ export default async function getTemplates() {
// starting with "(" trips the search lexer (the leading paren is dropped, see lex.ts), which
// corrupts the parse and matches every note.
const snippets = (await search.searchForNotes("#textSnippet OR #snippet"))
- .filter((snippet) => snippet.type === "text" && !snippet.isArchived);
+ .filter((snippet) => snippet.type === "text" && !snippet.isArchived && snippet.isContentAvailable());
const definitions: TemplateDefinition[] = [];
for (const snippet of snippets) {
const { description } = await invalidateCacheFor(snippet);