Skip to content

Fix Overview crash on hrefless README anchors#2671

Merged
Simek merged 1 commit into
react-native-community:mainfrom
jo-duchan:fix/markdown-hrefless-anchor-crash
Jul 20, 2026
Merged

Fix Overview crash on hrefless README anchors#2671
Simek merged 1 commit into
react-native-community:mainfrom
jo-duchan:fix/markdown-hrefless-anchor-crash

Conversation

@jo-duchan

Copy link
Copy Markdown
Contributor

Summary

Fixes #2670. The package Overview page throws a client-side exception and renders the error page when a README contains a hrefless anchor (e.g. <a name="section"></a>).

Cause

In MarkdownRenderer.tsx, the a handler starts with:

if (props.href.startsWith('#')) {

rehype-raw parses <a name="..."> into an anchor node with no href, so props.href is undefined and .startsWith() throws, which takes down the whole Overview render.

Fix

if (props.href?.startsWith('#')) {

The handler already ends with a return <span>{props.children}</span> fallback, so with optional chaining a hrefless anchor falls through to that fallback instead of crashing. One-character change, no behavior change for anchors that have an href.

Reproduction

Live example before the fix: https://reactnative.directory/package/tapflow (Overview tab crashes; Code / Versions / Score are fine). Console:

TypeError: Cannot read properties of undefined (reading 'startsWith')
  at MarkdownRenderer.tsx:90

Also reproduced the parse with a standard remark -> remark-rehype (allowDangerousHtml) -> rehype-raw pipeline: a <a name> anchor yields one <a> node without an href, which is what hits the unguarded .startsWith.

The `a` handler called `props.href.startsWith('#')` unconditionally, but
rehype-raw parses `<a name="...">` into an anchor node with no `href`, so
`props.href` was undefined and the whole Overview render threw a TypeError.

Use optional chaining so a hrefless anchor falls through to the existing
`<span>` fallback at the end of the handler instead of crashing.

Fixes react-native-community#2670

@Simek Simek 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.

Thanks for the bug report and fix! 👍

I have fixed it once in the past, but then broke this again when adding support for hash links. 😅 Need to find a way to correctly extract types for custom elements in @m2d/react-markdown/client markdown renderer, so we can avoid similar issues in the future.

@Simek
Simek merged commit 84250b6 into react-native-community:main Jul 20, 2026
1 check passed
@jo-duchan

Copy link
Copy Markdown
Contributor Author

Thanks for the quick merge! Makes sense — those custom-element types are tricky. Glad the optional-chaining fallback helps in the meantime.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Package Overview page crashes when a README contains a hrefless <a> anchor

2 participants