Skip to content

Build your personal knowledge base with Trilium Notes

fix(revisions): show link previews in the highlighted diff

Elian Doran563d355f6b59
diff --git a/apps/client/src/widgets/dialogs/revisions.spec.tsx b/apps/client/src/widgets/dialogs/revisions.spec.tsx
index 4c96d08afe..03e76e1b1d 100644
--- a/apps/client/src/widgets/dialogs/revisions.spec.tsx
+++ b/apps/client/src/widgets/dialogs/revisions.spec.tsx
@@ -4,11 +4,12 @@
* elements carrying metadata. Rendering the stored HTML on its own therefore shows less than the
* note does, and in the case of a link preview shows nothing at all (#10707).
*/
+import HtmlDiff from "htmldiff-js";
import { describe, expect, it, vi } from "vitest";
import { buildNote } from "../../test/easy-froca";
import { renderInto } from "../../test/render";
-import { RevisionContentText } from "./revisions";
+import { RevisionContentText, seedLinkPreviewTitles } from "./revisions";
vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));
@@ -72,3 +73,54 @@ describe("RevisionContentText", () => {
expect(container.querySelector("a.link-embed-mention")).toBeNull();
});
});
+
+describe("seedLinkPreviewTitles", () => {
+ const mention = '<span class="link-mention" data-url="https://example.com/post" data-title="A post"></span>';
+
+ /** The anchor the seeding put inside the preview, as the diff view would end up rendering it. */
+ function seededLink(html: string) {
+ const doc = new DOMParser().parseFromString(seedLinkPreviewTitles(html) ?? "", "text/html");
+ return doc.querySelector<HTMLAnchorElement>("a.link-embed-mention");
+ }
+
+ it("fills a preview with a link carrying the title it displays", () => {
+ const link = seededLink(`<p>x</p>${mention}`);
+ expect(link?.textContent).toBe("A post");
+ expect(link?.getAttribute("href")).toBe("https://example.com/post");
+ expect(link?.closest("span.link-mention")).toBeTruthy();
+
+ // No title was ever fetched for this one, so it is named the way the card would name it.
+ const untitled = seededLink('<section class="link-embed" data-url="https://example.com/post"></section>');
+ expect(untitled?.textContent).toBe("example.com");
+ });
+
+ it("leaves a preview naming nothing at all alone rather than adding an empty link", () => {
+ expect(seededLink('<span class="link-mention"></span>')).toBeNull();
+ });
+
+ it("passes content it has nothing to do with straight through", () => {
+ // Identity rather than a re-serialization, so an ordinary diff is untouched by this.
+ const plain = "<p>x</p><p><a href='https://example.com'>y</a></p>";
+ expect(seedLinkPreviewTitles(plain)).toBe(plain);
+ expect(seedLinkPreviewTitles(undefined)).toBeUndefined();
+ });
+
+ it("leaves a preview that already carries text alone", () => {
+ const filled = '<span class="link-mention" data-title="A post">Edited by hand</span>';
+ expect(seedLinkPreviewTitles(filled)).toContain(">Edited by hand</span>");
+ });
+
+ it("lets the differ tell an added preview from a removed one", () => {
+ const withPreview = seedLinkPreviewTitles(`<p>x</p><p>${mention}</p>`);
+ const without = seedLinkPreviewTitles("<p>x</p>");
+
+ const added = HtmlDiff.execute(without, withPreview);
+ const removed = HtmlDiff.execute(withPreview, without);
+
+ expect(added).toContain("diffins");
+ expect(removed).toContain("diffdel");
+ // Unseeded, htmldiff-js emits these two byte-identical: the empty element carries no word
+ // for it to compare, so neither direction produces a marker at all.
+ expect(added).not.toBe(removed);
+ });
+});
diff --git a/apps/client/src/widgets/dialogs/revisions.tsx b/apps/client/src/widgets/dialogs/revisions.tsx
index 9d680632e1..549df3502a 100644
--- a/apps/client/src/widgets/dialogs/revisions.tsx
+++ b/apps/client/src/widgets/dialogs/revisions.tsx
@@ -1,6 +1,6 @@
import "./revisions.css";
-import { dayjs, type RevisionItem, type RevisionPojo } from "@triliumnext/commons";
+import { dayjs, type RevisionItem, type RevisionPojo, safeHostname, safeLinkPreviewHref } from "@triliumnext/commons";
import clsx from "clsx";
import { diffWords } from "diff";
import HtmlDiff from "htmldiff-js";
@@ -718,7 +718,7 @@ function RevisionContentDiff({ noteContent, itemContent, itemType }: {
let diffHtml: string;
if (itemType === "text") {
// Use proper HTML-aware diff for rich text content
- diffHtml = HtmlDiff.execute(noteContent, itemContent);
+ diffHtml = HtmlDiff.execute(seedLinkPreviewTitles(noteContent), seedLinkPreviewTitles(itemContent));
} else {
// Use word diff for code/mermaid (plain text)
const diff = diffWords(noteContent ?? "", itemContent);
@@ -743,6 +743,61 @@ function RevisionContentDiff({ noteContent, itemContent, itemType }: {
/>;
}
+/**
+ * Fills every link preview with a real link carrying the title it displays, so that the differ can
+ * see it and the reader gets something they recognise as a link.
+ *
+ * htmldiff-js diffs words, and a link preview holds none: it is stored as an empty
+ * `<span class="link-mention">` / `<section class="link-embed">` carrying its metadata in data
+ * attributes, which only the note view turns into a card. Left alone the differ emits the element
+ * unmarked whether it was added or removed, so a revision that gained a link and one that lost it
+ * come out identical (#10707).
+ *
+ * What is built here is the anchor a preview without a favicon renders as anyway — same classes,
+ * same href — rather than the whole card: the differ marks up the text it compares, and rendering
+ * the card over the top of that would wipe the markup back out. So a changed link reads as a
+ * changed link, and an unchanged one still looks like the note it came from.
+ *
+ * Parsed with DOMParser rather than through an element's innerHTML: the document it builds is
+ * inert, so nothing a revision happens to contain loads or runs on the way past.
+ */
+export function seedLinkPreviewTitles(html: string | undefined) {
+ // Both sides of the diff are seeded, so the parse-and-serialize round trip would be symmetric
+ // and harmless — but skipping it leaves a diff of notes without previews exactly as it was.
+ if (!html || (!html.includes("link-mention") && !html.includes("link-embed"))) {
+ return html;
+ }
+
+ const doc = new DOMParser().parseFromString(html, "text/html");
+ for (const preview of doc.querySelectorAll<HTMLElement>("span.link-mention, section.link-embed")) {
+ if (preview.textContent?.trim()) {
+ continue;
+ }
+
+ const { title, url } = preview.dataset;
+ // Same fallback the preview itself uses, so the diff names the link the way the note does.
+ const text = title || (url ? safeHostname(url) : "");
+ if (!text) {
+ continue;
+ }
+
+ const anchor = doc.createElement("a");
+ anchor.className = "link-embed-mention";
+ anchor.href = safeLinkPreviewHref(url);
+ anchor.target = "_blank";
+ anchor.rel = "noopener noreferrer";
+
+ const titleEl = doc.createElement("span");
+ titleEl.className = "link-embed-mention-title";
+ titleEl.textContent = text;
+
+ anchor.append(titleEl);
+ preview.replaceChildren(anchor);
+ }
+
+ return doc.body.innerHTML;
+}
+
function FilePreview({ revisionItem, fullRevision }: { revisionItem: RevisionItem, fullRevision: RevisionPojo }) {
return (