diff --git a/CHANGELOG.md b/CHANGELOG.md index adb06c2d..077541dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p - preserve intent and disabled styling across focus and theme changes - `` - inline SVG images with `resizing="contain"` get `preserveAspectRatio="xMidYMid meet"` instead of an empty attribute +- `` + - rehype plugins are no longer accumulated in a module-level list on every render ## [26.2.1] - 2026-11-02 diff --git a/src/cmem/markdown/Markdown.test.tsx b/src/cmem/markdown/Markdown.test.tsx index fa647917..8647ecdf 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 0243add1..b0c42a47 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