Don't replace literal HTML elements with MDXProvider
See original GitHub issueRight now when you provide a component mapping to an MDXProvider
, every matching markdown element is replaced, even if those elements are authored in HTML instead of markdown.
For example, given this configuration:
// App.js
import { MDXProvider } from '@mdx-js/react'
const components = {
h2: () => (<span>Boo!</span>),
}
export default props =>
<MDXProvider components={components}>
<div {...props} />
</MDXProvider>
…the following MDX:
// Page.mdx
# My Page
## Should become "Boo!"
<h2>But please leave this one alone</h2>
…is rendered as:
<h1>My Page</h1>
<span>Boo!</span>
<span>Boo!</span>
This can be very surprising and challenging to work with when you are embedding components in your MDX that receive children:
// Page.mdx
import MyComponent from '../components/MyComponent'
# My Page
## Should become "Boo!"
<MyComponent>
<h2>Please leave this alone!</h2>
</MyComponent>
I would love a way to be able to tell MDX “please only transform markdown tags, not literal HTML tags” so that literal HTML can be used as an escape hatch.
Hope this makes sense, and thanks for the awesome work on MDX!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:12 (7 by maintainers)
Top Results From Across the Web
Using MDX
This article explains how to use MDX files in your project. It shows how you can pass props and how to import, define,...
Read more >Working With MDX Custom Elements and Shortcodes
Replace default HTML elements rendered from Markdown with custom components; Use shortcodes to get rid of us of importing components in every ...
Read more >Create a Next.js and MDX blog - LogRocket Blog
Learn how to build a blog in Next.js using MDX to allow for the creation of interactive content, along with adding styles and...
Read more >How do I type html in a markdown file without it rendering?
You can escape the < characters by replacing them with < , which is the HTML escape sequence for < . You're sentence...
Read more >Styling themes
I don't think this necessarily requires us to predict the future but it's also ... needs without having to define components or tuck...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Going to close this now since it’s implemented in the canary release of v2. You can take it for a spin with
yarn add @mdx-js/mdx@next @mdx-js/react@next
.And then use it like so:
Additionally, you can create a component if you wanted to “sandbox” elements:
Thanks for the input all!
Hey thanks for following up on this with me! The specific use case was writing documentation with inline code samples (specifically I was working on a redesign of the Tailwind CSS docs), and I wanted to bake styles into my headings, paragraphs, etc., but didn’t want those styles leaking into the examples:
Think stuff like this screenshot, where there I don’t want my markdown styles interfering with my demo styles:
Historically I have solved this with a bunch of convoluted CSS that uses direct descendant selectors and stuff to make sure the markdown styles never leak down into the examples, but was excited about being able to ditch that with MDX.