在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):markdown-it/markdown-it开源软件地址(OpenSource Url):https://github.com/markdown-it/markdown-it开源编程语言(OpenSource Language):JavaScript 97.3%开源软件介绍(OpenSource Introduction):markdown-it
Table of content
Installnode.js: npm install markdown-it --save browser (CDN): Usage examplesSee also:
Simple// node.js, "classic" way:
var MarkdownIt = require('markdown-it'),
md = new MarkdownIt();
var result = md.render('# markdown-it rulezz!');
// node.js, the same, but with sugar:
var md = require('markdown-it')();
var result = md.render('# markdown-it rulezz!');
// browser without AMD, added to "window" on script load
// Note, there is no dash in "markdownit".
var md = window.markdownit();
var result = md.render('# markdown-it rulezz!'); Single line rendering, without paragraph wrap: var md = require('markdown-it')();
var result = md.renderInline('__markdown-it__ rulezz!'); Init with presets and options(*) presets define combinations of active rules and options. Can be
// commonmark mode
var md = require('markdown-it')('commonmark');
// default mode
var md = require('markdown-it')();
// enable everything
var md = require('markdown-it')({
html: true,
linkify: true,
typographer: true
});
// full options list (defaults)
var md = require('markdown-it')({
html: false, // Enable HTML tags in source
xhtmlOut: false, // Use '/' to close single tags (<br />).
// This is only for full CommonMark compatibility.
breaks: false, // Convert '\n' in paragraphs into <br>
langPrefix: 'language-', // CSS language prefix for fenced blocks. Can be
// useful for external highlighters.
linkify: false, // Autoconvert URL-like text to links
// Enable some language-neutral replacement + quotes beautification
// For the full list of replacements, see https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js
typographer: false,
// Double + single quotes replacement pairs, when typographer enabled,
// and smartquotes on. Could be either a String or an Array.
//
// For example, you can use '«»„“' for Russian, '„“‚‘' for German,
// and ['«\xA0', '\xA0»', '‹\xA0', '\xA0›'] for French (including nbsp).
quotes: '“”‘’',
// Highlighter function. Should return escaped HTML,
// or '' if the source string is not changed and should be escaped externally.
// If result starts with <pre... internal wrapper is skipped.
highlight: function (/*str, lang*/) { return ''; }
}); Plugins loadvar md = require('markdown-it')()
.use(plugin1)
.use(plugin2, opts, ...)
.use(plugin3); Syntax highlightingApply syntax highlighting to fenced code blocks with the var hljs = require('highlight.js'); // https://highlightjs.org/
// Actual default values
var md = require('markdown-it')({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch (__) {}
}
return ''; // use external default escaping
}
}); Or with full wrapper override (if you need assign class to var hljs = require('highlight.js'); // https://highlightjs.org/
// Actual default values
var md = require('markdown-it')({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return '<pre class="hljs"><code>' +
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
'</code></pre>';
} catch (__) {}
}
return '<pre class="hljs"><code>' + md.utils.escapeHtml(str) + '</code></pre>';
}
}); Linkify
md.linkify.set({ fuzzyEmail: false }); // disables converting email to link APIIf you are going to write plugins - take a look at Development info. Syntax extensionsEmbedded (enabled by default):
Via plugins:
Manage rulesBy default all rules are enabled, but can be restricted by options. On plugin load all its rules are enabled automatically. // Activate/deactivate rules, with currying
var md = require('markdown-it')()
.disable([ 'link', 'image' ])
.enable([ 'link' ])
.enable('image');
// Enable everything
md = require('markdown-it')({
html: true,
linkify: true,
typographer: true,
}); You can find all rules in sources: parser_core.js, parser_block, parser_inline. BenchmarkHere is the result of readme parse at MB Pro Retina 2013 (2.4 GHz): make benchmark-deps
benchmark/benchmark.js readme
Selected samples: (1 of 28)
> README
Sample: README.md (7774 bytes)
> commonmark-reference x 1,222 ops/sec ±0.96% (97 runs sampled)
> current x 743 ops/sec ±0.84% (97 runs sampled)
> current-commonmark x 1,568 ops/sec ±0.84% (98 runs sampled)
> marked x 1,587 ops/sec ±4.31% (93 runs sampled) Note. CommonMark version runs with simplified link normalizers for more "honest" compare. Difference is ~ 1.5x. As you can see, markdown-it for enterpriseAvailable as part of the Tidelift Subscription. The maintainers of Authors
markdown-it is the result of the decision of the authors who contributed to 99% of the Remarkable code to move to a project with the same authorship but new leadership (Vitaly and Alex). It's not a fork. References / ThanksBig 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:
Ports
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论