-
Root problem: I want to write MDX content which has occasional conditional branches, like (pseudocode) ~~ if props.foo ~~
## Foo is true!
So you can rely on foo.
~~ else ~~
## There's no foo here!
So don't even think about using it.
~~ endif ~~
Anyhoo, let's talk about some other non foo related stuff Logically, I'd want something like this
...but that doesn't work of course, you get "## Foo is true! So you can rely on foo." without MDX interpretation. Now, I do know various ways to do this with MDX/JSX, like (untested) {props.foo
? <><h2>Foo is true!</h2> <p>So you can rely on foo.</p></>
: <><h2>There's no foo here!</h2> <p>So don't even think about using it.</p></>} But now within the conditional block we no longer have the benefit of MDX which is super sad. A different alternative is to make several MDX components, like
...but that leads to a proliferation of little files. Or, I could do this:
But that seems like I'm re-inventing an if-statement which must be wrong?? Also it's not great because both branches are processed regardless of the condition, which can lead to errors. And this seems like a very basic thing one might want to do in content, so I imagine I'm just missing some idiom here?? Other references
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
Hi! Right, so these are your options: a) write as JSX, b) move things to a component, c) some sort of That seems like enough options to me. For c):
Everything is “processed”. Not entirely sure what you mean by that word. This approach c) is the only one that would be a good fit for not processing: if there are static conditions, then you can write a plugin to drop the non-matching branch. Anyway, yeah, https://github.com/orgs/mdx-js/discussions/2276 holds up |
Beta Was this translation helpful? Give feedback.
-
Is there a standard plugin that implements
Sorry I was imprecise, I meant something closer to "evaluated" I think. What I mean is that if I do something like this...
...then if
One can write code to dodge this but it's clunky, and "if thing is available, use thing, otherwise don't" is an extraordinarily common pattern. Anyway @wooorm I politely disagree that the first two solutions (break out components, or use JSX) are acceptable for all use cases -- there are places where one really wants to preserve the flow of content while adding a conditional, but dropping to JSX is jarring and obnoxious -- but that's obviously subjective. Plugins are the usual answer for "I think this is important but you don't" so that's why I'm exploring this route... |
Beta Was this translation helpful? Give feedback.
-
Ok, to deal with the various issues (lack of plugin points, mainly) described above, I ended up having MDX emit JS with JSX tags still in it, and having my bundler (rollup) do the JSX-to-JS conversion. That way I can write the I ended up writing this to do the translation: https://github.com/egnor/rollup-plugin-jsx-if-for |
Beta Was this translation helpful? Give feedback.
Hi!
Right, so these are your options: a) write as JSX, b) move things to a component, c) some sort of
<if>
.That seems like enough options to me.
The problem with a) you describe, namely loosing markdown, is solved by b)
The problem with b) you describe, namely little files, is solved by a) — if they are so little, you can write a couple tags
For c):
Everything is “processed”. Not entirely sure what you mean by that word. This approach c) is the only one that would be a good fit for not processing: if there are static conditions, then you can write a plugin to drop the non-matching branch.
Anyway, yeah, https://github.com/orgs/mdx…