Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- `<Depiction />`
- inline SVG images with `resizing="contain"` get `preserveAspectRatio="xMidYMid meet"` instead of an empty attribute
- `<Markdown />`
- rehype plugins are no longer accumulated in a module-level list on every render

## [26.2.1] - 2026-11-02

Expand Down
17 changes: 17 additions & 0 deletions src/cmem/markdown/Markdown.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof import("react-markdown")>("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(<Markdown>First render</Markdown>);
const pluginCount = getPluginCount();
expect(pluginCount).toBeGreaterThan(0);

rerender(<Markdown>Second render</Markdown>);

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(<Markdown cutOff={60}>{linkHeavy}</Markdown>);
Expand Down
19 changes: 6 additions & 13 deletions src/cmem/markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"],
Expand All @@ -166,6 +162,7 @@ export const Markdown = ({

const reactMarkdownProperties = {
children: renderContent.trim(),
rehypePlugins,
...configDefault,
...configHtml,
...configTextOnly,
Expand Down Expand Up @@ -195,10 +192,6 @@ export const Markdown = ({
allowedElements,
};

if (reHypePlugins) {
reactMarkdownProperties.rehypePlugins = reactMarkdownProperties.rehypePlugins.concat(reHypePlugins);
}

const markdownDisplay = <ReactMarkdown {...reactMarkdownProperties} />;
return inheritBlock && !(otherProps["data-test-id"] || htmlContentBlockProps) ? (
markdownDisplay
Expand Down
Loading