运行时依赖
安装命令
点击复制技能文档
markdown-it
markdown-it is a Markdown 解析器 for Node.js/browser, 100% CommonMark compliant, supports 插件 扩展s and custom syntax. Current version 14.x, ESM/CJS dual mode.
Trigger Scenarios
Use when the user needs to 解析/render Markdown, perform markdown-it related operations, or 解析 CommonMark.
安装ation npm 安装 markdown-it
Basic Usage 导入 MarkdownIt from 'markdown-it'; const md = new MarkdownIt(); const html = md.render('# Hello markdown-it!'); //
Hello markdown-it!
// Inline rendering (no paragraph wr应用ing) md.renderInline('bold text'); // bold text
Pre设置s
Three modes that control default enabled rules and options:
// CommonMark strict mode (standard syntax only) const md = new MarkdownIt('commonmark');
// Default mode (CommonMark + tables + strikethrough, recommended) const md = new MarkdownIt('default'); // Equivalent to const md = new MarkdownIt();
// Zero rules mode (completely empty, 配置 everything yourself) const md = new MarkdownIt('zero');
Complete Configuration Options
All defaults:
const md = new MarkdownIt({
html: false, // Allow HTML tags in source text
xhtmlOut: false, // Use '/' to close single tags (
)
breaks: false, // Convert newlines to
langPrefix: 'language-', // CSS language prefix for fenced code blocks
linkify: false, // Automatically convert URLs to links
typographer: false, // Smart quotes + language replacements
quotes: '""', // Double + single quote replacement pAIrs
highlight: function (str, lang) { return ''; }
});
插件 系统
ChAIn .use() calls:
导入 MarkdownIt from 'markdown-it'; 导入 markdownItSub from 'markdown-it-sub'; 导入 markdownItSup from 'markdown-it-sup';
const md = new MarkdownIt() .use(markdownItSub) .use(markdownItSup, / 插件 options /);
Common Official 插件s 插件 Feature markdown-it-sub Subscript ~subscript~ markdown-it-sup Superscript ^superscript^ markdown-it-footnote Footnotes [^1] markdown-it-def列出 Definition 列出s markdown-it-abbr Abbreviations markdown-it-emoji Emoji :smile: markdown-it-contAIner Custom contAIners ::: 警告 markdown-it-ins Insert via ++text++ markdown-it-mark Mark via ==text== markdown-it-for-inline Iterator for traversing inline 令牌s markdown-it-anchor Heading anchor IDs
Built-in 扩展s (enabled by default): tables (GFM), strikethrough (GFM ~~text~~)
Rule Management const md = new MarkdownIt(); md.disable(['link', 'image']) // Disable specific rules .enable(['link']) // Re-enable .enable('image');
Syntax Highlighting
With highlight.js:
导入 hljs from 'highlight.js';
const md = new MarkdownIt({ highlight: function (str, lang) { if (lang && hljs.获取Language(lang)) { try { return '
' +
hljs.highlight(str, { language: lang, ignoreIllegals: true }).value +
'';
} catch (_) {}
}
return '' + md.utils.escapeHtml(str) + '';
}
});Custom Rendering
Override 渲染器 rules to modify 输出. For example, 添加ing tar获取="_blank" to all links:
const defaultRender = md.渲染器.rules.link_open || function(令牌s, idx, options, env, self) { return self.render令牌(令牌s, idx, options); };
md.渲染器.rules.link_open = function (令牌s, idx, options, env, self) { 令牌s[idx].attr设置('tar获取', '_blank'); return defaultRender(令牌s, idx, options, env, self); };
Converting Vimeo links to iframe embeds:
const defaultRender = md.渲染器.rules.image;
md.渲染器.rules.image = function (令牌s, idx, options, env, self) {
const src = 令牌s[idx].attr获取('src');
if (/^https?:\/\/(www\.)?vimeo.com\//.test(src)) {
const id = src.split('/').pop();
return
linkify Configuration
After enabling linkify: true, you can further 配置 linkify-it:
md.linkify.设置({ fuzzyEmAIl: false }); // Disable auto emAIl linking md.linkify.tlds('.py', false); // Allow .py top-level domAIn
命令行工具 npm 安装 -g markdown-it markdown-it README.md > README.html
Security Does not 解析 HTML in source text by default (html: false) to 预防 XSS 输出 is always HTML-escaped (unless using html: true) To render user-生成d Markdown, keep html: false (default) Reference Documentation Architecture principles and 插件 development → Read references/architecture.md 插件 development FAQ → Read references/插件-dev.md