UNPKG

10.2 kBMarkdownView Raw
1# markdown-it <!-- omit in toc -->
2
3[![CI](https://github.com/markdown-it/markdown-it/workflows/CI/badge.svg)](https://github.com/markdown-it/markdown-it/actions)
4[![NPM version](https://img.shields.io/npm/v/markdown-it.svg?style=flat)](https://www.npmjs.org/package/markdown-it)
5[![Coverage Status](https://coveralls.io/repos/markdown-it/markdown-it/badge.svg?branch=master&service=github)](https://coveralls.io/github/markdown-it/markdown-it?branch=master)
6[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/markdown-it/markdown-it)
7
8> Markdown parser done right. Fast and easy to extend.
9
10__[Live demo](https://markdown-it.github.io)__
11
12- Follows the __[CommonMark spec](http://spec.commonmark.org/)__ + adds syntax extensions & sugar (URL autolinking, typographer).
13- Configurable syntax! You can add new rules and even replace existing ones.
14- High speed.
15- [Safe](https://github.com/markdown-it/markdown-it/tree/master/docs/security.md) by default.
16- Community-written __[plugins](https://www.npmjs.org/browse/keyword/markdown-it-plugin)__ and [other packages](https://www.npmjs.org/browse/keyword/markdown-it) on npm.
17
18__Table of content__
19
20- [Install](#install)
21- [Usage examples](#usage-examples)
22 - [Simple](#simple)
23 - [Init with presets and options](#init-with-presets-and-options)
24 - [Plugins load](#plugins-load)
25 - [Syntax highlighting](#syntax-highlighting)
26 - [Linkify](#linkify)
27- [API](#api)
28- [Syntax extensions](#syntax-extensions)
29 - [Manage rules](#manage-rules)
30- [Benchmark](#benchmark)
31- [markdown-it for enterprise](#markdown-it-for-enterprise)
32- [Authors](#authors)
33- [References / Thanks](#references--thanks)
34
35## Install
36
37**node.js**:
38
39```bash
40npm install markdown-it --save
41```
42
43**browser (CDN):**
44
45- [jsDeliver CDN](http://www.jsdelivr.com/#!markdown-it "jsDelivr CDN")
46- [cdnjs.com CDN](https://cdnjs.com/libraries/markdown-it "cdnjs.com")
47
48
49## Usage examples
50
51See also:
52
53- __[API documentation](https://markdown-it.github.io/markdown-it/)__ - for more
54 info and examples.
55- [Development info](https://github.com/markdown-it/markdown-it/tree/master/docs) -
56 for plugins writers.
57
58
59### Simple
60
61```js
62// node.js, "classic" way:
63var MarkdownIt = require('markdown-it'),
64 md = new MarkdownIt();
65var result = md.render('# markdown-it rulezz!');
66
67// node.js, the same, but with sugar:
68var md = require('markdown-it')();
69var result = md.render('# markdown-it rulezz!');
70
71// browser without AMD, added to "window" on script load
72// Note, there is no dash in "markdownit".
73var md = window.markdownit();
74var result = md.render('# markdown-it rulezz!');
75```
76
77Single line rendering, without paragraph wrap:
78
79```js
80var md = require('markdown-it')();
81var result = md.renderInline('__markdown-it__ rulezz!');
82```
83
84
85### Init with presets and options
86
87(*) presets define combinations of active rules and options. Can be
88`"commonmark"`, `"zero"` or `"default"` (if skipped). See
89[API docs](https://markdown-it.github.io/markdown-it/#MarkdownIt.new) for more details.
90
91```js
92// commonmark mode
93var md = require('markdown-it')('commonmark');
94
95// default mode
96var md = require('markdown-it')();
97
98// enable everything
99var md = require('markdown-it')({
100 html: true,
101 linkify: true,
102 typographer: true
103});
104
105// full options list (defaults)
106var md = require('markdown-it')({
107 html: false, // Enable HTML tags in source
108 xhtmlOut: false, // Use '/' to close single tags (<br />).
109 // This is only for full CommonMark compatibility.
110 breaks: false, // Convert '\n' in paragraphs into <br>
111 langPrefix: 'language-', // CSS language prefix for fenced blocks. Can be
112 // useful for external highlighters.
113 linkify: false, // Autoconvert URL-like text to links
114
115 // Enable some language-neutral replacement + quotes beautification
116 // For the full list of replacements, see https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js
117 typographer: false,
118
119 // Double + single quotes replacement pairs, when typographer enabled,
120 // and smartquotes on. Could be either a String or an Array.
121 //
122 // For example, you can use '«»„“' for Russian, '„“‚‘' for German,
123 // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
124 quotes: '“”‘’',
125
126 // Highlighter function. Should return escaped HTML,
127 // or '' if the source string is not changed and should be escaped externally.
128 // If result starts with <pre... internal wrapper is skipped.
129 highlight: function (/*str, lang*/) { return ''; }
130});
131```
132
133### Plugins load
134
135```js
136var md = require('markdown-it')()
137 .use(plugin1)
138 .use(plugin2, opts, ...)
139 .use(plugin3);
140```
141
142
143### Syntax highlighting
144
145Apply syntax highlighting to fenced code blocks with the `highlight` option:
146
147```js
148var hljs = require('highlight.js'); // https://highlightjs.org/
149
150// Actual default values
151var md = require('markdown-it')({
152 highlight: function (str, lang) {
153 if (lang && hljs.getLanguage(lang)) {
154 try {
155 return hljs.highlight(str, { language: lang }).value;
156 } catch (__) {}
157 }
158
159 return ''; // use external default escaping
160 }
161});
162```
163
164Or with full wrapper override (if you need assign class to `<pre>`):
165
166```js
167var hljs = require('highlight.js'); // https://highlightjs.org/
168
169// Actual default values
170var md = require('markdown-it')({
171 highlight: function (str, lang) {
172 if (lang && hljs.getLanguage(lang)) {
173 try {
174 return '<pre class="hljs"><code>' +
175 hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
176 '</code></pre>';
177 } catch (__) {}
178 }
179
180 return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
181 }
182});
183```
184
185### Linkify
186
187`linkify: true` uses [linkify-it](https://github.com/markdown-it/linkify-it). To
188configure linkify-it, access the linkify instance through `md.linkify`:
189
190```js
191md.linkify.set({ fuzzyEmail: false }); // disables converting email to link
192```
193
194
195## API
196
197__[API documentation](https://markdown-it.github.io/markdown-it/)__
198
199If you are going to write plugins - take a look at
200[Development info](https://github.com/markdown-it/markdown-it/tree/master/docs).
201
202
203## Syntax extensions
204
205Embedded (enabled by default):
206
207- [Tables](https://help.github.com/articles/organizing-information-with-tables/) (GFM)
208- [Strikethrough](https://help.github.com/articles/basic-writing-and-formatting-syntax/#styling-text) (GFM)
209
210Via plugins:
211
212- [subscript](https://github.com/markdown-it/markdown-it-sub)
213- [superscript](https://github.com/markdown-it/markdown-it-sup)
214- [footnote](https://github.com/markdown-it/markdown-it-footnote)
215- [definition list](https://github.com/markdown-it/markdown-it-deflist)
216- [abbreviation](https://github.com/markdown-it/markdown-it-abbr)
217- [emoji](https://github.com/markdown-it/markdown-it-emoji)
218- [custom container](https://github.com/markdown-it/markdown-it-container)
219- [insert](https://github.com/markdown-it/markdown-it-ins)
220- [mark](https://github.com/markdown-it/markdown-it-mark)
221- ... and [others](https://www.npmjs.org/browse/keyword/markdown-it-plugin)
222
223
224### Manage rules
225
226By default all rules are enabled, but can be restricted by options. On plugin
227load all its rules are enabled automatically.
228
229```js
230// Activate/deactivate rules, with curring
231var md = require('markdown-it')()
232 .disable([ 'link', 'image' ])
233 .enable([ 'link' ])
234 .enable('image');
235
236// Enable everything
237md = require('markdown-it')({
238 html: true,
239 linkify: true,
240 typographer: true,
241});
242```
243
244You can find all rules in sources:
245[parser_core.js](lib/parser_core.js), [parser_block](lib/parser_block.js),
246[parser_inline](lib/parser_inline.js).
247
248
249## Benchmark
250
251Here is the result of readme parse at MB Pro Retina 2013 (2.4 GHz):
252
253```bash
254make benchmark-deps
255benchmark/benchmark.js readme
256
257Selected samples: (1 of 28)
258 > README
259
260Sample: README.md (7774 bytes)
261 > commonmark-reference x 1,222 ops/sec ±0.96% (97 runs sampled)
262 > current x 743 ops/sec ±0.84% (97 runs sampled)
263 > current-commonmark x 1,568 ops/sec ±0.84% (98 runs sampled)
264 > marked x 1,587 ops/sec ±4.31% (93 runs sampled)
265```
266
267__Note.__ CommonMark version runs with [simplified link normalizers](https://github.com/markdown-it/markdown-it/blob/master/benchmark/implementations/current-commonmark/index.js)
268for more "honest" compare. Difference is ~ 1.5x.
269
270As you can see, `markdown-it` doesn't pay with speed for it's flexibility.
271Slowdown of "full" version caused by additional features not available in
272other implementations.
273
274
275## markdown-it for enterprise
276
277Available as part of the Tidelift Subscription.
278
279The 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.](https://tidelift.com/subscription/pkg/npm-markdown-it?utm_source=npm-markdown-it&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
280
281
282## Authors
283
284- Alex Kocharin [github/rlidwka](https://github.com/rlidwka)
285- Vitaly Puzrin [github/puzrin](https://github.com/puzrin)
286
287_markdown-it_ is the result of the decision of the authors who contributed to
28899% of the _Remarkable_ code to move to a project with the same authorship but
289new leadership (Vitaly and Alex). It's not a fork.
290
291## References / Thanks
292
293Big thanks to [John MacFarlane](https://github.com/jgm) for his work on the
294CommonMark spec and reference implementations. His work saved us a lot of time
295during this project's development.
296
297**Related Links:**
298
299- https://github.com/jgm/CommonMark - reference CommonMark implementations in C & JS,
300 also contains latest spec & online demo.
301- http://talk.commonmark.org - CommonMark forum, good place to collaborate
302 developers' efforts.
303
304**Ports**
305
306- [motion-markdown-it](https://github.com/digitalmoksha/motion-markdown-it) - Ruby/RubyMotion
307- [markdown-it-py](https://github.com/ExecutableBookProject/markdown-it-py)- Python