Skip to content

Build your personal knowledge base with Trilium Notes

style/collapsible blocks: tweak formatting for quick search results

Adorian Dorandc4a0b5f3427
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 aaffa40aa8..db92d715b4 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -770,6 +770,25 @@ describe("Search", () => {
});
+ it("breaks a collapsible summary onto its own line in the quick-search snippet", () => {
+ // striptags concatenates block text with no separator, which would merge a
+ // collapsible's summary straight into its body ("Summary TitleBody text").
+ const noteBuilder = note("Collapsible note");
+ noteBuilder.note.getContent = () => "<details class=\"trilium-collapsible\"><summary>Summary Title</summary><p>Body text here</p></details><p>After the block</p>";
+ rootNote.child(noteBuilder);
+
+ // The snippet gets a newline at the summary boundary and after the whole block, so
+ // neither the summary nor the body runs into the surrounding text.
+ const snippet = searchService.extractContentSnippet(noteBuilder.note.noteId, [ "body" ]);
+ expect(snippet.split("\n")).toEqual([ "Summary Title", "Body text here", "After the block" ]);
+
+ // ...which the quick-search route (extractContentSnippet -> highlightSearchResults)
+ // turns into <br> tags in the HTML the dropdown renders.
+ const result: any = { notePathTitle: "Collapsible note", contentSnippet: snippet, attributeSnippet: "" };
+ searchService.highlightSearchResults([ result ], [ "body" ]);
+ expect(result.highlightedContentSnippet).toBe("Summary Title<br><b>Body</b> text here<br>After the block");
+ });
+
// FIXME: test what happens when we order without any filter criteria
// it("comparison between labels", () => {
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index 3da446e5d9..06e81ce008 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -498,6 +498,12 @@ function extractContentSnippet(noteId: string, searchTokens: string[], maxLength
// Strip HTML tags for text notes
if (note.type === "text") {
+ // A collapsible's summary and body must not run into surrounding text: striptags
+ // drops tags with no separator, so break the line after the summary and after the
+ // 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");
content = striptags(content);
} else if (note.type === "llmChat") {
// The note stores the whole conversation as a JSON blob; show the readable prose only.