Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): display user-made reference links

Elian Dorand5f7281d2c46
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
index 9d887f1475..3d3efdaee6 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatInputBar.tsx
@@ -65,12 +65,13 @@ function htmlToPlainText(html: string): string {
const title = (a.textContent ?? "").trim();
a.replaceWith(`[${title}](${href})`);
});
- container.querySelectorAll("br").forEach((br) => br.replaceWith("\n"));
+ // Two-space + newline = markdown hard line break (preserves shift+Enter).
+ container.querySelectorAll("br").forEach((br) => br.replaceWith(" \n"));
const blocks = Array.from(container.children) as HTMLElement[];
if (blocks.length === 0) {
return (container.textContent ?? "").trim();
}
- return blocks.map((b) => b.textContent ?? "").join("\n").trim();
+ return blocks.map((b) => b.textContent ?? "").join("\n\n").trim();
}
interface ChatInputBarProps {
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css
index 2f41c69c8d..ee447508d2 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css
@@ -39,11 +39,6 @@
line-height: 1.5;
}
-/* Preserve whitespace only for user messages (plain text) */
-.llm-chat-message-user .llm-chat-message-content {
- white-space: pre-wrap;
-}
-
.llm-chat-cursor {
display: inline-block;
width: 8px;
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 b018eb9be9..e2aa707201 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx
@@ -1,8 +1,9 @@
import "./ChatMessage.css";
import "../code/MarkdownCommons.css";
-import { type LlmCitation, renderToHtml } from "@triliumnext/commons";
+import { CustomMarkdownRenderer, type LlmCitation, renderToHtml } from "@triliumnext/commons";
import DOMPurify from "dompurify";
+import type { Tokens } from "marked";
import { useMemo } from "preact/hooks";
import { t } from "../../../services/i18n.js";
@@ -19,12 +20,29 @@ function shortenNumber(n: number): string {
return n.toString();
}
+/**
+ * Renderer that tags `#root/...` markdown links with the `reference-link` class
+ * so ReadOnlyTextContent's applyReferenceLinks pass decorates them with the
+ * note icon, color, and title — same shape as the `[[noteId]]` wiki-link
+ * extension's output, but for chat's `[Title](#root/noteId)` references.
+ */
+class ChatMarkdownRenderer extends CustomMarkdownRenderer {
+ override link(token: Tokens.Link): string {
+ const html = super.link(token);
+ if (token.href.startsWith("#root/")) {
+ return html.replace(/^<a\b/, '<a class="reference-link"');
+ }
+ return html;
+ }
+}
+
/** Parse markdown to HTML using the shared rendering pipeline. */
function renderMarkdown(markdown: string): string {
return renderToHtml(markdown, "", {
sanitize: (h) => DOMPurify.sanitize(h),
wikiLink: { formatHref: (id) => `#root/${id}` },
- demoteH1: false
+ demoteH1: false,
+ renderer: new ChatMarkdownRenderer({ async: false })
});
}
@@ -113,13 +131,15 @@ export default function ChatMessage({ message, isStreaming, onRetry }: Props) {
const isThinking = message.type === "thinking";
const textContent = typeof message.content === "string" ? message.content : getMessageText(message.content);
- // Render markdown for assistant messages with legacy string content
+ // Render markdown for plain-string content (assistant legacy content and user prompts).
+ // User prompts may contain `[Title](#root/noteId)` reference links produced by the
+ // chat input's @-mention feature, which markdown renders as proper clickable links.
const renderedContent = useMemo(() => {
- if (message.role === "assistant" && !isThinking && typeof message.content === "string") {
+ if (!isThinking && typeof message.content === "string") {
return renderMarkdown(message.content);
}
return null;
- }, [message.content, message.role, isThinking]);
+ }, [message.content, isThinking]);
const messageClasses = [
"llm-chat-message",
@@ -171,14 +191,10 @@ export default function ChatMessage({ message, isStreaming, onRetry }: Props) {
<div className={`llm-chat-message-wrapper llm-chat-message-wrapper-${message.role}`}>
<div className={messageClasses}>
<div className="llm-chat-message-content">
- {message.role === "assistant" ? (
- hasBlockContent ? (
- renderContentBlocks(message.content as ContentBlock[], isStreaming)
- ) : (
- <MarkdownContent html={renderedContent || ""} isStreaming={isStreaming} />
- )
+ {message.role === "assistant" && hasBlockContent ? (
+ renderContentBlocks(message.content as ContentBlock[], isStreaming)
) : (
- textContent
+ <MarkdownContent html={renderedContent || ""} isStreaming={isStreaming && message.role === "assistant"} />
)}
</div>
{message.citations && message.citations.length > 0 && (