mdx-js / mdx

Markdown for the component era

Home Page:https://mdxjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

remark-mdx crashes on "<"

leo-petrucci opened this issue · comments

Initial checklist

Affected packages and versions

remark-mdx@2.3.0

Link to runnable example

https://codesandbox.io/s/remark-mdx-issue-196smb?file=/src/index.js

Steps to reproduce

I'm trying to parse MDX from an editor, but I've encountered an issue whenever I try to type the character "<". The MDX parser thinks it's the start of a component and crashes the entire thing.

  • Try to parse a string that ends in "<"
  • Parsing fails with:
Unexpected end of file before name, expected a character that can start a name, such as a letter, `$`, or `_`

Expected behavior

I think the "<" should ideally be escaped like what happens when it's not placed at the end of the file:

1 < 2 -> 1 /< 2

Actual behavior

The parser throws an error and crashes.

Runtime

Node v16

Package manager

yarn v3

OS

macOS

Build and bundle tools

webpack

commented

This is very much intentional.
Have you tried typing a < somewhere in a javascript file? It crashes. Why should mdx behave differently?

(And I believe the example you show in expected behavior works fine: < plus a space is allowed)

@wooorm That's fair enough.

Do you have any advice if I was trying to parse a larger Markdown string that might end with a "<"? Any way for remark-mdx to ignore that single character?

commented

The advise is explained in the article on “What is MDX?” and Troubleshooting. Write MDX, which means in this case: handle <. By escaping it: \<.