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

Package detail

markdown-it-highlightjs

valeriangalliat121.4kUnlicense4.2.0TypeScript support: included

Preset to use highlight.js with markdown-it.

markdown-it-plugin

readme

markdown-it-highlightjs npm version

Preset to use highlight.js with markdown-it.

Usage

const md = require('markdown-it')()
  .use(require('markdown-it-highlightjs'), opts)

// All code blocks will be highlighted.

The opts object can contain:

Name Type Description Default
auto boolean Whether to automatically detect language if not specified. true
code boolean Whether to add the hljs class to raw code blocks (not fenced blocks). true
register object Register other languages which are not included in the standard pack. null
inline boolean Whether to highlight inline code. false
hljs object Provide the instance of highlight.js to use for highlighting require('highlight.js')
ignoreIllegals boolean Forces highlighting to finish even in case of detecting illegal syntax for the language instead of throwing an exception. true

Register languages

const md = require('markdown-it')()
  .use(require('markdown-it-highlightjs'), {
    register: {
      cypher: require('highlightjs-cypher')
    }
  })

Inline code highlighting

You can enable inline code highlighting by setting inline to true:

const md = require('markdown-it')()
  .use(require('markdown-it-highlightjs'), { inline: true })

You can specify the language for inline code using Pandoc syntax:

`x=4`{.js}

Or kramdown IAL syntax:

`x=4`{:.js}

If you do not specify a language, then highlight.js will attempt to guess the language if auto is true (which it is by default).

Usage with markdown-it-attrs

If you use markdown-it-attrs, make sure to include it after markdown-it-highlightjs if you want inline code highlighting to work:

const md = require('markdown-it')()
  .use(require('markdown-it-highlightjs'), { inline: true })
  .use(require('markdown-it-attrs'))

Provide the highlight.js instance

You can specify the hljs option to override the default highlight.js instance with your own:

const hljs = require('highlight.js/lib/core')

hljs.registerLanguage(
  'javascript',
  require('highlight.js/lib/languages/javascript')
)

const md = require('markdown-it')()
  .use(require('markdown-it-highlightjs'), { hljs })

Core plugin

You may import the core markdown-it-highlightjs plugin directly, without any default options. You must specify an instance of highlight.js for the hljs option.

const hljs = require('highlight.js/lib/core')

const md = require('markdown-it')()
  .use(require('markdown-it-highlightjs/core'), { hljs })

changelog

Changelog

This project adheres to Semantic Versioning.

Unreleased

4.2.0 - 2024-09-18

  • Support multiple <code> tags in the same <pre>. (#33, #34)

4.1.0 - 2024-05-16

  • Upgrade highlight.js to 11.9.0. (#29)

4.0.1 - 2022-04-11

  • Fix dist directory not being published.

4.0.0 - 2022-04-11

  • Rewrite in TypeScript. (#21)
  • Use Mocha as test runner.
  • Compatibility with markdown-it-attrs. (#24)
  • Potentially breaking: add hljs class to inline code when inline mode is enabled. (#25)
  • Potentially breaking: return plain code when highlighting fails in inline mode (instead of an empty string).

3.6.0 - 2021-12-26

  • Update highlight.js to version 11. (#22)

3.5.0 - 2021-05-16

  • Update to use new highlight.js API after old way got deprecated. (#20)

3.4.0 - 2020-12-04

  • Allow injecting the highlight.js instance. (#15)

3.3.1 - 2020-11-15

  • Proper escaping of language name. (#14)

3.3.0 - 2020-09-25

  • Upgrade highlight.js and support langPrefix option. (#11)

3.2.0 - 2020-06-01

  • Support inline code highlighting. (#10)

3.1.0 - 2020-03-05

  • Update dependencies. (#6)
  • Fix "unknown language" error. (#8)
  • Allow to register extra languages. (#9)

3.0.0 - 2017-02-26

  • Update highlight.js. (#2)
  • Drop Babel. This drops support for Node.js versions that doesn't support ES6.

2.0.0 - 2015-06-29

  • Also add the hljs class to indented code blocks (together with fenced code blocks). This new behavior can be disabled with the code option.
  • Never automatically detect the language if a language was specified, even if not recognized.
  • Add an auto option to control if fenced code without language should be automatically highlighted.

1.1.2 - 2015-05-25

  • Update license format in package.json.

1.1.1 - 2015-03-27

  • Ensure hljs class is set even if no language is given.

1.1.0 - 2015-03-17

  • Add hljs class and langPrefix to code blocks.

1.0.0 - 2015-03-17

  • Initial release.