theRDnotes

2020 February 16th

Part9: Syntax highlighting

For highlighting syntax in code-blocks and single backticks, we'll use gatsby-remark-prismjs.

This plugin uses PrismJS under the hood.

Installation

Install the plugin using below command

npm install gatsby-remark-prismjs prismjs

Configuration

I have added the below configuration to gatsby-config.js file under gatsby-transformer-remark plugin.

{
    resolve: `gatsby-transformer-remark`,
    options: {
    plugins: [
        {
        resolve: `gatsby-remark-prismjs`,
        options: {
            classPrefix: "language-",
            inlineCodeMarker: '±',
            aliases: { sh: "bash" },
            showLineNumbers: false,
            noInlineHighlight: false,
            languageExtensions: [
            {
                language: "superscript",
                extend: "javascript",
                definition: {
                superscript_types: /(SuperType)/,
                },
                insertBefore: {
                function: {
                    superscript_keywords: /(superif|superelse)/,
                },
                },
            },
            ],
            prompt: {
            user: "root",
            host: "localhost",
            global: false,
            },
            escapeEntities: {},
        },
        }
    ],
    },
}

To learn about all the options to customize this plugin, visit here.

Theme

To customize the theme for syntax highlighting, I have copied an existing theme to a new folder src/themes/prism-thersnotes.cssand then did the changes as per my liking.

Below is the css I am using on this website.

prism-thersnotes.css
/**
 * prism.js default theme for JavaScript, CSS and HTML
 * Based on dabblet (http://dabblet.com)
 * @author Lea Verou
 */

 code[class*="language-"],
 pre[class*="language-"] {
	 color: black;
	 background: none;
	 /* text-shadow: 0 1px white; */
	 font-family: 'menlo';
	 font-size: 0.9em;
	 text-align: left;
	 white-space: pre;
	 word-spacing: normal;
	 word-break: normal;
	 word-wrap: normal;
	 line-height: 1.3;
 
	 -moz-tab-size: 4;
	 -o-tab-size: 4;
	 tab-size: 4;
 
	 -webkit-hyphens: none;
	 -moz-hyphens: none;
	 -ms-hyphens: none;
	 hyphens: none;
 }
 
 pre[class*="language-"]::-moz-selection, pre[class*="language-"] ::-moz-selection,
 code[class*="language-"]::-moz-selection, code[class*="language-"] ::-moz-selection {
	 text-shadow: none;
	 background: #b3d4fc;
 }
 
 pre[class*="language-"]::selection, pre[class*="language-"] ::selection,
 code[class*="language-"]::selection, code[class*="language-"] ::selection {
	 text-shadow: none;
	 background: #b3d4fc;
 }
 
 @media print {
	 code[class*="language-"],
	 pre[class*="language-"] {
		 text-shadow: none;
	 }
 }
 
 /* Code blocks */
 pre[class*="language-"] {
	 padding: 0.5em;
	 margin: .5em 0;
	 overflow: auto;
 }
 
 :not(pre) > code[class*="language-"],
 pre[class*="language-"] {
	 background: #eceff1;
 }
 
 /* Inline code */
 :not(pre) > code[class*="language-"] {
	 padding-left: .3em;
	 padding-right: .3em;
	 /* border-radius: .3em; */
	 /* white-space: normal; */
	 /* background-color: transparent; */
	 color: #DD4A68;
 }
 
 .token.comment,
 .token.prolog,
 .token.doctype,
 .token.cdata {
	 color: slategray;
 }
 
 .token.punctuation {
	 color: #999;
 }
 
 .token.namespace {
	 opacity: .7;
 }
 
 .token.property,
 .token.tag,
 .token.boolean,
 .token.number,
 .token.constant,
 .token.symbol,
 .token.deleted {
	 color: #905;
 }
 
 .token.selector,
 .token.attr-name,
 .token.string,
 .token.char,
 .token.builtin,
 .token.inserted {
	 color: #690;
 }
 
 .token.operator,
 .token.entity,
 .token.url,
 .language-css .token.string,
 .style .token.string {
	 color: #9a6e3a;
	 /* background: hsla(0, 0%, 100%, .5); */
 }
 
 .token.atrule,
 .token.attr-value,
 .token.keyword {
	 color: #07a;
 }
 
 .token.function,
 .token.class-name {
	 color: #DD4A68;
 }
 
 .token.regex,
 .token.important,
 .token.variable {
	 color: #e90;
 }
 
 .token.important,
 .token.bold {
	 font-weight: bold;
 }
 .token.italic {
	 font-style: italic;
 }
 
 .token.entity {
	 cursor: help;
 }
 

Import the css in src/pages/index.js as below

import "../themes/prism-therdnotes.css";

Font

Also, I've changed the font to be used for syntax highlighting.
As you can see in the css inline(collapsed) above, I've set font-family: 'menlo';

Import the ttf in src/pages/index.js as below

import "../fonts/menlo-regular.ttf"

Colors

If you aren't seeing the colors in the code blocks. This might be because you haven't specified the language.

import "../fonts/menlo-regular.ttf"
import "../fonts/menlo-regular.ttf"

Language for the code in code block can be specified after the starting three ticks as below

```js

And in case of inline code i.e. using single backticks, we can use the symbol mentioned for inlineCodeMarker in plugin's config in gatsby-config.js file.

I have set inlineCodeMarker to ±
So this css±.some-class { background-color: red } will be renedered as .some-class { background-color: red }