From cfdb9dabe895256990cce827a2c5577b13c4ff4f Mon Sep 17 00:00:00 2001 From: Luke Parker <10430890+Hona@users.noreply.github.com> Date: Fri, 30 Oct 2026 20:17:09 +1000 Subject: [PATCH] fix(session-ui): allocate less per streamed markdown delta (#60549) --- .../src/components/markdown-cache.tsx | 20 +++---- .../src/components/markdown-solid.test.tsx | 21 ++++---- .../src/components/markdown-solid.tsx | 52 ++++++++----------- .../session-ui/src/components/markdown.tsx | 4 +- 4 files changed, 47 insertions(+), 50 deletions(-) diff --git a/packages/session-ui/src/components/markdown-cache.tsx b/packages/session-ui/src/components/markdown-cache.tsx index 7928eed6341..271fc7123c0 100644 --- a/packages/session-ui/src/components/markdown-cache.tsx +++ b/packages/session-ui/src/components/markdown-cache.tsx @@ -24,16 +24,16 @@ const pending = new Map< // A private instance keeps this hook set separate from other DOMPurify users. const purifier = typeof window !== "undefined" ? DOMPurify(window) : DOMPurify -const config = { - USE_PROFILES: { html: true, mathMl: true }, - SANITIZE_NAMED_PROPS: true, - FORBID_TAGS: ["style"], - FORBID_CONTENTS: ["style", "script"], - ADD_TAGS: ["svg", "path"], - ADD_ATTR: ["d", "viewBox", "preserveAspectRatio", "xmlns", "target"], -} - if (typeof window !== "undefined" && purifier.isSupported) { + // A config passed to sanitize() rebuilds every allow-list on each call; set once, every call reuses them. + purifier.setConfig({ + USE_PROFILES: { html: true, mathMl: true }, + SANITIZE_NAMED_PROPS: true, + FORBID_TAGS: ["style"], + FORBID_CONTENTS: ["style", "script"], + ADD_TAGS: ["svg", "path"], + ADD_ATTR: ["d", "viewBox", "preserveAspectRatio", "xmlns", "target"], + }) purifier.addHook("beforeSanitizeAttributes", (node) => { if (node instanceof HTMLAnchorElement) { // Local file links never navigate the document; the host decides how to open them. @@ -76,7 +76,7 @@ if (typeof window !== "undefined" && purifier.isSupported) { export function sanitizeMarkdown(html: string) { if (!purifier.isSupported) return "" - return purifier.sanitize(html, config) + return purifier.sanitize(html) } export function getCachedMarkdown(key: string) { diff --git a/packages/session-ui/src/components/markdown-solid.test.tsx b/packages/session-ui/src/components/markdown-solid.test.tsx index 0711b79e7a8..bc96353af1f 100644 --- a/packages/session-ui/src/components/markdown-solid.test.tsx +++ b/packages/session-ui/src/components/markdown-solid.test.tsx @@ -6,8 +6,15 @@ beforeAll(() => GlobalRegistrator.register()) afterAll(() => GlobalRegistrator.unregister()) +function parse(html: string, words: boolean, animate?: boolean) { + const template = document.createElement("template") + template.innerHTML = html + + return parseMarkdownNodes(template.content, words, animate) +} + test("assigns stable paths to elements and individual words", () => { - expect(parseMarkdownNodes('
Hello streaming world
', true)).toEqual([ + expect(parse('Hello streaming world
', true)).toEqual([ { key: "0", type: "element", @@ -31,7 +38,7 @@ test("assigns stable paths to elements and individual words", () => { }) test("keeps completed block text compact", () => { - expect(parseMarkdownNodes("Hello world
", false)).toEqual([ + expect(parse("Hello world
", false)).toEqual([ { key: "0", type: "element", @@ -43,7 +50,7 @@ test("keeps completed block text compact", () => { }) test("marks streamed inline code for animation", () => { - expect(parseMarkdownNodes("file.ts:29-43
file.ts:29-43
Hello
", true).flatMap((node) => (node.type === "element" ? node.children : [node]))[0], - ).toEqual({ + expect(parse("Hello
", true).flatMap((node) => (node.type === "element" ? node.children : [node]))[0]).toEqual({ key: "0.0:0", type: "word", text: "Hello", }) expect( - parseMarkdownNodes("Hello
", true, true).flatMap((node) => - node.type === "element" ? node.children : [node], - )[0], + parse("Hello
", true, true).flatMap((node) => (node.type === "element" ? node.children : [node]))[0], ).toEqual({ key: "0.0:0", type: "word", diff --git a/packages/session-ui/src/components/markdown-solid.tsx b/packages/session-ui/src/components/markdown-solid.tsx index a11b2e29773..36c8907e2d3 100644 --- a/packages/session-ui/src/components/markdown-solid.tsx +++ b/packages/session-ui/src/components/markdown-solid.tsx @@ -14,8 +14,8 @@ type MarkdownNode = | { key: string; type: "text"; text: string } | { key: string; type: "word"; text: string; animate?: true } -export function createMarkdownRenderer(root: HTMLDivElement, html: string, words: boolean) { - const [nodes, setNodes] = createStore(parseMarkdownNodes(html, words)) +export function createMarkdownRenderer(root: HTMLDivElement, source: ParentNode, words: boolean) { + const [nodes, setNodes] = createStore(parseMarkdownNodes(source, words)) let ready = false const dispose = render( @@ -26,7 +26,7 @@ export function createMarkdownRenderer(root: HTMLDivElement, html: string, words ready = true return { - update(next: string, nextWords: boolean, animate = true) { + update(next: ParentNode, nextWords: boolean, animate = true) { setNodes(reconcile(parseMarkdownNodes(next, nextWords, animate), { key: "key" })) }, dispose, @@ -63,11 +63,8 @@ function MarkdownDomNode(props: { node: MarkdownNode; animate: () => boolean }) ) } -export function parseMarkdownNodes(html: string, words: boolean, animate = false) { - const template = document.createElement("template") - template.innerHTML = html - - return Array.from(template.content.childNodes).flatMap((node, index) => parseNode(node, `${index}`, words, animate)) +export function parseMarkdownNodes(source: ParentNode, words: boolean, animate = false) { + return Array.from(source.childNodes).flatMap((node, index) => parseNode(node, `${index}`, words, animate)) } function parseNode(node: Node, key: string, words: boolean, animate: boolean, inlineCode = false): MarkdownNode[] { @@ -78,31 +75,28 @@ function parseNode(node: Node, key: string, words: boolean, animate: boolean, in if (!text) return [] if (/^\s+$/.test(text)) return [{ key: `${key}:${index}`, type: "text", text }] + const word: Extract