Skip to content

Expression resulting in markdown #2276

Discussion options

You must be logged in to vote

Heya @glitteringkatie and @scottybollinger! 👋

{ 1 < 2 ? '**yes**' : '_no_' } to render "yes" but it really renders "yes".

Right, in MDX when you enter a JSX {} expression, it will treat all the inner content as JS/JSX, not as MDX until it is closed.

A few options for mixing conditionals into content.

Create conditional tags

For example importing https://github.com/sindresorhus/react-extras#if

<If condition={1 < 2}>
  **yes**
</If>
<If condition={1 >= 2}>
  _no_
</If>

Use an MDX template literal

{ 1 < 2 ? mdx`**yes**` : mdx`_no_` }

This would require some additional wrapper code around MDX, see unifiedjs/unified#40 for inspiration.

Use JSX/HTML

{ 1 < 2 ? <strong>yes<strong> : <em>no</em> }

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by glitteringkatie
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants