Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
chore(llm): use shared Markdown renderer
Elian Doran72a7e8daa629
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsxindex 96609d0e6f..ff67d82dbb 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,9 @@import "./ChatMessage.css";import DOMPurify from "dompurify";-import { Marked, Renderer, type Tokens } from "marked";import { useEffect, useMemo, useRef } from "preact/hooks";-import { ADMONITION_TYPE_MAPPINGS, type LlmCitation, createWikiLinkExtension } from "@triliumnext/commons";+import { type LlmCitation, renderToHtml } from "@triliumnext/commons";import link from "../../../services/link.js";import { t } from "../../../services/i18n.js";@@ -19,38 +18,14 @@ 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>- gfm: true // GitHub Flavored Markdown-});-markedInstance.use({- extensions: [createWikiLinkExtension({ formatHref: (id) => `#root/${id}` })]-});--/** Parse markdown to HTML. */+/** Parse markdown to HTML using the shared rendering pipeline. */function renderMarkdown(markdown: string): 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;+ return renderToHtml(markdown, "", {+ sanitize: (h) => DOMPurify.sanitize(h),+ wikiLink: { formatHref: (id) => `#root/${id}` },+ demoteH1: false,+ breaks: true+ });}/** Renders markdown content with reference link title loading. */diff --git a/packages/commons/src/lib/markdown_renderer.ts b/packages/commons/src/lib/markdown_renderer.tsindex b2e99122ad..e77e4595b8 100644--- a/packages/commons/src/lib/markdown_renderer.ts+++ b/packages/commons/src/lib/markdown_renderer.ts@@ -57,6 +57,11 @@ export interface RenderToHtmlOptions {* marked attaches a parser to the renderer during parsing.*/renderer?: Renderer;+ /**+ * If `true`, convert single newlines (`\n`) to `<br>`. Useful for chat-style+ * content where LLMs use single newlines as line breaks. Default: `false`.+ */+ breaks?: boolean;}function escapeHtml(str: string): string {@@ -287,7 +292,7 @@ export function renderToHtml(content: string, title: string, options: RenderToHtconst { processedText, placeholderMap: formulaMap } = extractFormulas(content);- const marked = new Marked({ async: false });+ const marked = new Marked({ async: false, breaks: options.breaks ?? false, gfm: true });marked.use(markedFootnote());marked.use({// Order is important, especially for wikilinks.