marked/lib/marked.js

573 lines
11 KiB
JavaScript
Raw Normal View History

2011-07-24 08:15:35 -05:00
/**
* marked - A markdown parser
* Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed)
*/
2011-08-13 23:19:46 -05:00
;(function() {
2011-07-24 08:15:35 -05:00
/**
* Block-Level Grammar
*/
2011-08-13 23:19:46 -05:00
var block = {
2011-08-18 18:59:42 -05:00
newline: /^\n+/,
2011-10-22 08:05:31 -05:00
code: /^ {4,}[^\n]*(?:\n {4,}[^\n]*|\n)*(?=\n| *$)/,
2012-01-02 04:10:23 -06:00
gfm_code: /^ *``` *(\w+)? *\n([^\0]+?)\s*```(?= *\n| *$)/,
2011-08-21 17:00:42 -05:00
hr: /^( *[\-*_]){3,} *\n/,
2011-09-14 12:41:51 -05:00
heading: /^ *(#{1,6}) *([^\0]+?) *#* *\n+/,
2011-07-24 08:15:35 -05:00
lheading: /^([^\n]+)\n *(=|-){3,}/,
blockquote: /^ *>[^\n]*(?:\n *>[^\n]*)*/,
2011-08-22 10:58:51 -05:00
list: /^( *)([*+-]|\d+\.) [^\0]+?(?:\n{2,}(?! )|\s*$)(?!\1\2|\1\d+\.)/,
2011-10-04 20:46:35 -05:00
html: /^ *(?:<!--[^\0]*?-->|<(\w+)[^\0]+?<\/\1>|<\w+[^>]*>) *(?:\n{2,}|\s*$)/,
2011-07-24 08:15:35 -05:00
text: /^[^\n]+/
};
/**
2011-10-22 23:39:04 -05:00
* Block Lexer
2011-07-24 08:15:35 -05:00
*/
2011-08-13 23:19:46 -05:00
block.lexer = function(str) {
2011-08-13 17:06:08 -05:00
var tokens = []
, links = {};
2011-07-24 08:15:35 -05:00
2011-10-04 17:40:33 -05:00
str = str.replace(/\r\n|\r/g, '\n')
2011-08-14 02:47:56 -05:00
.replace(/\t/g, ' ');
2011-08-13 23:19:46 -05:00
2011-08-13 17:06:08 -05:00
str = str.replace(
2011-10-04 17:32:59 -05:00
/^ {0,3}\[([^\]]+)\]: *([^ ]+)(?: +"([^\n]+)")? *$/gm,
2011-08-21 09:01:03 -05:00
function(__, id, href, title) {
2011-08-13 23:19:46 -05:00
links[id] = {
href: href,
2011-08-21 09:01:03 -05:00
title: title
2011-08-13 23:19:46 -05:00
};
return '';
}
);
2011-08-13 17:06:08 -05:00
tokens.links = links;
2011-07-24 08:15:35 -05:00
2011-08-18 18:59:42 -05:00
return block.token(str, tokens);
2011-08-13 17:06:08 -05:00
};
2011-08-13 16:53:15 -05:00
2011-08-18 18:59:42 -05:00
block.token = function(str, tokens) {
2011-10-22 06:09:28 -05:00
var str = str.replace(/^ +$/gm, '')
, loose
2011-10-22 07:49:11 -05:00
, cap
, item
, space
, i
, l;
2011-10-22 06:09:28 -05:00
while (str) {
2011-10-22 23:39:04 -05:00
// newline
2011-10-22 06:09:28 -05:00
if (cap = block.newline.exec(str)) {
str = str.substring(cap[0].length);
if (cap[0].length > 1) {
tokens.push({
type: 'space'
});
2011-08-14 01:02:14 -05:00
}
2011-10-22 06:09:28 -05:00
continue;
}
2011-10-22 23:39:04 -05:00
// code
2011-10-22 08:05:31 -05:00
if (cap = block.code.exec(str)) {
2011-10-22 06:09:28 -05:00
str = str.substring(cap[0].length);
cap = cap[0].replace(/^ {4}/gm, '');
tokens.push({
2011-10-22 08:05:31 -05:00
type: 'code',
text: cap[cap.length-1] === '\n'
? cap.slice(0, -1)
: cap
2011-10-22 06:09:28 -05:00
});
continue;
}
2012-01-02 04:10:23 -06:00
// gfm_code
if (cap = block.gfm_code.exec(str)) {
str = str.substring(cap[0].length);
tokens.push({
type: 'code',
lang: cap[1],
text: cap[2]
});
continue;
}
2011-10-22 23:39:04 -05:00
// heading
2011-10-22 06:09:28 -05:00
if (cap = block.heading.exec(str)) {
str = str.substring(cap[0].length);
tokens.push({
type: 'heading',
depth: cap[1].length,
text: cap[2]
});
continue;
}
2011-10-22 23:39:04 -05:00
// lheading
2011-10-22 06:09:28 -05:00
if (cap = block.lheading.exec(str)) {
str = str.substring(cap[0].length);
tokens.push({
type: 'heading',
depth: cap[2] === '=' ? 1 : 2,
text: cap[1]
});
continue;
}
2011-10-22 23:39:04 -05:00
// hr
2011-10-22 06:09:28 -05:00
if (cap = block.hr.exec(str)) {
str = str.substring(cap[0].length);
tokens.push({
type: 'hr'
});
continue;
}
2011-10-22 23:39:04 -05:00
// blockquote
2011-10-22 06:09:28 -05:00
if (cap = block.blockquote.exec(str)) {
str = str.substring(cap[0].length);
tokens.push({
type: 'blockquote_start'
});
cap = cap[0].replace(/^ *>/gm, '');
block.token(cap, tokens);
tokens.push({
type: 'blockquote_end'
});
continue;
2011-08-14 01:02:14 -05:00
}
2011-10-22 06:09:28 -05:00
2011-10-22 23:39:04 -05:00
// list
2011-10-22 06:09:28 -05:00
if (cap = block.list.exec(str)) {
str = str.substring(cap[0].length);
tokens.push({
type: 'list_start',
ordered: isFinite(cap[2])
});
loose = /\n *\n *(?:[*+-]|\d+\.)/.test(cap[0]);
// get each top-level item
cap = cap[0].match(
/^( *)([*+-]|\d+\.)[^\n]*(?:\n(?!\1(?:\2|\d+\.))[^\n]*)*/gm
);
2011-10-22 23:39:04 -05:00
i = 0;
l = cap.length;
2011-10-22 07:49:11 -05:00
for (; i < l; i++) {
2011-10-22 06:09:28 -05:00
// remove the list items sigil
// so its seen as the next token
2011-10-22 07:49:11 -05:00
item = cap[i].replace(/^ *([*+-]|\d+\.) */, '');
2011-10-22 06:09:28 -05:00
// outdent whatever the
// list item contains, hacky
2011-10-22 07:49:11 -05:00
space = /\n( +)/.exec(item);
2011-10-22 06:09:28 -05:00
if (space) {
space = new RegExp('^' + space[1], 'gm');
item = item.replace(space, '');
2011-08-18 18:59:42 -05:00
}
2011-08-14 01:04:35 -05:00
tokens.push({
2011-10-22 06:09:28 -05:00
type: loose
? 'loose_item_start'
: 'list_item_start'
2011-08-14 01:04:35 -05:00
});
2011-10-22 06:09:28 -05:00
block.token(item, tokens);
2011-08-14 01:04:35 -05:00
tokens.push({
2011-10-22 06:09:28 -05:00
type: 'list_item_end'
2011-08-14 01:04:35 -05:00
});
2011-10-22 07:49:11 -05:00
}
2011-10-22 06:09:28 -05:00
tokens.push({
type: 'list_end'
});
continue;
}
2011-10-22 23:39:04 -05:00
// html
2011-10-22 06:09:28 -05:00
if (cap = block.html.exec(str)) {
str = str.substring(cap[0].length);
tokens.push({
type: 'html',
text: cap[0]
});
continue;
}
2011-10-22 23:39:04 -05:00
// text
2011-10-22 06:09:28 -05:00
if (cap = block.text.exec(str)) {
str = str.substring(cap[0].length);
tokens.push({
type: 'text',
text: cap[0]
});
continue;
2011-08-14 01:04:35 -05:00
}
2011-08-13 16:53:15 -05:00
}
2011-07-24 08:15:35 -05:00
return tokens;
};
/**
* Inline Processing
*/
2011-08-13 23:19:46 -05:00
var inline = {
2011-08-21 18:19:08 -05:00
escape: /^\\([\\`*{}\[\]()#+\-.!_])/,
2011-08-18 19:31:53 -05:00
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
2011-10-04 19:41:12 -05:00
tag: /^<!--[^\0]*?-->|^<\/?\w+[^>]*>/,
link: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\(([^\)]*)\)/,
2011-08-22 11:02:21 -05:00
reflink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\s*\[([^\]]*)\]/,
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
2011-08-21 22:39:43 -05:00
strong: /^__([^\0]+?)__(?!_)|^\*\*([^\0]+?)\*\*(?!\*)/,
2011-08-13 23:19:46 -05:00
em: /^_([^_]+)_|^\*([^*]+)\*/,
2011-08-22 16:19:43 -05:00
code: /^`([^`]+)`|^``([^\0]+?)``/,
br: /^ {2,}\n(?!\s*$)/,
2011-08-22 16:19:43 -05:00
text: /^/
2011-08-13 23:19:46 -05:00
};
2011-08-13 21:04:18 -05:00
2011-08-14 02:35:37 -05:00
// hacky, but performant
2011-08-22 16:19:43 -05:00
inline.text = (function() {
var body = [];
2011-08-14 02:35:37 -05:00
2011-08-22 16:19:43 -05:00
(function push(rule) {
rule = inline[rule].source;
body.push(rule.replace(/(^|[^\[])\^/g, '$1'));
return push;
})
('escape')
('tag')
('nolink')
('strong')
('em')
2011-08-24 10:10:34 -05:00
('code')
('br');
2011-08-14 02:35:37 -05:00
2011-10-04 17:32:59 -05:00
return new
2011-08-22 16:19:43 -05:00
RegExp('^[^\\0]+?(?=' + body.join('|') + '|$)');
})();
2011-08-13 23:19:46 -05:00
/**
* Inline Lexer
*/
inline.lexer = function(str) {
2011-08-14 01:02:14 -05:00
var out = ''
, links = tokens.links
2011-08-18 19:26:23 -05:00
, link
2011-08-14 01:02:14 -05:00
, text
2011-10-22 06:09:28 -05:00
, href
2011-08-14 01:02:14 -05:00
, cap;
2011-08-13 23:36:44 -05:00
2011-10-22 06:09:28 -05:00
while (str) {
2011-10-22 23:39:04 -05:00
// escape
2011-10-22 06:09:28 -05:00
if (cap = inline.escape.exec(str)) {
str = str.substring(cap[0].length);
out += cap[1];
continue;
}
2011-10-22 23:39:04 -05:00
// autolink
2011-10-22 06:09:28 -05:00
if (cap = inline.autolink.exec(str)) {
str = str.substring(cap[0].length);
if (cap[2] === '@') {
text = cap[1][6] === ':'
? mangle(cap[1].substring(7))
: mangle(cap[1]);
href = mangle('mailto:') + text;
} else {
text = escape(cap[1]);
href = text;
2011-08-13 21:04:18 -05:00
}
2011-10-22 08:05:31 -05:00
out += '<a href="'
+ href
+ '">'
2011-10-22 06:09:28 -05:00
+ text
+ '</a>';
continue;
2011-08-13 21:04:18 -05:00
}
2011-10-22 06:09:28 -05:00
2011-10-22 23:39:04 -05:00
// tag
2011-10-22 06:09:28 -05:00
if (cap = inline.tag.exec(str)) {
str = str.substring(cap[0].length);
out += cap[0];
continue;
}
2011-10-22 23:39:04 -05:00
// link
2011-10-22 06:09:28 -05:00
if (cap = inline.link.exec(str)) {
str = str.substring(cap[0].length);
text = /^\s*<?([^\s]*?)>?(?:\s+"([^\n]+)")?\s*$/.exec(cap[2]);
link = {
href: text[1],
title: text[2]
};
out += mlink(cap, link);
continue;
}
2011-10-22 23:39:04 -05:00
// reflink, nolink
2011-10-22 06:09:28 -05:00
if ((cap = inline.reflink.exec(str))
|| (cap = inline.nolink.exec(str))) {
str = str.substring(cap[0].length);
link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
link = links[link];
if (!link) {
out += cap[0][0];
str = cap[0].substring(1) + str;
continue;
}
out += mlink(cap, link);
continue;
}
2011-10-22 23:39:04 -05:00
// strong
2011-10-22 06:09:28 -05:00
if (cap = inline.strong.exec(str)) {
str = str.substring(cap[0].length);
out += '<strong>'
+ inline.lexer(cap[2] || cap[1])
+ '</strong>';
continue;
}
2011-10-22 23:39:04 -05:00
// em
2011-10-22 06:09:28 -05:00
if (cap = inline.em.exec(str)) {
str = str.substring(cap[0].length);
out += '<em>'
+ inline.lexer(cap[2] || cap[1])
+ '</em>';
continue;
}
2011-10-22 23:39:04 -05:00
// code
2011-10-22 06:09:28 -05:00
if (cap = inline.code.exec(str)) {
str = str.substring(cap[0].length);
out += '<code>'
+ escape(cap[2] || cap[1], true)
2011-10-22 06:09:28 -05:00
+ '</code>';
continue;
}
2011-10-22 23:39:04 -05:00
// br
2011-10-22 06:09:28 -05:00
if (cap = inline.br.exec(str)) {
str = str.substring(cap[0].length);
out += '<br>';
continue;
}
2011-10-22 23:39:04 -05:00
// text
2011-10-22 06:09:28 -05:00
if (cap = inline.text.exec(str)) {
str = str.substring(cap[0].length);
out += escape(cap[0]);
continue;
2011-08-14 01:04:35 -05:00
}
2011-08-13 23:19:46 -05:00
}
2011-08-13 21:04:18 -05:00
2011-08-13 23:19:46 -05:00
return out;
};
2011-08-13 21:04:18 -05:00
2011-10-22 06:09:28 -05:00
var mlink = function(cap, link) {
if (cap[0][0] !== '!') {
return '<a href="'
+ escape(link.href)
+ '"'
+ (link.title
? ' title="'
+ escape(link.title)
+ '"'
: '')
2011-10-22 06:09:28 -05:00
+ '>'
+ inline.lexer(cap[1])
+ '</a>';
} else {
return '<img src="'
+ escape(link.href)
+ '" alt="'
+ escape(cap[1])
+ '"'
+ (link.title
? ' title="'
+ escape(link.title)
+ '"'
: '')
2011-10-22 06:09:28 -05:00
+ '>';
}
};
2011-07-24 08:15:35 -05:00
/**
* Parsing
*/
2011-08-13 17:06:08 -05:00
var tokens
, token;
2011-08-13 16:38:46 -05:00
2011-07-24 08:15:35 -05:00
var next = function() {
return token = tokens.pop();
};
var tok = function() {
switch (token.type) {
2011-10-22 06:18:15 -05:00
case 'space': {
2011-08-18 18:59:42 -05:00
return '';
2011-10-22 06:18:15 -05:00
}
case 'hr': {
2011-07-24 08:15:35 -05:00
return '<hr>';
2011-10-22 06:18:15 -05:00
}
case 'heading': {
return '<h'
+ token.depth
+ '>'
2011-08-13 23:19:46 -05:00
+ inline.lexer(token.text)
2011-10-22 06:18:15 -05:00
+ '</h'
+ token.depth
+ '>';
}
2011-10-22 08:05:31 -05:00
case 'code': {
2011-10-04 17:32:59 -05:00
return '<pre><code>'
+ escape(token.text, true)
2011-07-24 08:15:35 -05:00
+ '</code></pre>';
2011-10-22 06:18:15 -05:00
}
case 'blockquote_start': {
2011-07-24 08:15:35 -05:00
var body = [];
2011-10-22 06:18:15 -05:00
2011-07-24 08:15:35 -05:00
while (next().type !== 'blockquote_end') {
body.push(tok());
}
2011-10-22 06:18:15 -05:00
2011-10-04 17:32:59 -05:00
return '<blockquote>'
+ body.join('')
2011-07-24 08:15:35 -05:00
+ '</blockquote>';
2011-10-22 06:18:15 -05:00
}
case 'list_start': {
2011-10-04 17:32:59 -05:00
var type = token.ordered ? 'ol' : 'ul'
, body = [];
2011-10-22 06:18:15 -05:00
2011-07-24 08:15:35 -05:00
while (next().type !== 'list_end') {
body.push(tok());
}
2011-10-22 06:18:15 -05:00
return '<'
+ type
+ '>'
2011-10-04 17:32:59 -05:00
+ body.join('')
2011-10-22 06:18:15 -05:00
+ '</'
+ type
+ '>';
}
case 'list_item_start': {
2011-07-24 08:15:35 -05:00
var body = [];
2011-10-22 06:18:15 -05:00
2011-07-24 08:15:35 -05:00
while (next().type !== 'list_item_end') {
2011-08-20 10:46:35 -05:00
body.push(token.type === 'text'
2011-10-04 17:32:59 -05:00
? text()
2011-08-15 20:35:57 -05:00
: tok());
2011-07-24 08:15:35 -05:00
}
2011-10-22 06:18:15 -05:00
2011-10-04 17:32:59 -05:00
return '<li>'
+ body.join(' ')
2011-07-24 08:15:35 -05:00
+ '</li>';
2011-10-22 06:18:15 -05:00
}
case 'loose_item_start': {
2011-08-20 08:59:47 -05:00
var body = [];
2011-10-22 06:18:15 -05:00
2011-08-20 08:59:47 -05:00
while (next().type !== 'list_item_end') {
body.push(tok());
}
2011-10-22 06:18:15 -05:00
2011-10-04 17:32:59 -05:00
return '<li>'
+ body.join(' ')
2011-08-20 10:46:35 -05:00
+ '</li>';
2011-10-22 06:18:15 -05:00
}
case 'html': {
2011-08-13 23:19:46 -05:00
return inline.lexer(token.text);
2011-10-22 06:18:15 -05:00
}
case 'text': {
2011-10-22 06:09:28 -05:00
return '<p>'
+ text()
+ '</p>';
2011-10-22 06:18:15 -05:00
}
}
2011-10-04 17:32:59 -05:00
};
var text = function() {
var body = [ token.text ]
, top;
2011-10-04 17:32:59 -05:00
while ((top = tokens[tokens.length-1])
&& top.type === 'text') {
2011-10-04 17:32:59 -05:00
body.push(next().text);
2011-07-24 08:15:35 -05:00
}
2011-10-04 17:32:59 -05:00
return inline.lexer(body.join('\n'));
2011-07-24 08:15:35 -05:00
};
var parse = function(src) {
tokens = src.reverse();
var out = [];
while (next()) {
out.push(tok());
}
tokens = null;
token = null;
2011-12-30 19:30:32 -06:00
return out.join('\n');
2011-07-24 08:15:35 -05:00
};
/**
* Helpers
*/
var escape = function(html, dbl) {
2011-07-24 08:15:35 -05:00
return html
.replace(!dbl
? /&(?!#?\w+;)/g
: /&/g, '&amp;')
2011-07-24 08:15:35 -05:00
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
};
var mangle = function(str) {
2011-08-14 02:47:56 -05:00
var out = ''
, ch
2011-07-24 08:15:35 -05:00
, i = 0
2011-08-14 02:47:56 -05:00
, l = str.length;
2011-07-24 08:15:35 -05:00
for (; i < l; i++) {
2011-08-13 23:19:46 -05:00
ch = str.charCodeAt(i);
if (Math.random() > 0.5) {
2011-07-24 08:15:35 -05:00
ch = 'x' + ch.toString(16);
}
out += '&#' + ch + ';';
}
return out;
};
/**
* Expose
*/
2011-08-13 23:19:46 -05:00
var marked = function(str) {
return parse(block.lexer(str));
2011-07-24 08:15:35 -05:00
};
2011-08-13 23:19:46 -05:00
marked.parser = parse;
marked.lexer = block.lexer;
2011-11-26 22:12:02 -06:00
marked.parse = marked;
2011-08-13 23:19:46 -05:00
if (typeof module !== 'undefined') {
module.exports = marked;
} else {
this.marked = marked;
}
2011-07-24 08:15:35 -05:00
2011-08-13 23:19:46 -05:00
}).call(this);