Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Difference between Code Block and inline code #244

Closed
thatanjan opened this issue Mar 8, 2022 · 4 comments
Closed

No Difference between Code Block and inline code #244

thatanjan opened this issue Mar 8, 2022 · 4 comments

Comments

@thatanjan
Copy link

I am using next-mdx-remote 4.0.0 version. When I try to add syntax highlights to code it doesn't distinguish between a code block and an inline code.
I am using prism-react-renderer for syntax highlight with the following code.

// CodeBlock.jsx

import React from 'react'
import Highlight, { defaultProps } from 'prism-react-renderer'
import vsDark from 'prism-react-renderer/themes/vsDark'

const CodeBlock = ({ children, className }) => {
	const language = className ? className.replace(/language-/, '') : ''

	return (
		<Highlight
			{...defaultProps}
			theme={vsDark}
			code={children.trim()}
			language={language}
		>
			{({ 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>
	)
}

export default CodeBlock

My mdx code

image

MDX component object

import CodeBlock from './CodeBlock'

const components = {}

components.code = CodeBlock

export default components

Result

image

Expected Result

image

It works with the version 3.0.8 but not with the 4.0.0 version

Thanks in advance.

@ayuhito
Copy link

ayuhito commented Mar 8, 2022

Can confirm this is affecting my project too. If you don't use the ternary operator for the variable language, it outright breaks because inline code returns className as undefined.

@alexcarpenter
Copy link

alexcarpenter commented Mar 8, 2022

@thatanjan Try switching to:

import CodeBlock from './CodeBlock'

const components = {}

components.pre = CodeBlock

export default components

@BRKalow
Copy link
Contributor

BRKalow commented Mar 8, 2022

Note that MDX was upgraded to v2 as part of this, which was a breaking change and requires changes in how you author content.

See the upgrade guide here: https://mdxjs.com/migrating/v2/#update-mdx-content

Of note:

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

@BRKalow BRKalow closed this as completed Mar 8, 2022
@thatanjan
Copy link
Author

Finally, it is working. Thank you so much @BRKalow and @alexcarpenter.
I switched the code tag to pre tag for CodeBlock.

import CodeBlock from './CodeBlock'

const components = {}

components.pre = CodeBlock

export default components

I also changed the prism code. Now it is getting the code from the nested code tag.

import React from 'react'
import Highlight, { defaultProps } from 'prism-react-renderer'
import vsDark from 'prism-react-renderer/themes/vsDark'

const CodeBlock = ({ children }) => {
	if (!children || children.type !== 'code') return null

	const {
		props: { className, children: code = '' },
	} = children

	const language = className ? className.replace(/language-/, '') : ''

	return (
		<Highlight
			{...defaultProps}
			theme={vsDark}
			code={code.trim()}
			language={language}
		>
			{({ 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>
	)
}

export default CodeBlock

dlcoffee added a commit to dlcoffee/website that referenced this issue Feb 24, 2023
some notable changes

- had to use `pre` for code blocks: hashicorp/next-mdx-remote#244
- had to force react version for mdxjs: PaulieScanlon/mdx-embed#237
- had to update mousetrail package to match react
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants