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(backlinks): quote a paragraph with several links to the note once
Elian Doran7cd12e6556ca
diff --git a/packages/trilium-core/src/services/backlink_excerpts.spec.ts b/packages/trilium-core/src/services/backlink_excerpts.spec.tsindex 36dd5074c0..137ada3d86 100644--- a/packages/trilium-core/src/services/backlink_excerpts.spec.ts+++ b/packages/trilium-core/src/services/backlink_excerpts.spec.ts@@ -1,12 +1,44 @@import { describe, expect, it } from "vitest";import { buildNote } from "../test/becca_easy_mocking";-import { findLlmChatExcerpts, findMindMapExcerpts } from "./backlink_excerpts";+import { findExcerpts, findLlmChatExcerpts, findMindMapExcerpts } from "./backlink_excerpts";function chatContent(messages: unknown[]) {return JSON.stringify({ messages });}+describe("findExcerpts", () => {+ it("quotes a paragraph holding several links to the note once, without nesting", () => {+ const link = `<a class="reference-link" href="#root/excMany">Many</a>`;+ const html = `<p>first ${link} and ${link} and ${link}</p><p>second ${link}</p>`;++ const excerpts = findExcerpts(html, "excMany");++ expect(excerpts).toEqual([+ `<div class="ck-content backlink-excerpt"><p>first ` ++ `<a class="reference-link backlink-link" href="#root/excMany">Many</a> and ` ++ `<a class="reference-link backlink-link" href="#root/excMany">Many</a> and ` ++ `<a class="reference-link backlink-link" href="#root/excMany">Many</a></p>` ++ `<p>second <a class="reference-link backlink-link" href="#root/excMany">Many</a></p></div>`+ ]);+ });++ it("quotes separate paragraphs separately and truncates a neighbor from its start", () => {+ const filler = "x".repeat(150);+ const html = `<p>${filler} <a href="#root/excSplit">one</a></p>` ++ `<p>start of the next ${"y".repeat(300)} end</p>` ++ `<p>${filler} <a href="#root/excSplit">two</a></p>`;++ const excerpts = findExcerpts(html, "excSplit");++ expect(excerpts).toHaveLength(2);+ expect(excerpts[0]).toContain(`<a href="#root/excSplit" class="backlink-link">one</a>`);+ expect(excerpts[0]).toContain("start of the next");+ expect(excerpts[0]).not.toContain(" end…");+ expect(excerpts[1]).toContain(`<a href="#root/excSplit" class="backlink-link">two</a>`);+ });+});+describe("findLlmChatExcerpts", () => {it("quotes the assistant prose around the wiki-link, links titled and text escaped", () => {buildNote({ id: "excTarget", title: "Chat target" });@@ -56,9 +88,9 @@ describe("findLlmChatExcerpts", () => {}]), "excMissing");- expect(excerpts).toHaveLength(2);+ expect(excerpts).toHaveLength(1);expect(excerpts[0]).toContain(">my name</a>");- expect(excerpts[1]).toContain(">excMissing</a>");+ expect(excerpts[0]).toContain(">excMissing</a>");});it("truncates long neighboring paragraphs with ellipses, one excerpt per mention", () => {diff --git a/packages/trilium-core/src/services/backlink_excerpts.ts b/packages/trilium-core/src/services/backlink_excerpts.tsindex 0a3f22e05d..4c0478d837 100644--- a/packages/trilium-core/src/services/backlink_excerpts.ts+++ b/packages/trilium-core/src/services/backlink_excerpts.ts@@ -30,15 +30,19 @@ export function findExcerpts(html: string, referencedNoteId: string): string[] {removeImages(document);- for (const linkEl of document.querySelectorAll("a")) {- const href = linkEl.getAttribute("href");+ const linkEls = document.querySelectorAll("a").filter((el) => el.getAttribute("href")?.endsWith(referencedNoteId));+ for (const linkEl of linkEls) {+ linkEl.classList.add("backlink-link");+ }++ // A link inside an already quoted element is covered by that excerpt.+ const quotedEls = new Set<ElementOrText>();- if (!href || !href.endsWith(referencedNoteId)) {+ for (const linkEl of linkEls) {+ if (isInside(linkEl, quotedEls)) {continue;}- linkEl.classList.add("backlink-link");-let centerEl: HTMLElement = linkEl;while (centerEl.tagName !== "BODY" && centerEl.parentNode && (centerEl.parentNode?.textContent?.length || 0) <= EXCERPT_CHAR_LIMIT) {@@ -79,7 +83,7 @@ export function findExcerpts(html: string, referencedNoteId: string): string[] {const nextText = next.textContent;if (nextText && nextText.length + excerptLength > EXCERPT_CHAR_LIMIT) {- const suffix = nextText.substr(nextText.length - (EXCERPT_CHAR_LIMIT - excerptLength));+ const suffix = nextText.substring(0, EXCERPT_CHAR_LIMIT - excerptLength);const textNode = new TextNode(`${suffix}…`);excerptEls.push(textNode);@@ -98,19 +102,26 @@ export function findExcerpts(html: string, referencedNoteId: string): string[] {}}- const excerptWrapper = new HTMLElement("div", {});- excerptWrapper.classList.add("ck-content");- excerptWrapper.classList.add("backlink-excerpt");-+ // Serialized rather than appended to a wrapper: `appendChild()` moves a node out of+ // `document`, and the next link needs its neighbors in place.for (const childEl of excerptEls) {- excerptWrapper.appendChild(childEl);+ quotedEls.add(childEl);}- excerpts.push(excerptWrapper.outerHTML);+ excerpts.push(`<div class="ck-content backlink-excerpt">${excerptEls.map(String).join("")}</div>`);}return excerpts;}+function isInside(node: ElementOrText, ancestors: Set<ElementOrText>) {+ for (let current: HTMLElement | null = node.parentNode; current; current = current.parentNode) {+ if (ancestors.has(current)) {+ return true;+ }+ }+ return ancestors.has(node);+}+function removeImages(document: HTMLElement) {const images = document.getElementsByTagName("img");for (const image of images) {