Meant as part of larger system [TODO].
$ npm install markdown-it markdown-it-jsx --save
var md = ;var jsx = ;md; console;
prints this JSX output
<h1>A sample document</h1><p>Two times three is <Doubler>3</Doubler></p><p>We can <em>intermix <strong>Markdown</strong> and JSX</em></p><p>The current date is </p>
which isn't quite a valid JSX expression, but will
be if you wrap it in an outer
<em> is now actually treated as JSX and not HTML, even though
it's also a standard HTML tag and would be treated as HTML by a normal
Again, the render output is not valid HTML in itself -- you'll
probably want to wrap a React component declaration around that outer
<div>, run that through Babel or TypeScript to make it runnable JS,
then use React to actually render the component to a browser
DOM. (Here, you'd need to define the
Doubler component, too.)
See example/render.js for a more complete example of writing a document and then rendering it (offline, in that case).
See [TODO] for an even bigger, in-browser, dynamic example.
I'm using parsimmon parser combinators in the JSX matcher instead of the regexes in the original HTML matcher, to scan over braced expressions for embedded JS. This is probably excessive, and a for loop with a state variable would suffice. The behavior is also not really correct (what about braces inside string literals in JS?), but good enough for now.
The render rule in index.js just passes the JSX code string straight through into the rendered output; the parser doesn't construct a coherent AST of the JSX or anything.
There's a hack for JSX blocks (like HTML blocks in standard Markdown); if we see a paragraph with JSX tags at its open and end, we remove the paragraph open and end, effectively promoting the JSX inside. Again, not really correct, and will treat some things differently from CommonMark spec, but good enough.
Similar things and classes of things:
- Andrey Popp's Reactdown was the first I saw, in its iteration from 2014. The old version was pretty similar but had a less reliable Markdown parser, I think. The new version seems to abandon the JSX syntax. Very much worth looking at, though.
- mdreact also appears similar but also abandons JSX syntax, and it looks a little unreliable.
- react-showdown can only generate the React component as a runtime object, not as JSX source; it's not a static compilation pipeline.
- rexxars/react-markdown and some others render ordinary Markdown to React components, but don't let you embed React components in the Markdown (since that would make the output a component instead of a static element tree)
- sunflowerdeath/react-markdown and some others provide a React component that renders children or props as React components. Again, can't actually embed custom tags in the Markdown.
Things I wanted from this syntax extension:
- Use arbitrary React components inside Markdown document. (This plugin alone doesn't deal with scoping and defining names used in your JSX, though. See [TODO] for one solution to that issue.)
- Use standard JSX syntax inside Markdown document.