8fold-marked 中文文档教程

发布于 6年前 浏览 163 项目主页 更新于 3年前

这个分支是出于必要而启动的。 需要明确的是,8fold 并没有试图劫持主项目。

如果主项目似乎再次出现,那么我们可能会让它回到那些有能力的人手中。 同时,这个项目是为了Marked用户前来协作(希望主项目的人没事)。 所有 PR 都应提交到此 repo,以便我们可以在此处执行合并到 master 并将它们提交到主项目。

marked

一个用 JavaScript 编写的全功能 markdown 解析器和编译器。 建成 为了速度。

NPM 版本

Install

npm install marked --save

Usage

最小用法:

var marked = require('marked');
console.log(marked('I am using __markdown__.'));
// Outputs: <p>I am using <strong>markdown</strong>.</p>

具有默认值的示例设置选项:

var marked = require('marked');
marked.setOptions({
  renderer: new marked.Renderer(),
  gfm: true,
  tables: true,
  breaks: false,
  pedantic: false,
  sanitize: false,
  smartLists: true,
  smartypants: false,
  xhtml: false,
  baseUrl: null
});

console.log(marked('I am using __markdown__.'));

Browser

<!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Marked in the browser</title>
  <script src="lib/marked.js"></script>
</head>
<body>
  <div id="content"></div>
  <script>
    document.getElementById('content').innerHTML =
      marked('# Marked in browser\n\nRendered by **marked**.');
  </script>
</body>
</html>

marked(markdownString [,options] [,callback])

markdownString

类型:string

要编译的 markdown 源字符串。

options

类型:object

选项的散列。 也可以使用 marked.setOptions 方法设置,如图所示 多于。

callback

类型:function

markdownString 被完全解析时调用的函数 异步突出显示。 如果省略 options 参数,这可以用作 第二个参数。

Options

highlight

类型:function

高亮代码块的函数。 下面的第一个示例使用异步突出显示 node-pygmentize-bundled,第二个是同步示例,使用 highlight.js:

var marked = require('marked');

var markdownString = '```js\n console.log("hello"); \n```';

// Async highlighting with pygmentize-bundled
marked.setOptions({
  highlight: function (code, lang, callback) {
    require('pygmentize-bundled')({ lang: lang, format: 'html' }, code, function (err, result) {
      callback(err, result.toString());
    });
  }
});

// Using async version of marked
marked(markdownString, function (err, content) {
  if (err) throw err;
  console.log(content);
});

// Synchronous highlighting with highlight.js
marked.setOptions({
  highlight: function (code) {
    return require('highlight.js').highlightAuto(code).value;
  }
});

console.log(marked(markdownString));

highlight arguments

code

类型:string

代码段传递给荧光笔。

lang

类型:string

代码块中指定的编程语言。

callback

类型:function

使用异步荧光笔时调用的回调函数。

renderer

类型:<代码>对象 默认值:new Renderer()

包含将标记呈现为 HTML 的函数的对象。

Overriding renderer methods

渲染器选项允许您以自定义方式渲染令牌。 这是一个 通过在 GitHub 上添加嵌入式锚标记来覆盖默认标题令牌呈现的示例:

var marked = require('marked');
var renderer = new marked.Renderer();

renderer.heading = function (text, level) {
  var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');

  return '<h' + level + '><a name="' +
                escapedText +
                 '" class="anchor" href="#' +
                 escapedText +
                 '"><span class="header-link"></span></a>' +
                  text + '</h' + level + '>';
},

console.log(marked('# heading+', { renderer: renderer }));

此代码将输出以下 HTML:

<h1>
  <a name="heading-" class="anchor" href="#heading-">
    <span class="header-link"></span>
  </a>
  heading+
</h1>

Block level renderer methods

  • code(string code, string language)
  • blockquote(string quote)
  • html(string html)
  • heading(string text, number level)
  • hr()
  • list(string body, boolean ordered)
  • listitem(string text)
  • paragraph(string text)
  • table(string header, string body)
  • tablerow(string content)
  • tablecell(string content, object flags)

flags 具有以下属性:

{
    header: true || false,
    align: 'center' || 'left' || 'right'
}

Inline level renderer methods

  • strong(string text)
  • em(string text)
  • codespan(string code)
  • br()
  • del(string text)
  • link(string href, string title, string text)
  • image(string href, string title, string text)

