Syntax highlighting for Metalsmith HTML templates using Prism.js
- Install metalsmith-prism with npm
npm install metalsmith-prism --save-dev
- Add language definition to code block
<code class="language-css">p { color: red }</code>
- Add
metalsmith-prism
plugin to metalsmith
var metalsmith = require('metalsmith');
var metalsmithPrism = require('metalsmith-prism');
metalsmith(__dirname)
.use(metalsmithPrism())
.build();
- OPTIONAL To use with Markdown code blocks rendered by metalsmith-markdown
var metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var metalsmithPrism = require('metalsmith-prism');
metalsmith(__dirname)
// Custom langPrefix option needed as markdown uses 'lang-' by default:
.use(markdown( { langPrefix: 'language-' } ))
.use(metalsmithPrism())
.build();
Supports all programming languages that have a corresponding Prism.js component file. Component files are found in the Prism.js components
directory.
JSON syntax highlighting is enhanced using the following syntax definition
Prism.languages.json = {
'keys': /".+"(?=:)/g,
'boolean': /\b(true|false)/g,
'punctuation': /({|}|:|\[|\]|,)/g,
'keyword': /\b(null)\b/g
}
<code class="language-json">
{
ping: "pong"
}
</code>
- Always decode the html entities when processing language of type
markup
Metalsmith(__dirname)
.use(metalsmithPrism({
decode: true
}))
- Supply custom JSON syntax definition
Metalsmith(__dirname)
.use(metalsmithPrism({
json: {
'keys': /".+"(?=:)/g,
'boolean': /\b(true|false)/g,
'punctuation': /({|}|:|\[|\]|,)/g,
'keyword': /\b(null)\b/g
}
}))
Robert McGuinness
Open source software components distributed or made available in the Availity Materials are licensed to Company under the terms of the applicable open source license agreements, which may be found in text files included in the Availity Materials.
Code and documentation copyright 2015 Availity, LLC. Code released under the MIT license.