@clarle/metalsmith-prism

Syntax highlighting for Metalsmith HTML templates using Prism.js (with line numbers)

Usage no npm install needed!

<script type="module">
  import clarleMetalsmithPrism from 'https://cdn.skypack.dev/@clarle/metalsmith-prism';
</script>

README

metalsmith-prism

Syntax highlighting for Metalsmith HTML templates using Prism.js

License NPM Dependency Status Linux Passing Windows Passing

The main difference between this package and Availity/metalsmith-prism is the support for line numbers, without any browser-side JavaScript. It's a bit of a hack, so use at your own risk.

Quickstart

  • Install metalsmith-prism
  npm install @clarle/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('@clarle/metalsmith-prism');
metalsmith(__dirname)
  .use(metalsmithPrism())
  .build();
var metalsmith = require('metalsmith');
var markdown = require('metalsmith-markdown');
var metalsmithPrism = require('@clarle/metalsmith-prism');
metalsmith(__dirname)
  // Custom langPrefix option needed as markdown uses 'lang-' by default:
  .use(markdown( { langPrefix: 'language-' } ))
  .use(metalsmithPrism())
  .build();

Language support

Supports all programming languages that have a corresponding Prism.js component file. Component files are found in the Prism.js components directory.

JSON

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>

Options

decode (optional)

  • Always decode the html entities when processing language of type markup
Metalsmith(__dirname)
  .use(metalsmithPrism({
    decode: true
  }))

json (optional)

  • Supply custom JSON syntax definition
Metalsmith(__dirname)
  .use(metalsmithPrism({
    json: {
      'keys': /".+"(?=:)/g,
      'boolean': /\b(true|false)/g,
      'punctuation': /({|}|:|\[|\]|,)/g,
      'keyword': /\b(null)\b/g
    }
  }))

lineNumbers (optional)

  • Appends class line-numbers to parent <pre> tag if present
Metalsmith(__dirname)
  .use(metalsmithPrism({
    lineNumbers: true
  }))

Authors

Robert McGuinness

Disclaimer

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.

Copyright and license

Code and documentation copyright 2016 Availity, LLC. Code released under the MIT license.