difficulty with code blocks in v2 #1891
-
I'm following this documentation and created a component I include it in my const components = {
code: CodeBlock
} However, it has the following issues.
The update guide says: The special component name inlineCode was removed, we recommend to use pre for the block version of code, and code for both the block and inline versions. Does this mean that the |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 12 replies
-
Specifically, this mdx: # Code
new `new`
```bash
# In your ~/.bash_profile
export PATH=~work/bin:$PATH
``` seems to be compiled into: return _jsxs(_Fragment, {
children: [_jsx(_components.h1, {
children: "Code"
}), "\n", _jsxs(_components.p, {
children: ["new ", _jsx(_components.code, {
children: "new"
})]
}), "\n", _jsx(_components.pre, {
children: _jsx(_components.code, {
className: "language-bash",
children: "# In your ~/.bash_profile\nexport PATH=~work/bin:$PATH\n"
})
})]
}); where the first invocation of |
Beta Was this translation helpful? Give feedback.
-
Following the suggestion to check for the import React from 'react'
import Highlight, {defaultProps} from 'prism-react-renderer'
import duotoneLight from 'prism-react-renderer/themes/duotoneLight';
export default (props) => {
const {className, ...rest} = props;
const match = /language-(\w+)/.exec(className || '')
if (match) {
const language = match[1];
const body = rest.children.trim(); // remove leading and trailing newlines
return (
<Highlight {...defaultProps} code={body} language={language} theme={duotoneLight}>
{({className, style, tokens, getLineProps, getTokenProps}) => (
<pre className={className} style={{...style, padding: '20px'}}>
{tokens.map((line, i) => (
<div key={i} {...getLineProps({line, key: i})}>
{line.map((token, key) => (
<span key={key} {...getTokenProps({token, key})} />
))}
</div>
))}
</pre>
)}
</Highlight>
)
} else {
return <code className={className} {...props} />
}
} Is this the recommended approach? |
Beta Was this translation helpful? Give feedback.
-
The page you linked, isn't the documentation page for using code blocks.
Yes, how to do this is documented at https://mdxjs.com/guides/syntax-highlighting/
For the same reason you mentioned in your original question and in the update guide
because markdown is transformed to HTML, and
The approaches in https://mdxjs.com/guides/syntax-highlighting/ are recommended. |
Beta Was this translation helpful? Give feedback.
-
PS: following the approach documented here still has the issue of the trailing newline causing an additional empty line, so adding props.children = props.children.trim() is still necessary. Also, FWIW, in next.js at least I couldn't get the first approach, even though I added the plugin to |
Beta Was this translation helpful? Give feedback.
-
That said you don't need to use example
|
Beta Was this translation helpful? Give feedback.
-
I have a follow-up question. How/where do I configure languages for the rehype-highlight plugin that's recommended? |
Beta Was this translation helpful? Give feedback.
@next/mdx
is maintained at https://github.com/vercel/next.js, thenext
team may be able to provide more context on why it isn't working https://github.com/vercel/next.js/discussionsThat said you don't need to use
@next/mdx
,@mdx-js/loader
can be used directly withnext
without a wrapper https://mdxjs.com/docs/getting-started/#nextjsand supports
rehype-highlight
, for example: https://codesandbox.io/s/mdx-loader-with-rehype-highlight-nu41jexample
next.config.mjs