DEV Community

Cover image for SyntaxHighlighter does not highlight the code block when used with ReactMarkdown
Mudit Choudhary
Mudit Choudhary

Posted on

SyntaxHighlighter does not highlight the code block when used with ReactMarkdown

I faced this little bug with SyntaxHighlighter does not highlight the markdown code block.

const Test = () => {
    const markdown = `Here is some JavaScript code:

    ~~~js
    console.log('It works!')
    ~~~
    `;
    return (
        <ReactMarkdown
            children={markdown}
            components={{
                code(props) {
                    console.log(props);
                    const { children, className, node, ...rest } = props;
                    const match = /language-(\w+)/.exec(className || "");
                    return match ? (
                        <SyntaxHighlighter
                            {...rest}
                            children={String(children).replace(/\n$/, "")}
                            language={match[1]}
                            PreTag="div"
                        />
                    ) : (
                        <code {...rest} className={className}>
                            {children}
                        </code>
                    );
                },
            }}
        />
    );
};
Enter fullscreen mode Exit fullscreen mode

I have this simple component that renders a mark-down code block as JSX. But the SyntaxHighlighter was not highlighting the code. I found that the issue was that the props argument in code function does not have any className property due to which the match does not have the language we specified in the markdown. Due to this the if condition is always false and the default <code> block always gets rendered.

Solution
I updated the code a bit and instead of using the className to know which language is used in the markdown code string. I extract the language from the children property, that is the content we have in the markdown string.

const Test = () => {
    const markdown = `Here is some JavaScript code:

    ~~~js
    console.log('It works!')
    ~~~
    `;
    return (
        <ReactMarkdown
            children={markdown}
            components={{
                code(props) {
                    console.log(props);
                    const { children, className, node, ...rest } = props;
                    const codeValue = props.children; // Get the code block content
                    console.log(codeValue);
                    const match = /~~~(\w+)/.exec(codeValue); // Use a regex to extract the language
                    console.log(match);
                    return match ? (
                        <SyntaxHighlighter
                            {...rest}
                            children={String(children).replace(/\n$/, "")}
                            language={match[1]}
                            PreTag="div"
                        />
                    ) : (
                        <code {...rest} className={className}>
                            {children}
                        </code>
                    );
                },
            }}
        />
    );
};
Enter fullscreen mode Exit fullscreen mode

Hope it will help you!! Thank you

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

ACI image

ACI.dev: Fully Open-source AI Agent Tool-Use Infra (Composio Alternative)

100% open-source tool-use platform (backend, dev portal, integration library, SDK/MCP) that connects your AI agents to 600+ tools with multi-tenant auth, granular permissions, and access through direct function calling or a unified MCP server.

Check out our GitHub!

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️