2 * Copyright 2013 Laurent Bovet <laurent.bovet@windmaster.ch>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 code: /^( {4}[^\n]+\n*)+/,
27 hr: /^( *[-*_]){3,} *(?:\n+|$)/,
28 heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
30 lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
31 blockquote: /^( *>[^\n]+(\n[^\n]+)*\n*)+/,
32 list: /^( *)(bull) [\s\S]+?(?:hr|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
33 html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
34 def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
36 paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
40 block.bullet = /(?:[*+-]|\d+\.)/;
41 block.item = /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/;
42 block.item = replace(block.item, 'gm')
43 (/bull/g, block.bullet)
46 block.list = replace(block.list)
47 (/bull/g, block.bullet)
48 ('hr', /\n+(?=(?: *[-*_]){3,} *(?:\n+|$))/)
52 + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code'
53 + '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo'
54 + '|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|@)\\b';
56 block.html = replace(block.html)
57 ('comment', /<!--[\s\S]*?-->/)
58 ('closed', /<(tag)[\s\S]+?<\/\1>/)
59 ('closing', /<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)
63 block.paragraph = replace(block.paragraph)
65 ('heading', block.heading)
66 ('lheading', block.lheading)
67 ('blockquote', block.blockquote)
68 ('tag', '<' + block._tag)
73 * Normal Block Grammar
76 block.normal = merge({}, block);
82 block.gfm = merge({}, block.normal, {
83 fences: /^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
87 block.gfm.paragraph = replace(block.paragraph)
89 + block.gfm.fences.source.replace('\\1', '\\2') + '|'
90 + block.list.source.replace('\\1', '\\3') + '|')
94 * GFM + Tables Block Grammar
97 block.tables = merge({}, block.gfm, {
98 nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,
99 table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/
106 function Lexer(options) {
108 this.tokens.links = {};
109 this.options = options || marked.defaults;
110 this.rules = block.normal;
112 if (this.options.gfm) {
113 if (this.options.tables) {
114 this.rules = block.tables;
116 this.rules = block.gfm;
131 Lexer.lex = function(src, options) {
132 var lexer = new Lexer(options);
133 return lexer.lex(src);
140 Lexer.prototype.lex = function(src) {
142 .replace(/\r\n|\r/g, '\n')
144 .replace(/\u00a0/g, ' ')
145 .replace(/\u2424/g, '\n');
147 return this.token(src, true);
154 Lexer.prototype.token = function(src, top) {
155 var src = src.replace(/^ +$/gm, '')
168 if (cap = this.rules.newline.exec(src)) {
169 src = src.substring(cap[0].length);
170 if (cap[0].length > 1) {
178 if (cap = this.rules.code.exec(src)) {
179 src = src.substring(cap[0].length);
180 cap = cap[0].replace(/^ {4}/gm, '');
183 text: !this.options.pedantic
184 ? cap.replace(/\n+$/, '')
191 if (cap = this.rules.fences.exec(src)) {
192 src = src.substring(cap[0].length);
202 if (cap = this.rules.heading.exec(src)) {
203 src = src.substring(cap[0].length);
206 depth: cap[1].length,
212 // table no leading pipe (gfm)
213 if (top && (cap = this.rules.nptable.exec(src))) {
214 src = src.substring(cap[0].length);
218 header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
219 align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
220 cells: cap[3].replace(/\n$/, '').split('\n')
223 for (i = 0; i < item.align.length; i++) {
224 if (/^ *-+: *$/.test(item.align[i])) {
225 item.align[i] = 'right';
226 } else if (/^ *:-+: *$/.test(item.align[i])) {
227 item.align[i] = 'center';
228 } else if (/^ *:-+ *$/.test(item.align[i])) {
229 item.align[i] = 'left';
231 item.align[i] = null;
235 for (i = 0; i < item.cells.length; i++) {
236 item.cells[i] = item.cells[i].split(/ *\| */);
239 this.tokens.push(item);
245 if (cap = this.rules.lheading.exec(src)) {
246 src = src.substring(cap[0].length);
249 depth: cap[2] === '=' ? 1 : 2,
256 if (cap = this.rules.hr.exec(src)) {
257 src = src.substring(cap[0].length);
265 if (cap = this.rules.blockquote.exec(src)) {
266 src = src.substring(cap[0].length);
269 type: 'blockquote_start'
272 cap = cap[0].replace(/^ *> ?/gm, '');
274 // Pass `top` to keep the current
275 // "toplevel" state. This is exactly
276 // how markdown.pl works.
277 this.token(cap, top);
280 type: 'blockquote_end'
287 if (cap = this.rules.list.exec(src)) {
288 src = src.substring(cap[0].length);
293 ordered: bull.length > 1
296 // Get each top-level item.
297 cap = cap[0].match(this.rules.item);
306 // Remove the list item's bullet
307 // so it is seen as the next token.
309 item = item.replace(/^ *([*+-]|\d+\.) +/, '');
311 // Outdent whatever the
312 // list item contains. Hacky.
313 if (~item.indexOf('\n ')) {
314 space -= item.length;
315 item = !this.options.pedantic
316 ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
317 : item.replace(/^ {1,4}/gm, '');
320 // Determine whether the next list item belongs here.
321 // Backpedal if it does not belong in this list.
322 if (this.options.smartLists && i !== l - 1) {
323 b = block.bullet.exec(cap[i + 1])[0];
324 if (bull !== b && !(bull.length > 1 && b.length > 1)) {
325 src = cap.slice(i + 1).join('\n') + src;
330 // Determine whether item is loose or not.
331 // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
332 // for discount behavior.
333 loose = next || /\n\n(?!\s*$)/.test(item);
335 next = item.charAt(item.length - 1) === '\n';
336 if (!loose) loose = next;
346 this.token(item, false);
349 type: 'list_item_end'
361 if (cap = this.rules.html.exec(src)) {
362 src = src.substring(cap[0].length);
364 type: this.options.sanitize
367 pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
374 if (top && (cap = this.rules.def.exec(src))) {
375 src = src.substring(cap[0].length);
376 this.tokens.links[cap[1].toLowerCase()] = {
384 if (top && (cap = this.rules.table.exec(src))) {
385 src = src.substring(cap[0].length);
389 header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
390 align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
391 cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n')
394 for (i = 0; i < item.align.length; i++) {
395 if (/^ *-+: *$/.test(item.align[i])) {
396 item.align[i] = 'right';
397 } else if (/^ *:-+: *$/.test(item.align[i])) {
398 item.align[i] = 'center';
399 } else if (/^ *:-+ *$/.test(item.align[i])) {
400 item.align[i] = 'left';
402 item.align[i] = null;
406 for (i = 0; i < item.cells.length; i++) {
407 item.cells[i] = item.cells[i]
408 .replace(/^ *\| *| *\| *$/g, '')
412 this.tokens.push(item);
417 // top-level paragraph
418 if (top && (cap = this.rules.paragraph.exec(src))) {
419 src = src.substring(cap[0].length);
422 text: cap[1].charAt(cap[1].length - 1) === '\n'
423 ? cap[1].slice(0, -1)
430 if (cap = this.rules.text.exec(src)) {
431 // Top-level should never reach here.
432 src = src.substring(cap[0].length);
442 Error('Infinite loop on byte: ' + src.charCodeAt(0));
450 * Inline-Level Grammar
454 escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
455 autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
457 tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
458 link: /^!?\[(inside)\]\(href\)/,
459 reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
460 nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
461 strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
462 em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
463 code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
464 br: /^ {2,}\n(?!\s*$)/,
466 text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
469 inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
470 inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
472 inline.link = replace(inline.link)
473 ('inside', inline._inside)
474 ('href', inline._href)
477 inline.reflink = replace(inline.reflink)
478 ('inside', inline._inside)
482 * Normal Inline Grammar
485 inline.normal = merge({}, inline);
488 * Pedantic Inline Grammar
491 inline.pedantic = merge({}, inline.normal, {
492 strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
493 em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/
500 inline.gfm = merge({}, inline.normal, {
501 escape: replace(inline.escape)('])', '~|])')(),
502 url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,
503 del: /^~~(?=\S)([\s\S]*?\S)~~/,
504 text: replace(inline.text)
511 * GFM + Line Breaks Inline Grammar
514 inline.breaks = merge({}, inline.gfm, {
515 br: replace(inline.br)('{2,}', '*')(),
516 text: replace(inline.gfm.text)('{2,}', '*')()
520 * Inline Lexer & Compiler
523 function InlineLexer(links, options) {
524 this.options = options || marked.defaults;
526 this.rules = inline.normal;
527 this.renderer = this.options.renderer || new Renderer;
531 Error('Tokens array requires a `links` property.');
534 if (this.options.gfm) {
535 if (this.options.breaks) {
536 this.rules = inline.breaks;
538 this.rules = inline.gfm;
540 } else if (this.options.pedantic) {
541 this.rules = inline.pedantic;
546 * Expose Inline Rules
549 InlineLexer.rules = inline;
552 * Static Lexing/Compiling Method
555 InlineLexer.output = function(src, links, options) {
556 var inline = new InlineLexer(links, options);
557 return inline.output(src);
564 InlineLexer.prototype.output = function(src) {
573 if (cap = this.rules.escape.exec(src)) {
574 src = src.substring(cap[0].length);
580 if (cap = this.rules.autolink.exec(src)) {
581 src = src.substring(cap[0].length);
582 if (cap[2] === '@') {
583 text = cap[1].charAt(6) === ':'
584 ? this.mangle(cap[1].substring(7))
585 : this.mangle(cap[1]);
586 href = this.mangle('mailto:') + text;
588 text = escape(cap[1]);
591 out += this.renderer.link(href, null, text);
596 if (cap = this.rules.url.exec(src)) {
597 src = src.substring(cap[0].length);
598 text = escape(cap[1]);
600 out += this.renderer.link(href, null, text);
605 if (cap = this.rules.tag.exec(src)) {
606 src = src.substring(cap[0].length);
607 out += this.options.sanitize
614 if (cap = this.rules.link.exec(src)) {
615 src = src.substring(cap[0].length);
616 out += this.outputLink(cap, {
624 if ((cap = this.rules.reflink.exec(src))
625 || (cap = this.rules.nolink.exec(src))) {
626 src = src.substring(cap[0].length);
627 link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
628 link = this.links[link.toLowerCase()];
629 if (!link || !link.href) {
630 out += cap[0].charAt(0);
631 src = cap[0].substring(1) + src;
634 out += this.outputLink(cap, link);
639 if (cap = this.rules.strong.exec(src)) {
640 src = src.substring(cap[0].length);
641 out += this.renderer.strong(this.output(cap[2] || cap[1]));
646 if (cap = this.rules.em.exec(src)) {
647 src = src.substring(cap[0].length);
648 out += this.renderer.em(this.output(cap[2] || cap[1]));
653 if (cap = this.rules.code.exec(src)) {
654 src = src.substring(cap[0].length);
655 out += this.renderer.codespan(escape(cap[2], true));
660 if (cap = this.rules.br.exec(src)) {
661 src = src.substring(cap[0].length);
662 out += this.renderer.br();
667 if (cap = this.rules.del.exec(src)) {
668 src = src.substring(cap[0].length);
669 out += this.renderer.del(this.output(cap[1]));
674 if (cap = this.rules.text.exec(src)) {
675 src = src.substring(cap[0].length);
676 out += escape(this.smartypants(cap[0]));
682 Error('Infinite loop on byte: ' + src.charCodeAt(0));
693 InlineLexer.prototype.outputLink = function(cap, link) {
694 var href = escape(link.href)
695 , title = link.title ? escape(link.title) : null;
697 if (cap[0].charAt(0) !== '!') {
698 return this.renderer.link(href, title, this.output(cap[1]));
700 return this.renderer.image(href, title, escape(cap[1]));
705 * Smartypants Transformations
708 InlineLexer.prototype.smartypants = function(text) {
709 if (!this.options.smartypants) return text;
712 .replace(/--/g, '\u2014')
714 .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
715 // closing singles & apostrophes
716 .replace(/'/g, '\u2019')
718 .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
720 .replace(/"/g, '\u201d')
722 .replace(/\.{3}/g, '\u2026');
729 InlineLexer.prototype.mangle = function(text) {
736 ch = text.charCodeAt(i);
737 if (Math.random() > 0.5) {
738 ch = 'x' + ch.toString(16);
740 out += '&#' + ch + ';';
750 function Renderer() {}
752 Renderer.prototype.code = function(code, lang) {
759 return '<pre><code class="'
764 + '\n</code></pre>\n';
767 Renderer.prototype.blockquote = function(quote) {
768 return '<blockquote>\n' + quote + '</blockquote>\n';
771 Renderer.prototype.html = function(html) {
775 Renderer.prototype.heading = function(text, level, raw, options) {
785 Renderer.prototype.hr = function() {
789 Renderer.prototype.list = function(body, ordered) {
790 var type = ordered ? 'ol' : 'ul';
791 return '<' + type + '>\n' + body + '</' + type + '>\n';
794 Renderer.prototype.listitem = function(text) {
795 return '<li>' + text + '</li>\n';
798 Renderer.prototype.paragraph = function(text) {
799 return '<p>' + text + '</p>\n';
802 Renderer.prototype.table = function(header, body) {
813 Renderer.prototype.tablerow = function(content) {
814 return '<tr>\n' + content + '</tr>\n';
817 Renderer.prototype.tablecell = function(content, flags) {
818 var type = flags.header ? 'th' : 'td';
819 var tag = flags.align
820 ? '<' + type + ' style="text-align:' + flags.align + '">'
822 return tag + content + '</' + type + '>\n';
825 // span level renderer
826 Renderer.prototype.strong = function(text) {
827 return '<strong>' + text + '</strong>';
830 Renderer.prototype.em = function(text) {
831 return '<em>' + text + '</em>';
834 Renderer.prototype.codespan = function(text) {
835 return '<code>' + text + '</code>';
838 Renderer.prototype.br = function() {
842 Renderer.prototype.del = function(text) {
843 return '<del>' + text + '</del>';
846 Renderer.prototype.link = function(href, title, text) {
847 var out = '<a href="' + href + '"';
849 out += ' title="' + title + '"';
851 out += '>' + text + '</a>';
855 Renderer.prototype.image = function(href, title, text) {
856 var out = '<img src="' + href + '" alt="' + text + '"';
858 out += ' title="' + title + '"';
865 * Parsing & Compiling
868 function Parser(options) {
871 this.options = options || marked.defaults;
872 this.options.renderer = this.options.renderer || new Renderer;
873 this.renderer = this.options.renderer;
877 * Static Parse Method
880 Parser.parse = function(src, options, renderer) {
881 var parser = new Parser(options, renderer);
882 return parser.parse(src);
889 Parser.prototype.parse = function(src) {
890 this.inline = new InlineLexer(src.links, this.options, this.renderer);
891 this.tokens = src.reverse();
894 while (this.next()) {
905 Parser.prototype.next = function() {
906 return this.token = this.tokens.pop();
913 Parser.prototype.peek = function() {
914 return this.tokens[this.tokens.length - 1] || 0;
921 Parser.prototype.parseText = function() {
922 var body = this.token.text;
924 while (this.peek().type === 'text') {
925 body += '\n' + this.next().text;
928 return this.inline.output(body);
932 * Parse Current Token
935 Parser.prototype.tok = function() {
936 switch (this.token.type) {
941 return this.renderer.hr();
944 return this.renderer.heading(
945 this.inline.output(this.token.text),
950 return this.renderer.code(this.token.text, this.token.lang);
963 for (i = 0; i < this.token.header.length; i++) {
964 flags = { header: true, align: this.token.align[i] };
965 cell += this.renderer.tablecell(
966 this.inline.output(this.token.header[i]),
967 { header: true, align: this.token.align[i] }
970 header += this.renderer.tablerow(cell);
972 for (i = 0; i < this.token.cells.length; i++) {
973 row = this.token.cells[i];
976 for (j = 0; j < row.length; j++) {
977 cell += this.renderer.tablecell(
978 this.inline.output(row[j]),
979 { header: false, align: this.token.align[j] }
983 body += this.renderer.tablerow(cell);
985 return this.renderer.table(header, body);
987 case 'blockquote_start': {
990 while (this.next().type !== 'blockquote_end') {
994 return this.renderer.blockquote(body);
998 , ordered = this.token.ordered;
1000 while (this.next().type !== 'list_end') {
1004 return this.renderer.list(body, ordered);
1006 case 'list_item_start': {
1009 while (this.next().type !== 'list_item_end') {
1010 body += this.token.type === 'text'
1015 return this.renderer.listitem(body);
1017 case 'loose_item_start': {
1020 while (this.next().type !== 'list_item_end') {
1024 return this.renderer.listitem(body);
1027 var html = !this.token.pre && !this.options.pedantic
1028 ? this.inline.output(this.token.text)
1030 return this.renderer.html(html);
1033 return this.renderer.paragraph(this.inline.output(this.token.text));
1036 return this.renderer.paragraph(this.parseText());
1045 function escape(html, encode) {
1047 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
1048 .replace(/</g, '<')
1049 .replace(/>/g, '>')
1050 .replace(/"/g, '"')
1051 .replace(/'/g, ''');
1054 function replace(regex, opt) {
1055 regex = regex.source;
1057 return function self(name, val) {
1058 if (!name) return new RegExp(regex, opt);
1059 val = val.source || val;
1060 val = val.replace(/(^|[^\[])\^/g, '$1');
1061 regex = regex.replace(name, val);
1069 function merge(obj) {
1074 for (; i < arguments.length; i++) {
1075 target = arguments[i];
1076 for (key in target) {
1077 if (Object.prototype.hasOwnProperty.call(target, key)) {
1078 obj[key] = target[key];
1091 function marked(src, opt, callback) {
1092 if (callback || typeof opt === 'function') {
1098 opt = merge({}, marked.defaults, opt || {});
1100 var highlight = opt.highlight
1106 tokens = Lexer.lex(src, opt)
1111 pending = tokens.length;
1113 var done = function() {
1117 out = Parser.parse(tokens, opt);
1122 opt.highlight = highlight;
1126 : callback(null, out);
1132 if (opt) opt = merge({}, marked.defaults, opt);
1133 return Parser.parse(Lexer.lex(src, opt), opt);
1135 e.message += '\nPlease report this to https://github.com/chjj/marked.';
1136 if ((opt || marked.defaults).silent) {
1137 return '<p>An error occured:</p><pre>'
1138 + escape(e.message + '', true)
1150 marked.setOptions = function(opt) {
1151 merge(marked.defaults, opt);
1164 renderer: new Renderer
1171 marked.Parser = Parser;
1172 marked.parser = Parser.parse;
1174 marked.Renderer = Renderer;
1176 marked.Lexer = Lexer;
1177 marked.lexer = Lexer.lex;
1179 marked.InlineLexer = InlineLexer;
1180 marked.inlineLexer = InlineLexer.output;
1182 marked.parse = marked;
1184 if (typeof exports === 'object') {
1185 module.exports = marked;
1186 } else if (typeof define === 'function' && define.amd) {
1187 define(function() { return marked; });
1189 this.marked = marked;
1192 }).call(function() {
1193 return this || (typeof window !== 'undefined' ? window : global);