markdown within javascript #2132
-
Is there a way to make strings within javascript be evaluated using the markdown syntax? Taking from the official documentation, could I get this
to evaluate to this html?
Why is this interesting? In a lot of my cases, it would be handy to define some data using javascript and access it from various places in my mdx files. For example, I could define some data like this
and pass this to components, possible filtered, or simply use them in the mdx file. For example
or to use them directly with something like Table component from ant design
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
The most idiomatic approach in MDX would be to have two MDX files. thing.mdx # Hello {props.place} document.mdx import Thing from './thing.mdx'
<Thing place="world" /> The syntax you propose, is not something MDX core supports, nor do I think core should support it. Having a plain text strings is a valid use case, and a common one as well, for example: export const frontmatter = { key: 'value' } Trying to support string either as MDX or plain text within a document with a heuristic, would likely cause confusion and unexpected errors, rather than improving the developer experience. Instead something that clearly differentiates between plain text and mdx text could be used. (though would be less idiomatic than using two files) export const Thing = () => mdx.evaluate('# World')
<Thing /> or by using a template tag function: export const Thing = () => mdx`# World`
<Thing /> Both of which could be supported today by importing mdx into the runtime. (this would be slower as it would include the runtime client side). |
Beta Was this translation helpful? Give feedback.
-
Thank you @ChristianMurphy for this very detailed and helpful answer. I now see that I can probably accomplish all or most of what I need with the idiomatic approach you suggest. One clarifying question regarding the use of something like ant design's Table component inside mdx.
with |
Beta Was this translation helpful? Give feedback.
The most idiomatic approach in MDX would be to have two MDX files.
A component file thing.mdx (which can also include
props
) and a page/document MDX file which imports thing.mdx and uses it as a component.This approach is already supported today.
thing.mdx
document.mdx
The syntax you propose, is not something MDX core supports, nor do I think core should support it.
Not all JavaScript strings are MDX nor should they be interpreted as such.
Having a plain text strings is a valid use case, and a common one as well, for example:
Trying to support string either as MDX o…