How to allow string style prop (instead of object) #2333
-
Hello! I'm rendering third-party mdx which sometimes has custom styles, e.g. Any tips for how I can allow + apply those string styles to the rendered mdx? If relevant: I'm using Sorry if this doesn't belong here or if the question has been asked before - I looked around but didn't find anything. I did find #145 but not a resolution. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
You should get those people to fix their MDX! |
Beta Was this translation helpful? Give feedback.
-
Welcome @zachkirsch! 👋 MDX is Markdown and JSX. That said, if you really want to change that behavior to accept a string. You could create a |
Beta Was this translation helpful? Give feedback.
-
Amazing thank you both! |
Beta Was this translation helpful? Give feedback.
Welcome @zachkirsch! 👋
I assume you are using React?
MDX is Markdown and JSX.
When used with React, React only accepts
style
as an object.I'd generally recommend sticking with React's convention.
That said, if you really want to change that behavior to accept a string.
You'd need to get into the parsing and abstract syntax tree internals.
I'd recommend starting by reading https://unifiedjs.com/learn/
Then playing around with https://mdxjs.com/playground/ to familiarize yourself with the 3 syntax trees MDX uses.
You could create a
recma
plugin to modify theesast
(JavaScript) syntax tree.Find the
style
attributes which are a string, and use a CSS parser to transform that text into a React…