JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 283087
  • Score
    100M100P100Q172717F
  • License MIT

Highlight code blocks

Package Exports

  • rehype-highlight

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 (rehype-highlight) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

rehype-highlight Build Status Coverage Status

Syntax highlighting for rehype.

Installation

npm:

npm install rehype-highlight

Usage

Say example.html looks as follows:

<h1>Hello World!</h1>

<pre><code class="language-js">var name = "World";
console.warn("Hello, " + name + "!")</code></pre>

...and example.js like this:

var vfile = require('to-vfile');
var report = require('vfile-reporter');
var rehype = require('rehype');
var highlight = require('rehype-highlight');

rehype()
  .data('settings', {fragment: true})
  .use(highlight)
  .process(vfile.readSync('example.html'), function (err, file) {
    console.error(report(err || file));
    console.log(String(file));
  });

Now, running node example yields:

example.html: no issues found
<h1>Hello World!</h1>

<pre><code class="hljs language-js"><span class="hljs-keyword">var</span> name = <span class="hljs-string">"World"</span>;
<span class="hljs-built_in">console</span>.warn(<span class="hljs-string">"Hello, "</span> + name + <span class="hljs-string">"!"</span>)</code></pre>

API

rehype().use(highlight[, options])

Syntax highlight pre > code. Uses lowlight under the hood, which is a virtual version of highlight.js.

Configure the language by using a lang-js or language-js class. Ignore code with a no-highlight or nohighlight class. Will auto-detect the syntax language otherwise.

options
options.prefix

string, default: 'hljs-' — Prefix to use before classes.

options.subset

boolean or Array.<string>, default: all languages — Scope of languages to check when auto-detecting. Pass false to not highlight code without language classes.

options.ignoreMissing

boolean, default: false. By default, unregistered syntaxes throw an error when they are used. Pass true to swallow those errors and thus ignore code with unknown code languages.

options.plainText

Array.<string>, default: []. Pass any languages you would like to be kept as plain-text instead of getting highlighted.

Contribute

See contribute.md in rehypejs/rehype for ways to get started.

This organisation has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Titus Wormer