Skip to content

Build your personal knowledge base with Trilium Notes

fix(highlights): stop listing a formatted run twice when it wraps a colour

Elian Doran777416bbcb55
diff --git a/apps/client/src/widgets/sidebar/highlights_extract.spec.ts b/apps/client/src/widgets/sidebar/highlights_extract.spec.ts
index f6415a71f2..d63546af25 100644
--- a/apps/client/src/widgets/sidebar/highlights_extract.spec.ts
+++ b/apps/client/src/widgets/sidebar/highlights_extract.spec.ts
@@ -49,4 +49,57 @@ describe("extractHighlightsFromStaticHtml", () => {
document.body.removeChild(container);
});
+
+ describe("formatting nested with a coloured run", () => {
+ function extract(html: string) {
+ const container = document.createElement("div");
+ container.innerHTML = html;
+ document.body.appendChild(container);
+
+ try {
+ return extractHighlightsFromStaticHtml(container);
+ } finally {
+ document.body.removeChild(container);
+ }
+ }
+
+ // Both nestings render identically, so both must report the run once rather than
+ // once per element. Only the first shape used to double up.
+ it("reports a coloured run inside formatting once, carrying both", () => {
+ const highlights = extract(`<p><strong><span style="color:red">text</span></strong></p>`);
+
+ expect(highlights.length).toBe(1);
+ expect(highlights[0].text).toBe("text");
+ expect(highlights[0].attrs).toMatchObject({ bold: true, color: "red" });
+ });
+
+ it("reports formatting inside a coloured run once", () => {
+ const highlights = extract(`<p><span style="color:red"><strong>text</strong></span></p>`);
+
+ expect(highlights.length).toBe(1);
+ expect(highlights[0].text).toBe("<strong>text</strong>");
+ expect(highlights[0].attrs).toMatchObject({ color: "red" });
+ });
+
+ // `**==hl==**` in a Markdown note renders as exactly this.
+ it("reports a bold highlight once", () => {
+ const highlights = extract(`<p><strong><span style="background-color:hsl(60, 75%, 60%)">hl</span></strong></p>`);
+
+ expect(highlights.length).toBe(1);
+ expect(highlights[0].attrs).toMatchObject({ bold: true, background: "hsl(60, 75%, 60%)" });
+ });
+
+ it("keeps formatting that only partly covers a coloured run", () => {
+ // The bold "a … c" is a run of its own — dropping it as a duplicate would lose it.
+ const highlights = extract(`<p><strong>a <span style="color:red">b</span> c</strong></p>`);
+
+ expect(highlights.map(h => h.text)).toEqual([ "b", `a <span style="color:red">b</span> c` ]);
+ });
+
+ it("keeps sibling formatting alongside a coloured run", () => {
+ const highlights = extract(`<p><span style="color:red">a</span> <strong>b</strong></p>`);
+
+ expect(highlights.map(h => h.text)).toEqual([ "a", "b" ]);
+ });
+ });
});
diff --git a/apps/client/src/widgets/sidebar/highlights_extract.ts b/apps/client/src/widgets/sidebar/highlights_extract.ts
index 212954ac0a..61dd7750e5 100644
--- a/apps/client/src/widgets/sidebar/highlights_extract.ts
+++ b/apps/client/src/widgets/sidebar/highlights_extract.ts
@@ -65,10 +65,9 @@ export function extractHighlightsFromStaticHtml(el: HTMLElement | null) {
const formattingElements = el.querySelectorAll<HTMLElement>("strong, em, u, b, i");
for (const formattedEl of formattingElements) {
- // Skip if already processed or inside a processed element
if (processedElements.has(formattedEl)) continue;
- if (Array.from(processedElements).some(processed => processed.contains(formattedEl))) continue;
if (!formattedEl.textContent?.trim()) continue;
+ if (isAlreadyReported(formattedEl, processedElements)) continue;
const attrs: RawHighlight["attrs"] = {
bold: formattedEl.matches("strong, b"),
@@ -92,3 +91,24 @@ export function extractHighlightsFromStaticHtml(el: HTMLElement | null) {
return highlights;
}
+
+/**
+ * Whether a run already recorded by the colour pass covers this element, making it a repeat.
+ *
+ * Either the element sits inside one, or one covers the whole of its text — the shape a
+ * coloured run inside formatting takes, since `**==hl==**` renders as
+ * `<strong><span style="background-color:…">hl</span></strong>`. The span is recorded first and
+ * already reports the bold (the colour pass resolves formatting with `closest`), so listing the
+ * `<strong>` too would repeat the same text, and drop the colour in the repeat.
+ *
+ * Formatting that only *partly* overlaps a recorded run is kept: in
+ * `<strong>a <span style="color:red">b</span> c</strong>` the bold "a … c" is a run of its own
+ * that nothing else reports.
+ */
+function isAlreadyReported(element: HTMLElement, processedElements: Set<Element>): boolean {
+ const text = element.textContent?.trim();
+
+ return Array.from(processedElements).some((processed) =>
+ processed.contains(element)
+ || (element.contains(processed) && processed.textContent?.trim() === text));
+}