UNPKG

7.73 kBMarkdownView Raw
1# marked
2
3> A full-featured markdown parser and compiler, written in javascript. Built
4> for speed.
5
6[![NPM version](https://badge.fury.io/js/marked.png)][badge]
7
8## Install
9
10``` bash
11npm install marked --save
12```
13
14## Usage
15
16Minimal usage:
17
18```js
19console.log(marked('I am using __markdown__.'));
20// Outputs: <p>I am using <i>markdown</i>.</p>
21```
22
23Example using all options:
24
25```js
26// Set default options except highlight which has no default
27marked.setOptions({
28 gfm: true,
29 highlight: function (code, lang, callback) {
30 pygmentize({ lang: lang, format: 'html' }, code, function (err, result) {
31 if (err) return callback(err);
32 callback(null, result.toString());
33 });
34 },
35 tables: true,
36 breaks: false,
37 pedantic: false,
38 sanitize: true,
39 smartLists: true,
40 smartypants: false,
41 langPrefix: 'lang-'
42});
43
44// Using async version of marked
45marked('I am using __markdown__.', function (err, content) {
46 if (err) throw err;
47 console.log(content);
48});
49```
50
51## marked(markdownString, [options], [callback])
52
53### markdownString
54
55Type: `String`
56
57String of markdown source to be compiled.
58
59### options
60
61Type: `Object`
62
63Hash of options. Can also be set using the `marked.setOptions` method as seen
64above.
65
66### callback
67
68Type: `Function`
69
70Function called when the `markdownString` has been fully parsed when using
71async highlighting. If the `options` argument is omitted, this can be used as
72the second argument as seen above:
73
74## Options
75
76### gfm
77
78Type: `Boolean`
79Default: `true`
80
81Enable [GitHub flavored markdown][gfm].
82
83### highlight
84
85Type: `Function`
86
87A function to highlight code blocks. The function takes three arguments: code,
88lang, and callback. The above example uses async highlighting with
89[node-pygementize-bundled][pygmentize], and here is a synchronous example using
90[highlight.js][highlight] which doesn't require the callback argument:
91
92```js
93marked.setOptions({
94 highlight: function (code, lang) {
95 return hljs.highlightAuto(lang, code).value;
96 }
97});
98```
99
100#### highlight arguments
101
102`code`
103
104Type: `String`
105
106The section of code to pass to the highlighter.
107
108`lang`
109
110Type: `String`
111
112The programming language specified in the code block.
113
114`callback`
115
116Type: `String`
117
118The callback function to call when using an async highlighter.
119
120### tables
121
122Type: `Boolean`
123Default: `true`
124
125Enable GFM [tables][tables].
126This option requires the `gfm` option to be true.
127
128### breaks
129
130Type: `Boolean`
131Default: `false`
132
133Enable GFM [line breaks][breaks].
134This option requires the `gfm` option to be true.
135
136### pedantic
137
138Type: `Boolean`
139Default: `false`
140
141Conform to obscure parts of `markdown.pl` as much as possible. Don't fix any of
142the original markdown bugs or poor behavior.
143
144### sanitize
145
146Type: `Boolean`
147Default: `false`
148
149Sanitize the output. Ignore any HTML that has been input.
150
151### smartLists
152
153Type: `Boolean`
154Default: `true`
155
156Use smarter list behavior than the original markdown. May eventually be
157default with the old behavior moved into `pedantic`.
158
159### smartypants
160
161Type: `Boolean`
162Default: `false`
163
164Use "smart" typograhic punctuation for things like quotes and dashes.
165
166### langPrefix
167
168Type: `String`
169Default: `lang-`
170
171Set the prefix for code block classes.
172
173## Access to lexer and parser
174
175You also have direct access to the lexer and parser if you so desire.
176
177``` js
178var tokens = marked.lexer(text, options);
179console.log(marked.parser(tokens));
180```
181
182``` js
183var lexer = new marked.Lexer(options);
184var tokens = lexer.lex(text);
185console.log(tokens);
186console.log(lexer.rules);
187```
188
189## CLI
190
191``` bash
192$ marked -o hello.html
193hello world
194^D
195$ cat hello.html
196<p>hello world</p>
197```
198
199## Benchmarks
200
201node v0.4.x
202
203``` bash
204$ node test --bench
205marked completed in 12071ms.
206showdown (reuse converter) completed in 27387ms.
207showdown (new converter) completed in 75617ms.
208markdown-js completed in 70069ms.
209```
210
211node v0.6.x
212
213``` bash
214$ node test --bench
215marked completed in 6448ms.
216marked (gfm) completed in 7357ms.
217marked (pedantic) completed in 6092ms.
218discount completed in 7314ms.
219showdown (reuse converter) completed in 16018ms.
220showdown (new converter) completed in 18234ms.
221markdown-js completed in 24270ms.
222```
223
224__Marked is now faster than Discount, which is written in C.__
225
226For those feeling skeptical: These benchmarks run the entire markdown test suite
2271000 times. The test suite tests every feature. It doesn't cater to specific
228aspects.
229
230node v0.8.x
231
232``` bash
233$ node test --bench
234marked completed in 3411ms.
235marked (gfm) completed in 3727ms.
236marked (pedantic) completed in 3201ms.
237robotskirt completed in 808ms.
238showdown (reuse converter) completed in 11954ms.
239showdown (new converter) completed in 17774ms.
240markdown-js completed in 17191ms.
241```
242
243## Another Javascript Markdown Parser
244
245The point of marked was to create a markdown compiler where it was possible to
246frequently parse huge chunks of markdown without having to worry about
247caching the compiled output somehow...or blocking for an unnecesarily long time.
248
249marked is very concise and still implements all markdown features. It is also
250now fully compatible with the client-side.
251
252marked more or less passes the official markdown test suite in its
253entirety. This is important because a surprising number of markdown compilers
254cannot pass more than a few tests. It was very difficult to get marked as
255compliant as it is. It could have cut corners in several areas for the sake
256of performance, but did not in order to be exactly what you expect in terms
257of a markdown rendering. In fact, this is why marked could be considered at a
258disadvantage in the benchmarks above.
259
260Along with implementing every markdown feature, marked also implements [GFM
261features][gfmf].
262
263``` bash
264$ node
265> require('marked').lexer('> i am using marked.')
266[ { type: 'blockquote_start' },
267 { type: 'paragraph',
268 text: 'i am using marked.' },
269 { type: 'blockquote_end' },
270 links: {} ]
271```
272
273## Running Tests & Contributing
274
275If you want to submit a pull request, make sure your changes pass the test
276suite. If you're adding a new feature, be sure to add your own test.
277
278The marked test suite is set up slightly strangely: `test/new` is for all tests
279that are not part of the original markdown.pl test suite (this is where your
280test should go if you make one). `test/original` is only for the original
281markdown.pl tests. `test/tests` houses both types of tests after they have been
282combined and moved/generated by running `node test --fix` or `marked --test
283--fix`.
284
285In other words, if you have a test to add, add it to `test/new/` and then
286regenerate the tests with `node test --fix`. Commit the result. If your test
287uses a certain feature, for example, maybe it assumes GFM is *not* enabled, you
288can add `.nogfm` to the filename. So, `my-test.text` becomes
289`my-test.nogfm.text`. You can do this with any marked option. Say you want
290line breaks and smartypants enabled, your filename should be:
291`my-test.breaks.smartypants.text`.
292
293To run the tests:
294
295``` bash
296cd marked/
297node test
298```
299
300### Contribution and License Agreement
301
302If you contribute code to marked, you are implicitly allowing your code to be
303distributed under the MIT license. You are also implicitly verifying that all
304code is your original work. `</legalese>`
305
306## License
307
308Copyright (c) 2011-2013, Christopher Jeffrey. (MIT License)
309
310See LICENSE for more info.
311
312[gfm]: https://help.github.com/articles/github-flavored-markdown
313[gfmf]: http://github.github.com/github-flavored-markdown/
314[pygmentize]: https://github.com/rvagg/node-pygmentize-bundled
315[highlight]: https://github.com/isagalaev/highlight.js
316[badge]: http://badge.fury.io/js/marked
317[tables]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#wiki-tables
318[breaks]: https://help.github.com/articles/github-flavored-markdown#newlines