Skip to content

Build your personal knowledge base with Trilium Notes

client/AI chat: fix the highlight list item ordering

Adorian Doran9db2ddf5e626
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights.ts
index 2d1b8e1ca0..0bffb92a96 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights.ts
+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights.ts
@@ -81,9 +81,16 @@ export function useChatHighlights(chat: UseLlmChatReturn, noteContext: NoteConte
if (!message.highlights?.length) continue;
const root = findMessageContentRoot(container, message.id);
if (!root) continue;
+ const resolved: { anchor: HighlightAnchor; range: Range }[] = [];
for (const anchor of message.highlights) {
const range = resolveAnchorRange(root, anchor);
if (!range) continue; // orphaned (e.g. regenerated message) — drop cleanly, never mis-paint
+ resolved.push({ anchor, range });
+ }
+ // Order by position within the message so the sidebar list follows the document, not the
+ // order the user happened to create the highlights in (`addHighlight` appends).
+ resolved.sort((a, b) => a.range.compareBoundaryPoints(Range.START_TO_START, b.range));
+ for (const { anchor, range } of resolved) {
rangeById.set(anchor.id, { range, messageId: message.id });
items.push({ id: anchor.id, messageId: message.id, text: anchor.quotedText });
}