markdown-it

README

markdown-it


CI

Markdown parser done right. Fast and easy to extend.


Live demo

Follows the CommonMark spec + adds syntax extensions & sugar (URL autolinking, typographer).
Configurable syntax! You can add new rules and even replace existing ones.
High speed.
Safe by default.
Community-written plugins and other packages on npm.

Table of content

Install
Usage examples
Simple
Init with presets and options
Plugins load
Syntax highlighting
Linkify

API
Syntax extensions
Manage rules

Benchmark
markdown-it for enterprise
Authors
References / Thanks

Install


node.js:

  1. ``` shell
  2. npm install markdown-it --save
  3. ```

browser (CDN):

jsDeliver CDN
cdnjs.com CDN

Usage examples


See also:

API documentation * - for more info and examples.
Development info - for plugins writers.

Simple


  1. ``` js
  2. // node.js, "classic" way:
  3. var MarkdownIt = require('markdown-it'),
  4.     md = new MarkdownIt();
  5. var result = md.render('# markdown-it rulezz!');

  6. // node.js, the same, but with sugar:
  7. var md = require('markdown-it')();
  8. var result = md.render('# markdown-it rulezz!');

  9. // browser without AMD, added to "window" on script load
  10. // Note, there is no dash in "markdownit".
  11. var md = window.markdownit();
  12. var result = md.render('# markdown-it rulezz!');
  13. ```

Single line rendering, without paragraph wrap:

  1. ``` js
  2. var md = require('markdown-it')();
  3. var result = md.renderInline('__markdown-it__ rulezz!');
  4. ```

Init with presets and options


(*) presets define combinations of active rules and options. Can be "commonmark", "zero" or "default" (if skipped). See API docs for more details.

  1. ``` js
  2. // commonmark mode
  3. var md = require('markdown-it')('commonmark');

  4. // default mode
  5. var md = require('markdown-it')();

  6. // enable everything
  7. var md = require('markdown-it')({
  8.   html: true,
  9.   linkify: true,
  10.   typographer: true
  11. });

  12. // full options list (defaults)
  13. var md = require('markdown-it')({
  14.   html:         false,        // Enable HTML tags in source
  15.   xhtmlOut:     false,        // Use '/' to close single tags (
    ).
  16.                               // This is only for full CommonMark compatibility.
  17.   breaks:       false,        // Convert '\n' in paragraphs into
  18.   langPrefix:   'language-',  // CSS language prefix for fenced blocks. Can be
  19.                               // useful for external highlighters.
  20.   linkify:      false,        // Autoconvert URL-like text to links

  21.   // Enable some language-neutral replacement + quotes beautification
  22.   // For the full list of replacements, see https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js
  23.   typographer:  false,

  24.   // Double + single quotes replacement pairs, when typographer enabled,
  25.   // and smartquotes on. Could be either a String or an Array.
  26.   //
  27.   // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
  28.   // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
  29.   quotes: '“”‘’',

  30.   // Highlighter function. Should return escaped HTML,
  31.   // or '' if the source string is not changed and should be escaped externally.
  32.   // If result starts with
  33.   highlight: function (/*str, lang*/) { return ''; }
  34. });
  35. ```

Plugins load


  1. ``` js
  2. var md = require('markdown-it')()
  3.             .use(plugin1)
  4.             .use(plugin2, opts, ...)
  5.             .use(plugin3);
  6. ```

Syntax highlighting


Apply syntax highlighting to fenced code blocks with the highlight option:

  1. ``` js
  2. var hljs = require('highlight.js'); // https://highlightjs.org

  3. // Actual default values
  4. var md = require('markdown-it')({
  5.   highlight: function (str, lang) {
  6.     if (lang && hljs.getLanguage(lang)) {
  7.       try {
  8.         return hljs.highlight(str, { language: lang }).value;
  9.       } catch (__) {}
  10.     }

  11.     return ''; // use external default escaping
  12.   }
  13. });
  14. ```

Or with full wrapper override (if you need assign class to <pre> ):

  1. ``` js
  2. var hljs = require('highlight.js'); // https://highlightjs.org

  3. // Actual default values
  4. var md = require('markdown-it')({
  5.   highlight: function (str, lang) {
  6.     if (lang && hljs.getLanguage(lang)) {
  7.       try {
  8.         return '<pre class="hljs"><code>' +
  9.                hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
  10.                '</code></pre>';
  11.       } catch (__) {}
  12.     }

  13.     return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
  14.   }
  15. });
  16. ```

Linkify


linkify: true uses linkify-it. To configure linkify-it, access the linkify instance through md.linkify :

  1. ``` js
  2. md.linkify.set({ fuzzyEmail: false });  // disables converting email to link
  3. ```

API


API documentation

If you are going to write plugins, please take a look at Development info.

Syntax extensions


Embedded (enabled by default):

Tables (GFM)
Strikethrough (GFM)

Via plugins:

subscript
superscript
footnote
definition list
abbreviation
emoji
custom container
insert
mark
... and others

Manage rules


By default all rules are enabled, but can be restricted by options. On plugin load all its rules are enabled automatically.

  1. ``` js
  2. // Activate/deactivate rules, with currying
  3. var md = require('markdown-it')()
  4.             .disable([ 'link', 'image' ])
  5.             .enable([ 'link' ])
  6.             .enable('image');

  7. // Enable everything
  8. md = require('markdown-it')({
  9.   html: true,
  10.   linkify: true,
  11.   typographer: true,
  12. });
  13. ```

You can find all rules in sources:

parser_core.js
parser_block.js
parser_inline.js

Benchmark


Here is the result of readme parse at MB Pro Retina 2013 (2.4 GHz):

  1. ``` shell
  2. make benchmark-deps
  3. benchmark/benchmark.js readme

  4. Selected samples: (1 of 28)
  5. > README

  6. Sample: README.md (7774 bytes)
  7. > commonmark-reference x 1,222 ops/sec ±0.96% (97 runs sampled)
  8. > current x 743 ops/sec ±0.84% (97 runs sampled)
  9. > current-commonmark x 1,568 ops/sec ±0.84% (98 runs sampled)
  10. > marked x 1,587 ops/sec ±4.31% (93 runs sampled)
  11. ```

Note.CommonMark version runs with simplified link normalizers for more "honest" compare. Difference is ≈1.5×.

As you can see, markdown-it doesn't pay with speed for its flexibility. Slowdown of "full" version caused by additional features not available in other implementations.

markdown-it for enterprise


Available as part of the Tidelift Subscription.

The maintainers of markdown-it and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

Authors


Alex Kocharin github/rlidwka
Vitaly Puzrin github/puzrin

markdown-itis the result of the decision of the authors who contributed to 99% of the Remarkablecode to move to a project with the same authorship but new leadership (Vitaly and Alex). It's not a fork.

References / Thanks


Big thanks to John MacFarlane for his work on the CommonMark spec and reference implementations. His work saved us a lot of time during this project's development.

Related Links:

https://github.com/jgm/CommonMark - reference CommonMark implementations in C & JS, also contains latest spec & online demo.
http://talk.commonmark.org - CommonMark forum, good place to collaborate developers' efforts.

Ports

motion-markdown-it - Ruby/RubyMotion
markdown-it-py - Python