Skip to content

Build your personal knowledge base with Trilium Notes

feat(autocomplete): show a note's matching attributes as badges

Elian Doran83beaeea503a
diff --git a/apps/client/src/widgets/collections/search/SearchResultCard.tsx b/apps/client/src/widgets/collections/search/SearchResultCard.tsx
index 9bdb8db972..3868cbd83f 100644
--- a/apps/client/src/widgets/collections/search/SearchResultCard.tsx
+++ b/apps/client/src/widgets/collections/search/SearchResultCard.tsx
@@ -2,10 +2,10 @@ import type { HighlightedTokenInfo, SearchResultDetails } from "@triliumnext/com
import { t } from "../../../services/i18n";
import { calculateHash, type ViewScope } from "../../../services/link";
-import { Badge } from "../../react/Badge";
+import { AttributeSnippetBadges } from "../../react/Badge";
import { useNote, useNoteTitle } from "../../react/hooks";
import Icon from "../../react/Icon";
-import RawHtml, { HighlightedText, RawHtmlBlock } from "../../react/RawHtml";
+import { HighlightedText, RawHtmlBlock } from "../../react/RawHtml";
interface SearchResultCardProps {
noteId: string;
@@ -44,7 +44,10 @@ export default function SearchResultCard({ noteId, details, loading, highlighted
{breadcrumb && <span className="search-result-card-path">{breadcrumb}</span>}
</div>
<SearchResultSnippet details={details} loading={loading} />
- <SearchResultBadges snippet={details?.highlightedAttributeSnippet} />
+ <AttributeSnippetBadges
+ snippet={details?.highlightedAttributeSnippet}
+ className="search-result-card-badges"
+ />
</a>
);
}
@@ -64,21 +67,6 @@ function SearchResultSnippet({ details, loading }: { details: SearchResultDetail
return <RawHtmlBlock className="search-result-card-snippet" html={details.highlightedContentSnippet ?? ""} />;
}
-function SearchResultBadges({ snippet }: { snippet: string | undefined }) {
- if (!snippet) return null;
-
- const lines = snippet.split(/<br\s*\/?>/i).map((line) => line.trim()).filter(Boolean);
- if (lines.length === 0) return null;
-
- return (
- <div className="search-result-card-badges">
- {lines.map((line, index) => (
- <Badge key={index} outline text={<RawHtml html={line} />} />
- ))}
- </div>
- );
-}
-
/** The breadcrumb shows the ancestor path only, i.e. the note-path title minus the note's own title. */
export function getBreadcrumbTitle(notePathTitle: string | undefined): string {
if (!notePathTitle) return "";
diff --git a/apps/client/src/widgets/react/Badge.tsx b/apps/client/src/widgets/react/Badge.tsx
index 9b79ba06eb..aceffc4280 100644
--- a/apps/client/src/widgets/react/Badge.tsx
+++ b/apps/client/src/widgets/react/Badge.tsx
@@ -7,6 +7,7 @@ import { useRef } from "preact/hooks";
import Dropdown, { DropdownProps } from "./Dropdown";
import { useStaticTooltip } from "./hooks";
import Icon from "./Icon";
+import RawHtml from "./RawHtml";
interface SimpleBadgeProps {
className?: string;
@@ -54,6 +55,23 @@ export function Badge({ icon, className, text, tooltip, href, outline, ...contai
);
}
+/**
+ * A search result's `highlightedAttributeSnippet`, which the server joins with `<br>`, as one outline
+ * badge per attribute, its search highlights kept.
+ */
+export function AttributeSnippetBadges({ snippet, className }: { snippet: string | undefined; className: string }) {
+ const lines = snippet?.split(/<br\s*\/?>/i).map((line) => line.trim()).filter(Boolean);
+ if (!lines?.length) return null;
+
+ return (
+ <div className={className}>
+ {lines.map((line, index) => (
+ <Badge key={index} outline text={<RawHtml html={line} />} />
+ ))}
+ </div>
+ );
+}
+
export function BadgeWithDropdown({ text, children, tooltip, className, dropdownProps, ...props }: BadgeProps & {
children: ComponentChildren,
dropdownProps?: Partial<DropdownProps>
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.css b/apps/client/src/widgets/react/NoteAutocomplete.css
index c18261aa05..f04dad3192 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.css
+++ b/apps/client/src/widgets/react/NoteAutocomplete.css
@@ -46,11 +46,27 @@
white-space: normal;
overflow-wrap: anywhere;
- .search-result-attributes {
+ /* Lets a badge too long for the row shrink to its width and end in an ellipsis. */
+ .note-suggestion-text {
+ min-width: 0;
+ }
+
+ .note-suggestion-description {
display: block;
font-size: small;
color: var(--muted-text-color);
}
+
+ .note-suggestion-attributes {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 4px;
+ margin-block: 2px;
+
+ .ext-badge {
+ --color: var(--muted-text-color);
+ }
+ }
}
/*
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
index 47f0bd875c..f72709c99c 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx
@@ -123,7 +123,7 @@ describe("NoteAutocomplete's suggestion list", () => {
const notes: Suggestion[] = [
{ notePath: "root/a", noteTitle: "Alpha", notePathTitle: "Alpha", highlightedNotePathTitle: "<b>Al</b>pha", icon: "bx bx-file" },
{ notePath: "root/x/b", noteTitle: "Beta", notePathTitle: "X / Beta", highlightedNotePathTitle: "X / Beta",
- highlightedAttributeSnippet: "#tag" }
+ highlightedAttributeSnippet: "#tag<br>#status=\"<b>al</b>\"" }
];
const commands: Suggestion[] = [
{ action: "command", commandId: "cmd1", noteTitle: "Cmd One", highlightedNotePathTitle: "Cmd One",
@@ -203,7 +203,10 @@ describe("NoteAutocomplete's suggestion list", () => {
expect(alpha.querySelector(".tn-icon")?.className).toBe("bx bx-file tn-icon");
expect(alpha.querySelector(".search-result-title")?.innerHTML).toBe("<b>Al</b>pha");
expect(beta.querySelector(".tn-icon")?.className).toBe("bx bx-note tn-icon");
- expect(beta.querySelector(".search-result-attributes")?.textContent).toBe("#tag");
+ // One outline badge per attribute, its highlight kept.
+ const badges = beta.querySelectorAll(".note-suggestion-attributes > .ext-badge.outline");
+ expect([ ...badges ].map((badge) => badge.textContent)).toEqual([ "#tag", "#status=\"al\"" ]);
+ expect(badges[1].querySelector("b")?.textContent).toBe("al");
expect(input.getAttribute("aria-expanded")).toBe("true");
expect(input.getAttribute("aria-activedescendant")).toBe(alpha.id);
});
@@ -445,7 +448,7 @@ describe("NoteAutocomplete's suggestion list", () => {
const beta = menu?.querySelectorAll<HTMLElement>(".dropdown-item")[1];
expect(beta?.querySelector(".tn-icon")?.className).toBe("bx bx-note tn-icon");
expect(beta?.querySelector(".search-result-title")?.innerHTML).toBe("X / Beta");
- expect(beta?.querySelector(".search-result-attributes")?.textContent).toBe("#tag");
+ expect(beta?.querySelectorAll(".note-suggestion-attributes > .ext-badge")).toHaveLength(2);
await act(async () => {
input.focus();
@@ -480,11 +483,11 @@ describe("NoteAutocomplete's suggestion list", () => {
const [ described, bare ] = host.querySelectorAll<HTMLElement>(".note-suggestion-list > .dropdown-item");
expect(described.querySelector(".tn-icon")?.className).toBe("bx bx-cog tn-icon");
expect(described.querySelector(".search-result-title")?.textContent).toBe("Cmd One");
- expect(described.querySelector(".search-result-attributes")?.textContent).toBe("Does a thing");
+ expect(described.querySelector(".note-suggestion-description")?.textContent).toBe("Does a thing");
// One key each, drawn as a search row's are.
expect(described.querySelectorAll(".note-suggestion-shortcut > kbd")).toHaveLength(2);
expect(bare.querySelector(".tn-icon")?.className).toBe("bx bx-terminal tn-icon");
- expect(bare.querySelector(".search-result-attributes, kbd")).toBeNull();
+ expect(bare.querySelector(".note-suggestion-description, kbd")).toBeNull();
host.remove();
// Elsewhere a `>` is only text to search for.
diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsx
index 801b315669..275094d2de 100644
--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx
+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx
@@ -12,6 +12,7 @@ import froca from "../../services/froca";
import { t } from "../../services/i18n";
import { createSearchScheduler, createNoteFromSuggestion, getCommandSuggestions, getNoteSuggestions, type Options, type Suggestion } from "../../services/note_autocomplete";
import { escapeHtml, isMobile } from "../../services/utils";
+import { AttributeSnippetBadges } from "./Badge";
import { useAutocomplete } from "./FormAutocomplete";
import { FormDropdownDivider } from "./FormList";
import { useSyncedRef } from "./hooks";
@@ -412,14 +413,20 @@ function NoteSuggestionMenuItem({ suggestion }: { suggestion: Suggestion }) {
function NoteSuggestionMenuItemContent({ suggestion }: { suggestion: Suggestion }) {
const isCommand = suggestion.action === "command";
const icon = isCommand ? (suggestion.icon || "bx bx-terminal") : suggestionIcon(suggestion);
- const description = isCommand ? suggestion.commandDescription : suggestion.highlightedAttributeSnippet;
return <>
<Icon icon={icon} />
<span className="tn-menu-gap" />
<div className="note-suggestion-text">
<RawHtml className="search-result-title" html={suggestion.highlightedNotePathTitle ?? ""} />
- {description && <RawHtml className="search-result-attributes" html={description} />}
+ {isCommand
+ ? suggestion.commandDescription && (
+ <span className="note-suggestion-description">{suggestion.commandDescription}</span>
+ )
+ : <AttributeSnippetBadges
+ snippet={suggestion.highlightedAttributeSnippet}
+ className="note-suggestion-attributes"
+ />}
</div>
<SuggestionShortcut suggestion={suggestion} />
</>;
diff --git a/apps/client/src/widgets/search_field_editor.css b/apps/client/src/widgets/search_field_editor.css
index 955143678b..4a03c429b8 100644
--- a/apps/client/src/widgets/search_field_editor.css
+++ b/apps/client/src/widgets/search_field_editor.css
@@ -63,7 +63,7 @@
opacity: 0.7;
}
- /* Matches the second line of a note row, `.search-result-attributes`. */
+ /* Matches the second line of a command row, `.note-suggestion-description`. */
.cm-completionDetail {
flex-basis: 100%;
margin-left: 0;