UNPKG

9.77 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
19var marked = require('marked');
20console.log(marked('I am using __markdown__.'));
21// Outputs: <p>I am using <strong>markdown</strong>.</p>
22```
23
24Example setting options with default values:
25
26```js
27var marked = require('marked');
28marked.setOptions({
29 renderer: new marked.Renderer(),
30 gfm: true,
31 tables: true,
32 breaks: false,
33 pedantic: false,
34 sanitize: true,
35 smartLists: true,
36 smartypants: false
37});
38
39console.log(marked('I am using __markdown__.'));
40```
41
42### Browser
43
44```html
45<!doctype html>
46<html>
47<head>
48 <meta charset="utf-8"/>
49 <title>Marked in the browser</title>
50 <script src="lib/marked.js"></script>
51</head>
52<body>
53 <div id="content"></div>
54 <script>
55 document.getElementById('content').innerHTML =
56 marked('# Marked in browser\n\nRendered by **marked**.');
57 </script>
58</body>
59</html>
60```
61
62## marked(markdownString [,options] [,callback])
63
64### markdownString
65
66Type: `string`
67
68String of markdown source to be compiled.
69
70### options
71
72Type: `object`
73
74Hash of options. Can also be set using the `marked.setOptions` method as seen
75above.
76
77### callback
78
79Type: `function`
80
81Function called when the `markdownString` has been fully parsed when using
82async highlighting. If the `options` argument is omitted, this can be used as
83the second argument.
84
85## Options
86
87### highlight
88
89Type: `function`
90
91A function to highlight code blocks. The first example below uses async highlighting with
92[node-pygmentize-bundled][pygmentize], and the second is a synchronous example using
93[highlight.js][highlight]:
94
95```js
96var marked = require('marked');
97
98var markdownString = '```js\n console.log("hello"); \n```';
99
100// Async highlighting with pygmentize-bundled
101marked.setOptions({
102 highlight: function (code, lang, callback) {
103 require('pygmentize-bundled')({ lang: lang, format: 'html' }, code, function (err, result) {
104 callback(err, result.toString());
105 });
106 }
107});
108
109// Using async version of marked
110marked(markdownString, function (err, content) {
111 if (err) throw err;
112 console.log(content);
113});
114
115// Synchronous highlighting with highlight.js
116marked.setOptions({
117 highlight: function (code) {
118 return require('highlight.js').highlightAuto(code).value;
119 }
120});
121
122console.log(marked(markdownString));
123```
124
125#### highlight arguments
126
127`code`
128
129Type: `string`
130
131The section of code to pass to the highlighter.
132
133`lang`
134
135Type: `string`
136
137The programming language specified in the code block.
138
139`callback`
140
141Type: `function`
142
143The callback function to call when using an async highlighter.
144
145### renderer
146
147Type: `object`
148Default: `new Renderer()`
149
150An object containing functions to render tokens to HTML.
151
152#### Overriding renderer methods
153
154The renderer option allows you to render tokens in a custom manor. Here is an
155example of overriding the default heading token rendering by adding an embedded anchor tag like on GitHub:
156
157```javascript
158var marked = require('marked');
159var renderer = new marked.Renderer();
160
161renderer.heading = function (text, level) {
162 var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
163
164 return '<h' + level + '><a name="' +
165 escapedText +
166 '" class="anchor" href="#' +
167 escapedText +
168 '"><span class="header-link"></span></a>' +
169 text + '</h' + level + '>';
170},
171
172console.log(marked('# heading+', { renderer: renderer }));
173```
174This code will output the following HTML:
175```html
176<h1>
177 <a name="heading-" class="anchor" href="#heading-">
178 <span class="header-link"></span>
179 </a>
180 heading+
181</h1>
182```
183
184#### Block level renderer methods
185
186- code(*string* code, *string* language)
187- blockquote(*string* quote)
188- html(*string* html)
189- heading(*string* text, *number* level)
190- hr()
191- list(*string* body, *boolean* ordered)
192- listitem(*string* text)
193- paragraph(*string* text)
194- table(*string* header, *string* body)
195- tablerow(*string* content)
196- tablecell(*string* content, *object* flags)
197
198`flags` has the following properties:
199
200```js
201{
202 header: true || false,
203 align: 'center' || 'left' || 'right'
204}
205```
206
207#### Inline level renderer methods
208
209- strong(*string* text)
210- em(*string* text)
211- codespan(*string* code)
212- br()
213- del(*string* text)
214- link(*string* href, *string* title, *string* text)
215- image(*string* href, *string* title, *string* text)
216
217### gfm
218
219Type: `boolean`
220Default: `true`
221
222Enable [GitHub flavored markdown][gfm].
223
224### tables
225
226Type: `boolean`
227Default: `true`
228
229Enable GFM [tables][tables].
230This option requires the `gfm` option to be true.
231
232### breaks
233
234Type: `boolean`
235Default: `false`
236
237Enable GFM [line breaks][breaks].
238This option requires the `gfm` option to be true.
239
240### pedantic
241
242Type: `boolean`
243Default: `false`
244
245Conform to obscure parts of `markdown.pl` as much as possible. Don't fix any of
246the original markdown bugs or poor behavior.
247
248### sanitize
249
250Type: `boolean`
251Default: `false`
252
253Sanitize the output. Ignore any HTML that has been input.
254
255### smartLists
256
257Type: `boolean`
258Default: `true`
259
260Use smarter list behavior than the original markdown. May eventually be
261default with the old behavior moved into `pedantic`.
262
263### smartypants
264
265Type: `boolean`
266Default: `false`
267
268Use "smart" typograhic punctuation for things like quotes and dashes.
269
270## Access to lexer and parser
271
272You also have direct access to the lexer and parser if you so desire.
273
274``` js
275var tokens = marked.lexer(text, options);
276console.log(marked.parser(tokens));
277```
278
279``` js
280var lexer = new marked.Lexer(options);
281var tokens = lexer.lex(text);
282console.log(tokens);
283console.log(lexer.rules);
284```
285
286## CLI
287
288``` bash
289$ marked -o hello.html
290hello world
291^D
292$ cat hello.html
293<p>hello world</p>
294```
295
296## Philosophy behind marked
297
298The point of marked was to create a markdown compiler where it was possible to
299frequently parse huge chunks of markdown without having to worry about
300caching the compiled output somehow...or blocking for an unnecesarily long time.
301
302marked is very concise and still implements all markdown features. It is also
303now fully compatible with the client-side.
304
305marked more or less passes the official markdown test suite in its
306entirety. This is important because a surprising number of markdown compilers
307cannot pass more than a few tests. It was very difficult to get marked as
308compliant as it is. It could have cut corners in several areas for the sake
309of performance, but did not in order to be exactly what you expect in terms
310of a markdown rendering. In fact, this is why marked could be considered at a
311disadvantage in the benchmarks above.
312
313Along with implementing every markdown feature, marked also implements [GFM
314features][gfmf].
315
316## Benchmarks
317
318node v0.8.x
319
320``` bash
321$ node test --bench
322marked completed in 3411ms.
323marked (gfm) completed in 3727ms.
324marked (pedantic) completed in 3201ms.
325robotskirt completed in 808ms.
326showdown (reuse converter) completed in 11954ms.
327showdown (new converter) completed in 17774ms.
328markdown-js completed in 17191ms.
329```
330
331__Marked is now faster than Discount, which is written in C.__
332
333For 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.
334
335### Pro level
336
337You also have direct access to the lexer and parser if you so desire.
338
339``` js
340var tokens = marked.lexer(text, options);
341console.log(marked.parser(tokens));
342```
343
344``` js
345var lexer = new marked.Lexer(options);
346var tokens = lexer.lex(text);
347console.log(tokens);
348console.log(lexer.rules);
349```
350
351``` bash
352$ node
353> require('marked').lexer('> i am using marked.')
354[ { type: 'blockquote_start' },
355 { type: 'paragraph',
356 text: 'i am using marked.' },
357 { type: 'blockquote_end' },
358 links: {} ]
359```
360
361## Running Tests & Contributing
362
363If you want to submit a pull request, make sure your changes pass the test
364suite. If you're adding a new feature, be sure to add your own test.
365
366The marked test suite is set up slightly strangely: `test/new` is for all tests
367that are not part of the original markdown.pl test suite (this is where your
368test should go if you make one). `test/original` is only for the original
369markdown.pl tests. `test/tests` houses both types of tests after they have been
370combined and moved/generated by running `node test --fix` or `marked --test
371--fix`.
372
373In other words, if you have a test to add, add it to `test/new/` and then
374regenerate the tests with `node test --fix`. Commit the result. If your test
375uses a certain feature, for example, maybe it assumes GFM is *not* enabled, you
376can add `.nogfm` to the filename. So, `my-test.text` becomes
377`my-test.nogfm.text`. You can do this with any marked option. Say you want
378line breaks and smartypants enabled, your filename should be:
379`my-test.breaks.smartypants.text`.
380
381To run the tests:
382
383``` bash
384cd marked/
385node test
386```
387
388### Contribution and License Agreement
389
390If you contribute code to this project, you are implicitly allowing your code
391to be distributed under the MIT license. You are also implicitly verifying that
392all code is your original work. `</legalese>`
393
394## License
395
396Copyright (c) 2011-2014, Christopher Jeffrey. (MIT License)
397
398See LICENSE for more info.
399
400[gfm]: https://help.github.com/articles/github-flavored-markdown
401[gfmf]: http://github.github.com/github-flavored-markdown/
402[pygmentize]: https://github.com/rvagg/node-pygmentize-bundled
403[highlight]: https://github.com/isagalaev/highlight.js
404[badge]: http://badge.fury.io/js/marked
405[tables]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#wiki-tables
406[breaks]: https://help.github.com/articles/github-flavored-markdown#newlines