Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): keep a multi-line value and a matched definition in its badge

Elian Doran7284a18fd24e
diff --git a/apps/client/src/widgets/react/Badge.spec.tsx b/apps/client/src/widgets/react/Badge.spec.tsx
index f28fb857c3..5005f19301 100644
--- a/apps/client/src/widgets/react/Badge.spec.tsx
+++ b/apps/client/src/widgets/react/Badge.spec.tsx
@@ -12,11 +12,14 @@ describe("parseAttributeSnippetLine", () => {
.toEqual({ icon: "bx bx-transfer", name: "author", value: "J. R. R. "Tolkien"" });
});
- it("gives a definition its field's icon and its bare name, its options being no value", () => {
+ it("gives a definition its field's icon and its bare name, its options only where matched", () => {
expect(parseAttributeSnippetLine("#label:<b>due</b>=&quot;promoted,single,date&quot;"))
.toEqual({ icon: "bx bx-calendar", name: "<b>due</b>" });
expect(parseAttributeSnippetLine("#relation:author=&quot;promoted&quot;"))
.toEqual({ icon: "bx bx-transfer", name: "author" });
+ // Options the search matched stay, as the reason the note is listed.
+ expect(parseAttributeSnippetLine("#label:due=&quot;promoted,single,<b>date</b>&quot;"))
+ .toEqual({ icon: "bx bx-calendar", name: "due", value: "promoted,single,<b>date</b>" });
// A highlight across the prefix leaves the name as text.
expect(parseAttributeSnippetLine("#<b>label:due</b>=&quot;promoted&quot;"))
.toEqual({ icon: "bx bx-text", name: "due" });
diff --git a/apps/client/src/widgets/react/Badge.tsx b/apps/client/src/widgets/react/Badge.tsx
index 2390f7c9f0..93ca432bc8 100644
--- a/apps/client/src/widgets/react/Badge.tsx
+++ b/apps/client/src/widgets/react/Badge.tsx
@@ -113,8 +113,8 @@ export interface AttributeSnippetLine {
* Reads a line of `highlightedAttributeSnippet` as `extractAttributeSnippet()` writes it, `#name`,
* `#name="value"` or `~name="target title"`, escaped, so its quotes arrive as `&quot;`. The icon is
* the attributes panel's; a definition (`#label:name="promoted,…"`) is shown by its bare name, the
- * icon standing for the field it sets up. Returns `null` for a line in no such shape, such as one the
- * server cut short.
+ * icon standing for the field it sets up, and its options only where the search matched them. Returns
+ * `null` for a line in no such shape, such as one the server cut short.
*/
export function parseAttributeSnippetLine(line: string): AttributeSnippetLine | null {
const match = /^([#~])(.+?)(?:=&quot;(.*)&quot;)?$/s.exec(line);
@@ -131,7 +131,8 @@ export function parseAttributeSnippetLine(line: string): AttributeSnippetLine |
const bareName = name.startsWith(definitionPrefix)
? name.substring(definitionPrefix.length)
: escapeHtml(plainName.substring(definitionPrefix.length));
- return { icon, name: bareName };
+ // Its options are no value of the note's, unless the search matched them.
+ return { icon, name: bareName, ...(value?.includes("<b") ? { value } : {}) };
}
return { icon, name, ...(value ? { value } : {}) };
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index 1c77a1399b..d93b90a79a 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -761,7 +761,8 @@ function extractAttributeSnippet(noteId: string, searchTokens: HighlightedTokenI
if (hasMatch) {
matchingAttributes.push({
name: attr.name || "",
- value: attr.value || "",
+ // One line per attribute: the lines are joined with newlines, which become `<br>`.
+ value: (attr.value || "").replace(/\s*[\r\n]+\s*/g, " "),
type: attrType
});
}
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 c501065c6e..9b7e0a2292 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
@@ -164,6 +164,20 @@ describe("buildSearchResultDetails", () => {
expect(details.attributeSnippet).toBe(`#topic="hidden"`);
});
+ it("keeps a multi-line value on its attribute's line, the lines standing for attributes", () => {
+ const child = note("Recipe").label("steps", "chop the onions\r\nfry them\n\nserve").label("course", "main");
+ rootNote.child(child);
+ const result = new SearchResult(["root", child.note.noteId]);
+
+ const searchContext = new SearchContext();
+ searchContext.highlightedTokens.push("onions", "main");
+
+ const [details] = searchService.buildSearchResultDetails([result], searchContext);
+
+ expect(details.attributeSnippet).toBe(`#steps="chop the onions fry them serve"\n#course="main"`);
+ expect(details.highlightedAttributeSnippet?.split("<br>")).toHaveLength(2);
+ });
+
it("matches attribute values with a regex token from getHighlightedTokenInfos", () => {
const child = note("Server config").label("env", "production");
rootNote.child(child);