jonschlinkert/remarkable

README

remarkable


Markdown parser done right. Fast and easy to extend.


Live demo

Supports the CommonMark spec + syntax extensions + sugar (URL autolinking, typographer).
Configurable syntax! You can add new rules and even replace existing ones.
High speed !
Community plugins on npm.

Install


node.js:

  1. ``` shell
  2. npm install remarkable --save
  3. ```

browser (CDN):

jsDeliver CDN
cdnjs

Usage


  1. ``` js
  2. import { Remarkable } from 'remarkable';
  3. var md = new Remarkable();

  4. console.log(md.render('# Remarkable rulezz!'));
  5. // =>

    Remarkable rulezz!

  6. ```

or with commonjs

  1. ``` js
  2. const { Remarkable } = require('remarkable');
  3. var md = new Remarkable();

  4. console.log(md.render('# Remarkable rulezz!'));
  5. // =>

    Remarkable rulezz!

  6. ```

If installed globally with npm :

  1. ``` shell
  2. cat myfile.md | remarkable
  3. remarkable --file myfile.md

  4. # get options
  5. remarkable -h
  6. ```

Documentation


See the docs directory for documentation on the following topics:

parser
parsing_block
parsing_core
parsing_inline
plugins
renderer

Options


By default, remarkable is configured to be similar to GFM, but with HTML disabled. This is easy to change if you prefer different settings.

There are two ways to define options.

constructor


Define options in the constructor:

  1. ``` js
  2. // Actual default values
  3. var md = new Remarkable({
  4.   html:         false,        // Enable HTML tags in source
  5.   xhtmlOut:     false,        // Use '/' to close single tags (
    )
  6.   breaks:       false,        // Convert '\n' in paragraphs into
  7.   langPrefix:   'language-',  // CSS language prefix for fenced blocks

  8.   // Enable some language-neutral replacement + quotes beautification
  9.   typographer:  false,

  10.   // Double + single quotes replacement pairs, when typographer enabled,
  11.   // and smartquotes on. Set doubles to '«»' for Russian, '„“' for German.
  12.   quotes: '“”‘’',

  13.   // Highlighter function. Should return escaped HTML,
  14.   // or '' if the source string is not changed
  15.   highlight: function (/*str, lang*/) { return ''; }
  16. });

  17. console.log(md.render('# Remarkable rulezz!'));
  18. // =>

    Remarkable rulezz!

  19. ```

.set


Or define options via the .set() method:

  1. ``` js
  2. import { Remarkable } from 'remarkable';

  3. var md = new Remarkable();

  4. md.set({
  5.   html: true,
  6.   breaks: true
  7. });
  8. ```

Note:To achieve the best possible performance, don't modify a Remarkable instance on the fly. If you need multiple configurations, create multiple instances and initialize each with a configuration that is ideal for that instance.

Presets


Remarkable offers some "presets" as a convenience to quickly enable/disable active syntax rules and options for common use cases.

commonmark


Enable strict CommonMark mode with the commonmark preset:

  1. ``` js
  2. import { Remarkable } from 'remarkable';
  3. var md = new Remarkable('commonmark');
  4. ```

full


Enable all available rules (but still with default options, if not set):

  1. ``` js
  2. import { Remarkable } from 'remarkable';
  3. var md = new Remarkable('full');

  4. // Or with options:
  5. var md = new Remarkable('full', {
  6.   html: true,
  7.   typographer: true
  8. });
  9. ```

Syntax highlighting


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

  1. ``` js
  2. import { Remarkable } from 'remarkable';
  3. import hljs from 'highlight.js' // https://highlightjs.org/

  4. // Actual default values
  5. var md = new Remarkable({
  6.   highlight: function (str, lang) {
  7.     if (lang && hljs.getLanguage(lang)) {
  8.       try {
  9.         return hljs.highlight(lang, str).value;
  10.       } catch (err) {}
  11.     }

  12.     try {
  13.       return hljs.highlightAuto(str).value;
  14.     } catch (err) {}

  15.     return ''; // use external default escaping
  16.   }
  17. });
  18. ```

Syntax extensions


Enabled by default:

Footnotes
Tables (GFM)
<del> (GFM strikethrough) - `deleted text`

Disabled by default:

