版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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