Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): support admonitions

Elian Doran298db2cba645
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
index 26a7f36c46..96609d0e6f 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
@@ -1,10 +1,10 @@
import "./ChatMessage.css";
import DOMPurify from "dompurify";
-import { Marked } from "marked";
+import { Marked, Renderer, type Tokens } from "marked";
import { useEffect, useMemo, useRef } from "preact/hooks";
-import { type LlmCitation, createWikiLinkExtension } from "@triliumnext/commons";
+import { ADMONITION_TYPE_MAPPINGS, type LlmCitation, createWikiLinkExtension } from "@triliumnext/commons";
import link from "../../../services/link.js";
import { t } from "../../../services/i18n.js";
@@ -19,6 +19,25 @@ function shortenNumber(n: number): string {
return n.toString();
}
+/** Renderer that turns GitHub-style `> [!NOTE]` blockquotes into admonition asides. */
+class ChatMarkdownRenderer extends Renderer {
+ override blockquote({ tokens }: Tokens.Blockquote): string {
+ const body = this.parser.parse(tokens);
+ const match = /^<p>\[\!([A-Z]+)\]/.exec(body);
+ if (match) {
+ const type = match[1].toLowerCase();
+ if (ADMONITION_TYPE_MAPPINGS[type]) {
+ const bodyWithoutHeader = body
+ // `breaks: true` turns the newline after the marker into a <br>, so strip that too.
+ .replace(/^<p>\[\!([A-Z]+)\]\s*(?:<br\s*\/?>\s*)?/, "<p>")
+ .replace(/^<p><\/p>/, "");
+ return `<aside class="admonition ${type}">${bodyWithoutHeader.trim()}</aside>`;
+ }
+ }
+ return `<blockquote>${body}</blockquote>`;
+ }
+}
+
// Configure marked for safe rendering with client-side URL format
const markedInstance = new Marked({
breaks: true, // Convert \n to <br>
@@ -30,7 +49,8 @@ markedInstance.use({
/** Parse markdown to HTML. */
function renderMarkdown(markdown: string): string {
- return markedInstance.parse(markdown) as string;
+ // marked attaches a parser to the renderer during parsing, so use a fresh instance per call.
+ return markedInstance.parse(markdown, { renderer: new ChatMarkdownRenderer() }) as string;
}
/** Renders markdown content with reference link title loading. */
diff --git a/apps/server/src/services/llm/providers/base_provider.ts b/apps/server/src/services/llm/providers/base_provider.ts
index 5000b4fcbf..16b29cab3c 100644
--- a/apps/server/src/services/llm/providers/base_provider.ts
+++ b/apps/server/src/services/llm/providers/base_provider.ts
@@ -122,6 +122,18 @@ export abstract class BaseProvider implements LlmProvider {
);
}
+ // Admonition formatting hint
+ parts.push(
+ `When calling out information that deserves visual emphasis, use GitHub-style admonitions. They render as colored callouts in the chat. Use them sparingly — only when a plain paragraph would under-sell the point — and pick the type that matches the intent:\n`
+ + `- \`> [!NOTE]\` — neutral side information worth highlighting\n`
+ + `- \`> [!TIP]\` — an optional improvement or shortcut\n`
+ + `- \`> [!IMPORTANT]\` — information the user should not miss\n`
+ + `- \`> [!WARNING]\` — something that may cause problems or surprise\n`
+ + `- \`> [!CAUTION]\` — a destructive or irreversible action\n\n`
+ + `Syntax: the marker must be on its own line, and every content line must also start with \`>\`. For example:\n\n`
+ + `> [!WARNING]\n> Deleting a note also removes its children and attachments.`
+ );
+
return parts.length > 0 ? parts.join("\n\n") : undefined;
}