Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): flatten the HTML left in a tool call's note preview

Elian Dorand9eff5958c10
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
index 07649e5f03..a431db452b 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
@@ -205,7 +205,12 @@ describe("ToolCallCard", () => {
result: JSON.stringify({
totalResults: 5,
results: [
- { noteId: "_help_abc", title: "Cloning", path: "Basic Concepts > Notes", contentPreview: "**Clones** share a note" },
+ {
+ noteId: "_help_abc",
+ title: "Cloning",
+ path: "Basic Concepts > Notes",
+ contentPreview: "**Clones** share&nbsp;<a class=\"reference-link\" href=\"#root/x\">a note</a>: <kbd>Ctrl</kbd>+<kbd>C</kbd> <span class=\"tn-icon bx bx-copy\"></span>&amp; 1 < 2"
+ },
{ noteId: "_help_def", title: "Tree", path: "", contentPreview: null }
]
})
@@ -220,7 +225,7 @@ describe("ToolCallCard", () => {
path: row.querySelector(".llm-chat-note-result-parent")?.textContent ?? null,
preview: row.querySelector(".llm-chat-note-result-preview")?.textContent ?? null
}))).toEqual([
- { note: "_help_abc", path: "Basic Concepts > Notes", preview: "Clones share a note" },
+ { note: "_help_abc", path: "Basic Concepts > Notes", preview: "Clones share a note: Ctrl+C & 1 < 2" },
{ note: "_help_def", path: null, preview: null }
]);
expect(line?.querySelector(".llm-chat-note-results-more")?.textContent)
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
index 1bda863551..d934c295d5 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -221,12 +221,26 @@ function parseSearchNotesResult(result: string): SearchNotesResult | null {
export function markdownToPlainPreview(markdown: string): string {
if (/^\[\d+KB - /.test(markdown)) return "";
+ // The Markdown export keeps what it cannot express (reference links, `<kbd>`, icon spans) as
+ // HTML. Entities are decoded last, so an escaped `&lt;b&gt;` stays text.
return markdown
+ .replace(/<\/?[a-z][^>]*>/gi, "")
.replace(/!\[[^\]]*\]\([^)]*\)/g, "")
.replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
.replace(/^\s*\|?[\s:|-]+\|?\s*$/gm, "")
.replace(/^\s{0,3}(#{1,6}|>|[-*+]|\d+\.)\s+/gm, "")
.replace(/\*\*|__|~~|`|\|/g, "")
+ .replace(/&(#x[\da-f]+|#\d+|[a-z]+);/gi, decodeEntity)
.replace(/\s+/g, " ")
.trim();
}
+
+const NAMED_ENTITIES: Record<string, string> = { nbsp: " ", amp: "&", lt: "<", gt: ">", quot: "\"", apos: "'" };
+
+function decodeEntity(entity: string, name: string): string {
+ if (name.startsWith("#")) {
+ const codePoint = name[1] === "x" || name[1] === "X" ? parseInt(name.slice(2), 16) : parseInt(name.slice(1), 10);
+ return codePoint <= 0x10FFFF ? String.fromCodePoint(codePoint) : entity;
+ }
+ return NAMED_ENTITIES[name.toLowerCase()] ?? entity;
+}