From 623377c8b66b02249c79d80481494c216c098a8b Mon Sep 17 00:00:00 2001 From: Andreas Schultz Date: Wed, 25 Nov 2026 17:45:11 +0100 Subject: [PATCH 1/3] Fix Markdown plugin accumulation and add plain-text extraction - Build the rehype plugin list per render to prevent duplicate entries - Export markdownToText to extract visible text on one line, preserving link labels and code while omitting Markdown syntax and hidden link URLs - Add regression tests for repeated renders and Markdown-to-text conversion --- src/cmem/index.ts | 1 + src/cmem/markdown/Markdown.test.tsx | 17 ++++++++++++ src/cmem/markdown/Markdown.tsx | 19 ++++--------- src/cmem/markdown/markdownToText.test.ts | 35 ++++++++++++++++++++++++ src/cmem/markdown/markdownToText.tsx | 19 +++++++++++++ 5 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 src/cmem/markdown/markdownToText.test.ts create mode 100644 src/cmem/markdown/markdownToText.tsx diff --git a/src/cmem/index.ts b/src/cmem/index.ts index 831223438..7914ef2c8 100644 --- a/src/cmem/index.ts +++ b/src/cmem/index.ts @@ -4,4 +4,5 @@ export * from "./ContentBlobToggler"; export * from "./DateTimeDisplay/ElapsedDateTimeDisplay"; export * from "./markdown/Markdown"; export * from "./markdown/highlightSearchWords"; +export * from "./markdown/markdownToText"; export * from "./react-flow"; diff --git a/src/cmem/markdown/Markdown.test.tsx b/src/cmem/markdown/Markdown.test.tsx index fa647917c..8647ecdf2 100644 --- a/src/cmem/markdown/Markdown.test.tsx +++ b/src/cmem/markdown/Markdown.test.tsx @@ -1,11 +1,28 @@ import React from "react"; +import ReactMarkdown from "react-markdown"; import { render } from "@testing-library/react"; import { truncateMarkdownDisplay } from "../../common/utils/truncateMarkdownDisplay"; import { Markdown } from "./Markdown"; +jest.mock("react-markdown", () => { + const actual = jest.requireActual("react-markdown"); + return { ...actual, __esModule: true, default: jest.fn(actual.default) }; +}); + describe("Markdown", () => { + it("keeps the default rehype plugin count stable across renders", () => { + const getPluginCount = () => jest.mocked(ReactMarkdown).mock.lastCall?.[0].rehypePlugins?.length; + const { rerender } = render(First render); + const pluginCount = getPluginCount(); + expect(pluginCount).toBeGreaterThan(0); + + rerender(Second render); + + expect(getPluginCount()).toBe(pluginCount); + }); + it("keeps markdown links valid when cutOff is calculated from rendered link labels", () => { const linkHeavy = Array.from({ length: 20 }, (_, i) => `[click](https://example.com/${i})`).join(" "); const { container } = render({linkHeavy}); diff --git a/src/cmem/markdown/Markdown.tsx b/src/cmem/markdown/Markdown.tsx index 0243add19..b0c42a478 100644 --- a/src/cmem/markdown/Markdown.tsx +++ b/src/cmem/markdown/Markdown.tsx @@ -115,8 +115,6 @@ const configDefault = { */ // @see https://github.com/remarkjs/remark/blob/main/doc/plugins.md#list-of-plugins remarkPlugins: [remarkGfm, remarkDefinitionList] as PluggableList, - // @see https://github.com/rehypejs/rehype/blob/main/doc/plugins.md#list-of-plugins - rehypePlugins: [] as PluggableList, allowedElements: [...markdownAllowedInlineElements, ...markdownAllowedBlockElements], // remove all unwanted HTML markup unwrapDisallowed: true, @@ -138,16 +136,14 @@ export const Markdown = ({ cutOffSuffix = DEFAULT_CUTOFF_SUFFIX, ...otherProps }: MarkdownProps) => { - const configHtmlExternalLinks = { - rel: ["nofollow"], - target: linkTargetName, - }; - - configDefault.rehypePlugins = configDefault.rehypePlugins.concat([[rehypeExternalLinks, configHtmlExternalLinks]]); + const rehypePlugins: PluggableList = [ + [rehypeExternalLinks, { rel: ["nofollow"], target: linkTargetName }], + ...(allowHtml ? [rehypeRaw] : []), + ...(reHypePlugins ?? []), + ]; const configHtml = allowHtml ? { - rehypePlugins: [...configDefault.rehypePlugins].concat([rehypeRaw]), // switch from allowed list to disallowed list allowedElements: undefined, disallowedElements: ["applet", "script", "style", "link", "iframe", "form", "button"], @@ -166,6 +162,7 @@ export const Markdown = ({ const reactMarkdownProperties = { children: renderContent.trim(), + rehypePlugins, ...configDefault, ...configHtml, ...configTextOnly, @@ -195,10 +192,6 @@ export const Markdown = ({ allowedElements, }; - if (reHypePlugins) { - reactMarkdownProperties.rehypePlugins = reactMarkdownProperties.rehypePlugins.concat(reHypePlugins); - } - const markdownDisplay = ; return inheritBlock && !(otherProps["data-test-id"] || htmlContentBlockProps) ? ( markdownDisplay diff --git a/src/cmem/markdown/markdownToText.test.ts b/src/cmem/markdown/markdownToText.test.ts new file mode 100644 index 000000000..c84be3d23 --- /dev/null +++ b/src/cmem/markdown/markdownToText.test.ts @@ -0,0 +1,35 @@ +import { markdownToText } from "../../../index"; + +describe("markdownToText", () => { + it("keeps link labels and code, and decodes entities", () => { + expect( + markdownToText( + "Use **regular expressions** and [replacement rules](https://example.com/hidden). `x < y` & `a*b`.", + ), + ).toBe("Use regular expressions and replacement rules. x < y & a*b."); + }); + + it("preserves word boundaries across paragraphs, lists, line breaks, and table cells", () => { + const markdown = [ + "# Overview", + "", + "- First item", + "- Second item", + "", + "Hard break\\", + "next line", + "", + "| Name | Value |", + "| --- | --- |", + "| alpha | one |", + ].join("\n"); + + expect(markdownToText(markdown)).toBe( + "Overview First item Second item Hard break next line Name Value alpha one", + ); + }); + + it("returns an empty string for blank Markdown", () => { + expect(markdownToText(" \n\t ")).toBe(""); + }); +}); diff --git a/src/cmem/markdown/markdownToText.tsx b/src/cmem/markdown/markdownToText.tsx new file mode 100644 index 000000000..4631dc212 --- /dev/null +++ b/src/cmem/markdown/markdownToText.tsx @@ -0,0 +1,19 @@ +import React from "react"; + +import { reduceToText } from "../../common/utils/reduceToText"; + +import { Markdown } from "./Markdown"; + +/** Extracts visible Markdown text and collapses whitespace to a single line. */ +export const markdownToText = (markdown: string): string => { + if (!markdown.trim()) { + return ""; + } + + return reduceToText( + + {markdown} + , + { decodeHtmlEntities: true }, + ).replace(/\s+/g, " "); +}; From a37d2b422eb6aaa81e24b6f7781827f229b57f6a Mon Sep 17 00:00:00 2001 From: Michael Haschke Date: Tue, 1 Dec 2026 13:03:03 +0100 Subject: [PATCH 2/3] remove unused helper function --- src/cmem/index.ts | 1 - src/cmem/markdown/markdownToText.test.ts | 35 ------------------------ src/cmem/markdown/markdownToText.tsx | 19 ------------- 3 files changed, 55 deletions(-) delete mode 100644 src/cmem/markdown/markdownToText.test.ts delete mode 100644 src/cmem/markdown/markdownToText.tsx diff --git a/src/cmem/index.ts b/src/cmem/index.ts index 7914ef2c8..831223438 100644 --- a/src/cmem/index.ts +++ b/src/cmem/index.ts @@ -4,5 +4,4 @@ export * from "./ContentBlobToggler"; export * from "./DateTimeDisplay/ElapsedDateTimeDisplay"; export * from "./markdown/Markdown"; export * from "./markdown/highlightSearchWords"; -export * from "./markdown/markdownToText"; export * from "./react-flow"; diff --git a/src/cmem/markdown/markdownToText.test.ts b/src/cmem/markdown/markdownToText.test.ts deleted file mode 100644 index c84be3d23..000000000 --- a/src/cmem/markdown/markdownToText.test.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { markdownToText } from "../../../index"; - -describe("markdownToText", () => { - it("keeps link labels and code, and decodes entities", () => { - expect( - markdownToText( - "Use **regular expressions** and [replacement rules](https://example.com/hidden). `x < y` & `a*b`.", - ), - ).toBe("Use regular expressions and replacement rules. x < y & a*b."); - }); - - it("preserves word boundaries across paragraphs, lists, line breaks, and table cells", () => { - const markdown = [ - "# Overview", - "", - "- First item", - "- Second item", - "", - "Hard break\\", - "next line", - "", - "| Name | Value |", - "| --- | --- |", - "| alpha | one |", - ].join("\n"); - - expect(markdownToText(markdown)).toBe( - "Overview First item Second item Hard break next line Name Value alpha one", - ); - }); - - it("returns an empty string for blank Markdown", () => { - expect(markdownToText(" \n\t ")).toBe(""); - }); -}); diff --git a/src/cmem/markdown/markdownToText.tsx b/src/cmem/markdown/markdownToText.tsx deleted file mode 100644 index 4631dc212..000000000 --- a/src/cmem/markdown/markdownToText.tsx +++ /dev/null @@ -1,19 +0,0 @@ -import React from "react"; - -import { reduceToText } from "../../common/utils/reduceToText"; - -import { Markdown } from "./Markdown"; - -/** Extracts visible Markdown text and collapses whitespace to a single line. */ -export const markdownToText = (markdown: string): string => { - if (!markdown.trim()) { - return ""; - } - - return reduceToText( - - {markdown} - , - { decodeHtmlEntities: true }, - ).replace(/\s+/g, " "); -}; From f6c842e6e05ac7950f7ce958f19a9c4722998476 Mon Sep 17 00:00:00 2001 From: Michael Haschke Date: Tue, 1 Dec 2026 13:58:56 +0100 Subject: [PATCH 3/3] update changelog re the Markdown fix --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c782d4f5f..aa28ebb4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - preserve the editor height when disabled - apply callback, extension, and appearance changes in place, including `multiline` changes in `` - preserve intent and disabled styling across focus and theme changes +- `` + - rehype plugins are no longer accumulated in a module-level list on every render ## [26.2.1] - 2026-11-02