3
> A full-featured markdown parser and compiler, written in JavaScript. Built
6
[![NPM version](https://badge.fury.io/js/marked.png)][badge]
11
npm install marked --save
19
var marked = require('marked');
20
console.log(marked('I am using __markdown__.'));
21
// Outputs: <p>I am using <strong>markdown</strong>.</p>
24
Example setting options with default values:
27
var marked = require('marked');
29
renderer: new marked.Renderer(),
39
console.log(marked('I am using __markdown__.'));
48
<meta charset="utf-8"/>
49
<title>Marked in the browser</title>
50
<script src="lib/marked.js"></script>
53
<div id="content"></div>
55
document.getElementById('content').innerHTML =
56
marked('# Marked in browser\n\nRendered by **marked**.');
62
## marked(markdownString [,options] [,callback])
68
String of markdown source to be compiled.
74
Hash of options. Can also be set using the `marked.setOptions` method as seen
81
Function called when the `markdownString` has been fully parsed when using
82
async highlighting. If the `options` argument is omitted, this can be used as
91
A 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]:
96
var marked = require('marked');
98
var markdownString = '```js\n console.log("hello"); \n```';
100
// Async highlighting with pygmentize-bundled
102
highlight: function (code, lang, callback) {
103
require('pygmentize-bundled')({ lang: lang, format: 'html' }, code, function (err, result) {
104
callback(err, result.toString());
109
// Using async version of marked
110
marked(markdownString, function (err, content) {
112
console.log(content);
115
// Synchronous highlighting with highlight.js
117
highlight: function (code) {
118
return require('highlight.js').highlightAuto(code).value;
122
console.log(marked(markdownString));
125
#### highlight arguments
131
The section of code to pass to the highlighter.
137
The programming language specified in the code block.
143
The callback function to call when using an async highlighter.
148
Default: `new Renderer()`
150
An object containing functions to render tokens to HTML.
152
#### Overriding renderer methods
154
The renderer option allows you to render tokens in a custom manner. Here is an
155
example of overriding the default heading token rendering by adding an embedded anchor tag like on GitHub:
158
var marked = require('marked');
159
var renderer = new marked.Renderer();
161
renderer.heading = function (text, level) {
162
var escapedText = text.toLowerCase().replace(/[^\w]+/g, '-');
164
return '<h' + level + '><a name="' +
166
'" class="anchor" href="#' +
168
'"><span class="header-link"></span></a>' +
169
text + '</h' + level + '>';
172
console.log(marked('# heading+', { renderer: renderer }));
174
This code will output the following HTML:
177
<a name="heading-" class="anchor" href="#heading-">
178
<span class="header-link"></span>
184
#### Block level renderer methods
186
- code(*string* code, *string* language)
187
- blockquote(*string* quote)
188
- html(*string* html)
189
- heading(*string* text, *number* level)
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)
198
`flags` has the following properties:
202
header: true || false,
203
align: 'center' || 'left' || 'right'
207
#### Inline level renderer methods
209
- strong(*string* text)
211
- codespan(*string* code)
214
- link(*string* href, *string* title, *string* text)
215
- image(*string* href, *string* title, *string* text)
222
Enable [GitHub flavored markdown][gfm].
229
Enable GFM [tables][tables].
230
This option requires the `gfm` option to be true.
237
Enable GFM [line breaks][breaks].
238
This option requires the `gfm` option to be true.
245
Conform to obscure parts of `markdown.pl` as much as possible. Don't fix any of
246
the original markdown bugs or poor behavior.
253
Sanitize the output. Ignore any HTML that has been input.
260
Use smarter list behavior than the original markdown. May eventually be
261
default with the old behavior moved into `pedantic`.
268
Use "smart" typograhic punctuation for things like quotes and dashes.
270
## Access to lexer and parser
272
You also have direct access to the lexer and parser if you so desire.
275
var tokens = marked.lexer(text, options);
276
console.log(marked.parser(tokens));
280
var lexer = new marked.Lexer(options);
281
var tokens = lexer.lex(text);
283
console.log(lexer.rules);
289
$ marked -o hello.html
296
## Philosophy behind marked
298
The point of marked was to create a markdown compiler where it was possible to
299
frequently parse huge chunks of markdown without having to worry about
300
caching the compiled output somehow...or blocking for an unnecesarily long time.
302
marked is very concise and still implements all markdown features. It is also
303
now fully compatible with the client-side.
305
marked more or less passes the official markdown test suite in its
306
entirety. This is important because a surprising number of markdown compilers
307
cannot pass more than a few tests. It was very difficult to get marked as
308
compliant as it is. It could have cut corners in several areas for the sake
309
of performance, but did not in order to be exactly what you expect in terms
310
of a markdown rendering. In fact, this is why marked could be considered at a
311
disadvantage in the benchmarks above.
313
Along with implementing every markdown feature, marked also implements [GFM
322
marked completed in 3411ms.
323
marked (gfm) completed in 3727ms.
324
marked (pedantic) completed in 3201ms.
325
robotskirt completed in 808ms.
326
showdown (reuse converter) completed in 11954ms.
327
showdown (new converter) completed in 17774ms.
328
markdown-js completed in 17191ms.
331
__Marked is now faster than Discount, which is written in C.__
333
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.
337
You also have direct access to the lexer and parser if you so desire.
340
var tokens = marked.lexer(text, options);
341
console.log(marked.parser(tokens));
345
var lexer = new marked.Lexer(options);
346
var tokens = lexer.lex(text);
348
console.log(lexer.rules);
353
> require('marked').lexer('> i am using marked.')
354
[ { type: 'blockquote_start' },
356
text: 'i am using marked.' },
357
{ type: 'blockquote_end' },
361
## Running Tests & Contributing
363
If you want to submit a pull request, make sure your changes pass the test
364
suite. If you're adding a new feature, be sure to add your own test.
366
The marked test suite is set up slightly strangely: `test/new` is for all tests
367
that are not part of the original markdown.pl test suite (this is where your
368
test should go if you make one). `test/original` is only for the original
369
markdown.pl tests. `test/tests` houses both types of tests after they have been
370
combined and moved/generated by running `node test --fix` or `marked --test
373
In other words, if you have a test to add, add it to `test/new/` and then
374
regenerate the tests with `node test --fix`. Commit the result. If your test
375
uses a certain feature, for example, maybe it assumes GFM is *not* enabled, you
376
can 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
378
line breaks and smartypants enabled, your filename should be:
379
`my-test.breaks.smartypants.text`.
388
### Contribution and License Agreement
390
If you contribute code to this project, you are implicitly allowing your code
391
to be distributed under the MIT license. You are also implicitly verifying that
392
all code is your original work. `</legalese>`
396
Copyright (c) 2011-2014, Christopher Jeffrey. (MIT License)
398
See LICENSE for more info.
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