Skip to content

Build your personal knowledge base with Trilium Notes

client/AI chat: fix some issues

Adorian Doran12f5510c2a81
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.ts
index ce864117ee..d4dae81f2e 100644
--- a/apps/client/src/services/content_renderer.ts
+++ b/apps/client/src/services/content_renderer.ts
@@ -382,7 +382,11 @@ async function renderLlmChat(note: FNote, $renderedContent: JQuery<HTMLElement>)
let messages: StoredMessage[] = [];
if (source.trim()) {
try {
- messages = (JSON.parse(source) as LlmChatContent).messages ?? [];
+ const parsed = JSON.parse(source);
+ // JSON.parse("null") (or any non-object) must not throw on `.messages`.
+ if (parsed && typeof parsed === "object") {
+ messages = (parsed as LlmChatContent).messages ?? [];
+ }
} catch {
// Malformed content → empty preview rather than throwing.
}
diff --git a/apps/client/src/widgets/react/CKEditor.spec.ts b/apps/client/src/widgets/react/CKEditor.spec.ts
new file mode 100644
index 0000000000..4a766a913b
--- /dev/null
+++ b/apps/client/src/widgets/react/CKEditor.spec.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from "vitest";
+
+import { groupQuoteLinesIntoParagraphs } from "./CKEditor.js";
+
+describe("groupQuoteLinesIntoParagraphs", () => {
+ it("keeps contiguous lines in a single paragraph", () => {
+ expect(groupQuoteLinesIntoParagraphs(["a", "b", "c"])).toEqual([["a", "b", "c"]]);
+ });
+
+ it("starts a new paragraph at a blank line", () => {
+ expect(groupQuoteLinesIntoParagraphs(["a", "", "b"])).toEqual([["a"], ["b"]]);
+ });
+
+ it("collapses consecutive blank lines into a single paragraph break", () => {
+ expect(groupQuoteLinesIntoParagraphs(["a", "", "", "b"])).toEqual([["a"], ["b"]]);
+ });
+
+ it("ignores leading and trailing blank lines", () => {
+ expect(groupQuoteLinesIntoParagraphs(["", "a", "b", ""])).toEqual([["a", "b"]]);
+ });
+
+ it("returns no paragraphs for an all-blank quote", () => {
+ expect(groupQuoteLinesIntoParagraphs([])).toEqual([]);
+ expect(groupQuoteLinesIntoParagraphs(["", ""])).toEqual([]);
+ });
+});
diff --git a/apps/client/src/widgets/react/CKEditor.tsx b/apps/client/src/widgets/react/CKEditor.tsx
index 13d0b12eb6..14aca18e9a 100644
--- a/apps/client/src/widgets/react/CKEditor.tsx
+++ b/apps/client/src/widgets/react/CKEditor.tsx
@@ -74,15 +74,22 @@ export default function CKEditor({ apiRef, currentValue, editor, config, disable
writer.remove(onlyChild);
}
- // One paragraph inside the quote, its lines joined by soft breaks so the whole
- // excerpt stays a single contiguous blockquote.
- const paragraph = writer.createElement("paragraph");
- lines.forEach((line, index) => {
- if (index > 0) writer.appendElement("softBreak", paragraph);
- if (line.length > 0) writer.appendText(line, paragraph);
- });
+ // A blank quote line separates paragraphs; lines within a paragraph are joined by
+ // soft breaks. Emitting one <paragraph> per group round-trips to `> a\n>\n> b`
+ // instead of collapsing the blank separator into a spurious hard break.
const quote = writer.createElement("blockQuote");
- writer.append(paragraph, quote);
+ for (const paragraphLines of groupQuoteLinesIntoParagraphs(lines)) {
+ const paragraph = writer.createElement("paragraph");
+ writer.append(paragraph, quote);
+ paragraphLines.forEach((line, index) => {
+ if (index > 0) writer.appendElement("softBreak", paragraph);
+ writer.appendText(line, paragraph);
+ });
+ }
+ // A quote of only blank lines still needs a block child to stay schema-valid.
+ if (quote.isEmpty) {
+ writer.append(writer.createElement("paragraph"), quote);
+ }
writer.insert(quote, writer.createPositionAt(root, "end"));
// A trailing empty paragraph below the quote holds the cursor so the user types
@@ -153,3 +160,26 @@ export default function CKEditor({ apiRef, currentValue, editor, config, disable
/>
)
}
+
+/**
+ * Groups the (already `> `-stripped) lines of a markdown quote into paragraphs: a blank line starts a
+ * new paragraph, and consecutive non-blank lines belong to the same one. Returns one entry per
+ * paragraph, each holding its lines (which become soft breaks). Used by {@link CKEditorApi.appendBlockQuote}
+ * so a `> a\n>\n> b` quote becomes two paragraphs rather than one paragraph with a doubled hard break.
+ */
+export function groupQuoteLinesIntoParagraphs(lines: string[]): string[][] {
+ const paragraphs: string[][] = [];
+ let current: string[] | null = null;
+ for (const line of lines) {
+ if (line.length === 0) {
+ current = null;
+ continue;
+ }
+ if (!current) {
+ current = [];
+ paragraphs.push(current);
+ }
+ current.push(line);
+ }
+ return paragraphs;
+}