gatsby-remark-prismjs is a Gatsby plugin designed to add syntax highlighting to code blocks within Markdown files during the build process, leveraging the popular PrismJS library. It is currently at version 7.16.0 and is a core component of the Gatsby ecosystem, with its release and maintenance closely aligned with Gatsby's own development cycle. As an official Gatsby plugin, it is actively maintained. Its primary differentiator is performing syntax highlighting at build time, pre-rendering the highlighted code into static HTML. This approach eliminates the need for client-side JavaScript to process and highlight code, leading to improved perceived performance and a faster initial page load compared to client-side highlighting solutions. It offers extensive configuration for themes, line numbering, language aliases, and custom language definitions.
npm install gatsby-remark-prismjsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the core setup for `gatsby-remark-prismjs` in `gatsby-config.js` as a child plugin of `gatsby-transformer-remark`. It includes options for line numbers, language aliases, and a custom language extension. It also shows the necessary CSS theme and plugin imports in `gatsby-browser.js` and provides example Markdown code blocks for testing.
Upgrade Gatsby to `^5.0.0` or higher, and ensure your Node.js version is `^18.0.0` or higher. For example, `npm install gatsby@latest`.
Ensure `gatsby-transformer-remark` is installed (`npm install gatsby-transformer-remark`) and correctly configured in your `gatsby-config.js` with `gatsby-remark-prismjs` nested within its `plugins` array.
Add `require("prismjs/themes/<your-chosen-theme>.css")` to your `gatsby-browser.js`. Replace `<your-chosen-theme>` with a valid PrismJS theme name.For MDX projects, consider using client-side highlighting with `prism-react-renderer` or ensure a custom MDX component is set up to render code blocks with PrismJS, rather than relying on `gatsby-remark-prismjs`.
Arrange your remark plugins in `gatsby-config.js` in the desired processing order. Place `gatsby-remark-prismjs` after any plugins that might extract or transform code blocks before highlighting is applied.
Install `gatsby-transformer-remark` by running `npm install gatsby-transformer-remark` or `yarn add gatsby-transformer-remark`.
Ensure `require("prismjs/themes/<your-theme>.css")` is present in `gatsby-browser.js`. Verify the language specified in your Markdown code block (e.g., ````javascript````) is a supported PrismJS language or an alias configured in the plugin options. Check for any conflicting client-side PrismJS initialization.Update your Node.js environment to a version compatible with Gatsby v5 (e.g., Node.js 18.0.0 or higher, with Node.js 20 and 22 being formally supported). Use a tool like `nvm` to manage Node.js versions.