Detalhes do pacote

@mapbox/rehype-prism

mapbox228.7kMIT0.9.0

rehype plugin to highlight code blocks in HTML with Prism

rehype, rehype-plugin, syntax-highlighting, prism

readme (leia-me)

@mapbox/rehype-prism

Build Status

rehype plugin to highlight code blocks in HTML with Prism (via refractor).

(If you would like to highlight code blocks with highlight.js, instead, check out rehype-highlight.)

Best suited for usage in Node. If you would like to perform syntax highlighting in the browser, you should look into less heavy ways to use refractor.

Installation

npm install @mapbox/rehype-prism

API

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

Syntax highlights pre > code. Under the hood, it uses refractor, which is a virtual version of Prism.

The code language is configured by setting a language-{name} class on the <code> element. You can use any language supported by refractor.

If no language-{name} class is found on a <code> element, it will be skipped.

options

options.ignoreMissing

Type: boolean. Default: false.

By default, if {name} does not correspond to a language supported by refractor an error will be thrown.

If you would like to silently skip <code> elements with invalid languages, set this option to true.

options.alias

Type: Record<string, string | string[]>. Default: undefined.

Provide aliases to refractor to register as alternative names for a language.

Usage

Use this package as a rehype plugin.

Some examples of how you might do that:

const rehype = require('rehype');
const rehypePrism = require('@mapbox/rehype-prism');

rehype()
  .use(rehypePrism)
  .process(/* some html */);
const unified = require('unified');
const rehypeParse = require('rehype-parse');
const rehypePrism = require('@mapbox/rehype-prism');

unified()
  .use(rehypeParse)
  .use(rehypePrism)
  .processSync(/* some html */);

If you'd like to get syntax highlighting in Markdown, parse the Markdown (with remark-parse), convert it to rehype, then use this plugin.

const unified = require('unified');
const remarkParse = require('remark-parse');
const remarkRehype = require('remark-rehype');
const rehypePrism = require('@mapbox/rehype-prism');

unified()
  .use(remarkParse)
  .use(remarkRehype)
  .use(rehypePrism)
  .process(/* some markdown */);

FAQ

<summary>Why does rehype-prism copy the language- class to the <pre> tag?</summary> Prism recommends adding the language- class to the <code> tag like this: html <pre><code class="language-css">p { color: red }</code></pre> It bases this recommendation on the HTML5 spec. However, an undocumented behavior of their JavaScript is that, in the process of highlighting the code, they also copy the language- class to the <pre> tag: html <pre class="language-css"><code class="language-css"><span class="token selector">p</span> <span class="token punctuation">{</span> <span class="token property">color</span><span class="token punctuation">:</span> red <span class="token punctuation">}</span></code></pre> This resulted in many Prism themes relying on this behavior by using CSS selectors like pre[class*="language-"]. So in order for people using rehype-prism to get the most out of these themes, we decided to do the same.

changelog (log de mudanças)

Changelog

0.9.0

  • Update dependencies

0.8.0

  • Add alias option to support aliases in refractor.

0.7.0

  • Update dependencies: refractor@3.4.0, eslint, jest, and prettier.

0.6.0

  • Update dependencies.

0.5.0

  • Update dependencies.

0.4.0

  • Update snapshot, use Node 10, remove yarn in favor of package-lock.json, update Jest to fix security alerts.

0.3.1

  • Allow uppercase language names in the language-* class (e.g. language-CSS).

0.3.0

  • Add language-* class to the <pre> tag of the output, because many Prism themes rely on this undocumented pattern.

0.2.0

  • Breaking: Add options.ignoreMissing which defaults to false. If you are relying on silent failures to highlight when the language is not defined, you'll need to use this option.
  • Breaking: Remove support for nohighlight and no-highlight classes. You can skip highlighting for any given <code> by not putting a language-* class on it.
  • Under the hood, use refractor instead of Parse5 and PrismJS directly.

0.1.0

  • Initial release.