gfm

类型:boolean 默认值:true

启用 GitHub flavored markdown

tables

类型:<代码>布尔值 默认值:true

启用 GFM 表格。 此选项要求 gfm 选项为真。

breaks

类型:<代码>布尔值 默认值:false

启用 GFM 换行符。 此选项要求 gfm 选项为真。

pedantic

类型:<代码>布尔值 默认值:false

尽可能符合 markdown.pl 的模糊部分。 不要修复任何 原始降价错误或不良行为。

sanitize

类型:<代码>布尔值 默认值:false

清理输出。 忽略任何已输入的 HTML。

smartLists

类型:<代码>布尔值 默认值:true

使用比原始降价更智能的列表行为。 最终可能是 default 与旧行为移动到 pedantic

smartypants

类型:<代码>布尔值 默认值:false

对引号和破折号等内容使用“智能”印刷标点符号。

xhtml

类型:<代码>布尔值 默认值:false

根据 XHTML 的要求,使用“/”自关闭 void 元素(
等)的标签。

baseUrl

类型:<代码>字符串 默认值:null

用针对指定基础解析的值替换相对链接和图像 URL。

Access to lexer and parser

如果您愿意,您还可以直接访问词法分析器和解析器。

var tokens = marked.lexer(text, options);
console.log(marked.parser(tokens));
var lexer = new marked.Lexer(options);
var tokens = lexer.lex(text);
console.log(tokens);
console.log(lexer.rules);

CLI

$ marked -o hello.html
hello world
^D
$ cat hello.html
<p>hello world</p>

Philosophy behind marked

marked 的目的是创建一个 markdown 编译器,它可以 经常解析大量降价而不必担心 以某种方式缓存编译后的输出……或者阻塞不必要的长时间。

marked 非常简洁,仍然实现了所有 markdown 功能。 也是 现在与客户端完全兼容。

marked 或多或少通过了官方的 markdown 测试套件 整体。 这很重要,因为数量惊人的降价编译器 不能通过多于几个测试。 很难被标记为 符合原样。 为了它的缘故,它本可以在几个方面偷工减料 性能,但不是为了完全符合您的预期 降价渲染。 事实上,这就是为什么 marked 可以被认为是 上述基准测试中的劣势。

除了实现每个 markdown 功能外,marked 还实现了 GFM 特点

Benchmarks

node v0.8.x

$ node test --bench
marked completed in 3411ms.
marked (gfm) completed in 3727ms.
marked (pedantic) completed in 3201ms.
robotskirt completed in 808ms.
showdown (reuse converter) completed in 11954ms.
showdown (new converter) completed in 17774ms.
markdown-js completed in 17191ms.

Marked 现在比用 C 语言编写的 Discount 更快。

对于那些持怀疑态度的人:这些基准测试运行整个 markdown 测试套件 1000 次。 测试套件测试每个功能。 它不迎合特定方面。

Pro level

如果您愿意,您还可以直接访问词法分析器和解析器。

var tokens = marked.lexer(text, options);
console.log(marked.parser(tokens));
var lexer = new marked.Lexer(options);
var tokens = lexer.lex(text);
console.log(tokens);
console.log(lexer.rules);
$ node
> require('marked').lexer('> i am using marked.')
[ { type: 'blockquote_start' },
  { type: 'paragraph',
    text: 'i am using marked.' },
  { type: 'blockquote_end' },
  links: {} ]

Running Tests & Contributing

如果您想提交拉取请求,请确保您的更改通过测试 套房。 如果您要添加新功能,请务必添加您自己的测试。

标记的测试套件设置有点奇怪:test/new 用于所有测试 不是原始 markdown.pl 测试套件的一部分(这是您的 如果你做了一个测试应该去)。 test/original 只针对原版 markdown.pl 测试。 test/tests 包含两种类型的测试 通过运行 node test --fixmarked --test 组合和移动/生成 --修复。

换句话说,如果你有一个测试要添加,将它添加到 test/new/ 然后 使用 node test --fix 重新生成测试。 提交结果。 如果你的测试 使用特定功能,例如,它可能假设 GFM 启用,您 可以将 .nogfm 添加到文件名中。 所以,my-test.text 变成了 <代码>我的测试.nogfm.文本。 您可以使用任何标记的选项来执行此操作。 说你想要 换行符和 smartypants 启用,你的文件名应该是: my-test.breaks.smartypants.text

