Skip to content

Build your personal knowledge base with Trilium Notes

feat(ckeditor): add the diagram, callout and collapsible AI reformats

Elian Doran30bb87a61787
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index f6a4f01c77..bcace755ba 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2846,6 +2846,9 @@
"command_tone": "Change tone to {{tone}}",
"command_bullet_list": "Turn into a bullet list",
"command_table": "Turn into a table",
+ "command_diagram": "Turn into a diagram",
+ "command_callout": "Turn into a callout",
+ "command_collapsible": "Turn into a collapsible section",
"command_action_items": "Extract action items",
"command_translate": "Translate to {{language}}",
"action_fix_typos": "Fix typos",
@@ -2863,6 +2866,9 @@
"tone_confident": "Confident",
"reformat_bullet_list": "Bullet list",
"reformat_table": "Table",
+ "reformat_diagram": "Diagram",
+ "reformat_callout": "Callout",
+ "reformat_collapsible": "Collapsible",
"reformat_action_items": "Action items",
"lang_english": "English",
"lang_german": "German",
diff --git a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.spec.ts b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.spec.ts
index 19200fb79c..6d46e7268c 100644
--- a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.spec.ts
@@ -163,6 +163,35 @@ describe("the Markdown pipeline", () => {
expect(html).toContain("language-mermaid");
});
+ // Markdown has no collapsible syntax, so the model writes the HTML — which only keeps its
+ // Markdown formatted when the blank lines separate it from the surrounding block.
+ it("formats the content inside a collapsible, given the blank lines the prompt asks for", async () => {
+ responseChunks = ["<details>\n<summary>More</summary>\n\n- one\n- two\n\n</details>"];
+
+ const [html] = (await run()).rendered;
+ expect(html).toContain("<summary>More</summary>");
+ expect(html).toContain("<ul><li>one</li><li>two</li></ul>");
+ });
+
+ it("leaves a collapsible's content unformatted when the model omits them", async () => {
+ // Documented so the prompt's insistence on blank lines is not mistaken for noise.
+ responseChunks = ["<details><summary>More</summary>\n- one\n</details>"];
+
+ const [html] = (await run()).rendered;
+ expect(html).toContain("- one");
+ expect(html).not.toContain("<li>one</li>");
+ });
+
+ // The Diagram action asks for the code block and nothing else, so the whole answer is a
+ // fence — one the stripper must not mistake for a wrapper.
+ it("keeps a diagram whose fence is the entire answer", async () => {
+ responseChunks = ["```mermaid\nflowchart TD\n a-->b\n```"];
+
+ const [html] = (await run()).rendered;
+ expect(html).toContain("language-mermaid");
+ expect(html).toContain("flowchart TD");
+ });
+
it("strips a fence the model wrapped the whole answer in", async () => {
responseChunks = ["\u0060\u0060\u0060markdown\n**bold**\n\u0060\u0060\u0060"];
@@ -188,6 +217,13 @@ describe("stripMarkdownFences", () => {
expect(stripMarkdownFences(fence + "markdown\nst")).toBe("st");
});
+ it("leaves a fence that names content alone", () => {
+ expect(stripMarkdownFences(fence + "mermaid\nflowchart TD\n" + fence))
+ .toBe(fence + "mermaid\nflowchart TD\n" + fence);
+ expect(stripMarkdownFences(fence + "js\nconst a = 1;\n" + fence))
+ .toBe(fence + "js\nconst a = 1;\n" + fence);
+ });
+
it("does not treat a fence inside the content as a closing one", () => {
expect(stripMarkdownFences(fence + "markdown\nuse " + fence + " for fences\nmore"))
.toBe("use " + fence + " for fences\nmore");
@@ -228,6 +264,9 @@ describe("buildAiAssistantQuickActions", () => {
expect(actions("reformat").map((action) => [action.label, action.commandLabel])).toEqual([
["ai_assistant.reformat_bullet_list", "ai_assistant.command_bullet_list"],
["ai_assistant.reformat_table", "ai_assistant.command_table"],
+ ["ai_assistant.reformat_diagram", "ai_assistant.command_diagram"],
+ ["ai_assistant.reformat_callout", "ai_assistant.command_callout"],
+ ["ai_assistant.reformat_collapsible", "ai_assistant.command_collapsible"],
["ai_assistant.reformat_action_items", "ai_assistant.command_action_items"]
]);
});
diff --git a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts
index 77a945e24e..894b2f6f08 100644
--- a/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts
+++ b/apps/client/src/widgets/type_widgets/text/ai_assistant_stream.ts
@@ -149,6 +149,14 @@ export function buildAiAssistantQuickActions(): AiQuickActionGroup[] {
"Rewrite this content as a bulleted list, one point per item, without losing information."),
spelledOutAction("table", t("ai_assistant.reformat_table"), t("ai_assistant.command_table"),
"Reorganize this content into a table with a header row, choosing columns that fit what the content describes."),
+ spelledOutAction("diagram", t("ai_assistant.reformat_diagram"), t("ai_assistant.command_diagram"),
+ "Express this content as a Mermaid diagram inside a `mermaid` code block — a flowchart unless another Mermaid diagram type fits the content better. Respond with the code block only."),
+ spelledOutAction("callout", t("ai_assistant.reformat_callout"), t("ai_assistant.command_callout"),
+ "Turn this content into a single callout, opening with the marker that fits it best — `> [!NOTE]`, `> [!TIP]`, `> [!IMPORTANT]`, `> [!CAUTION]` or `> [!WARNING]` — and keeping the wording."),
+ // The blank lines are not cosmetic: without them the content sits inside one HTML
+ // block and its Markdown is never processed (verified against the renderer).
+ spelledOutAction("collapsible", t("ai_assistant.reformat_collapsible"), t("ai_assistant.command_collapsible"),
+ "Wrap this content in a collapsible section: a line with <details>, then a <summary> line saying in a few words what it hides, then a blank line, then the content unchanged, then a blank line and </details>."),
// A real task list rather than a plain one: `- [ ]` is Markdown the model already
// writes, and the renderer turns it into the editor's `todo-list` markup.
spelledOutAction("actionItems", t("ai_assistant.reformat_action_items"), t("ai_assistant.command_action_items"),
@@ -265,13 +273,24 @@ async function loadMarkdownRenderer(): Promise<(markdown: string) => string> {
}
/**
- * Removes a leading markdown code fence (\`\`\`markdown or \`\`\`) and, when present, the matching
- * closing fence. Models add these despite instructions not to; the stripper is applied to the
- * cumulative stream, so it must also handle a fence whose closing half has not arrived yet.
+ * Info strings that mean "here is my answer" rather than naming content. A fence carrying any
+ * other language is part of the response: a `mermaid` block is the whole point of the Diagram
+ * action, and unwrapping it leaves the diagram source rendering as a paragraph of text.
+ */
+const WRAPPER_FENCE_LANGUAGES = new Set(["", "markdown", "md", "html"]);
+
+/**
+ * Removes the code fence a model wrapped its whole answer in, closing half included when it has
+ * arrived. Models add these despite instructions not to; the stripper runs against the cumulative
+ * stream, so it also has to handle a fence whose closing half has not streamed in yet.
+ *
+ * A bare ``` is treated as a wrapper, which is what it almost always is. The cost is that an
+ * answer that is nothing but an unlabelled code block loses its fence — cheap next to leaving
+ * every wrapped answer rendering as source.
*/
export function stripMarkdownFences(cumulative: string): string {
- const opening = /^\s*```[a-z]*\s*\n?/i.exec(cumulative);
- if (!opening) {
+ const opening = /^\s*```([a-z]*)\s*\n?/i.exec(cumulative);
+ if (!opening || !WRAPPER_FENCE_LANGUAGES.has(opening[1].toLowerCase())) {
return cumulative;
}