Custom props of code blocks
See original GitHub issuemdx-bundler
version: 7.0.0node
version: 14.18.1npm
version: 6.14.15
Relevant code or config
Here’s how I handle an MDX file with mdx-bundler
:
import { bundleMdx } from 'mdx-bundler'
const content = file.readFileSync(filePath, 'utf8')
const { code } = await bundleMdx(content)
Here’s how I define the React component:
import { getMDXComponent } from 'mdx-bundler/client'
export function getMdxComponent(code: string) {
const Component = getMDXComponent(code)
return function MdxComponent({ components, ...rest }: MDXContentProps) {
return (
<Component
components={{
code(props) {
console.log(props) // <--
return <code {...props} />
}
}}
{...rest}
/>
)
}
}
What you did:
Here’s my MDX file that’s not parsing correctly:
```js a=1 b=2
function foo() {}
\```
I wish for the a
and b
props given to the code block to be exposed under props
when writing custom components object for the getMDXComponent
.
What happened:
The props
of the code
component do not have the a
and b
keys. I’ve noticed that only className
and children
props are passed to the code
component (may be that only the HTML props are supported).
Suggested solution:
I believe the issue lies somewhere in MDX parsing, which is likely to be done by a different library (xdm
). Still, this is where it’s surfaced for me, I think it’s nice to keep track of it in this repository for posterity (we may create an issue in xdm
if necessary).
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top GitHub Comments
Thanks a lot @PsyGik. I’m using
'''
only here, to create this comments, but as you can see here(https://user-images.githubusercontent.com/4103305/184169786-1283655e-7b70-434d-9bc3-df93a9d84c4b.png), I’m using backticks correctly. Thanks a lot for you pacience.Checking you code and mine, I’ve noticed that ‘remark-prismjs’ was removing meta, so I’ve changed to
rehypePrism
and it’s all good now 😃Thank you for the code samples @allangrds. Couple of things to note here:
'''
(quotes) instead of```
(backticks) to render a code block.I’ve created a bare-bones sample repo here. https://github.com/PsyGik/mdx-bundler-nextjs/tree/main.
Follow the usual steps,
npm install && npm run dev
and navigate tolocalhost:3000/hello
. In your terminal, you should see the logs which prints the meta values.