Skip to content

Build your personal knowledge base with Trilium Notes

client/search notes: expand collapsible blocks to reveal search results

Adorian Doran9429daba6c7b
diff --git a/apps/client/src/widgets/find_in_html.spec.ts b/apps/client/src/services/collapsible.spec.ts
similarity index 96%
rename from apps/client/src/widgets/find_in_html.spec.ts
rename to apps/client/src/services/collapsible.spec.ts
index eb43295273..3a8f9c935d 100644
--- a/apps/client/src/widgets/find_in_html.spec.ts
+++ b/apps/client/src/services/collapsible.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { expandAncestorDetails } from "./find_in_html.js";
+import { expandAncestorDetails } from "./collapsible.js";
/** Build a DOM tree from an HTML string and return the element matching `selector`. */
function mount(html: string, selector: string): HTMLElement {
diff --git a/apps/client/src/services/collapsible.ts b/apps/client/src/services/collapsible.ts
new file mode 100644
index 0000000000..2afb230120
--- /dev/null
+++ b/apps/client/src/services/collapsible.ts
@@ -0,0 +1,10 @@
+/** Open every collapsed `<details>` ancestor of `el` so content inside it becomes visible. */
+export function expandAncestorDetails(el: HTMLElement) {
+ let details = el.closest("details");
+ while (details) {
+ if (!details.open) {
+ details.open = true;
+ }
+ details = details.parentElement?.closest("details") ?? null;
+ }
+}
diff --git a/apps/client/src/widgets/find_in_html.ts b/apps/client/src/widgets/find_in_html.ts
index 45cc21fd35..1d71e22a3b 100644
--- a/apps/client/src/widgets/find_in_html.ts
+++ b/apps/client/src/widgets/find_in_html.ts
@@ -2,6 +2,7 @@
// uses for highlighting matches, use the same one on CodeMirror
// for consistency
import type Mark from "mark.js";
+import { expandAncestorDetails } from "../services/collapsible.js";
import utils from "../services/utils.js";
import type FindWidget from "./find.js";
import type { FindResult } from "./find.js";
@@ -93,14 +94,3 @@ export default class FindInHtml {
}
}
}
-
-/** Open every collapsed `<details>` ancestor of `el` so a match inside it becomes visible. */
-export function expandAncestorDetails(el: HTMLElement) {
- let details = el.closest("details");
- while (details) {
- if (!details.open) {
- details.open = true;
- }
- details = details.parentElement?.closest("details") ?? null;
- }
-}
diff --git a/apps/client/src/widgets/react/hooks.spec.tsx b/apps/client/src/widgets/react/hooks.spec.tsx
index 13843a0bf6..e22f496171 100644
--- a/apps/client/src/widgets/react/hooks.spec.tsx
+++ b/apps/client/src/widgets/react/hooks.spec.tsx
@@ -4,7 +4,7 @@ import { useRef } from "preact/hooks";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
-import { type DelayedVisibilityPhase, useDelayedVisibility, useStaticTooltip } from "./hooks";
+import { type DelayedVisibilityPhase, useDelayedVisibility, useImperativeSearchHighlighlighting, useStaticTooltip } from "./hooks";
let currentPhase: DelayedVisibilityPhase | undefined;
@@ -126,3 +126,67 @@ describe("useStaticTooltip", () => {
expect(document.querySelector(".tooltip")).toBeNull();
});
});
+
+describe("useImperativeSearchHighlighlighting", () => {
+ let container: HTMLElement;
+ let highlight: ((el: HTMLElement | null | undefined) => void) | undefined;
+
+ function Probe({ tokens }: { tokens: string[] | null | undefined }) {
+ highlight = useImperativeSearchHighlighlighting(tokens);
+ return null;
+ }
+
+ beforeEach(() => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ highlight = undefined;
+ });
+
+ async function mount(tokens: string[] | null | undefined) {
+ await act(async () => render(<Probe tokens={tokens} />, container));
+ }
+
+ function content(html: string): HTMLElement {
+ const el = document.createElement("div");
+ el.innerHTML = html;
+ document.body.appendChild(el);
+ return el;
+ }
+
+ it("highlights matches and opens the collapsed <details> that contains them", async () => {
+ await mount([ "needle" ]);
+ const target = content("<details><summary>t</summary><p>a needle here</p></details>");
+
+ highlight?.(target);
+
+ expect(target.querySelectorAll(".ck-find-result").length).toBeGreaterThan(0);
+ expect(target.querySelector("details")?.open).toBe(true);
+ target.remove();
+ });
+
+ it("leaves a collapsed block closed when it holds no match", async () => {
+ await mount([ "needle" ]);
+ const target = content("<details><summary>t</summary><p>nothing relevant</p></details>");
+
+ highlight?.(target);
+
+ expect(target.querySelector("details")?.open).toBe(false);
+ target.remove();
+ });
+
+ it("does nothing without tokens", async () => {
+ await mount([]);
+ const target = content("<details><summary>t</summary><p>needle</p></details>");
+
+ highlight?.(target);
+
+ expect(target.querySelectorAll(".ck-find-result").length).toBe(0);
+ expect(target.querySelector("details")?.open).toBe(false);
+ target.remove();
+ });
+});
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index c0871e5286..406443ac20 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -12,6 +12,7 @@ import NoteContext, { NoteContextDataMap } from "../../components/note_context";
import FBlob from "../../entities/fblob";
import FNote from "../../entities/fnote";
import attributes from "../../services/attributes";
+import { expandAncestorDetails } from "../../services/collapsible";
import froca from "../../services/froca";
import { t } from "../../services/i18n";
import keyboard_actions from "../../services/keyboard_actions";
@@ -1259,7 +1260,12 @@ export function useImperativeSearchHighlighlighting(highlightedTokens: string[]
mark.current.unmark();
mark.current.markRegExp(highlightRegex, {
element: "span",
- className: "ck-find-result"
+ className: "ck-find-result",
+ // Reveal matches that landed inside collapsed <details> blocks — they
+ // are highlighted in the DOM but hidden until the block is expanded.
+ done: () => {
+ el.querySelectorAll<HTMLElement>(".ck-find-result").forEach(expandAncestorDetails);
+ }
});
};
}