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