Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): rank a user's own attribute names above the built-in ones

Elian Doran4a8cf5b46fed
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.ts
index 444e1760bd..20008d4843 100644
--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts
@@ -92,6 +92,13 @@ describe("searchCompletionSource", () => {
expect((await complete("#!bo"))?.from).toBe(2);
});
+ it("ranks a built-in below a name of the user's own that matches as well", async () => {
+ const result = await complete("#a");
+
+ expect(optionFor(result, "archived")?.boost).toBe(-99);
+ expect(optionFor(result, "book")?.boost).toBeUndefined();
+ });
+
it("fetches relations for ~, and for the segment after note.relations.", async () => {
await complete("~aut");
diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.ts
index 0b72e00ce3..d0306e14bc 100644
--- a/apps/client/src/widgets/ribbon/search_completions.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.ts
@@ -82,10 +82,15 @@ async function attributeCompletions(type: "label" | "relation", from: number): P
return {
from,
- options: names.map((name) => ({
- label: name,
- type: isBuiltinAttribute(type, name) ? SYSTEM_ATTRIBUTE : type
- })),
+ options: names.map((name) => {
+ const isBuiltin = isBuiltinAttribute(type, name);
+
+ return {
+ label: name,
+ type: isBuiltin ? SYSTEM_ATTRIBUTE : type,
+ boost: isBuiltin ? BUILTIN_BOOST : undefined
+ };
+ }),
validFor: SEGMENT_TYPED
};
}
@@ -93,6 +98,13 @@ async function attributeCompletions(type: "label" | "relation", from: number): P
/** Stands in for a completion's own type, a name Trilium attaches a meaning to being marked first. */
const SYSTEM_ATTRIBUTE = "system-attribute";
+/**
+ * The furthest CodeMirror lets an option be moved down, which it adds to the match score. Penalties
+ * there run to the hundreds, so a built-in sinks below a name of the user's own that matches as
+ * well, while a distinctly better match keeps its place.
+ */
+const BUILTIN_BOOST = -99;
+
const COMPLETION_ICONS: Record<string, string> = {
[SYSTEM_ATTRIBUTE]: "bx bx-cog",
label: "bx bx-hash",