unified

Project:remarkjs/remark-highlight.js

Package:remark-highlight.js@6.0.0

  1. Dependencies:2·Dependents:37
  2. remark plugin to highlight code blocks with highlight.js
  1. remark 180
  2. unified 131
  3. plugin 122
  4. markdown 101
  5. mdast 69
  6. remark-plugin 62
  7. syntax 29
  8. highlight 4

remark-highlight.js

Build Coverage Downloads Size Sponsors Backers Chat

remark plugin to highlight code blocks with highlight.js (via lowlight).

This package integrates with remark-html. It may be better to work with rehype, which is specifically made for HTML, and to use rehype-highlight instead of this package.

Install

npm:

npm install remark-highlight.js

Use

Say we have the following Markdown file, example.md:

~~~css
h1 {
  color: red;
}
~~~

And our script, example.js, looks as follows:

const vfile = require('to-vfile')
const report = require('vfile-reporter')
const unified = require('unified')
const markdown = require('remark-parse')
const html = require('remark-html')
const highlight = require('remark-highlight.js')

unified()
  .use(markdown)
  .use(highlight)
  .use(html)
  .process(vfile.readSync('example.md'), (err, file) => {
    console.error(report(err || file))
    console.log(String(file))
  })

Now, running node example yields:

example.md: no issues found
<pre><code class="hljs language-css"><span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">color</span>: red;
}</code></pre>

API

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

highlight code blocks with highlight.js (via lowlight).

For a list of languages that you can pass to these options, see the highlight.js documentation.

options
options.include

If this option is defined (Array), this plugin will only highlight languages that are in this list.

options.exclude

If this option is defined (Array), this plugin will only highlight languages that are not in this list.

options.prefix

If this option is defined (string), this plugin will use this prefix for classes instead of hljs-.

Security

Use of remark-highlight.js should be safe to use as lowlight should be safe to use. When in doubt, use rehype-sanitize.

Contribute

See contributing.md in remarkjs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Ben Briggs