# marked - markdown 文件解析工具

# 1. 介绍

将 .md 文件的内容 解析为 HTML 内容

文档: https://marked.js.org/

# 2. 使用

安装:

npm i marked

示例:

import { marked } from 'marked';

const content = 'hello world';

marked.parse(content).then((html) => {
  console.log(html); // => <p>hello world</p>
});

# 3. 插件

# 3.1. marked-xhtml

说明:

  • 闭合单标签

示例:

import { marked } from "marked";
import { markedXhtml } from "marked-xhtml";

marked.use(markedXhtml());

marked.parse("---");
// <hr/>

# 3.2. isomorphic-dompurify

说明:

  • 处理 HTML 内容

示例:

import { marked } from 'marked';
import DOMPurify from 'isomorphic-dompurify';

// Override function
function postprocess(html) {
  return DOMPurify.sanitize(html);
}

marked.use({ hooks: { postprocess } });

// Run marked
console.log(marked.parse(`
<img src=x onerror=alert(1)//>
`));

// output
// <img src="x">

# 4. 参考

本章目录