Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mdx-prism-2

raulfdm13MIT8.0.5TypeScript support: included

A fork from mdx-prism plugin to highlight code blocks in HTML with Prism and more

rehype, rehype-plugin, mdx-plugin, mdx-plugin-2, syntax-highlighting, prism, html

readme

Attention: This package is no longer maintained

⚠️ This is Fork from mdx-prism which automate the release process. There are a few fixes that does not have a published version there.

Maybe in the future I can delete this repository in favor of having a single but for now I need things a bit more professional.

mdx-prism-2

This is a fork of @mapbox/rehype-prism that adds line highlighting capabilities, e.g.:

Snippet of code from Dan Abramov’s blog

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 mdx-prism-2

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

Parameter Type Default Description
ignoreMissing boolean 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.
lineHighlight.component string (HTML element) div The HTML tag used to wrap the highlight line.
lineHighlight.className string mdx-marker CSS class applied to highlight line.

Usage

Use this package as a rehype plugin.

Some examples of how you might do that:

const rehype = require('rehype');
const mdxPrism = require('mdx-prism-2');

rehype().use(mdxPrism).process(/* some html */);
const rehype = require('rehype');
const mdxPrism = require('mdx-prism-2');

rehype()
  .use(mdxPrism, {
    lineHighlight: {
      component: 'span',
      className: 'my-line-highlight-class',
    },
  })
  .process(/* some html */);
const unified = require('unified');
const rehypeParse = require('rehype-parse');
const mdxPrism = require('mdx-prism-2');

unified().use(rehypeParse).use(mdxPrism).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 mdxPrism = require('mdx-prism-2');

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

FAQ

<summary>Why does mdx-prism-2 copy the language- class to the <pre> tag?</summary>

Prism recommends adding the language- class to the <code> tag like this:

<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:

<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 mdx-prism-2 to get the most out of these themes, we decided to do the same.

changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

8.0.3 (2021-10-07)

Note: Version bump only for package mdx-prism-2

8.0.2 (2021-10-04)

Note: Version bump only for package mdx-prism-2

8.0.1 (2021-10-01)

Note: Version bump only for package mdx-prism-2

8.0.0 (2021-10-01)

Code Refactoring

  • remove external dependencies from the bundle (44f5183)

BREAKING CHANGES

  • Now, we no longer add all dependencies in the final bundle.

7.1.3 (2021-09-21)

Note: Version bump only for package mdx-prism-2

7.1.2 (2021-09-10)

Note: Version bump only for package mdx-prism-2

7.1.1 (2021-09-07)

Note: Version bump only for package mdx-prism-2

7.1.0 (2021-08-16)

Features

7.0.1 (2021-08-16)

Note: Version bump only for package mdx-prism-2

7.0.0 (2021-08-15)

chore

  • deps: upgrade dependencies (8eaf00c)

BREAKING CHANGES

  • deps: upgrade major dependencies version. Since rehype and its utils are moving to full esm, needed to include both rehype and rehype-parse to de bundle to avoid incorrect resolution while in monorepo

6.2.2 (2021-08-14)

Bug Fixes

  • includes unified to into the bundle (653520f)

6.2.1 (2021-08-13)

Note: Version bump only for package mdx-prism-2

6.2.0 (2021-08-07)

Features

  • add support to svelte language (fc58cd6)

6.1.4 (2021-07-26)

Note: Version bump only for package mdx-prism-2

6.1.3 (2021-07-22)

Note: Version bump only for package mdx-prism-2

6.1.2 (2021-07-16)

Note: Version bump only for package mdx-prism-2

6.1.1 (2021-07-16)

Note: Version bump only for package mdx-prism-2

6.1.0 (2021-07-12)

Features

  • add new options for highlight line (b139dd3)

6.0.8 (2021-07-10)

Note: Version bump only for package mdx-prism-2

6.0.7 (2021-07-10)

Note: Version bump only for package mdx-prism-2

6.0.6 (2021-07-10)

Note: Version bump only for package mdx-prism-2

6.0.5 (2021-07-10)

Note: Version bump only for package mdx-prism-2

6.0.4 (2021-06-09)

Note: Version bump only for package mdx-prism-2

6.0.3 (2021-05-31)

Note: Version bump only for package mdx-prism-2

6.0.2 (2021-05-26)

Note: Version bump only for package mdx-prism-2

6.0.1 (2021-05-18)

Note: Version bump only for package mdx-prism-2

6.0.0 (2021-05-13)

chore

BREAKING CHANGES

  • deps: this bumps a major version which had an API change

Co-authored-by: Raul Melo melo.raulf@gmail.com

5.0.3 (2021-05-13)

Note: Version bump only for package mdx-prism-2

5.0.2 (2021-05-12)

Note: Version bump only for package mdx-prism-2

5.0.1 (2021-05-10)

Note: Version bump only for package mdx-prism-2

5.0.0 (2021-05-07)

chore

BREAKING CHANGES

  • deps: @rollup/plugin-node-resolve was upgraded from v11 to v13

4.0.2 (2021-05-02)

Note: Version bump only for package mdx-prism-2

4.0.0 (2021-05-02)

Code Refactoring

  • convert lib to es6 and compile to ES6 (fc892b5)

BREAKING CHANGES

  • upgrade unist* major version.

3.0.14 (2021-03-28)

Note: Version bump only for package mdx-prism-2

3.0.13 (2021-03-28)

Note: Version bump only for package mdx-prism-2

3.0.12 (2021-03-28)

Note: Version bump only for package mdx-prism-2

3.0.9 (2021-03-27)

Note: Version bump only for package mdx-prism-2

3.0.7 (2021-03-27)

Note: Version bump only for package mdx-prism-2

3.0.6 (2021-03-22)

Note: Version bump only for package mdx-prism-2

3.0.5 (2021-03-22)

Note: Version bump only for package mdx-prism-2

3.0.4 (2021-03-22)

Note: Version bump only for package mdx-prism-2

3.0.3 (2021-03-22)

Note: Version bump only for package mdx-prism-2

3.0.2 (2021-03-21)

Note: Version bump only for package mdx-prism-2

3.0.1 (2021-03-21)

Note: Version bump only for package mdx-prism-2

3.0.0 (2021-03-19)

Bug Fixes

chore

  • deps: upgrade all dependencies to latest version (7c1b319)

Features

  • copy updated addMarker script (d7b2e0a)

BREAKING CHANGES

  • deps: Despite the tests are passing and works in existing application, some deps were very out to date.

Changelog

0.3.1+fork.0.1.0

  • Fork from @mapbox/rehype-prism
  • Add markers following react-refractor example
  • Add highlight line support following gatsby-remark-prismjs example

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.