Package Exports
- gatsby-remark-prismjs
- gatsby-remark-prismjs/highlight-code
- gatsby-remark-prismjs/highlight-code.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (gatsby-remark-prismjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
gatsby-remark-prismjs
Adds syntax highlighting to code blocks in markdown files using PrismJS.
Install
npm install --save gatsby-transformer-remark gatsby-remark-prismjs
How to use
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
{
resolve: `gatsby-remark-prismjs`,
options: {
// Class prefix for <pre> tags containing syntax highlighting;
// defaults to 'language-' (eg <pre class="language-js">).
// If your site loads Prism into the browser at runtime,
// (eg for use with libraries like react-live),
// you may use this to prevent Prism from re-processing syntax.
// This is an uncommon use-case though;
// If you're unsure, it's best to use the default value.
classPrefix: "language-",
// This is used to allow setting a language for inline code
// (i.e. single backticks) by creating a separator.
// This separator is a string and will do no white-space
// stripping.
// A suggested value for English speakers is the non-ascii
// character '›'.
inlineCodeMarker: null,
// This lets you set up language aliases. For example,
// setting this to '{ sh: "bash" }' will let you use
// the language "sh" which will highlight using the
// bash highlighter.
aliases: {},
},
},
],
},
},
];Include CSS
Required: Pick a PrismJS theme or create your own
PrismJS ships with a number of themes (previewable on the PrismJS website) that you can easily include in your Gatsby site, or you can build your own by copying and modifying an example (which is what we've done for gatsbyjs.org).
To load a theme, just require its CSS file in your layouts/index.js file, e.g.
// layouts/index.js
require("prismjs/themes/prism-solarizedlight.css");Optional: Add line highlighting styles
If you want to highlight lines of code, you also need to add some additional CSS that targets our custom line highlighting implementation (which slightly differs from PrismJS's own plugin for that – more on that later).
For line highlights similar to PrismJS's, try:
.gatsby-highlight-code-line {
background-color: #feb;
display: block;
margin-right: -1em;
margin-left: -1em;
padding-right: 1em;
padding-left: 0.75em;
border-left: 0.25em solid #f99;
}This should work out quite nicely for the "Solarized Light" PrismJS theme we just added in the previous part. However, you will notice that when a highlighted line runs wider than the surrounding code block container (causing a horizontal scrollbar), its background won't be drawn for the initially hidden, overflowing part. :(
We saw others fix that problem and decided to do so, too. Just add the following
CSS along your PrismJS theme and the styles for .gatsby-highlight-code-line:
/**
* Add back the container background-color, border-radius, padding, margin
* and overflow that we removed from <pre>.
*/
.gatsby-highlight {
background-color: #fdf6e3;
border-radius: 0.3em;
margin: 0.5em 0;
padding: 1em;
overflow: auto;
}
/**
* Remove the default PrismJS theme background-color, border-radius, margin,
* padding and overflow.
* 1. Make the element just wide enough to fit its content.
* 2. Always fill the visible space in .gatsby-highlight.
*/
.gatsby-highlight pre[class*="language-"] {
background-color: transparent;
margin: 0;
padding: 0;
overflow: initial;
float: left; /* 1 */
min-width: 100%; /* 2 */
}Usage in Markdown
This is some beautiful code:
```javascript
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
`gatsby-remark-prismjs`,
]
}
}
]
```You can also add line highlighting. It adds a span around lines of code with a
special class .gatsby-highlight-code-line that you can target with styles. See
this README for more info.
In the following code snippet, lines 1 and 4 through 6 will get the line highlighting. The line range parsing is done with https://www.npmjs.com/package/parse-numeric-range.
```javascript{1,4-6}
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [
`gatsby-remark-prismjs`,
]
}
}
]
```In addition to fenced code blocks, inline code blocks will be passed through PrismJS as well.
If you set the inlineCodeMarker, then you can also specify a format style.
Here's an example of how to use this if the inlineCodeMarker was set to ±:
I can highlight `css±.some-class { background-color: red }` with CSS syntax.This will be rendered in a <code class=language-css> with just the (syntax
highlighted) text of .some-class { background-color: red }
If you need to prevent any escaping or highlighting, you can use the none
language; the inner contents will not be changed at all.
Implementation notes
Line highlighting
Please note that we do not use PrismJS's line highlighting plugin. Here's why:
- PrismJS plugins assume you're running things client side, but we are build-time folks.
- PrismJS's line highlighting plugin implementation does not allow for solid background colors or 100% wide backgrounds that are drawn beyond the visible part of the container when content is overflowing.
Our approach follows the Pygments-based implementation of the React Tutorial/Documentation for line highlights:
- It uses a wrapper element
<div class="gatsby-highlight">around the PrismJS-formatted<pre><code>-blocks. - Highlighted lines are wrapped in
<span class="gatsby-highlight-code-line">. - We insert a linebreak before the closing tag of
.gatsby-highlight-code-lineso it ends up at the start of the follwing line.
With all of this in place, we can apply float:left; min-width:100% to <pre>,
throw our overflow and background on .gatsby-highlight, and use
display:block on .gatsby-highlight-code-line – all of this coming together
to facilitate the desired line highlight behavior.
Contributing Notes
We vendor the prismjs components file in prism-language-dependencies.js. We use the prism-language-dependencies.js file to tell gatsby-remark-prismjs which languages it can syntax highlight. Thus, when the prismjs library is updated, we need to update the our prism-language-dependencies.js file. To do this, we have created a script at scripts/get-prism-language-dependencies.js. To run this script:
Move into the
gatsby/packages/gatsby-remark-prismjs/scriptsdircd gatsby/packages/gatsby-remark-prismjs/scriptsRun the script
node get-prism-language-dependencies.js