<sup> - 19^th^
<sub> - `H2O`
abbreviations
<ins>- ++inserted text++ (experimental)
<mark>- ==marked text== (experimental)

HEADS UP!: Experimental extensions can be changed later for something like Critic Markup, but you will still be able to use old-style rules via external plugins if you prefer.

Manage rules


  1. ``` js
  2. var md = new Remarkable();
  3. md.inline.ruler.enable([ 'ins', 'mark' ]);
  4. md.block.ruler.disable([ 'table', 'footnote' ]);

  5. // Enable everything
  6. md = new Remarkable('full', {
  7.   html: true,
  8.   typographer: true,
  9. });

  10. //
  11. // Manually enable rules, disabled by default:
  12. //
  13. var md = new Remarkable();
  14. md.core.ruler.enable([
  15.   'abbr'
  16. ]);
  17. md.block.ruler.enable([
  18.   'footnote',
  19.   'deflist'
  20. ]);
  21. md.inline.ruler.enable([
  22.   'footnote_inline',
  23.   'ins',
  24.   'mark',
  25.   'sub',
  26.   'sup'
  27. ]);
  28. ```

Typographer


Although full-weight typographical replacements are language specific, remarkable provides coverage for the most common and universal use cases:

  1. ``` js
  2. import { Remarkable } from 'remarkable';
  3. var md = new Remarkable({
  4.   typographer: true,
  5.   quotes: '“”‘’'
  6. });

  7. // Disable rules at all:
  8. md.core.ruler.disable([ 'replacements', 'smartquotes' ]);

  9. // Actual default replacements:
  10. //
  11. // '' → ‘’
  12. // "" → “”. Set '«»' for Russian, '„“' for German, empty to disable
  13. // (c) (C) → ©
  14. // (tm) (TM) → ™
  15. // (r) (R) → ®
  16. // +- → ±
  17. // (p) (P) -> §
  18. // ... → … (also ?.... → ?.., !.... → !..)
  19. // ???????? → ???, !!!!! → !!!, `,,` → `,`
  20. // -- → –, --- → —
  21. //
  22. ```

Of course, you can also add your own rules or replace the defaults with something more advanced or specific to your language.

Plugins


Easily load plugins with the .use() method:

  1. ``` js
  2. var md = new Remarkable();

  3. md.use(plugin1)
  4.   .use(plugin2, opts)
  5.   .use(plugin3);
  6. ```

Please refer to the plugin documentation to create your own plugins.

linkify plugin


Autoconvert URL-like text to links

  1. ``` js
  2. import { Remarkable } from 'remarkable';
  3. import { linkify } from 'remarkable/linkify';

  4. var md = new Remarkable().use(linkify);
  5. ```

UMD


UMD bundle provides linkify out of the box

  1. ``` js
  2. const { Remarkable, linkify, utils } = window.remarkable;
  3. ```

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.

Development / Modification


Parser consists of several responsibility chains filled with rules. You can reconfigure any of them as you wish. Renderer also can be modified and extended. See source code to understand details. Pay attention to these properties:

  1. ``` js
  2. Remarkable.core
  3. Remarkable.core.ruler
  4. Remarkable.block
  5. Remarkable.block.ruler
  6. Remarkable.inline
  7. Remarkable.inline.ruler
  8. Remarkable.renderer
  9. Remarkable.renderer.rules
  10. ```

Benchmark


Here is result of CommonMark spec parse at Core i5 2.4 GHz (i5-4258U):

  1. ``` shell
  2. $ benchmark/benchmark.js spec
  3. Selected samples: (1 of 27)
  4. > spec

  5. Sample: spec.txt (110610 bytes)
  6. > commonmark-reference x 40.42 ops/sec ±4.07% (51 runs sampled)
  7. > current x 74.99 ops/sec ±4.69% (67 runs sampled)
  8. > current-commonmark x 93.76 ops/sec ±1.23% (79 runs sampled)
  9. > marked-0.3.2 x 22.92 ops/sec ±0.79% (41 runs sampled)
  10. ```

As you can see, remarkable doesn't pay with speed for its flexibility. Because it's written in monomorphic style and uses JIT inline caches effectively.

Authors


Jon Schlinkert github/jonschlinkert
Alex Kocharin github/rlidwka
Vitaly Puzrin github/puzrin

License


MIT