Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): keep line breaks and paragraphs apart in result snippets (#11787)

Elian Doranc1b66d07c9eb
diff --git a/packages/trilium-core/src/services/search/services/search.spec.ts b/packages/trilium-core/src/services/search/services/search.spec.ts
index 22082e4a66..72f1efe688 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -1108,6 +1108,16 @@ describe("Search", () => {
expect(result.highlightedContentSnippet).toBe("Summary Title<br><b>Body</b> text here<br>After the block");
});
+ it("keeps soft line breaks and paragraphs apart in the snippet (#11787)", () => {
+ // Shift+Enter in the text editor inserts a <br> rather than starting a new paragraph.
+ const noteBuilder = note("Soft breaks note");
+ noteBuilder.note.getContent = () => "<p>First line<br>second line<br/>third line</p><p>Next paragraph</p>";
+ rootNote.child(noteBuilder);
+
+ const snippet = searchService.extractContentSnippet(noteBuilder.note.noteId, [ "second" ]);
+ expect(snippet.split("\n")).toEqual([ "First line", "second line", "third line", "Next paragraph" ]);
+ });
+
it("escapes angle brackets in the note title instead of dropping them", () => {
// The title is interpolated into the autocomplete dropdown as raw HTML, so a title
// containing markup-like text must come back escaped. Stripping only "<" would render
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index ffcfd5fa62..0e6e98c6b8 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -566,6 +566,9 @@ function getTextRepresentationForNote(note: BNote): string | null {
return row?.textRepresentation ?? null;
}
+/** Closing tags of the block elements the text editor writes, each of which ends a line. */
+const BLOCK_END_TAG_RE = /<\/(?:p|h[1-6]|li|blockquote|pre|tr|figcaption|div)>/gi;
+
function extractContentSnippet(noteId: string, searchTokens: HighlightedTokenInfo[] | string[], maxLength: number = 200): string {
const note = becca.notes[noteId];
if (!note) {
@@ -601,7 +604,10 @@ function extractContentSnippet(noteId: string, searchTokens: HighlightedTokenInf
// whole block. The newlines become paragraph breaks in the snippet (rendered as <br>).
content = content
.replace(/<\/summary>/gi, "</summary>\n")
- .replace(/<\/details>/gi, "</details>\n");
+ .replace(/<\/details>/gi, "</details>\n")
+ // The same goes for soft line breaks (Shift+Enter) and the end of every block.
+ .replace(/<br\s*\/?>/gi, "$&\n")
+ .replace(BLOCK_END_TAG_RE, "$&\n");
// Link previews (link-embed / link-mention) keep their url/title/description in data
// attributes that striptags would drop; surface them as separate lines instead.
content = content.replace(/<(section|span)\b[^>]*\bclass="[^"]*\blink-(?:embed|mention)\b[^"]*"[^>]*>[\s\S]*?<\/\1>/gi, (element) => {
@@ -622,6 +628,8 @@ function extractContentSnippet(noteId: string, searchTokens: HighlightedTokenInf
// Decode HTML entities so the snippet shows real characters instead of escape codes
// (e.g. "&lt;", "&amp;", "&nbsp;") — attribute-sourced text above is entity-encoded too.
content = unescapeHtml(content).replace(/&nbsp;/g, " ");
+ // Nested blocks (`</p></li>`, `</p></details>`) end several lines at once; keep one.
+ content = content.replace(/\n\s*\n/g, "\n");
} else if (note.type === "llmChat") {
// The note stores the whole conversation as a JSON blob; show the readable prose only.
content = extractLlmChatText(content);