Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client: highlight search queries in backend log
Adorian Doran173f208626f9
diff --git a/apps/client/src/stylesheets/theme-next-dark.css b/apps/client/src/stylesheets/theme-next-dark.cssindex ae03b095d3..5d2c999823 100644--- a/apps/client/src/stylesheets/theme-next-dark.css+++ b/apps/client/src/stylesheets/theme-next-dark.css@@ -280,6 +280,8 @@--log-query-verb-color: #fa74bb;--log-query-line-background-color: #edbe670f;--log-sql-color: #f4c8ae;+ --log-search-verb-color: #3f9189;+ --log-search-count-color: #818181;--log-error-color: #FF867F;--log-error-line-background-color: #d44b4b1f;--log-info-color: inherit; /* Placeholder: give it a colour to make info lines stand out. */diff --git a/apps/client/src/stylesheets/theme-next-light.css b/apps/client/src/stylesheets/theme-next-light.cssindex 9809f865c1..9fb7cac364 100644--- a/apps/client/src/stylesheets/theme-next-light.css+++ b/apps/client/src/stylesheets/theme-next-light.css@@ -278,6 +278,8 @@--log-query-line-background-color: #edbe670b;--log-query-verb-color: #ce4f7f;--log-sql-color: #df5815;+ --log-search-verb-color: #3f9189;+ --log-search-count-color: #00000077;--log-error-color: #b32017;--log-error-line-background-color: #ff000017;--log-info-color: inherit; /* Placeholder: give it a colour to make info lines stand out. */diff --git a/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts b/packages/codemirror/src/extensions/trilium_log_highlighter.spec.tsindex 166770284e..1646b4017f 100644--- a/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts+++ b/packages/codemirror/src/extensions/trilium_log_highlighter.spec.ts@@ -107,6 +107,50 @@ describe("triliumLogHighlighter", () => {expect(classesOf(doc)).not.toContain("cm-log-sql");});+ it("highlights a search: the `Search` verb, the query as search syntax and the count", () => {+ // The quote is never closed, so the string must stop at the end of the query.+ const doc = "17:02:11.408 Search: #!archived and note.title *= 'tower (returned 12 items)";+ const spans = decorationsOf(state(doc));++ expect(sliceOf(doc, spans, "cm-log-search-verb")).toBe("Search");+ expect(slicesOf(doc, spans, "cm-search-label")).toEqual([ "#", "archived" ]);+ expect(sliceOf(doc, spans, "cm-search-negation")).toBe("!");+ expect(sliceOf(doc, spans, "cm-search-keyword")).toBe("and");+ expect(slicesOf(doc, spans, "cm-search-property")).toEqual([ "note", ".title" ]);+ expect(sliceOf(doc, spans, "cm-search-operator")).toBe("*=");+ expect(sliceOf(doc, spans, "cm-search-string")).toBe("'tower");+ expect(sliceOf(doc, spans, "cm-log-search-count")).toBe("(returned 12 items)");+ expect(lineClassesAt(state(doc), 1)).toEqual([]);+ });++ it("frames the whole query of a search in one box around its tokens", () => {+ const doc = "17:02:11.408 Search: #book and towers (returned 3 items)";+ expect(sliceOf(doc, decorationsOf(state(doc)), "cm-log-search-query")).toBe("#book and towers");++ // The border must wrap the token spans, not be cut into a box per token.+ const view = new EditorView({ state: state(doc), parent: document.body });+ try {+ const frames = view.contentDOM.querySelectorAll(".cm-log-search-query");+ expect(frames).toHaveLength(1);+ expect(frames[0].textContent).toBe("#book and towers");+ expect(frames[0].querySelector(".cm-search-label")?.textContent).toBe("#book");+ } finally {+ view.destroy();+ }++ expect(classesOf("17:02:11.408 Search: (returned 0 items)"))+ .not.toContain("cm-log-search-query");+ });++ it("does not mistake other entries that start with `Search` for searches", () => {+ for (const doc of [+ "18:00:00.000 Search note's relation author has not been found.",+ "18:00:00.000 Search: #book"+ ]) {+ expect(classesOf(doc)).toEqual([ "cm-log-timestamp" ]);+ }+ });+it("does not mistake sibling timing lines for queries", () => {// Same `Slow … took <n>ms` shape, but not a query — must stay a plain entry.for (const doc of [diff --git a/packages/codemirror/src/extensions/trilium_log_highlighter.ts b/packages/codemirror/src/extensions/trilium_log_highlighter.tsindex 45cca27db2..b9bdd411f1 100644--- a/packages/codemirror/src/extensions/trilium_log_highlighter.ts+++ b/packages/codemirror/src/extensions/trilium_log_highlighter.ts@@ -3,6 +3,8 @@ import { type EditorState, type Extension, RangeSetBuilder, StateField } from "@import { Decoration, type DecorationSet, EditorView } from "@codemirror/view";import { type Highlighter, highlightTree } from "@lezer/highlight";+import { decorateSearchQuery, searchHighlightTheme } from "./trilium_search_highlighter.js";+/*** Lightweight regex highlighter for the Trilium backend log. Each log entry is a single line* beginning with a `HH:MM:SS.mmm` timestamp. The decorations live in a {@link StateField} (rather@@ -25,6 +27,8 @@ import { type Highlighter, highlightTree } from "@lezer/highlight";* not the HTTP method's), and the statement (absent for recursive queries) highlighted with the* SQL language in the active code theme, or in one colour without a language. Unlike HTTP,* `Slow` is not flagged as a warning here: queries are only logged when slow, so it is uninformative.+ * - search lines `<ts> Search: <query> (returned <n> items)`, which follow a `GET /api/search`+ * request: the verb bolded, the query highlighted as search syntax and the count muted* - `<ts> JS Error: …` and `<ts> ERROR: …` lines — coloured red, together with their following* continuation lines (e.g. wrapped stack traces), which carry no timestamp of their own* - `<ts> JS Info: …` lines (and their continuation lines) — placeholder class, no colour yet@@ -51,6 +55,8 @@ const INFO_LINE = /^\d{2}:\d{2}:\d{2}\.\d{3} JS Info:/;// the parts that follow. Anchored on `query` so sibling timings — `Slow autocomplete took …ms`,// `Becca (note cache) load took …ms`, `Content hash computation took …ms` — are not caught.const SLOW_QUERY = /^\d{2}:\d{2}:\d{2}\.\d{3} (Slow (?:recursive )?query)( took )(\d+ms)(?:: (.+))?/;+// The query of a `GET /api/search` request, decoded, and the number of notes it found.+const SEARCH = /^\d{2}:\d{2}:\d{2}\.\d{3} (Search): (.*) {2}(\(returned \d+ items\))$/;/*** Upper bound on how many (trailing) lines are highlighted, to cap the per-rebuild work. Sized well@@ -65,6 +71,9 @@ const verbMark = Decoration.mark({ class: "cm-log-verb" });const urlMark = Decoration.mark({ class: "cm-log-url" });const queryVerbMark = Decoration.mark({ class: "cm-log-query-verb" });const sqlMark = Decoration.mark({ class: "cm-log-sql" });+const searchVerbMark = Decoration.mark({ class: "cm-log-search-verb" });+const searchQueryMark = Decoration.mark({ class: "cm-log-search-query" });+const searchCountMark = Decoration.mark({ class: "cm-log-search-count" });const httpLine = Decoration.line({ class: "cm-log-http" });const queryLine = Decoration.line({ class: "cm-log-query" });const errorLine = Decoration.line({ class: "cm-log-error" });@@ -91,6 +100,13 @@ const logHighlightTheme = EditorView.baseTheme({".cm-log-query": { backgroundColor: "var(--log-query-line-background-color, color-mix(in srgb, var(--main-text-color) 3%, transparent))" },".cm-log-query-verb": { fontWeight: "bold", color: "var(--log-query-verb-color, #bf3989)" },".cm-log-sql": { color: "var(--log-sql-color, #8957e5)" },+ ".cm-log-search-verb": { fontWeight: "bold", color: "var(--log-search-verb-color, #3f9189)" },+ ".cm-log-search-query": {+ border: "1px solid var(--log-search-query-border-color, var(--main-border-color))",+ borderRadius: "3px",+ padding: "0 2px"+ },+ ".cm-log-search-count": { color: "var(--log-search-count-color, var(--muted-text-color))" },".cm-log-status": { fontWeight: "bold" },".cm-log-status-2xx": { color: "var(--log-status-success-color, #2ea043)" },".cm-log-status-3xx": { color: "var(--log-status-redirect-color, var(--muted-text-color))" },@@ -142,7 +158,7 @@ export function triliumLogHighlighter(sqlLanguage: Language | null = null): ExteEditorView.decorations.from(f, (highlights) => highlights.sql)]});- return [ field, logHighlightTheme ];+ return [ field, logHighlightTheme, searchHighlightTheme ];}function buildHighlights(state: EditorState, sqlLanguage: Language | null): LogHighlights {@@ -226,6 +242,7 @@ function decorateLine(const httpMatch = HTTP_REQUEST.exec(text);const queryMatch = httpMatch ? null : SLOW_QUERY.exec(text);+ const searchMatch = httpMatch || queryMatch ? null : SEARCH.exec(text);const newBlockLine = httpMatch ? null : blockLineFor(text);// Line decorations must be added at the line start before any mark that begins there.if (httpMatch) {@@ -274,6 +291,21 @@ function decorateLine(builder.add(sqlStart, sqlStart + sql.length, sqlMark);}}+ } else if (searchMatch) {+ // Layout: `<12-char ts><space>Search: <query> (returned <n> items)`.+ const [ , searchVerb, query, count ] = searchMatch;+ const verbStart = lineStart + TIMESTAMP_LENGTH + 1;+ const lineEnd = lineStart + text.length;++ const queryStart = verbStart + searchVerb.length + 2; // ": "++ builder.add(verbStart, verbStart + searchVerb.length, searchVerbMark);+ // Added before the token marks that start at the same position, so it wraps them.+ if (query) {+ builder.add(queryStart, queryStart + query.length, searchQueryMark);+ }+ decorateSearchQuery(builder, query, queryStart);+ builder.add(lineEnd - count.length, lineEnd, searchCountMark);}return newBlockLine;diff --git a/packages/codemirror/src/extensions/trilium_search_highlighter.ts b/packages/codemirror/src/extensions/trilium_search_highlighter.tsindex c6f07a8d37..51e8790f98 100644--- a/packages/codemirror/src/extensions/trilium_search_highlighter.ts+++ b/packages/codemirror/src/extensions/trilium_search_highlighter.ts@@ -183,7 +183,7 @@ const NEGATION_MARK = Decoration.mark({ class: "cm-search-negation" });* The fallbacks below are what the legacy themes — which don't define those variables — render* with, so they are mid-tones that hold up on a light and a dark background alike.*/-const searchHighlightTheme = EditorView.baseTheme({+export const searchHighlightTheme = EditorView.baseTheme({".cm-search-label": { color: "var(--search-label-color, #539bf5)" },".cm-search-relation": { color: "var(--search-relation-color, #8957e5)" },".cm-search-negation": { color: "var(--search-negation-color, #e5534b)" },@@ -212,22 +212,30 @@ export const triliumSearchHighlighter: Extension = [ searchHighlightField, searcfunction buildSearchDecorations(state: EditorState): DecorationSet {const builder = new RangeSetBuilder<Decoration>();- const text = state.doc.toString();+ decorateSearchQuery(builder, state.doc.toString());+ return builder.finish();+}+/**+ * Adds the marks of the search query `text`, which starts at `offset` in the document, to+ * `builder`. The marks need {@link searchHighlightTheme} for their colors.+ */+export function decorateSearchQuery(+ builder: RangeSetBuilder<Decoration>, text: string, offset = 0+) {for (const token of tokenizeSearchQuery(text)) {- const negation = token.from + 1;+ const from = offset + token.from;+ const to = offset + token.to;- if (ATTRIBUTE_KINDS.has(token.kind) && text[negation] === "!") {- builder.add(token.from, negation, MARKS[token.kind]);- builder.add(negation, negation + 1, NEGATION_MARK);- builder.add(negation + 1, token.to, MARKS[token.kind]);+ if (ATTRIBUTE_KINDS.has(token.kind) && text[token.from + 1] === "!") {+ builder.add(from, from + 1, MARKS[token.kind]);+ builder.add(from + 1, from + 2, NEGATION_MARK);+ builder.add(from + 2, to, MARKS[token.kind]);continue;}- builder.add(token.from, token.to, MARKS[token.kind]);+ builder.add(from, to, MARKS[token.kind]);}-- return builder.finish();}/** The tokens spelled with a marker, which is the only place a `!` can negate. */