Comparing version
@@ -15,3 +15,3 @@ /** | ||
code: /^ {4,}[^\n]*(?:\n {4,}[^\n]*|\n)*(?:\n+|$)/, | ||
gfm_code: /^ *``` *(\w+)? *\n([^\0]+?)\s*``` *(?:\n+|$)/, | ||
fences: noop, | ||
hr: /^( *[\-*_]){3,} *(?:\n+|$)/, | ||
@@ -58,3 +58,2 @@ heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/, | ||
}) | ||
('gfm_code') | ||
('hr') | ||
@@ -71,2 +70,22 @@ ('heading') | ||
block.normal = { | ||
fences: block.fences, | ||
paragraph: block.paragraph | ||
}; | ||
block.gfm = { | ||
fences: /^ *``` *(\w+)? *\n([^\0]+?)\s*``` *(?:\n+|$)/, | ||
paragraph: /^/ | ||
}; | ||
block.gfm.paragraph = (function() { | ||
var paragraph = block.paragraph.source | ||
, fences = block.gfm.fences.source; | ||
fences = fences.replace(/(^|[^\[])\^/g, '$1'); | ||
paragraph = paragraph.replace(/(\(\?!)/, '$1' + fences + '|'); | ||
return new RegExp(paragraph); | ||
})(); | ||
/** | ||
@@ -115,3 +134,5 @@ * Block Lexer | ||
type: 'code', | ||
text: cap.replace(/\n+$/, '') | ||
text: !options.pedantic | ||
? cap.replace(/\n+$/, '') | ||
: cap | ||
}); | ||
@@ -121,4 +142,4 @@ continue; | ||
// gfm_code | ||
if (cap = block.gfm_code.exec(src)) { | ||
// fences (gfm) | ||
if (cap = block.fences.exec(src)) { | ||
src = src.substring(cap[0].length); | ||
@@ -167,2 +188,3 @@ tokens.push({ | ||
src = src.substring(cap[0].length); | ||
tokens.push({ | ||
@@ -182,2 +204,3 @@ type: 'blockquote_start' | ||
}); | ||
continue; | ||
@@ -216,3 +239,5 @@ } | ||
space -= item.length; | ||
item = item.replace(new RegExp('^ {1,' + space + '}', 'gm'), ''); | ||
item = !options.pedantic | ||
? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '') | ||
: item.replace(/^ {1,4}/gm, ''); | ||
} | ||
@@ -303,3 +328,3 @@ | ||
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/, | ||
gfm_autolink: /^(\w+:\/\/[^\s]+[^.,:;"')\]\s])/, | ||
url: noop, | ||
tag: /^<!--[^\0]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/, | ||
@@ -313,5 +338,28 @@ link: /^!?\[((?:\[[^\]]*\]|[^\[\]]|\[|\](?=[^[\]]*\]))*)\]\(([^\)]*)\)/, | ||
br: /^ {2,}\n(?!\s*$)/, | ||
text: /^[^\0]+?(?=[\\<!\[_*`]| {2,}\n|$)/ | ||
}; | ||
inline.normal = { | ||
url: inline.url, | ||
strong: inline.strong, | ||
em: inline.em, | ||
text: inline.text | ||
}; | ||
inline.pedantic = { | ||
strong: /^__(?=\S)([^\0]*?\S)__(?!_)|^\*\*(?=\S)([^\0]*?\S)\*\*(?!\*)/, | ||
em: /^_(?=\S)([^\0]*?\S)_(?!_)|^\*(?=\S)([^\0]*?\S)\*(?!\*)/ | ||
}; | ||
inline.gfm = { | ||
url: /^(\w+:\/\/[^\s]+[^.,:;"')\]\s])/, | ||
// br: /^\n(?!\s*$)/, | ||
text: /^[^\0]+?(?=[\\<!\[_*`]|\w+:\/\/| {2,}\n|$)/ | ||
}; | ||
inline.discount = { | ||
strong: /^__([^\0]+?)__(?!_)|^\*\*([^\0]+?)\*\*(?!\*)/, | ||
em: /^\b_(?=\S)([^\0]*?\S)_\b|^\*(?=\S)([^\0]*?(?:\*\*|[^\s*]))\*(?!\*)/ | ||
}; | ||
/** | ||
@@ -357,4 +405,4 @@ * Inline Lexer | ||
// gfm_autolink | ||
if (cap = inline.gfm_autolink.exec(src)) { | ||
// url (gfm) | ||
if (cap = inline.url.exec(src)) { | ||
src = src.substring(cap[0].length); | ||
@@ -374,3 +422,5 @@ text = escape(cap[1]); | ||
src = src.substring(cap[0].length); | ||
out += cap[0]; | ||
out += options.sanitize | ||
? escape(cap[0]) | ||
: cap[0]; | ||
continue; | ||
@@ -576,3 +626,6 @@ } | ||
case 'html': { | ||
return !token.pre | ||
if (options.sanitize) { | ||
return inline.lexer(token.text); | ||
} | ||
return !token.pre && !options.pedantic | ||
? inline.lexer(token.text) | ||
@@ -659,13 +712,73 @@ : token.text; | ||
function noop() {} | ||
noop.exec = noop; | ||
/** | ||
* Expose | ||
* Marked | ||
*/ | ||
var marked = function(src) { | ||
var marked = function(src, opt) { | ||
setOptions(opt); | ||
return parse(block.lexer(src)); | ||
}; | ||
marked.parser = parse; | ||
marked.lexer = block.lexer; | ||
/** | ||
* Options | ||
*/ | ||
var options | ||
, defaults; | ||
var setOptions = function(opt) { | ||
if (!opt) opt = defaults; | ||
if (options === opt) return; | ||
options = opt; | ||
if (options.gfm) { | ||
block.fences = block.gfm.fences; | ||
block.paragraph = block.gfm.paragraph; | ||
inline.text = inline.gfm.text; | ||
inline.url = inline.gfm.url; | ||
} else { | ||
block.fences = block.normal.fences; | ||
block.paragraph = block.normal.paragraph; | ||
inline.text = inline.normal.text; | ||
inline.url = inline.normal.url; | ||
} | ||
if (options.pedantic) { | ||
inline.em = inline.pedantic.em; | ||
inline.strong = inline.pedantic.strong; | ||
} else { | ||
inline.em = inline.normal.em; | ||
inline.strong = inline.normal.strong; | ||
} | ||
}; | ||
marked.options = | ||
marked.setOptions = function(opt) { | ||
defaults = opt; | ||
setOptions(opt); | ||
}; | ||
marked.options({ | ||
gfm: true, | ||
pedantic: false, | ||
sanitize: false | ||
}); | ||
/** | ||
* Expose | ||
*/ | ||
marked.parser = function(src, opt) { | ||
setOptions(opt); | ||
return parse(src); | ||
}; | ||
marked.lexer = function(src, opt) { | ||
setOptions(opt); | ||
return block.lexer(src); | ||
}; | ||
marked.parse = marked; | ||
@@ -672,0 +785,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"author": "Christopher Jeffrey", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"main": "./lib/marked.js", | ||
@@ -8,0 +8,0 @@ "bin": "./bin/marked", |
@@ -22,8 +22,9 @@ # marked | ||
$ node test --bench | ||
marked completed in 6485ms. | ||
marked (with gfm) completed in 7466ms. | ||
discount completed in 7169ms. | ||
showdown (reuse converter) completed in 15937ms. | ||
showdown (new converter) completed in 18279ms. | ||
markdown-js completed in 23572ms. | ||
marked completed in 6448ms. | ||
marked (gfm) completed in 7357ms. | ||
marked (pedantic) completed in 6092ms. | ||
discount completed in 7314ms. | ||
showdown (reuse converter) completed in 16018ms. | ||
showdown (new converter) completed in 18234ms. | ||
markdown-js completed in 24270ms. | ||
``` | ||
@@ -65,6 +66,22 @@ | ||
## Options | ||
marked has 3 different switches which change behavior. | ||
- __pedantic__: Conform to obscure parts of `markdown.pl` as much as possible. | ||
Don't fix any of the original markdown bugs or poor behavior. | ||
- __gfm__: Enabled github flavored markdown (default for backward compatibility). | ||
- __sanitize__: Sanitize the output. Ignore any HTML that has been input. | ||
None of the above are mutually exclusive/inclusive. | ||
## Usage | ||
``` js | ||
var marked = require('marked'); | ||
// set default options | ||
marked.setOptions({ | ||
gfm: true, | ||
pedantic: false, | ||
sanitize: true | ||
}); | ||
console.log(marked('i am using __markdown__.')); | ||
@@ -71,0 +88,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24580
15%663
16.11%153
12.5%