logo

Marked 中文网

Marked 是

¥Marked is

  1. 为速度而构建。*

    ¥built for speed.*

  2. 用于解析 markdown 的底层 markdown 编译器,无需长时间缓存或阻塞。**

    ¥a low-level markdown compiler for parsing markdown without caching or blocking for long periods of time.**

  3. 轻量级,同时实现受支持的风格和规范中的所有 markdown 功能。***

    ¥light-weight while implementing all markdown features from the supported flavors & specifications.***

  4. 可用作命令行接口 (CLI),并在客户端或服务器端 JavaScript 项目中运行。

    ¥available as a command line interface (CLI) and running in client- or server-side JavaScript projects.

*仍在研究用于比较分析和定义的指标。
**尽可能少的依赖。
***严格遵守规定可能会导致运行比较基准测试时处理速度变慢。

演示

签出 演示页面 以查看 marked 的实际操作 ⛹️

¥Checkout the demo page to see marked in action ⛹️

这些文档页面也使用标记渲染 💯

¥These documentation pages are also rendered using marked 💯

安装

CLI:npm install -g marked

浏览器内:

¥In-browser:

npm install marked

用法

警告:🚨 已标记不会 sanitize 输出 HTML。如果你正在处理可能不安全的字符串,则过滤可能的 XSS 攻击非常重要。一些过滤选项包括输出 HTML 上的 DOMPurify(推荐)、js-xsssanitize-htmlinsane!🚨

¥Warning: 🚨 Marked does not sanitize the output HTML. If you are processing potentially unsafe strings, it's important to filter for possible XSS attacks. Some filtering options include DOMPurify (recommended), js-xss, sanitize-html and insane on the output HTML! 🚨

DOMPurify.sanitize(marked.parse(`<img src="x" onerror="alert('not happening')">`));

⚠️ 输入:特殊的零宽度 unicode 字符(例如 \uFEFF)可能会干扰解析。一些文本编辑器将它们添加到文件的开头(参见:#2139)。

¥⚠️ Input: special ZERO WIDTH unicode characters (for example \uFEFF) might interfere with parsing. Some text editors add them at the start of the file (see: #2139).

// remove the most common zerowidth characters from the start of the file
marked.parse(
  contents.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/,"")
)

CLI

# Example with stdin input
$ marked -o hello.html
hello world
^D
$ cat hello.html
<p>hello world</p>
# Example with string input
$ marked -s "*hello world*"
<p><em>hello world</em></p>
# Example with file input

echo "**bold text example**" > readme.md

$ marked -i readme.md -o readme.html
$ cat readme.html
<p><strong>bold text example</strong></p>
# Print all options
$ marked --help

CLI 配置

¥CLI Config

配置文件可用于配置 marked cli。

¥A config file can be used to configure the marked cli.

如果它是一个 .json 文件,它应该是一个 JSON 对象,将被传递给标记为选项。

¥If it is a .json file it should be a JSON object that will be passed to marked as options.

如果使用 .js,它应该有一个标记选项对象的默认导出或一个以 marked 作为参数的函数。它可以使用 marked 参数使用 marked.use 安装扩展。

¥If .js is used it should have a default export of a marked options object or a function that takes marked as a parameter. It can use the marked parameter to install extensions using marked.use.

默认情况下,marked cli 将按以下顺序在你的主目录中查找配置文件。

¥By default the marked cli will look for a config file in your home directory in the following order.

  • ~/.marked.json

  • ~/.marked.js

  • ~/.marked/index.js

# Example with custom config

echo '{ "breaks": true }' > config.json

$ marked -s 'line1\nline2' -c config.json
<p>line1<br>line2</p>

浏览器

¥Browser

<!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 browser\n\nRendered by **marked**.');
  </script>
</body>
</html>

或导入 esm 模块

¥or import esm module

<script type="module">
  import { marked } from "https://cdn.jsdelivr.net/npm/marked/lib/marked.esm.js";
  document.getElementById('content').innerHTML =
    marked.parse('# Marked in the browser\n\nRendered by **marked**.');
</script>

Node.js

import { marked } from 'marked';
// or const { marked } = require('marked');

const html = marked.parse('# Marked in Node.js\n\nRendered by **marked**.');

Marked 也提供 高级配置extensibility

¥Marked offers advanced configurations and extensibility as well.

支持的 Markdown 规范

我们积极支持以下 Markdown 风格 的功能。

¥We actively support the features of the following Markdown flavors.

风味 版本 状态
原始 markdown.pl --
CommonMark 0.31 正在进行中
GitHub Flavored Markdown 0.29 正在进行中

通过支持上述 Markdown 风格,Marked 也可以帮助你使用其他风格;但是,这些并未得到社区的积极支持。

¥By supporting the above Markdown flavors, it's possible that Marked can help you use other flavors as well; however, these are not actively supported by the community.

使用 Marked 的工具列表

我们积极支持 Marked 在超快速 Markdown 转换中的可用性,一些使用 Marked 进行单页创作的工具是

¥We actively support the usability of Marked in super-fast markdown transformation, some of Tools using Marked for single-page creations are

工具 说明
zero-md 一个原生的 markdown-to-html web 组件,用于加载和显示外部 MD 文件。它使用 Marked 进行超快速的 markdown 转换。
texme TeXMe 是一款轻量级 JavaScript 实用程序,用于创建自渲染 Markdown + LaTeX 文档。
StrapDown.js StrapDown.js 是一款出色的即时 Markdown 到 HTML 文本处理器。
raito 8kb JavaScript 中的迷你 Markdown Wiki/CMS。
Homebrewery Homebrewery 是一种使用 Markdown 制作真实 D&D 内容的工具。它是根据 MIT 的条款分发的。

安全

唯一完全安全的系统是根本不存在的系统。话虽如此,我们非常重视 Marked 的安全性。

¥The only completely secure system is the one that doesn't exist in the first place. Having said that, we take the security of Marked very seriously.

因此,请通过电子邮件向项目 committers列出的所有者 NPM 披露潜在的安全问题。我们将在 48 小时内提供安全报告的初步评估,并应在 2 周内应用补丁(此外,请随时为该问题做出贡献)。

¥Therefore, please disclose potential security issues by email to the project committers as well as the listed owners within NPM. We will provide an initial assessment of security reports within 48 hours and should apply patches within 2 weeks (also, feel free to contribute a fix for the issue).