2 * marked - a markdown parser
3 * Copyright (c) 2011-2014, Christopher Jeffrey. (MIT Licensed)
4 * https://github.com/chjj/marked
16 code: /^( {4}[^\n]+\n*)+/,
18 hr: /^( *[-*_]){3,} *(?:\n+|$)/,
19 heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
21 lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
22 blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
23 list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
24 html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
25 def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
27 paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
31 block.bullet = /(?:[*+-]|\d+\.)/;
32 block.item = /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/;
33 block.item = replace(block.item, 'gm')
34 (/bull/g, block.bullet)
37 block.list = replace(block.list)
38 (/bull/g, block.bullet)
39 ('hr', '\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))')
40 ('def', '\\n+(?=' + block.def.source + ')')
43 block.blockquote = replace(block.blockquote)
48 + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code'
49 + '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo'
50 + '|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b';
52 block.html = replace(block.html)
53 ('comment', /<!--[\s\S]*?-->/)
54 ('closed', /<(tag)[\s\S]+?<\/\1>/)
55 ('closing', /<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)
59 block.paragraph = replace(block.paragraph)
61 ('heading', block.heading)
62 ('lheading', block.lheading)
63 ('blockquote', block.blockquote)
64 ('tag', '<' + block._tag)
69 * Normal Block Grammar
72 block.normal = merge({}, block);
78 block.gfm = merge({}, block.normal, {
79 fences: /^ *(`{3,}|~{3,})[ \.]*(\S+)? *\n([\s\S]*?)\s*\1 *(?:\n+|$)/,
81 heading: /^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)/
84 block.gfm.paragraph = replace(block.paragraph)
86 + block.gfm.fences.source.replace('\\1', '\\2') + '|'
87 + block.list.source.replace('\\1', '\\3') + '|')
91 * GFM + Tables Block Grammar
94 block.tables = merge({}, block.gfm, {
95 nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,
96 table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/
103 function Lexer(options) {
105 this.tokens.links = {};
106 this.options = options || marked.defaults;
107 this.rules = block.normal;
109 if (this.options.gfm) {
110 if (this.options.tables) {
111 this.rules = block.tables;
113 this.rules = block.gfm;
128 Lexer.lex = function (src, options) {
129 var lexer = new Lexer(options);
130 return lexer.lex(src);
137 Lexer.prototype.lex = function (src) {
139 .replace(/\r\n|\r/g, '\n')
141 .replace(/\u00a0/g, ' ')
142 .replace(/\u2424/g, '\n');
144 return this.token(src, true);
151 Lexer.prototype.token = function (src, top, bq) {
152 var src = src.replace(/^ +$/gm, '')
165 if (cap = this.rules.newline.exec(src)) {
166 src = src.substring(cap[0].length);
167 if (cap[0].length > 1) {
175 if (cap = this.rules.code.exec(src)) {
176 src = src.substring(cap[0].length);
177 cap = cap[0].replace(/^ {4}/gm, '');
180 text: !this.options.pedantic
181 ? cap.replace(/\n+$/, '')
188 if (cap = this.rules.fences.exec(src)) {
189 src = src.substring(cap[0].length);
199 if (cap = this.rules.heading.exec(src)) {
200 src = src.substring(cap[0].length);
203 depth: cap[1].length,
209 // table no leading pipe (gfm)
210 if (top && (cap = this.rules.nptable.exec(src))) {
211 src = src.substring(cap[0].length);
215 header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
216 align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
217 cells: cap[3].replace(/\n$/, '').split('\n')
220 for (i = 0; i < item.align.length; i++) {
221 if (/^ *-+: *$/.test(item.align[i])) {
222 item.align[i] = 'right';
223 } else if (/^ *:-+: *$/.test(item.align[i])) {
224 item.align[i] = 'center';
225 } else if (/^ *:-+ *$/.test(item.align[i])) {
226 item.align[i] = 'left';
228 item.align[i] = null;
232 for (i = 0; i < item.cells.length; i++) {
233 item.cells[i] = item.cells[i].split(/ *\| */);
236 this.tokens.push(item);
242 if (cap = this.rules.lheading.exec(src)) {
243 src = src.substring(cap[0].length);
246 depth: cap[2] === '=' ? 1 : 2,
253 if (cap = this.rules.hr.exec(src)) {
254 src = src.substring(cap[0].length);
262 if (cap = this.rules.blockquote.exec(src)) {
263 src = src.substring(cap[0].length);
266 type: 'blockquote_start'
269 cap = cap[0].replace(/^ *> ?/gm, '');
271 // Pass `top` to keep the current
272 // "toplevel" state. This is exactly
273 // how markdown.pl works.
274 this.token(cap, top, true);
277 type: 'blockquote_end'
284 if (cap = this.rules.list.exec(src)) {
285 src = src.substring(cap[0].length);
290 ordered: bull.length > 1
293 // Get each top-level item.
294 cap = cap[0].match(this.rules.item);
303 // Remove the list item's bullet
304 // so it is seen as the next token.
306 item = item.replace(/^ *([*+-]|\d+\.) +/, '');
308 // Outdent whatever the
309 // list item contains. Hacky.
310 if (~item.indexOf('\n ')) {
311 space -= item.length;
312 item = !this.options.pedantic
313 ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
314 : item.replace(/^ {1,4}/gm, '');
317 // Determine whether the next list item belongs here.
318 // Backpedal if it does not belong in this list.
319 if (this.options.smartLists && i !== l - 1) {
320 b = block.bullet.exec(cap[i + 1]) [0];
321 if (bull !== b && !(bull.length > 1 && b.length > 1)) {
322 src = cap.slice(i + 1).join('\n') + src;
327 // Determine whether item is loose or not.
328 // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
329 // for discount behavior.
330 loose = next || /\n\n(?!\s*$)/.test(item);
332 next = item.charAt(item.length - 1) === '\n';
333 if (!loose) loose = next;
343 this.token(item, false, bq);
346 type: 'list_item_end'
358 if (cap = this.rules.html.exec(src)) {
359 src = src.substring(cap[0].length);
361 type: this.options.sanitize
364 pre: !this.options.sanitizer
365 && (cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style'),
372 if ((!bq && top) && (cap = this.rules.def.exec(src))) {
373 src = src.substring(cap[0].length);
374 this.tokens.links[cap[1].toLowerCase()] = {
382 if (top && (cap = this.rules.table.exec(src))) {
383 src = src.substring(cap[0].length);
387 header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
388 align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
389 cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n')
392 for (i = 0; i < item.align.length; i++) {
393 if (/^ *-+: *$/.test(item.align[i])) {
394 item.align[i] = 'right';
395 } else if (/^ *:-+: *$/.test(item.align[i])) {
396 item.align[i] = 'center';
397 } else if (/^ *:-+ *$/.test(item.align[i])) {
398 item.align[i] = 'left';
400 item.align[i] = null;
404 for (i = 0; i < item.cells.length; i++) {
405 item.cells[i] = item.cells[i]
406 .replace(/^ *\| *| *\| *$/g, '')
410 this.tokens.push(item);
415 // top-level paragraph
416 if (top && (cap = this.rules.paragraph.exec(src))) {
417 src = src.substring(cap[0].length);
420 text: cap[1].charAt(cap[1].length - 1) === '\n'
421 ? cap[1].slice(0, -1)
428 if (cap = this.rules.text.exec(src)) {
429 // Top-level should never reach here.
430 src = src.substring(cap[0].length);
440 Error('Infinite loop on byte: ' + src.charCodeAt(0));
448 * Inline-Level Grammar
452 escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
453 autolink: /^<([^ <>]+(@|:\/)[^ <>]+)>/,
455 tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^<'">])*?>/,
456 link: /^!?\[(inside)\]\(href\)/,
457 reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
458 nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
459 strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
460 em: /^\b_((?:[^_]|__)+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
461 code: /^(`+)([\s\S]*?[^`])\1(?!`)/,
462 br: /^ {2,}\n(?!\s*$)/,
464 text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
467 inline._inside = /(?:\[[^\]]*\]|\\[\[\]]|[^\[\]]|\](?=[^\[]*\]))*/;
468 inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
470 inline.link = replace(inline.link)
471 ('inside', inline._inside)
472 ('href', inline._href)
475 inline.reflink = replace(inline.reflink)
476 ('inside', inline._inside)
480 * Normal Inline Grammar
483 inline.normal = merge({}, inline);
486 * Pedantic Inline Grammar
489 inline.pedantic = merge({}, inline.normal, {
490 strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
491 em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/
498 inline.gfm = merge({}, inline.normal, {
499 escape: replace(inline.escape)('])', '~|])')(),
500 url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,
501 del: /^~~(?=\S)([\s\S]*?\S)~~/,
502 text: replace(inline.text)
509 * GFM + Line Breaks Inline Grammar
512 inline.breaks = merge({}, inline.gfm, {
513 br: replace(inline.br)('{2,}', '*')(),
514 text: replace(inline.gfm.text)('{2,}', '*')()
518 * Inline Lexer & Compiler
521 function InlineLexer(links, options) {
522 this.options = options || marked.defaults;
524 this.rules = inline.normal;
525 this.renderer = this.options.renderer || new Renderer;
526 this.renderer.options = this.options;
530 Error('Tokens array requires a `links` property.');
533 if (this.options.gfm) {
534 if (this.options.breaks) {
535 this.rules = inline.breaks;
537 this.rules = inline.gfm;
539 } else if (this.options.pedantic) {
540 this.rules = inline.pedantic;
545 * Expose Inline Rules
548 InlineLexer.rules = inline;
551 * Static Lexing/Compiling Method
554 InlineLexer.output = function (src, links, options) {
555 var inline = new InlineLexer(links, options);
556 return inline.output(src);
563 InlineLexer.prototype.output = function (src) {
572 if (cap = this.rules.escape.exec(src)) {
573 src = src.substring(cap[0].length);
579 if (cap = this.rules.autolink.exec(src)) {
580 src = src.substring(cap[0].length);
581 if (cap[2] === '@') {
583 cap[1].charAt(6) === ':'
584 ? this.mangle(cap[1].substring(7))
585 : this.mangle(cap[1])
587 href = this.mangle('mailto:') + text;
589 text = escape(cap[1]);
592 out += this.renderer.link(href, null, text);
597 if (!this.inLink && (cap = this.rules.url.exec(src))) {
598 src = src.substring(cap[0].length);
599 text = escape(cap[1]);
601 out += this.renderer.link(href, null, text);
606 if (cap = this.rules.tag.exec(src)) {
607 if (!this.inLink && /^<a /i.test(cap[0])) {
609 } else if (this.inLink && /^<\/a>/i.test(cap[0])) {
612 src = src.substring(cap[0].length);
613 out += this.options.sanitize
614 ? this.options.sanitizer
615 ? this.options.sanitizer(cap[0])
622 if (cap = this.rules.link.exec(src)) {
623 src = src.substring(cap[0].length);
625 out += this.outputLink(cap, {
634 if ((cap = this.rules.reflink.exec(src))
635 || (cap = this.rules.nolink.exec(src))) {
636 src = src.substring(cap[0].length);
637 link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
638 link = this.links[link.toLowerCase()];
639 if (!link || !link.href) {
640 out += cap[0].charAt(0);
641 src = cap[0].substring(1) + src;
645 out += this.outputLink(cap, link);
651 if (cap = this.rules.strong.exec(src)) {
652 src = src.substring(cap[0].length);
653 out += this.renderer.strong(this.output(cap[2] || cap[1]));
658 if (cap = this.rules.em.exec(src)) {
659 src = src.substring(cap[0].length);
660 out += this.renderer.em(this.output(cap[2] || cap[1]));
665 if (cap = this.rules.code.exec(src)) {
666 src = src.substring(cap[0].length);
667 out += this.renderer.codespan(escape(cap[2].trim(), true));
672 if (cap = this.rules.br.exec(src)) {
673 src = src.substring(cap[0].length);
674 out += this.renderer.br();
679 if (cap = this.rules.del.exec(src)) {
680 src = src.substring(cap[0].length);
681 out += this.renderer.del(this.output(cap[1]));
686 if (cap = this.rules.text.exec(src)) {
687 src = src.substring(cap[0].length);
688 out += this.renderer.text(escape(this.smartypants(cap[0])));
694 Error('Infinite loop on byte: ' + src.charCodeAt(0));
705 InlineLexer.prototype.outputLink = function (cap, link) {
706 var href = escape(link.href)
707 , title = link.title ? escape(link.title) : null;
709 return cap[0].charAt(0) !== '!'
710 ? this.renderer.link(href, title, this.output(cap[1]))
711 : this.renderer.image(href, title, escape(cap[1]));
715 * Smartypants Transformations
718 InlineLexer.prototype.smartypants = function (text) {
719 if (!this.options.smartypants) return text;
722 .replace(/---/g, '\u2014')
724 .replace(/--/g, '\u2013')
726 .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
727 // closing singles & apostrophes
728 .replace(/'/g, '\u2019')
730 .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
732 .replace(/"/g, '\u201d')
734 .replace(/\.{3}/g, '\u2026');
741 InlineLexer.prototype.mangle = function (text) {
742 if (!this.options.mangle) return text;
749 ch = text.charCodeAt(i);
750 if (Math.random() > 0.5) {
751 ch = 'x' + ch.toString(16);
753 out += '&#' + ch + ';';
763 function Renderer(options) {
764 this.options = options || {};
767 Renderer.prototype.code = function (code, lang, escaped) {
768 if (this.options.highlight) {
769 var out = this.options.highlight(code, lang);
770 if (out != null && out !== code) {
778 + (escaped ? code : escape(code, true))
782 return '<pre><code class="'
783 + this.options.langPrefix
786 + (escaped ? code : escape(code, true))
787 + '\n</code></pre>\n';
790 Renderer.prototype.blockquote = function (quote) {
791 return '<blockquote>\n' + quote + '</blockquote>\n';
794 Renderer.prototype.html = function (html) {
798 Renderer.prototype.heading = function (text, level, raw) {
802 + this.options.headerPrefix
803 + raw.toLowerCase().replace(/[^\w]+/g, '-')
811 Renderer.prototype.hr = function () {
812 return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
815 Renderer.prototype.list = function (body, ordered) {
816 var type = ordered ? 'ol' : 'ul';
817 return '<' + type + '>\n' + body + '</' + type + '>\n';
820 Renderer.prototype.listitem = function (text) {
821 return '<li>' + text + '</li>\n';
824 Renderer.prototype.paragraph = function (text) {
825 return '<p>' + text + '</p>\n';
828 Renderer.prototype.table = function (header, body) {
839 Renderer.prototype.tablerow = function (content) {
840 return '<tr>\n' + content + '</tr>\n';
843 Renderer.prototype.tablecell = function (content, flags) {
844 var type = flags.header ? 'th' : 'td';
845 var tag = flags.align
846 ? '<' + type + ' style="text-align:' + flags.align + '">'
848 return tag + content + '</' + type + '>\n';
851 // span level renderer
852 Renderer.prototype.strong = function (text) {
853 return '<strong>' + text + '</strong>';
856 Renderer.prototype.em = function (text) {
857 return '<em>' + text + '</em>';
860 Renderer.prototype.codespan = function (text) {
861 return '<code>' + text + '</code>';
864 Renderer.prototype.br = function () {
865 return this.options.xhtml ? '<br/>' : '<br>';
868 Renderer.prototype.del = function (text) {
869 return '<del>' + text + '</del>';
872 Renderer.prototype.link = function (href, title, text) {
873 if (this.options.sanitize) {
875 var prot = decodeURIComponent(unescape(href))
876 .replace(/[^\w:]/g, '')
881 if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0 || prot.indexOf('data:') === 0) {
885 if (this.options.baseUrl && !originIndependentUrl.test(href)) {
886 href = resolveUrl(this.options.baseUrl, href);
888 var out = '<a href="' + href + '"';
890 out += ' title="' + title + '"';
892 out += '>' + text + '</a>';
896 Renderer.prototype.image = function (href, title, text) {
897 if (this.options.baseUrl && !originIndependentUrl.test(href)) {
898 href = resolveUrl(this.options.baseUrl, href);
900 var out = '<img src="' + href + '" alt="' + text + '"';
902 out += ' title="' + title + '"';
904 out += this.options.xhtml ? '/>' : '>';
908 Renderer.prototype.text = function (text) {
913 * Parsing & Compiling
916 function Parser(options) {
919 this.options = options || marked.defaults;
920 this.options.renderer = this.options.renderer || new Renderer;
921 this.renderer = this.options.renderer;
922 this.renderer.options = this.options;
926 * Static Parse Method
929 Parser.parse = function (src, options, renderer) {
930 var parser = new Parser(options, renderer);
931 return parser.parse(src);
938 Parser.prototype.parse = function (src) {
939 this.inline = new InlineLexer(src.links, this.options, this.renderer);
940 this.tokens = src.reverse();
943 while (this.next()) {
954 Parser.prototype.next = function () {
955 return this.token = this.tokens.pop();
962 Parser.prototype.peek = function () {
963 return this.tokens[this.tokens.length - 1] || 0;
970 Parser.prototype.parseText = function () {
971 var body = this.token.text;
973 while (this.peek().type === 'text') {
974 body += '\n' + this.next().text;
977 return this.inline.output(body);
981 * Parse Current Token
984 Parser.prototype.tok = function () {
985 switch (this.token.type) {
990 return this.renderer.hr();
993 return this.renderer.heading(
994 this.inline.output(this.token.text),
999 return this.renderer.code(this.token.text,
1001 this.token.escaped);
1014 for (i = 0; i < this.token.header.length; i++) {
1015 flags = { header: true, align: this.token.align[i] };
1016 cell += this.renderer.tablecell(
1017 this.inline.output(this.token.header[i]),
1018 { header: true, align: this.token.align[i] }
1021 header += this.renderer.tablerow(cell);
1023 for (i = 0; i < this.token.cells.length; i++) {
1024 row = this.token.cells[i];
1027 for (j = 0; j < row.length; j++) {
1028 cell += this.renderer.tablecell(
1029 this.inline.output(row[j]),
1030 { header: false, align: this.token.align[j] }
1034 body += this.renderer.tablerow(cell);
1036 return this.renderer.table(header, body);
1038 case 'blockquote_start': {
1041 while (this.next().type !== 'blockquote_end') {
1045 return this.renderer.blockquote(body);
1047 case 'list_start': {
1049 , ordered = this.token.ordered;
1051 while (this.next().type !== 'list_end') {
1055 return this.renderer.list(body, ordered);
1057 case 'list_item_start': {
1060 while (this.next().type !== 'list_item_end') {
1061 body += this.token.type === 'text'
1066 return this.renderer.listitem(body);
1068 case 'loose_item_start': {
1071 while (this.next().type !== 'list_item_end') {
1075 return this.renderer.listitem(body);
1078 var html = !this.token.pre && !this.options.pedantic
1079 ? this.inline.output(this.token.text)
1081 return this.renderer.html(html);
1084 return this.renderer.paragraph(this.inline.output(this.token.text));
1087 return this.renderer.paragraph(this.parseText());
1096 function escape(html, encode) {
1098 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
1099 .replace(/</g, '<')
1100 .replace(/>/g, '>')
1101 .replace(/"/g, '"')
1102 .replace(/'/g, ''');
1105 function unescape(html) {
1106 // explicitly match decimal, hex, and named HTML entities
1107 return html.replace(/&(#(?:\d+)|(?:#x[0-9A-Fa-f]+)|(?:\w+));?/ig, function (_, n) {
1108 n = n.toLowerCase();
1109 if (n === 'colon') return ':';
1110 if (n.charAt(0) === '#') {
1111 return n.charAt(1) === 'x'
1112 ? String.fromCharCode(parseInt(n.substring(2), 16))
1113 : String.fromCharCode(+n.substring(1));
1119 function replace(regex, opt) {
1120 regex = regex.source;
1122 return function self(name, val) {
1123 if (!name) return new RegExp(regex, opt);
1124 val = val.source || val;
1125 val = val.replace(/(^|[^\[])\^/g, '$1');
1126 regex = regex.replace(name, val);
1131 function resolveUrl(base, href) {
1132 if (!baseUrls[' ' + base]) {
1133 // we can ignore everything in base after the last slash of its path component,
1134 // but we might need to add _that_
1135 // https://tools.ietf.org/html/rfc3986#section-3
1136 if (/^[^:]+:\/*[^/]*$/.test(base)) {
1137 baseUrls[' ' + base] = base + '/';
1139 baseUrls[' ' + base] = base.replace(/[^/]*$/, '');
1142 base = baseUrls[' ' + base];
1144 if (href.slice(0, 2) === '//') {
1145 return base.replace(/:[\s\S]*/, ':') + href;
1146 } else if (href.charAt(0) === '/') {
1147 return base.replace(/(:\/*[^/]*)[\s\S]*/, '$1') + href;
1153 var originIndependentUrl = /^$|^[a-z][a-z0-9+.-]*:|^[?#]/i;
1158 function merge(obj) {
1163 for (; i < arguments.length; i++) {
1164 target = arguments[i];
1165 for (key in target) {
1166 if (Object.prototype.hasOwnProperty.call(target, key)) {
1167 obj[key] = target[key];
1180 function marked(src, opt, callback) {
1181 if (callback || typeof opt === 'function') {
1187 opt = merge({}, marked.defaults, opt || {});
1189 var highlight = opt.highlight
1195 tokens = Lexer.lex(src, opt)
1200 pending = tokens.length;
1202 var done = function (err) {
1204 opt.highlight = highlight;
1205 return callback(err);
1211 out = Parser.parse(tokens, opt);
1216 opt.highlight = highlight;
1220 : callback(null, out);
1223 if (!highlight || highlight.length < 3) {
1227 delete opt.highlight;
1229 if (!pending) return done();
1231 for (; i < tokens.length; i++) {
1233 if (token.type !== 'code') {
1234 return --pending || done();
1236 return highlight(token.text, token.lang, function (err, code) {
1237 if (err) return done(err);
1238 if (code == null || code === token.text) {
1239 return --pending || done();
1242 token.escaped = true;
1243 --pending || done();
1251 if (opt) opt = merge({}, marked.defaults, opt);
1252 return Parser.parse(Lexer.lex(src, opt), opt);
1254 e.message += '\nPlease report this to https://github.com/chjj/marked.';
1255 if ((opt || marked.defaults).silent) {
1256 return '<p>An error occurred:</p><pre>'
1257 + escape(e.message + '', true)
1269 marked.setOptions = function (opt) {
1270 merge(marked.defaults, opt);
1285 langPrefix: 'lang-',
1288 renderer: new Renderer,
1297 marked.Parser = Parser;
1298 marked.parser = Parser.parse;
1300 marked.Renderer = Renderer;
1302 marked.Lexer = Lexer;
1303 marked.lexer = Lexer.lex;
1305 marked.InlineLexer = InlineLexer;
1306 marked.inlineLexer = InlineLexer.output;
1308 marked.parse = marked;
1310 if (typeof module !== 'undefined' && typeof exports === 'object') {
1311 module.exports = marked;
1312 } else if (typeof define === 'function' && define.amd) {
1313 define(function () { return marked; });
1315 this.marked = marked;
1318 }).call(function () {
1319 return this || (typeof window !== 'undefined' ? window : global);