Skip to content

Fix MarkdownHooks reapplying URL transforms and filters on rerender - #947

Open
Motaghi wants to merge 4 commits into
remarkjs:mainfrom
Motaghi:fix/react-markdown-hooks-post
Open

Motaghi wants to merge 4 commits into
remarkjs:mainfrom
Motaghi:fix/react-markdown-hooks-post

Conversation

@Motaghi

@Motaghi Motaghi commented Nov 14, 2026

Copy link
Copy Markdown

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and discussions and couldn’t find anything or linked relevant results below
  • I made sure the docs are up to date
  • I included tests (or that’s not needed)

Description of changes

This continues OskarEichler's report and implementation in #944, particularly commits 13576306, a6e6651c, fa82e404 and 36cbbac7. The lib/index.js change is essentially his. This adds eight regression tests and a readme note; his PR closed before the later commits received review.

MarkdownHooks keeps a processed tree in state, but post mutates it on every render. A transform such as url => '/proxy' + url accumulates prefixes, and removing a filter cannot restore nodes already removed from the tree. StrictMode also exposes the repeated transform on the initial render.

The URL and element visitor moves into an internal rehype plugin after the user's rehype plugins. The processor memo includes all six URL and filter props. The conflicting allowedElements / disallowedElements check stays in post, after the deprecation checks and before JSX conversion. With invalid props, URL or element callbacks can now run before post throws.

Changing those processing props reruns the pipeline, including async plugins. Functions and arrays need stable references to avoid processing on every parent render; the readme note covers these inputs alongside plugins and remarkRehypeOptions.

All eight new regression cases fail against the original source and pass with the fix. The recorded npm test run passes with 99 tests and 100% runtime and type coverage.

AI assistance: Codex (gpt-6.1-sol) prepared the patch, ran the checks and drafted this description, and Claude Opus 5.5 reviewed the change independently. I've read and verified the diff myself.
Co-authored-by: Oskar Eichler 62393985+OskarEichler@users.noreply.github.com

@github-actions github-actions Bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Nov 14, 2026
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (f9ca66e) to head (41f5f8d).

Additional details and impacted files
@@            Coverage Diff             @@
##              main      #947    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files            3         3            
  Lines         1778      1941   +163     
  Branches       119       127     +8     
==========================================
+ Hits          1778      1941   +163     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@remcohaszing remcohaszing left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have just one comment, but otherwise this looks good. Thanks!

Comment thread readme.md Outdated
Comment on lines +232 to +236
Pass plugins, `remarkRehypeOptions`, `allowedElements`, `disallowedElements`,
`allowElement`, and `urlTransform` as stable references,
for example by defining them outside your component.
A new value on each render processes the markdown again.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fairly basic React knowledge. It also applies only to client components, not to server components. It’s not specific to react-markdown, so I don’t think we need to mention it. It’s also solved by the React compiler, which makes it even more situational.

Suggested change
Pass plugins, `remarkRehypeOptions`, `allowedElements`, `disallowedElements`,
`allowElement`, and `urlTransform` as stable references,
for example by defining them outside your component.
A new value on each render processes the markdown again.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed, thanks.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤞 phase/open Post is being triaged manually

Development

Successfully merging this pull request may close these issues.

2 participants