运行测试:

cd marked/
node test

Contribution and License Agreement

如果你为这个项目贡献代码,你就隐含地允许你的代码 在麻省理工学院许可下分发。 您还隐含地验证了 所有代码都是您的原创作品。

License

版权所有 (c) 2011-2014,Christopher Jeffrey。 (麻省理工学院许可证)

有关更多信息,请参阅许可证。

This fork is starting out of necessity. To be clear, 8fold is not attempting to hijack the main project.

If the main project appears to pick up again, then we will probably let it go back to those capable hands. In the meantime, this project is here for Marked users to come and collaborate (I hope the folks on the main project are okay). All PRs should be submitted to this repo so we can perform merges into master here and submit those to the main project as well.

marked

A full-featured markdown parser and compiler, written in JavaScript. Built for speed.

NPM version

Install

npm install marked --save

Usage

Minimal usage:

var marked = require('marked');
console.log(marked('I am using __markdown__.'));
// Outputs: <p>I am using <strong>markdown</strong>.</p>

Example setting options with default values:

var marked = require('marked');
marked.setOptions({
  renderer: new marked.Renderer(),
  gfm: true,
  tables: true,
  breaks: false,
  pedantic: false,
  sanitize: false,
  smartLists: true,
  smartypants: false,
  xhtml: false,
  baseUrl: null
});

console.log(marked('I am using __markdown__.'));

Browser

<!doctype html>
<html>
<head>
  <meta charset="utf-8"/>
  <title>Marked in the browser</title>
  <script src="lib/marked.js"></script>
</head>
<body>
  <div id="content"></div>
  <script>
    document.getElementById('content').innerHTML =
      marked('# Marked in browser\n\nRendered by **marked**.');
  </script>
</body>
</html>

marked(markdownString [,options] [,callback])

markdownString

Type: string

String of markdown source to be compiled.

options

Type: object

Hash of options. Can also be set using the marked.setOptions method as seen above.

callback

Type: function

Function called when the markdownString has been fully parsed when using async highlighting. If the options argument is omitted, this can be used as the second argument.

Options

highlight

Type: function

A function to highlight code blocks. The first example below uses async highlighting with node-pygmentize-bundled, and the second is a synchronous example using highlight.js:

var marked = require('marked');

var markdownString = '```js\n console.log("hello"); \n```';

// Async highlighting with pygmentize-bundled
marked.setOptions({
  highlight: function (code, lang, callback) {
    require('pygmentize-bundled')({ lang: lang, format: 'html' }, code, function (err, result) {
      callback(err, result.toString());
    });
  }
});

// Using async version of marked
marked(markdownString, function (err, content) {
  if (err) throw err;
  console.log(content);
});

// Synchronous highlighting with highlight.js
marked.setOptions({
  highlight: function (code) {
    return require('highlight.js').highlightAuto(code).value;
  }
});

console.log(marked(markdownString));

highlight arguments

code

Type: string

The section of code to pass to the highlighter.

lang

Type: string

The programming language specified in the code block.

callback

Type: function

The callback function to call when using an async highlighter.

renderer

Type: object Default: new Renderer()

An object containing functions to render tokens to HTML.

Overriding renderer methods

The renderer option allows you to render tokens in a custom manner. Here is an example of overriding the default heading token rendering by adding an embedded anchor tag like on GitHub:

var marked = require('marked');
var renderer = new marked.Renderer();

renderer.heading = function (text, level) {
  var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');

  return '<h' + level + '><a name="' +
                escapedText +
                 '" class="anchor" href="#' +
                 escapedText +
                 '"><span class="header-link"></span></a>' +
                  text + '</h' + level + '>';
},

console.log(marked('# heading+', { renderer: renderer }));

This code will output the following HTML:

<h1>
  <a name="heading-" class="anchor" href="#heading-">
    <span class="header-link"></span>
  </a>
  heading+
</h1>

Block level renderer methods

  • code(string code, string language)
  • blockquote(string quote)
  • html(string html)
  • heading(string text, number level)
  • hr()
  • list(string body, boolean ordered)
  • listitem(string text)
  • paragraph(string text)
  • table(string header, string body)
  • tablerow(string content)
  • tablecell(string content, object flags)

flags has the following properties:

{
    header: true || false,
    align: 'center' || 'left' || 'right'
}

