Skip to content

Build your personal knowledge base with Trilium Notes

client/search: enable link previews in quick search

Adorian Doran0833d8381ad7
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 db92d715b4..a1741b2b92 100644
--- a/packages/trilium-core/src/services/search/services/search.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search.spec.ts
@@ -789,6 +789,17 @@ describe("Search", () => {
expect(result.highlightedContentSnippet).toBe("Summary Title<br><b>Body</b> text here<br>After the block");
});
+ it("surfaces link-preview url/title/description as separate lines in the quick-search snippet", () => {
+ // The url/title/description live in data attributes that striptags would otherwise drop,
+ // leaving a blank snippet even though the note matched on the embedded title.
+ const noteBuilder = note("Link preview note");
+ noteBuilder.note.getContent = () => "<section class=\"link-embed\" data-url=\"https://en.wikipedia.org/wiki/The_Terminator\" data-title=\"The Terminator - Wikipedia\" data-description=\"A 1984 science fiction film.\">&nbsp;</section>";
+ rootNote.child(noteBuilder);
+
+ const snippet = searchService.extractContentSnippet(noteBuilder.note.noteId, [ "terminator" ]);
+ expect(snippet.split("\n")).toEqual([ "https://en.wikipedia.org/wiki/The_Terminator", "The Terminator - Wikipedia", "A 1984 science fiction film." ]);
+ });
+
// 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 06e81ce008..4b657a669f 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -504,6 +504,14 @@ function extractContentSnippet(noteId: string, searchTokens: string[], maxLength
content = content
.replace(/<\/summary>/gi, "</summary>\n")
.replace(/<\/details>/gi, "</details>\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) => {
+ const url = element.match(/\bdata-url="([^"]*)"/i)?.[1] ?? "";
+ const title = element.match(/\bdata-title="([^"]*)"/i)?.[1] ?? "";
+ const description = element.match(/\bdata-description="([^"]*)"/i)?.[1] ?? "";
+ return `\n${[url, title, description].filter(Boolean).join("\n")}\n`;
+ });
content = striptags(content);
} else if (note.type === "llmChat") {
// The note stores the whole conversation as a JSON blob; show the readable prose only.