Home
Head's Up: I'm in the middle of upgrading my site. Most things are in place, but there are something missing and/or broken including image alt text. Please bear with me while I'm getting things fixed.

Add Line Numbers to Prismjs Syntax Highlighting Code Blocks in a Next.js project

### NOTE - This may not be the best way to do this

I'm having problems with line highlighting with errors like :

Warning: Prop ``className`` did not match. Server: "line-numbers language-jsx" Client: "line-numbers"

I'm now looking at this :

https : //github.com/FormidableLabs/prism - react - renderer

( _ An important note I often forget is that addind < code > className="language - jsx" < /code > to the [TODO: Code shorthand span ] block is required to get highlighting to work. Putting that up here on the top as a reminder _ )

None of the posts I found on the Prism syntax highlighter](https://prismjs.com/) show how to turn on line numbers in a [Next.js project.

In the spirit of sharing so others don't have to hack around like I did, here's how to do it :

## 1. The Install

Assuming you already have your Next.js project setup and ready, install Prism with :

bash
npm install prismjs

## 2. The Full Code Example

Next up, the code. Here's a fully functional example you can use in a file (e.g. 'pages/prism - example.js'). It includes a theme (okaidia), a language (jsx), and line numbering. (Details further below.)

jsx
import { useEffect } from 'react'

import Prism from 'prismjs'

import 'prismjs/themes/prism-okaidia.css'
import 'prismjs/components/prism-jsx.js'
import 'prismjs/plugins/line-numbers/prism-line-numbers.js'
import 'prismjs/plugins/line-numbers/prism-line-numbers.css'

export default function Page() {
  useEffect(() => {
    Prism.highlightAll()
  }, [])

  const codeSample = `<div className="example">
  {Math.random()}
</div>`

  return (
    <main>
      <pre className="line-numbers">
        <code className="language-jsx">{codeSample}</code>
      </pre>
    </main>
  )
}

Using that will output a highlighted and numbered code snippet that looks like this .

## 3. The Details

Here's how it all works :

Prism is made available with :

jsx
import Prism from 'prismjs'

The theme comes from :

jsx
import 'prismjs/themes/prism-okaidia.css'

This is one of the default themes Prism ships with. The others are located in the project's 'node _ modules/prismjs/themes' directory. They are :

- prism.css - prism-coy.css - prism-dark.css - prism-funky.css - prism-okaidia.css - prism-solarizedlight.css - prism-tomorrow.css - prism-twilight.css

Switch out the [TODO: Code shorthand span ] call to point to whichever one you prefer.

There are also ways to make and use custom themes as well. That's left as an exercise for the reader.

Be default, Prism loads syntax highlighting for "markup, css, clike, and javascript". Other languages need to be imported explicitly. In the example, I'm adding JSX with :

jsx
import 'prismjs/components/prism-jsx.js'

This is what lets us call the < code className="language - jsx border border - black" > language - jsx < /code > class in our < code className="border border - black language - markup" > <code> < /code > tags.

(Look in your project's 'node _ modules/prismjs/components' directory for all the available languages.)

Prepping to use the line numbers is done with these two import statements :

jsx
import 'prismjs/plugins/line-numbers/prism-line-numbers.js'
import 'prismjs/plugins/line-numbers/prism-line-numbers.css'

There's a bunch of other plugins available in the project's 'node _ modules/prismjs/plugins' like < code className="language - bash" > copy - to - clipboard < /code > , < code className="language - bash" > line - highlight < /code > , etc... that are worth looking at too.

Actually getting Prism to do its thing is done with < code className="language - jsx" > Prism.highlightAll() < /code > in < code className="language - jsx" > useEffect() < /code > with these lines :

jsx
useEffect(() => {
    Prism.highlightAll()
}, [])

The last two parts that turn on the line numbers and set the language go hand in hand. They're done with < code className="language - jsx" > className="line - numbers" < /code > and < code className="language - jsx" > className="language - jsx" < /code > in :

jsx
<pre className="line-numbers">
    <code className="language-jsx">{codeSample}</code>
</pre>

One last note. I use next-mdx-remote](https://github.com/hashicorp/next-mdx-remote) for my site. I make code blocks with code fences (e.g. ```). There's no way in the markdown to send a signal to turn on the line numbers in the <pre> tag. I work around that by [replacing the default components like this :

jsx
<MDXRemote
  {...source}
  components={{
    pre: (props) => (
      <pre className="line-numbers">{props.children}</pre>
    ),
  }}
/>

That adds the < code className="language - jsx" > className="line - numbers" < /code > call to all my < code className="language - markdown" > <pre> < /code > tags.

## The Review

Now that you've seen the details, here's that code sample one more time.

jsx
import { useEffect } from 'react'

import Prism from 'prismjs'

import 'prismjs/themes/prism-okaidia.css'
import 'prismjs/components/prism-jsx.js'
import 'prismjs/plugins/line-numbers/prism-line-numbers.js'
import 'prismjs/plugins/line-numbers/prism-line-numbers.css'

export default function Page() {
  useEffect(() => {
    Prism.highlightAll()
  }, [])

  const codeSample = `<div className="example">
  {Math.random()}
</div>`

  return (
    <main>
      <pre className="line-numbers">
        <code className="language-jsx">{codeSample}</code>
      </pre>
    </main>
  )
}

Prism also offers line highlighting. There isn't a way to use it with default markdown code blocks since the line numbers are dependent on the specific code snippets. Getting those in place (along with some of the other Prism plugins) requires making new, custom components.

But, that's for another time. For now, syntax highlighting with line numbers has me taken care of.