UNPKG

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