Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): leave docName and docUrl out of the attribute snippet

Elian Dorand489a9a2fb01
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index 99355d8bb5..64c3f4ff6b 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -719,6 +719,12 @@ function extractContentSnippet(noteId: string, searchTokens: HighlightedTokenInf
}
}
+/**
+ * Labels left out of the attribute snippet: the help notes' `docName` and `docUrl` spell out the
+ * page's own path and address, which match a search for its title and repeat it.
+ */
+const SNIPPET_HIDDEN_LABELS = new Set([ "docName", "docUrl" ]);
+
function extractAttributeSnippet(noteId: string, searchTokens: HighlightedTokenInfo[] | string[], maxLength: number = 200): string {
const note = becca.notes[noteId];
if (!note) {
@@ -738,6 +744,10 @@ function extractAttributeSnippet(noteId: string, searchTokens: HighlightedTokenI
// Look for attributes that match the search tokens
for (const attr of attributes) {
+ if (attr.type === "label" && SNIPPET_HIDDEN_LABELS.has(attr.name)) {
+ continue;
+ }
+
const attrName = attr.name?.toLowerCase() || "";
const attrValue = attr.value?.toLowerCase() || "";
const attrType = attr.type || "";
diff --git a/packages/trilium-core/src/services/search/services/search_highlight.spec.ts b/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
index 5a5e2bf065..c501065c6e 100644
--- a/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
+++ b/packages/trilium-core/src/services/search/services/search_highlight.spec.ts
@@ -148,6 +148,22 @@ describe("buildSearchResultDetails", () => {
expect(details.highlightedAttributeSnippet).toContain("<b>phoenix</b>");
});
+ it("leaves the help notes' docName and docUrl out of the attribute snippet", () => {
+ const child = note("Hidden Notes")
+ .label("docName", "User Guide/Hidden Notes")
+ .label("docUrl", "https://docs.triliumnotes.org/hidden-notes")
+ .label("topic", "hidden");
+ rootNote.child(child);
+ const result = new SearchResult(["root", child.note.noteId]);
+
+ const searchContext = new SearchContext();
+ searchContext.highlightedTokens.push("hidden");
+
+ const [details] = searchService.buildSearchResultDetails([result], searchContext);
+
+ expect(details.attributeSnippet).toBe(`#topic="hidden"`);
+ });
+
it("matches attribute values with a regex token from getHighlightedTokenInfos", () => {
const child = note("Server config").label("env", "production");
rootNote.child(child);