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

Package detail

markdown-it-prism

jGleitz33.9kMIT3.0.0TypeScript support: included

Highlights code blocks in markdown-it using Prism.

markdown-it, markdown-it-plugin, highlight, prism, prismjs

readme

markdown-it-prism Build Status npm version Bower version

markdown-it plugin to highlight code blocks using Prism

Usage

const md = require('markdown-it')();
const prism = require('markdown-it-prism');

md.use(prism, options);

The plugin will insert the necessary markup into all code blocks. Include one of Prism’s stylesheets in your HTML to get highlighted code.

Options

The options object may contain:

Name Description Default
highlightInlineCode Whether to highlight inline code. false
plugins Array of Prism Plugins to load. The names to use can be found here. Please note that some prism plugins (notably line-numbers) rely on the DOM being present and can thus not be used with this package (see #1). []
init A function called after setting up prism. Will receive the prism instance as only argument. Useful for plugins needing further intialisation. () => {}
defaultLanguageForUnknown The language to use for code blocks that specify a language that Prism does not know. No default will be used if this option is undefined. undefined
defaultLanguageForUnspecified The language to use for code block that do not specify a language. No default will be used if this option is undefined. undefined
defaultLanguage Shorthand to set both defaultLanguageForUnknown and defaultLanguageForUnspecified to the same value. undefined

Inline Code

When highlightInlineCode is set, inline code will be highlighted just like fenced code blocks are. To specifiy the language of inline code, add {language=<your-language>} after the code segment:

`class Demo { };`{language=cpp}

This syntax is compatible with markdown-it-attrs: The language=<x> part will be stripped, but everything else between { and } will work with markdown-it-attrs as usual.

Usage with Webpack

If you want to use this plugin together with Webpack, you need to import all languages you intend to use:

import MarkdownIt from 'markdown-it';
import prism from 'markdown-it-prism';

import "prismjs/components/prism-clike"
import "prismjs/components/prism-java"

function component() {
    const md = new MarkdownIt();
    md.use(prism);
    const element = document.createElement('div');
    element.innerHTML = md.render(`
Here is some *code*:
\`\`\`java
public class Test {
  public void foo() {}
}
\`\`\`
`);

    return element;
}

document.body.appendChild(component());

Beware: Prisms languages have dependencies onto each other. You need to import the languages together with their dependencies in the correct order.

changelog

Changelog

This project adheres to Semantic Versioning.

3.0.0 (2025-04-01)

  • fix!: require Node.JS >=v20, test against v20-v23 (0f68ca1)

BREAKING CHANGES

  • This package now requires Node.JS >= v20

2.3.1 (2025-03-11)

Bug Fixes

  • deps: update dependency prismjs to v1.30.0 [security] (#917) (cb4bfeb)

2.3.0 (2022-08-30)

Features

2.2.5 (2022-08-23)

Bug Fixes

  • deps: update dependency prismjs to v1.29.0 (f20d725)

2.2.4 (2022-04-17)

Bug Fixes

  • deps: update dependency prismjs to v1.28.0 (a12eba5)

2.2.3 (2022-02-18)

Bug Fixes

  • deps: update dependency prismjs to v1.27.0 (9d51407)

2.2.2 (2022-01-06)

Bug Fixes

  • deps: update dependency prismjs to v1.26.0 (a246096)

2.2.1 (2021-09-16)

Bug Fixes

  • deps: update dependency prismjs to v1.25.0 (49fb336)

2.2.0 (2021-08-23)

Features

  • compatibility with markdown-it-attrs (9b2eca1)

2.1.8 (2021-07-03)

Bug Fixes

  • deps: update dependency prismjs to v1.24.1 (15940ed)

2.1.7 (2021-06-27)

Bug Fixes

  • deps: update dependency prismjs to v1.24.0 (aff5137)

2.1.6 (2021-03-06)

Bug Fixes

  • declare MIT licence in package.json (6871e22)

2.1.5 (2021-03-06)

Bug Fixes

  • license: re-license under MIT (7151b08)

2.1.4 (2020-12-31)

Bug Fixes

  • deps: update dependency prismjs to v1.23.0 (2c48e75)

2.1.3 (2020-10-10)

Bug Fixes

  • deps: update dependency prismjs to v1.22.0 (e8265b8)

2.1.2 (2020-09-29)

Bug Fixes

  • escape the language class name so it cannot be used to inject HTML (#137) (c1c074b)

2.1.1 (2020-08-07)

Bug Fixes

  • deps: update dependency prismjs to v1.21.0 (d494125)

2.1.0 (2020-07-09)

Bug Fixes

  • only require Node.JS >= 6 (5a4176c)

Features

  • publish Typescript types (35ba58b)

2.0.6 (2020-07-06)

Bug Fixes

  • deps: update dependency prismjs to v1.20.0 (a68434f)

2.0.5 (2020-03-02)

Bug Fixes

  • node >=10.0.0 is supported (1a442a0)

2.0.4 (2020-02-18)

  • Updated Prism JS to 1.19

2.0.3 (2019-09-07)

  • Updated dependencies

2.0.2 (2019-05-29)

Fixed

  • Languages that have dependencies are now resolved correctly (#22, thanks to @jlice and @RunDevelopment!)
  • Test cases have been adapted to new output of markdown-it prism Java

2.0.1 (2018-12-09)

Fixed

  • Regression that exported the module’s main function only under the default key, not also as default export (#17)

2.0.0 (2018-12-02)

Deprecated

  • Deprecate Node.JS 4 and require Node.JS >= 6

    Added

  • Note about adding a stylesheet to the HTML in README
  • Options defaultLanguage, defaultLanguageForUnknown and defaultLanguageForUnspecified to specify fallback prism languages

    Fixed

  • Update all packages and eliminate dependency versions with known security issues
  • Improve the project setup
  • Improve the documentation in code

1.1.2 (2018-03-17)

  • Upgrade dependencies

1.1.1 (2017-05-22)

  • Removed a bad \n\t before the code that was affecting the HTML output.

1.1.0 (2017-05-21)

  • Add the language-* class also to the <pre> tag (not only to <code>).
  • Add the class language-x if x is the defined language, even if x cannot be highlighted with Prism.

1.0.1 (2016-10-19)

  • Fixed build bug that led to the plugin not being exported correctly.

1.0.0 (2016-10-16)

  • Initial release.