Inline level renderer methods

  • strong(string text)
  • em(string text)
  • codespan(string code)
  • br()
  • del(string text)
  • link(string href, string title, string text)
  • image(string href, string title, string text)

gfm

Type: boolean Default: true

Enable GitHub flavored markdown.

tables

Type: boolean Default: true

Enable GFM tables. This option requires the gfm option to be true.

breaks

Type: boolean Default: false

Enable GFM line breaks. This option requires the gfm option to be true.

pedantic

Type: boolean Default: false

Conform to obscure parts of markdown.pl as much as possible. Don't fix any of the original markdown bugs or poor behavior.

sanitize

Type: boolean Default: false

Sanitize the output. Ignore any HTML that has been input.

smartLists

Type: boolean Default: true

Use smarter list behavior than the original markdown. May eventually be default with the old behavior moved into pedantic.

smartypants

Type: boolean Default: false

Use "smart" typograhic punctuation for things like quotes and dashes.

xhtml

Type: boolean Default: false

Self-close the tags for void elements (<br/>, <img/>, etc.) with a "/" as required by XHTML.

baseUrl

Type: string Default: null

Replace relative link and image URLs with values resolved against the specified base.

Access to lexer and parser

You also have direct access to the lexer and parser if you so desire.

var tokens = marked.lexer(text, options);
console.log(marked.parser(tokens));
var lexer = new marked.Lexer(options);
var tokens = lexer.lex(text);
console.log(tokens);
console.log(lexer.rules);

CLI

$ marked -o hello.html
hello world
^D
$ cat hello.html
<p>hello world</p>

Philosophy behind marked

The point of marked was to create a markdown compiler where it was possible to frequently parse huge chunks of markdown without having to worry about caching the compiled output somehow…or blocking for an unnecesarily long time.

marked is very concise and still implements all markdown features. It is also now fully compatible with the client-side.

marked more or less passes the official markdown test suite in its entirety. This is important because a surprising number of markdown compilers cannot pass more than a few tests. It was very difficult to get marked as compliant as it is. It could have cut corners in several areas for the sake of performance, but did not in order to be exactly what you expect in terms of a markdown rendering. In fact, this is why marked could be considered at a disadvantage in the benchmarks above.

Along with implementing every markdown feature, marked also implements GFM features.

Benchmarks

node v0.8.x

$ node test --bench
marked completed in 3411ms.
marked (gfm) completed in 3727ms.
marked (pedantic) completed in 3201ms.
robotskirt completed in 808ms.
showdown (reuse converter) completed in 11954ms.
showdown (new converter) completed in 17774ms.
markdown-js completed in 17191ms.

Marked is now faster than Discount, which is written in C.

For those feeling skeptical: These benchmarks run the entire markdown test suite 1000 times. The test suite tests every feature. It doesn't cater to specific aspects.

Pro level

You also have direct access to the lexer and parser if you so desire.

var tokens = marked.lexer(text, options);
console.log(marked.parser(tokens));
var lexer = new marked.Lexer(options);
var tokens = lexer.lex(text);
console.log(tokens);
console.log(lexer.rules);
$ node
> require('marked').lexer('> i am using marked.')
[ { type: 'blockquote_start' },
  { type: 'paragraph',
    text: 'i am using marked.' },
  { type: 'blockquote_end' },
  links: {} ]

Running Tests & Contributing

If you want to submit a pull request, make sure your changes pass the test suite. If you're adding a new feature, be sure to add your own test.

The marked test suite is set up slightly strangely: test/new is for all tests that are not part of the original markdown.pl test suite (this is where your test should go if you make one). test/original is only for the original markdown.pl tests. test/tests houses both types of tests after they have been combined and moved/generated by running node test --fix or marked --test --fix.

In other words, if you have a test to add, add it to test/new/ and then regenerate the tests with node test --fix. Commit the result. If your test uses a certain feature, for example, maybe it assumes GFM is not enabled, you can add .nogfm to the filename. So, my-test.text becomes my-test.nogfm.text. You can do this with any marked option. Say you want line breaks and smartypants enabled, your filename should be: my-test.breaks.smartypants.text.

To run the tests:

cd marked/
node test

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work. </legalese>

License

Copyright (c) 2011-2014, Christopher Jeffrey. (MIT License)

See LICENSE for more info.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文