Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): suggest the MIME types the notes carry after note.mime

Elian Doran72b05051ad2b
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.ts
index 84d6508221..eb3d0278a4 100644
--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts
@@ -239,18 +239,38 @@ describe("searchCompletionAt", () => {
expect(await titlesOf(complete("note.isProtected ="))).toContain("=*");
expect(await titlesOf(complete("note.isArchived = t"))).toEqual([ "true", "false" ]);
- const mimes = complete("note.mime = pyt");
+ expect(server.get).not.toHaveBeenCalled();
+ });
+
+ it("offers the MIME types the notes carry, in the order the server counts them", async () => {
+ vi.mocked(server.get).mockResolvedValue([
+ { mime: "application/pdf", count: 12 },
+ { mime: "text/x-python", count: 3 },
+ { mime: "text/mermaid", count: 2 },
+ { mime: "image/png", count: 1 }
+ ]);
- expect((await entryFor(mimes, "text/x-python"))?.description).toBe("Python");
+ const mimes = complete("note.mime = ");
+
+ expect(await titlesOf(mimes)).toEqual([ "application/pdf", "text/x-python", "text/mermaid", "image/png" ]);
+ expect(server.get).toHaveBeenCalledWith("search/note-mimes");
+ // A code language is named and drawn as the language dropdown draws it, the rest as the
+ // note tree draws a note of that type.
+ expect(await entryFor(mimes, "text/x-python")).toMatchObject({ description: "Python", icon: "bx bxl-python" });
+ expect((await entryFor(mimes, "application/pdf"))?.icon).toBe("bx bxs-file-pdf");
+ expect((await entryFor(mimes, "image/png"))?.icon).toBe("bx bx-image");
+ expect((await entryFor(mimes, "text/mermaid"))?.icon).toBe("bx bx-selection");
// The lexer splits a bare `text/x-python` at the dash, so the value is inserted quoted.
expect((await entryFor(mimes, "text/x-python"))?.insert).toBe("\"text/x-python\"");
+ expect((await entryFor(complete("note.mime = 'app"), "application/pdf"))?.insert).toBe("application/pdf'");
- expect(server.get).not.toHaveBeenCalled();
+ vi.mocked(server.get).mockRejectedValue(new Error("offline"));
+ expect(await entriesOf(complete("note.mime = "))).toEqual([]);
});
it("follows the property through a traversal and a relation, and leaves the rest alone", async () => {
expect(await titlesOf(complete("note.parents.type = co"))).toContain("code");
- expect(await titlesOf(complete("note.type = code and note.mime = pyt"))).toContain("text/x-python");
+ expect(complete("note.type = code and note.mime = pyt")).toMatchObject({ key: "property:mime" });
expect(await titlesOf(complete("~author.type = co"))).toContain("code");
// A property whose values nothing can enumerate falls through to the keywords, which
diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.ts
index 7412503231..6cd7c8d82a 100644
--- a/apps/client/src/widgets/ribbon/search_completions.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.ts
@@ -1,4 +1,7 @@
-import { ALLOWED_NOTE_TYPES, allowedSearchOperators, MIME_TYPES_DICT, NOTE_TYPE_ICONS, SEARCH_NOTE_PATH } from "@triliumnext/commons";
+import {
+ ALLOWED_NOTE_TYPES, allowedSearchOperators, getMimeIcon, type MermaidMimeType, MIME_TYPES_DICT,
+ NOTE_TYPE_ICONS, type NoteMimeCount, SEARCH_NOTE_PATH
+} from "@triliumnext/commons";
import { t } from "../../services/i18n";
import server from "../../services/server";
@@ -72,7 +75,7 @@ export function searchCompletionAt(before: string, explicit: boolean): SearchCom
const property = propertyValueBeingTyped(context);
if (property) {
- return entries(context, `property:${property.property}`, context.pos - property.typed.length, () => property.entries);
+ return entries(context, `property:${property.property}`, context.pos - property.typed.length, property.entries);
}
// An ordering names a key and sorts on it, comparing nothing.
@@ -192,11 +195,19 @@ function propertyValueBeingTyped(context: Context) {
}
const [ , property, tail ] = match;
- const values = PROPERTY_VALUES.get(property.toLowerCase());
- const value = values && valueBeingTyped(tail);
+ const key = property.toLowerCase();
+ const fetched = FETCHED_PROPERTY_VALUES.get(key);
+ const values = PROPERTY_VALUES.get(key);
+ const value = (fetched || values) && valueBeingTyped(tail);
if (!value) {
return null;
}
+ if (fetched) {
+ return { ...value, property: key, entries: () => fetched(value.quote) };
+ }
+ if (!values) {
+ return null;
+ }
// A quote the user opened is a value they are spelling out, so the ones that mean anything
// only bare are dropped. For a date property that leaves nothing to offer.
@@ -210,7 +221,7 @@ function propertyValueBeingTyped(context: Context) {
insert: verbatim ? label : applyValue(label, value.quote)
}));
- return offered.length ? { ...value, property: property.toLowerCase(), entries: offered } : null;
+ return offered.length ? { ...value, property: key, entries: () => offered } : null;
}
/**
@@ -235,12 +246,11 @@ function valueBeingTyped(tail: string) {
/**
* The values each enumerable property holds, keyed lower-case as `PROP_MAPPING` is because the
- * lexer lowercases the query. The lists are the ones the note row and the code-note MIME dropdown
- * already use, so a type or a MIME added there is offered here without further work.
+ * lexer lowercases the query. The note types are the list the note row already uses, so a type added
+ * there is offered here as well.
*/
const PROPERTY_VALUES = new Map<string, () => PropertyValue[]>([
[ "type", () => ALLOWED_NOTE_TYPES.map((noteType) => ({ label: noteType, icon: noteTypeIcon(noteType) })) ],
- [ "mime", () => MIME_TYPES_DICT.map(({ mime, title }) => ({ label: mime, detail: title, icon: "bx bx-code-alt" })) ],
[ "isprotected", booleanValues ],
[ "isarchived", booleanValues ],
[ "datecreated", dateValues ],
@@ -249,6 +259,14 @@ const PROPERTY_VALUES = new Map<string, () => PropertyValue[]>([
[ "utcdatemodified", dateValues ]
]);
+/**
+ * The values of the properties that are looked up in the database, keyed as `PROPERTY_VALUES` is.
+ * A lookup runs when the list opens, not on each keystroke.
+ */
+const FETCHED_PROPERTY_VALUES = new Map<string, (quote: string) => Promise<SearchEntry[]>>([
+ [ "mime", mimeValues ]
+]);
+
interface PropertyValue {
label: string;
detail?: string;
@@ -261,6 +279,37 @@ function noteTypeIcon(noteType: string) {
return (NOTE_TYPE_ICONS as Record<string, string>)[noteType] ?? "bx bx-note";
}
+/**
+ * The MIME types the user's notes carry, the most used first. A code language is shown with its
+ * name and icon from `MIME_TYPES_DICT`, any other type with the icon `getMimeIcon()` gives it.
+ */
+async function mimeValues(quote: string): Promise<SearchEntry[]> {
+ let mimes: NoteMimeCount[];
+ try {
+ mimes = await server.get<NoteMimeCount[]>("search/note-mimes");
+ } catch {
+ // A failed lookup offers no values, and the query is typed as usual.
+ return [];
+ }
+
+ return mimes.map(({ mime }) => {
+ const language = MIME_TYPES_DICT.find((definition) => definition.mime === mime);
+ return {
+ id: mime,
+ title: mime,
+ description: language?.title,
+ icon: language ? (language.icon ?? NOTE_TYPE_ICONS.code) : mimeIcon(mime),
+ insert: applyValue(mime, quote)
+ };
+ });
+}
+
+function mimeIcon(mime: string) {
+ return MERMAID_MIMES.has(mime) ? NOTE_TYPE_ICONS.mermaid : getMimeIcon(mime);
+}
+
+const MERMAID_MIMES: ReadonlySet<string> = new Set<MermaidMimeType>([ "text/mermaid", "text/vnd.mermaid" ]);
+
function booleanValues(): PropertyValue[] {
return [ { label: "true", icon: "bx bx-toggle-right" }, { label: "false", icon: "bx bx-toggle-left" } ];
}
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index c242bd3d03..d2cfeea321 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -866,6 +866,13 @@ export interface TemplatesResponse {
newTemplateNoteIds: string[];
}
+/** A media type the notes carry, as the search autocomplete offers it after `note.mime`. */
+export interface NoteMimeCount {
+ mime: string;
+ /** How many notes carry it, system notes left out. */
+ count: number;
+}
+
export interface CloneResponse {
success: boolean;
message?: string;
diff --git a/packages/trilium-core/src/routes/api/search.spec.ts b/packages/trilium-core/src/routes/api/search.spec.ts
index 73d228e6a9..e8e3a81dc6 100644
--- a/packages/trilium-core/src/routes/api/search.spec.ts
+++ b/packages/trilium-core/src/routes/api/search.spec.ts
@@ -1,6 +1,7 @@
-import { dayjs, type SearchResultDetailsResponse, type SearchWithTokensResponse, type TemplatesResponse } from "@triliumnext/commons";
+import { dayjs, type NoteMimeCount, type SearchResultDetailsResponse, type SearchWithTokensResponse, type TemplatesResponse } from "@triliumnext/commons";
import { beforeAll, describe, expect, it } from "vitest";
+import becca from "../../becca/becca.js";
import { createTextNote } from "../../test/api_fixtures";
import { CoreApiTester } from "../../test/api_tester";
import { isNewTemplate } from "./search";
@@ -344,6 +345,35 @@ describe("Search API (core)", () => {
expect(detail?.contentSnippet?.startsWith("padding words padding")).toBe(false);
});
});
+
+ it("counts the MIME types of the user's notes, the most used first", async () => {
+ const createCodeNote = (parentNoteId: string, mime: string) =>
+ api.post(`/api/notes/${parentNoteId}/children?target=into`, {
+ body: { title: "MIME", type: "code", mime, content: "" }
+ });
+ await createCodeNote("root", "text/x-zz-once");
+ await createCodeNote("root", "text/x-zz-twice");
+ await createCodeNote("root", "text/x-zz-twice");
+ // `_share` lies in the hidden subtree, but what the user puts there is still theirs.
+ expect((await createCodeNote("_share", "text/x-zz-shared")).status).toBe(200);
+
+ const res = await api.get<NoteMimeCount[]>("/api/search/note-mimes");
+
+ expect(res.status).toBe(200);
+ const mimes = res.body.map(({ mime }) => mime);
+ expect(res.body.find(({ mime }) => mime === "text/x-zz-twice")?.count).toBe(2);
+ expect(res.body.find(({ mime }) => mime === "text/x-zz-once")?.count).toBe(1);
+ expect(mimes.indexOf("text/x-zz-twice")).toBeLessThan(mimes.indexOf("text/x-zz-once"));
+ expect(mimes).toContain("text/x-zz-shared");
+ expect(mimes).not.toContain("");
+
+ // System notes carry MIME types too, but only the user's notes are counted.
+ const notes = Object.values(becca.notes);
+ expect(notes.some(({ noteId, mime }) => noteId.startsWith("_") && mime)).toBe(true);
+ for (const { mime, count } of res.body) {
+ expect(count).toBe(notes.filter((note) => !note.noteId.startsWith("_") && note.mime === mime).length);
+ }
+ });
});
describe("isNewTemplate", () => {
diff --git a/packages/trilium-core/src/routes/api/search.ts b/packages/trilium-core/src/routes/api/search.ts
index 161081180c..14d014c306 100644
--- a/packages/trilium-core/src/routes/api/search.ts
+++ b/packages/trilium-core/src/routes/api/search.ts
@@ -1,5 +1,5 @@
import {
- dayjs, type QuickSearchResponse, type SearchLintRequest, type SearchLintResponse,
+ dayjs, type NoteMimeCount, type QuickSearchResponse, type SearchLintRequest, type SearchLintResponse,
type SearchResultDetails, type SearchResultDetailsRequest, type SearchResultDetailsResponse,
type SearchWithTokensResponse, type TemplatesResponse
} from "@triliumnext/commons";
@@ -277,7 +277,26 @@ export function isNewTemplate(utcDateCreated: string | null, rootCreationDate: s
return dayjs.utc().diff(creationDate, "day", true) <= NEW_TEMPLATE_MAX_AGE;
}
+/**
+ * The MIME types the user's notes carry, with how many carry each, the most used first. System
+ * notes, whose IDs start with `_`, are left out so the built-in ones do not outnumber the user's.
+ */
+function getNoteMimes(): NoteMimeCount[] {
+ const counts = new Map<string, number>();
+ for (const note of Object.values(becca.notes)) {
+ if (!note.mime || note.noteId.startsWith("_")) {
+ continue;
+ }
+ counts.set(note.mime, (counts.get(note.mime) ?? 0) + 1);
+ }
+
+ return [ ...counts ]
+ .map(([ mime, count ]) => ({ mime, count }))
+ .sort((a, b) => b.count - a.count || a.mime.localeCompare(b.mime));
+}
+
export default {
+ getNoteMimes,
lintSearchString,
searchFromNote,
getSearchResultDetails,
diff --git a/packages/trilium-core/src/routes/index.ts b/packages/trilium-core/src/routes/index.ts
index bec719d4e8..ad68a475a6 100644
--- a/packages/trilium-core/src/routes/index.ts
+++ b/packages/trilium-core/src/routes/index.ts
@@ -244,6 +244,7 @@ export function buildSharedApiRoutes({ route, asyncRoute, asyncRouteWithoutTrans
apiRoute(PST, "/api/search-and-execute-note/:noteId", searchRoute.searchAndExecute);
apiRoute(PST, "/api/search-related", searchRoute.getRelatedNotes);
apiRoute(PST, "/api/search/lint", searchRoute.lintSearchString);
+ apiRoute(GET, "/api/search/note-mimes", searchRoute.getNoteMimes);
apiRoute(GET, "/api/search", searchRoute.search);
apiRoute(GET, "/api/search-templates", searchRoute.searchTemplates);