question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom props of code blocks

See original GitHub issue
  • mdx-bundler version: 7.0.0
  • node version: 14.18.1
  • npm 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:open
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
allangrdscommented, Aug 15, 2022

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 😃

1reaction
PsyGikcommented, Aug 15, 2022

Thank you for the code samples @allangrds. Couple of things to note here:

  • It seems like in your markdown, you are using ''' (quotes) instead of ``` (backticks) to render a code block.
  • I’d ideally keep prism syntax highlighting plugin as the last one in the list, as I’ve observed it to cause problems with other plugins. (NOTE: that I use a custom version of prism syntax highlighting, and this point may or may not be true depending on you use-case. But it never hurts to try 🤷🏽 )

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 to localhost:3000/hello. In your terminal, you should see the logs which prints the meta values.

❯ npm run dev

> dev
> next dev

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
event - compiled client and server successfully in 382 ms (178 modules)
wait  - compiling...
event - compiled client and server successfully in 91 ms (178 modules)
wait  - compiling /hello (client and server)...
event - compiled client and server successfully in 159 ms (193 modules)
node.data.meta title='code.js'
node.data.meta filename="awesome.js"
Read more comments on GitHub >

github_iconTop Results From Across the Web

How pass custom props to code blocks? #2099 - mdx-js/mdx
I'm trying to pass some properties to the code, to treat it visually when rendered. I'm using a lib mdx-bundler wich uses ...
Read more >
Custom Code Blocks with MDX & Gatsby | Anna Rossetti
In this comprehensive tutorial you'll learn how to enhance your MDX & Gatsby blog with custom code snippets.
Read more >
Custom code - Use block props/options - Builder.io Forum
Hi I'm trying to generate a blurDataURL for my images using a node.js library, so I created an endpoint /api/images/[src].js where src is ......
Read more >
Defining Custom Block Nodes - TheSunny - GitBook
Slate lets you define any type of custom blocks you want, like block quotes, code blocks, list items, etc. We'll show you how....
Read more >
Write a custom block - Blocks UI
A Block is a component that the editor can control. It's typically a component that has pre-made styles and accepts a few props...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found