现在有很多流行的Markdown编辑器,但有时需要的不是一个庞大的编辑器,而是一个可以将Markdown文本格式化为Html代码的工具,所以找到了:Markedjs
CLI:
bashnpm install -g marked
In-browser:
bashnpm install marked
npm install @types/marked # For TypeScript projects
** Warning: 🚨 Marked does not sanitize the output HTML. Please use a sanitize library, like DOMPurify (recommended), sanitize-html or insane on the output HTML! 🚨 **
DOMPurify.sanitize(marked.parse(`<img src="x" onerror="alert('not happening')">`));
bash# Example with stdin input
$ marked -o hello.html
hello world
^D
$ cat hello.html
<p>hello world</p>
bash# Print all options
$ marked --help
通过
marked.parse()
方法格式化Markdown文本。
html<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Marked in the browser</title>
</head>
<body>
<div id="content"></div>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script>
document.getElementById('content').innerHTML =
marked.parse('# Marked in the browser\n\nRendered by **marked**.');
</script>
</body>
</html>
本文作者:DingDangDog
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!