#Highlighting code syntax in markdown

1 messages · Page 1 of 1 (latest)

charred sail
#

How do I highlight the code syntax in React Markdown?

sweet nimbusBOT
#

Hi @charred sail. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To highlight code syntax in React Markdown, you can use plugins from the remark and rehype ecosystem. Although the provided extracts do not specify a plugin for code syntax highlighting, they mention that such plugins exist. For instance, you can use the rehype-pretty-code plugin for syntax highlighting.

Here's an example of how you might use a plugin with remark and rehype:

import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeSanitize from 'rehype-sanitize'
import rehypeStringify from 'rehype-stringify'
import yourSyntaxHighlightPlugin from 'your-syntax-highlight-plugin' // replace with actual plugin
 
main()
 
async function main() {
 const file = await unified()
 .use(remarkParse) // Convert into markdown AST
 .use(remarkRehype) // Transform to HTML AST
 .use(yourSyntaxHighlightPlugin) // Use your syntax highlight plugin here
 .use(rehypeSanitize) // Sanitize HTML input
 .use(rehypeStringify) // Convert AST into serialized HTML
 .process('Hello, Next.js!')
 
 console.log(String(file)) // <p>Hello, Next.js!</p>
}

Please replace 'your-syntax-highlight-plugin' with the actual plugin you decide to use.

Remember, when using @next/mdx, you do not need to use remark or rehype directly, as it is handled for you.

For more information, refer to the Next.js documentation on MDX (https://nextjs.org/docs/app/building-your-application/configuring/mdx).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@charred sail kapa.ai is still learning and improving, please let us know how it did by reacting below

charred sail
#

@sweet nimbus
I am using react-syntax-highlighter but as soon as I invoke it (meaning even if I import something mentioning Syntaxhighlighter) , I get an error:

Server Error
TypeError: Super expression must either be null or a function

This error happened while generating the page. Any console logs will be displayed in the terminal window.

If I remove SyntaxHighlighter the issue gets resolved
I am using nextJS 13

sweet nimbusBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Hmm, I don't know enough to give you a confident answer yet. The provided extracts do not contain specific information about the react-syntax-highlighter package or the error you're encountering.

However, the error message you're seeing typically occurs when a class extends from something that's not a constructor. This could potentially be a compatibility issue between Next.js 13 and the version of react-syntax-highlighter you're using.

I would recommend checking the official documentation or the GitHub issues for react-syntax-highlighter to see if others have encountered a similar problem and found a solution.

Also, ensure that you're using the latest version of react-syntax-highlighter and that it's compatible with the version of React and Next.js you're using.

If the issue persists, consider creating a minimal reproduction and posting it as an issue on the react-syntax-highlighter GitHub repository.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: