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
client: fix an issue
Adorian Dorand84c016ddce3
diff --git a/apps/client/src/services/content_renderer_text.spec.ts b/apps/client/src/services/content_renderer_text.spec.tsindex 979999b53f..d986be7931 100644--- a/apps/client/src/services/content_renderer_text.spec.ts+++ b/apps/client/src/services/content_renderer_text.spec.ts@@ -311,13 +311,19 @@ describe("Text content renderer", () => {expect(tableWidth(excerpt)).toBe("800px");});- it("marks the list items whose first paragraph is aligned", async () => {- const full = document.createElement("div");- await renderTextContent(buildNote({ title: "Lists" }),- "<ul><li><p style=\"text-align:center;\">a</p></li><li>b</li></ul>", $(full));-- expect([ ...full.querySelectorAll("li") ].map((item) => item.className))- .toEqual([ "trilium-list-item-aligned-center", "" ]);+ it("marks the list items whose first paragraph is aligned, in text and Markdown", async () => {+ const list = "<ul><li><p style=\"text-align:center;\">a</p></li><li>b</li></ul>";+ const classes = (root: HTMLElement) =>+ [ ...root.querySelectorAll("li") ].map((item) => item.className);++ const text = document.createElement("div");+ await renderTextContent(buildNote({ title: "Lists" }), list, $(text));+ expect(classes(text)).toEqual([ "trilium-list-item-aligned-center", "" ]);++ // The Markdown renderers run only `postProcessRichContent()` on their HTML.+ const $markdown = $("<div>").append($("<div class=\"ck-content\">").html(list));+ await postProcessRichContent(buildNote({ title: "Markdown" }), $markdown);+ expect(classes($markdown[0])).toEqual([ "trilium-list-item-aligned-center", "" ]);});it("renders nothing for an empty note with no children when noChildrenList is set", async () => {diff --git a/apps/client/src/services/content_renderer_text.ts b/apps/client/src/services/content_renderer_text.tsindex 44198e5fb8..0d93244450 100644--- a/apps/client/src/services/content_renderer_text.ts+++ b/apps/client/src/services/content_renderer_text.ts@@ -58,7 +58,6 @@ export async function renderTextContent(} else if (hasContent) {addContentDisposer($content[0], applyTableScroll($content[0]));addContentDisposer($content[0], applyStickyTableHeaders($content[0]));- applyListItemAlignment($content[0]);// Runs before `postProcessRichContent()` renders embeds, so their blocks are not searched.if (options.highlightBlock) {highlightBlockReference($content[0], options.highlightBlock);@@ -72,14 +71,15 @@ export async function renderTextContent(/*** Apply the post-render passes that make CKEditor-compatible HTML fully- * interactive: expand `<figure class="include-note">`, render inline math and- * Mermaid diagrams, rewrite reference-link titles, and highlight code blocks.- * Assumes the caller has already appended the HTML inside a `.ck-content` child- * of `$renderedContent`.+ * interactive: mark aligned list items, expand `<figure class="include-note">`,+ * render inline math and Mermaid diagrams, rewrite reference-link titles, and+ * highlight code blocks. Assumes the caller has already appended the HTML inside+ * a `.ck-content` child of `$renderedContent`.*/export async function postProcessRichContent(note: FNote | FAttachment, $renderedContent: JQuery<HTMLElement>, options: RenderOptions = {}) {const seenNoteIds = options.seenNoteIds ?? new Set<string>();seenNoteIds.add(getEmbedKey("noteId" in note ? note.noteId : note.attachmentId, options.block));+ applyListItemAlignment($renderedContent[0]);// Runs before the embeds render, as each embed draws its own diagrams.await rewriteMermaidDiagramsInContainer($renderedContent[0] as HTMLDivElement);await applyInlineMermaid($renderedContent[0] as HTMLDivElement);diff --git a/apps/client/src/widgets/dialogs/revisions.spec.tsx b/apps/client/src/widgets/dialogs/revisions.spec.tsxindex 364433eccf..04acfe194b 100644--- a/apps/client/src/widgets/dialogs/revisions.spec.tsx+++ b/apps/client/src/widgets/dialogs/revisions.spec.tsx@@ -9,7 +9,7 @@ import { describe, expect, it, vi } from "vitest";import { buildNote } from "../../test/easy-froca";import { renderInto } from "../../test/render";-import { RevisionContentText, seedLinkPreviewTitles } from "./revisions";+import { RevisionContentDiff, RevisionContentText, seedLinkPreviewTitles } from "./revisions";vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));@@ -100,6 +100,23 @@ describe("RevisionContentText", () => {});});+describe("RevisionContentDiff", () => {+ it("marks the list items whose first paragraph is aligned", async () => {+ const list = "<ul><li><p style=\"text-align:center;\">a</p></li><li>b</li></ul>";+ const container = renderInto(<RevisionContentDiff+ noteContent={`<p>before</p>${list}`}+ itemContent={`<p>after</p>${list}`}+ itemType="text"+ />);++ await vi.waitFor(() => {+ const items = container.querySelectorAll(".ck-content li");+ expect([ ...items ].map((item) => item.className))+ .toEqual([ "trilium-list-item-aligned-center", "" ]);+ });+ });+});+describe("seedLinkPreviewTitles", () => {const mention = '<span class="link-mention" data-url="https://example.com/post" data-title="A post"></span>';diff --git a/apps/client/src/widgets/dialogs/revisions.tsx b/apps/client/src/widgets/dialogs/revisions.tsxindex 1843b6da22..2ab2a1b360 100644--- a/apps/client/src/widgets/dialogs/revisions.tsx+++ b/apps/client/src/widgets/dialogs/revisions.tsx@@ -8,7 +8,7 @@ import clsx from "clsx";import { diffWords } from "diff";import HtmlDiff from "htmldiff-js";import { type CSSProperties, Fragment } from "preact";-import { Dispatch, StateUpdater, useEffect, useRef, useState } from "preact/hooks";+import { Dispatch, StateUpdater, useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";import appContext from "../../components/app_context";import FNote from "../../entities/fnote";@@ -707,11 +707,18 @@ export function RevisionContentText({ content, noteId }: {return <RawHtmlBlock containerRef={contentRef} className="ck-content" html={sanitizeNoteContentHtml(content as string)} />;}-function RevisionContentDiff({ noteContent, itemContent, itemType }: {+export function RevisionContentDiff({ noteContent, itemContent, itemType }: {noteContent?: string,itemContent: string | Uint8Array | undefined,itemType: string}) {+ const contentRef = useRef<HTMLDivElement>(null);+ useLayoutEffect(() => {+ if (contentRef.current) {+ applyListItemAlignment(contentRef.current);+ }+ }, [ noteContent, itemContent, itemType ]);+if (typeof itemContent !== "string") {return (<div className="revision-diff-content">@@ -743,6 +750,7 @@ function RevisionContentDiff({ noteContent, itemContent, itemType }: {}return <SanitizedHtml+ containerRef={contentRef}className={clsx("revision-diff-content", itemType === "text" ? "ck-content" : "revision-diff-code")}html={diffHtml}/>;diff --git a/apps/client/src/widgets/react/RawHtml.tsx b/apps/client/src/widgets/react/RawHtml.tsxindex ddae8e3f16..3e1462d517 100644--- a/apps/client/src/widgets/react/RawHtml.tsx+++ b/apps/client/src/widgets/react/RawHtml.tsx@@ -69,9 +69,15 @@ export function HighlightedText({ className, text, highlightedTokens }: {* Use this instead of {@link RawHtml} when the HTML originates from* untrusted sources (e.g. LLM responses, user-generated markdown).*/-export function SanitizedHtml({ className, html, style }: { className?: string; html?: HTMLElementLike; style?: CSSProperties }) {+export function SanitizedHtml({ className, html, style, containerRef }: {+ className?: string;+ html?: HTMLElementLike;+ style?: CSSProperties;+ containerRef?: RefObject<HTMLDivElement | null>;+}) {return (<div+ ref={containerRef}className={className}style={style}// eslint-disable-next-line react/no-danger