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
fix(highlights): stop a run at the start of a block listing the whole block
Elian Doranb4adeb47eadc
diff --git a/apps/client/src/widgets/sidebar/HighlightsList.tsx b/apps/client/src/widgets/sidebar/HighlightsList.tsxindex d068cfed3d..a7a79aa74b 100644--- a/apps/client/src/widgets/sidebar/HighlightsList.tsx+++ b/apps/client/src/widgets/sidebar/HighlightsList.tsx@@ -8,7 +8,7 @@ import { FormListToggleableItem } from "../react/FormList";import { useActiveNoteContext, useContentElement, useGetContextData, useIsNoteReadOnly, useMathRendering, useNoteProperty, useTextEditor, useTriliumOptionJson } from "../react/hooks";import RawHtml from "../react/RawHtml";import { HIGHLIGHT_FORMATS, HighlightFormat } from "../type_widgets/options/highlights_list_options";-import { extractHighlightsFromStaticHtml, type DomHighlight, type RawHighlight } from "./highlights_extract";+import { extractHighlightsFromStaticHtml, htmlForRun, type DomHighlight, type RawHighlight } from "./highlights_extract";import RightPanelWidget from "./RightPanelWidget";import SidebarHelp from "./SidebarHelp";@@ -249,15 +249,14 @@ function extractHighlightsFromTextEditor(editor: CKTextEditor) {};if (Object.values(attrs).some(Boolean)) {- // Get HTML content from DOM (includes nested elements like math)+ // Take the run's markup from the DOM, so nested content survives into the list.let html = item.data;try {const modelPos = editor.model.createPositionAt(item.textNode, "before");const viewPos = editor.editing.mapper.toViewPosition(modelPos);const domPos = editor.editing.view.domConverter.viewPositionToDom(viewPos);if (domPos?.parent instanceof HTMLElement) {- // Get the formatting span's innerHTML (includes math elements)- html = domPos.parent.innerHTML;+ html = htmlForRun(domPos.parent, item.data);}} catch {// During change:data events, the view may not be fully synchronized with the model.diff --git a/apps/client/src/widgets/sidebar/highlights_extract.spec.ts b/apps/client/src/widgets/sidebar/highlights_extract.spec.tsindex d63546af25..b8980c9d4e 100644--- a/apps/client/src/widgets/sidebar/highlights_extract.spec.ts+++ b/apps/client/src/widgets/sidebar/highlights_extract.spec.ts@@ -1,5 +1,5 @@import { describe, expect, it } from "vitest";-import { extractHighlightsFromStaticHtml } from "./highlights_extract.js";+import { extractHighlightsFromStaticHtml, htmlForRun } from "./highlights_extract.js";describe("extractHighlightsFromStaticHtml", () => {it("extracts a single highlight containing text and math equation together", () => {@@ -103,3 +103,32 @@ describe("extractHighlightsFromStaticHtml", () => {});});});++describe("htmlForRun", () => {+ function element(html: string) {+ const el = document.createElement("div");+ el.innerHTML = html;+ return el;+ }++ it("keeps the markup when the element holds the run and nothing else", () => {+ // What a formatting element wrapping the run looks like — nested content has to survive.+ const el = element(`Mass <span class="math-tex">\\(e=mc^2\\)</span>`);++ expect(htmlForRun(el, "Mass \\(e=mc^2\\)")).toBe(`Mass <span class="math-tex">\\(e=mc^2\\)</span>`);+ });++ it("falls back to the run's text when the element holds more than the run", () => {+ // The mapper lands on the block for a run at its start, so the element here is the whole+ // paragraph; taking its markup would list every word as a single highlight.+ const paragraph = element(`<span style="color:red">One</span> Two Three`);++ expect(htmlForRun(paragraph, "One")).toBe("One");+ });++ it("ignores whitespace around the run when comparing", () => {+ const el = element(`\n <b>One</b>\n`);++ expect(htmlForRun(el, "One ")).toBe(`\n <b>One</b>\n`);+ });+});diff --git a/apps/client/src/widgets/sidebar/highlights_extract.ts b/apps/client/src/widgets/sidebar/highlights_extract.tsindex 61dd7750e5..1f978072b6 100644--- a/apps/client/src/widgets/sidebar/highlights_extract.ts+++ b/apps/client/src/widgets/sidebar/highlights_extract.ts@@ -92,6 +92,21 @@ export function extractHighlightsFromStaticHtml(el: HTMLElement | null) {return highlights;}+/**+ * The rendered markup of a run, read off the element the editor's mapper landed in — but only+ * when that element holds the run and nothing besides, so nested content (a formula inside a+ * coloured run) survives into the list.+ *+ * The mapper does not reliably land on the formatting element. A run at the very start of a+ * block maps to the *block*, because `Mapper#findPositionIn` returns the container position+ * unchanged when the node after it is not a text node — so a paragraph opening with a coloured+ * word would otherwise be listed with the whole paragraph as its text. The run's own text is+ * the safe answer there.+ */+export function htmlForRun(element: HTMLElement, data: string): string {+ return element.textContent?.trim() === data.trim() ? element.innerHTML : data;+}+/*** Whether a run already recorded by the colour pass covers this element, making it a repeat.*