iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🎨

Step-by-Step Guide to Implementing Syntax Highlighting in Gatsby

に公開
2026/02/08

This is quite old, so it might not be very helpful.
Honestly, you're better off asking ChatGPT to help you make it.
2026-02-08 Sotono

Enabling Syntax Highlighting on Gatsby

If you are using gatsby-starter-blog,
it is available for use without any extra configuration.
You might want to at least read sections 4. and 5..

1. Install the following modules

  • prismjs
  • gatsby-remark-prismjs
  • gatsby-remark-prismjs-title
terminal
npm install -S prismjs gatsby-remark-prismjs gatsby-remark-prismjs-title

2. Rewriting gatsby-config.js

*Note: This step can be skipped if you are using gatsby-starter-blog.

gatsby-config.js
resolve: `gatsby-transformer-remark`,
options: {
  plugins: [
    `gatsby-remark-prismjs-title`,
    `gatsby-remark-prismjs`,
  ]
},

3. Rewriting gatsby-browser.js

Configure it with your preferred theme by referring to Prism.

For example, if you want the default theme:

gatsby-browser.js
// Highlighting for code blocks
import "prismjs/themes/prism.css"

If you want to apply Tomorrownight, for instance:

gatsby-browser.js
// Highlighting for code blocks
import "prismjs/themes/prism-tomorrow.css"

It would look something like that.

4. Adding a Title to the Highlight

If you have configured everything correctly so far,

this

python:title=aiueo

will become like this when you run gatsby develop.

<div class="gatsby-code-title">
  <span>aiueo</span>
</div>

Now, let's set up the style sheet for this.
You can modify any CSS file you are importing.
In this case, everything is put into style.css, so I will write it here.

Feel free to modify this to match your theme.

style.css
.gatsby-code-title {
  background: #F5F2F0;
  color: #000;
  font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace;
  margin: 20px 0px -24px;
  padding: 8px 1rem 20px;
  font: size 1em;
  line-height: 1;
  display: table;
}

5. Tips for When the Article is... Not Highlighted

Q. I should have set everything correctly, but the syntax highlighting isn't working...

A. It's possible that your Markdown syntax is incorrect.

For example,

index.md
python :aiueo

In this case, neither the title nor the syntax highlighting will appear.

index.md
python:title=aiueo

Use this instead.
The trick is to not insert a space after the language you want to highlight.

Completion

Screenshot 2021-02-09 16.10.39.png

References

Applying Syntax Highlighting to a Blog Made with GatsbyJS - Kikunantoka.com
Adding Titles and Line Highlighting to Code Blocks in a GatsbyJS Blog - Nofunofurogu

Discussion