|
1 |
| -<!-- styles hack until ndoc updated --> |
2 |
| -<style> |
3 |
| -header .name_prefix { font-weight: normal; } |
4 |
| -</style> |
| 1 | +# markdown-it |
5 | 2 |
|
6 |
| -# markdown-it API |
| 3 | +## Install |
7 | 4 |
|
8 |
| -### Simple use |
| 5 | +**node.js** & **bower**: |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install markdown-it --save |
| 9 | +bower install markdown-it --save |
| 10 | +``` |
| 11 | + |
| 12 | +**browser (CDN):** |
| 13 | + |
| 14 | +- [jsDeliver CDN](http://www.jsdelivr.com/#!markdown-it "jsDelivr CDN") |
| 15 | +- [cdnjs.com CDN](https://cdnjs.com/libraries/markdown-it "cdnjs.com") |
| 16 | + |
| 17 | + |
| 18 | +## Usage examples |
| 19 | + |
| 20 | +See also: |
| 21 | + |
| 22 | +- [Development info](https://github.com/markdown-it/markdown-it/tree/master/docs) - |
| 23 | + for plugins writers. |
9 | 24 |
|
10 |
| -In most cases you will use `markdown-it` in very simple way: |
11 | 25 |
|
12 |
| -```javascript |
| 26 | +### Simple |
| 27 | + |
| 28 | +```js |
| 29 | +// node.js, "classic" way: |
| 30 | +var MarkdownIt = require('markdown-it'), |
| 31 | + md = new MarkdownIt(); |
| 32 | +var result = md.render('# markdown-it rulezz!'); |
| 33 | + |
| 34 | +// node.js, the same, but with sugar: |
13 | 35 | var md = require('markdown-it')();
|
| 36 | +var result = md.render('# markdown-it rulezz!'); |
| 37 | + |
| 38 | +// browser without AMD, added to "window" on script load |
| 39 | +// Note, there is no dash in "markdownit". |
| 40 | +var md = window.markdownit(); |
| 41 | +var result = md.render('# markdown-it rulezz!'); |
| 42 | +``` |
14 | 43 |
|
15 |
| -var result = md.render('your_markdown_string'); |
| 44 | +Single line rendering, without paragraph wrap: |
16 | 45 |
|
17 |
| -// Or for inline (without paragraths & blocks) |
18 |
| -var resultInline = md.renderInline('your_markdown_inline_string'); |
| 46 | +```js |
| 47 | +var md = require('markdown-it')(); |
| 48 | +var result = md.renderInline('__markdown-it__ rulezz!'); |
19 | 49 | ```
|
20 | 50 |
|
21 |
| -### Advanced use |
22 | 51 |
|
23 |
| -Advanced use consist of this steps: |
| 52 | +### Init with presets and options |
24 | 53 |
|
25 |
| -1. Create instance with desired preset & options. |
26 |
| -2. Add plugins. |
27 |
| -3. Enable/Disable additional rules. |
28 |
| -4. Rewrite renderer functions. |
29 |
| -5. Use result to call `.render()` or `.renderInline()` method. |
| 54 | +(*) presets define combinations of active rules and options. Can be |
| 55 | +`"commonmark"`, `"zero"` or `"default"` (if skipped). See |
| 56 | +[API docs](https://markdown-it.github.io/markdown-it/#MarkdownIt.new) for more details. |
30 | 57 |
|
31 |
| -Of cause, you can skip not needed steps, or change sequense. |
| 58 | +```js |
| 59 | +// commonmark mode |
| 60 | +var md = require('markdown-it')('commonmark'); |
32 | 61 |
|
| 62 | +// default mode |
| 63 | +var md = require('markdown-it')(); |
33 | 64 |
|
34 |
| -__Example 1.__ Minimalistic mode with bold, italic and line breaks: |
| 65 | +// enable everything |
| 66 | +var md = require('markdown-it')({ |
| 67 | + html: true, |
| 68 | + linkify: true, |
| 69 | + typographer: true |
| 70 | +}); |
| 71 | + |
| 72 | +// full options list (defaults) |
| 73 | +var md = require('markdown-it')({ |
| 74 | + html: false, // Enable HTML tags in source |
| 75 | + xhtmlOut: false, // Use '/' to close single tags (<br />). |
| 76 | + // This is only for full CommonMark compatibility. |
| 77 | + breaks: false, // Convert '\n' in paragraphs into <br> |
| 78 | + langPrefix: 'language-', // CSS language prefix for fenced blocks. Can be |
| 79 | + // useful for external highlighters. |
| 80 | + linkify: false, // Autoconvert URL-like text to links |
| 81 | + |
| 82 | + // Enable some language-neutral replacement + quotes beautification |
| 83 | + typographer: false, |
| 84 | + |
| 85 | + // Double + single quotes replacement pairs, when typographer enabled, |
| 86 | + // and smartquotes on. Could be either a String or an Array. |
| 87 | + // |
| 88 | + // For example, you can use '«»„“' for Russian, '„“‚‘' for German, |
| 89 | + // and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp). |
| 90 | + quotes: '“”‘’', |
| 91 | + |
| 92 | + // Highlighter function. Should return escaped HTML, |
| 93 | + // or '' if the source string is not changed and should be escaped externaly. |
| 94 | + // If result starts with <pre... internal wrapper is skipped. |
| 95 | + highlight: function (/*str, lang*/) { return ''; } |
| 96 | +}); |
| 97 | +``` |
35 | 98 |
|
36 |
| -```javascript |
37 |
| -var md = require('markdown-it')('zero', { breaks: true }) |
38 |
| - .enable([ 'newline', 'emphasis' ]); |
| 99 | +### Plugins load |
39 | 100 |
|
40 |
| -var result = md.renderInline(...); |
| 101 | +```js |
| 102 | +var md = require('markdown-it')() |
| 103 | + .use(plugin1) |
| 104 | + .use(plugin2, opts, ...) |
| 105 | + .use(plugin3); |
41 | 106 | ```
|
42 | 107 |
|
43 | 108 |
|
44 |
| -__Example 2.__ Load plugin and disable tables: |
| 109 | +### Syntax highlighting |
45 | 110 |
|
46 |
| -```javascript |
47 |
| -var md = require('markdown-it')() |
48 |
| - .use(require('markdown-it-emoji')) |
49 |
| - .disable('table'); |
| 111 | +Apply syntax highlighting to fenced code blocks with the `highlight` option: |
50 | 112 |
|
51 |
| -var result = md.render(...); |
52 |
| -``` |
| 113 | +```js |
| 114 | +var hljs = require('highlight.js') // https://highlightjs.org/ |
53 | 115 |
|
| 116 | +// Actual default values |
| 117 | +var md = require('markdown-it')({ |
| 118 | + highlight: function (str, lang) { |
| 119 | + if (lang && hljs.getLanguage(lang)) { |
| 120 | + try { |
| 121 | + return hljs.highlight(lang, str).value; |
| 122 | + } catch (__) {} |
| 123 | + } |
54 | 124 |
|
55 |
| -__Example 3.__ Replace `<strong>` with `<b>` in rendered result: |
| 125 | + return ''; // use external default escaping |
| 126 | + } |
| 127 | +}); |
| 128 | +``` |
56 | 129 |
|
57 |
| -```javascript |
58 |
| -var md = require('markdown-it')(); |
| 130 | +Or with full wrapper override (if you need assign class to `<pre>`): |
| 131 | + |
| 132 | +```js |
| 133 | +var hljs = require('highlight.js') // https://highlightjs.org/ |
| 134 | + |
| 135 | +// Actual default values |
| 136 | +var md = require('markdown-it')({ |
| 137 | + highlight: function (str, lang) { |
| 138 | + if (lang && hljs.getLanguage(lang)) { |
| 139 | + try { |
| 140 | + return '<pre class="hljs"><code>' + |
| 141 | + hljs.highlight(lang, str, true).value + |
| 142 | + '</code></pre>'; |
| 143 | + } catch (__) {} |
| 144 | + } |
| 145 | + |
| 146 | + return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>'; |
| 147 | + } |
| 148 | +}); |
| 149 | +``` |
| 150 | + |
| 151 | +### Linkify |
59 | 152 |
|
60 |
| -md.renderer.rules.strong_open = function () { return '<b>'; }; |
61 |
| -md.renderer.rules.strong_close = function () { return '</b>'; }; |
| 153 | +`linkify: true` uses [linkify-it](https://github.com/markdown-it/linkify-it). To |
| 154 | +configure linkify-it, access the linkify instance through `md.linkify`: |
62 | 155 |
|
63 |
| -var result = md.renderInline(...); |
| 156 | +```js |
| 157 | +md.linkify.tlds('.py', false); // disables .py as top level domain |
64 | 158 | ```
|
65 | 159 |
|
| 160 | +## Syntax extensions |
| 161 | + |
| 162 | +Embedded (enabled by default): |
| 163 | + |
| 164 | +- [Tables](https://help.github.com/articles/github-flavored-markdown/#tables) (GFM) |
| 165 | +- [Strikethrough](https://help.github.com/articles/github-flavored-markdown/#strikethrough) (GFM) |
66 | 166 |
|
67 |
| -See classes doc for all available features and more examples. |
| 167 | +Via plugins: |
| 168 | + |
| 169 | +- [subscript](https://github.com/markdown-it/markdown-it-sub) |
| 170 | +- [superscript](https://github.com/markdown-it/markdown-it-sup) |
| 171 | +- [footnote](https://github.com/markdown-it/markdown-it-footnote) |
| 172 | +- [definition list](https://github.com/markdown-it/markdown-it-deflist) |
| 173 | +- [abbreviation](https://github.com/markdown-it/markdown-it-abbr) |
| 174 | +- [emoji](https://github.com/markdown-it/markdown-it-emoji) |
| 175 | +- [custom container](https://github.com/markdown-it/markdown-it-container) |
| 176 | +- [insert](https://github.com/markdown-it/markdown-it-ins) |
| 177 | +- [mark](https://github.com/markdown-it/markdown-it-mark) |
| 178 | +- ... and [others](https://www.npmjs.org/browse/keyword/markdown-it-plugin) |
| 179 | + |
| 180 | + |
| 181 | +### Manage rules |
| 182 | + |
| 183 | +By default all rules are enabled, but can be restricted by options. On plugin |
| 184 | +load all its rules are enabled automatically. |
| 185 | + |
| 186 | +```js |
| 187 | +// Activate/deactivate rules, with curring |
| 188 | +var md = require('markdown-it')() |
| 189 | + .disable([ 'link', 'image' ]) |
| 190 | + .enable([ 'link' ]) |
| 191 | + .enable('image'); |
| 192 | + |
| 193 | +// Enable everything |
| 194 | +md = require('markdown-it')('full', { |
| 195 | + html: true, |
| 196 | + linkify: true, |
| 197 | + typographer: true, |
| 198 | +}); |
| 199 | +``` |
0 commit comments