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(revisions): render link previews in note revisions (closes #10707)
Elian Dorande8ec17dad94
diff --git a/apps/client/src/widgets/dialogs/revisions.spec.tsx b/apps/client/src/widgets/dialogs/revisions.spec.tsxnew file mode 100644index 0000000000..4c96d08afe--- /dev/null+++ b/apps/client/src/widgets/dialogs/revisions.spec.tsx@@ -0,0 +1,74 @@+/**+ * A revision is stored as CKEditor's data downcast output, which is not always self-contained: the+ * constructs whose visible content the note view builds at render time arrive here as empty+ * 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 { describe, expect, it, vi } from "vitest";++import { buildNote } from "../../test/easy-froca";+import { renderInto } from "../../test/render";+import { RevisionContentText } from "./revisions";++vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));++/**+ * Every fixture starts with a paragraph that is not the subject of its assertion: DOMPurify running+ * on happy-dom unwraps the outermost element of the fragment it is given (`<p>x</p>` sanitizes to+ * `x`), which real browsers do not do, so the element under test must not be the first one.+ */++/** Renders revision HTML and lets the reference-link lookups (froca, then the icon) settle. */+async function renderRevision(content: string) {+ const container = renderInto(<RevisionContentText content={content} />);+ await vi.waitFor(() => expect(container.querySelector(".ck-content")).toBeTruthy());+ return container;+}++describe("RevisionContentText", () => {+ it("renders an inline link preview that is stored as an empty element", async () => {+ const container = await renderRevision(+ '<p>before</p><p><span class="link-mention" data-url="https://github.com/TriliumNext/Trilium/issues"'+ + ' data-title="Issues · TriliumNext/Trilium"></span></p><p>after</p>'+ );++ const mention = await vi.waitFor(() => {+ const el = container.querySelector<HTMLAnchorElement>("span.link-mention a.link-embed-mention");+ expect(el).toBeTruthy();+ return el;+ });+ expect(mention?.textContent).toBe("Issues · TriliumNext/Trilium");+ expect(mention?.getAttribute("href")).toBe("https://github.com/TriliumNext/Trilium/issues");+ });++ it("renders a card link preview that is stored as an empty element", async () => {+ const container = await renderRevision(+ '<p>before</p><section class="link-embed" data-url="https://example.com/post" data-title="A post"'+ + ' data-site-name="Example"></section>'+ );++ await vi.waitFor(() => {+ expect(container.querySelector("section.link-embed")?.textContent).toContain("A post");+ });+ });++ it("resolves reference link titles against the current note tree", async () => {+ const note = buildNote({ title: "Target note" });+ const container = await renderRevision(+ `<p><a class="reference-link" href="#root/${note.noteId}">Stale title</a></p>`+ );++ // The stored title is whatever it was when the revision was taken; the note view replaces it+ // with the note's own, wrapped in the <span> the theme styles.+ await vi.waitFor(() => {+ const link = container.querySelector("a.reference-link > span");+ expect(link?.textContent).toContain("Target note");+ });+ });++ it("leaves a preview without a URL alone rather than rendering an empty card", async () => {+ const container = await renderRevision('<p>before</p><p><span class="link-mention" data-title="No URL"></span></p>');++ expect(container.querySelector("a.link-embed-mention")).toBeNull();+ });+});diff --git a/apps/client/src/widgets/dialogs/revisions.tsx b/apps/client/src/widgets/dialogs/revisions.tsxindex 80feba96a2..9d680632e1 100644--- a/apps/client/src/widgets/dialogs/revisions.tsx+++ b/apps/client/src/widgets/dialogs/revisions.tsx@@ -13,6 +13,7 @@ import FNote from "../../entities/fnote";import dialog from "../../services/dialog";import froca from "../../services/froca";import { t } from "../../services/i18n";+import { applyLinkEmbeds } from "../../services/link_embed";import open from "../../services/open";import options from "../../services/options";import protected_session_holder from "../../services/protected_session_holder";@@ -31,6 +32,7 @@ import Modal from "../react/Modal";import NoItems from "../react/NoItems";import { RawHtmlBlock, SanitizedHtml } from "../react/RawHtml";import PdfViewer from "../type_widgets/file/PdfViewer";+import { applyReferenceLinks } from "../type_widgets/text/read_only_helper";const DIFFABLE_TYPES = ["text", "code", "mermaid"];@@ -669,8 +671,22 @@ function RevisionContent({ noteContent, revisionItem, fullRevision, showDiff }:}}-function RevisionContentText({ content }: { content: string | Uint8Array | undefined }) {+export function RevisionContentText({ content }: { content: string | Uint8Array | undefined }) {const contentRef = useRef<HTMLDivElement>(null);++ // A revision stores what CKEditor's data downcast produced, and two of those constructs carry+ // no visible content of their own: a link preview is an empty `<span class="link-mention">` /+ // `<section class="link-embed">` holding only its metadata as data attributes, and a reference+ // link stores a bare title that the note view turns into an icon-and-colour span. Without these+ // passes a link preview is an empty gap in the revision (#10707).+ useEffect(() => {+ const container = contentRef.current;+ if (!container) return;++ applyLinkEmbeds(container);+ void applyReferenceLinks(container);+ }, [content]);+useEffect(() => {if (contentRef.current?.querySelector("span.math-tex")) {// KaTeX is heavy, so the math service is only loaded when there are formulas to render.