refactor options.
This commit is contained in:
parent
87a72cda33
commit
aef3a954da
@ -72,7 +72,15 @@ block.gfm.paragraph = replace(block.paragraph)
|
||||
function Lexer(options) {
|
||||
this.tokens = [];
|
||||
this.tokens.links = {};
|
||||
this.options = options || {};
|
||||
this.options = options || marked.defaults;
|
||||
|
||||
if (this.options.gfm) {
|
||||
block.fences = block.gfm.fences;
|
||||
block.paragraph = block.gfm.paragraph;
|
||||
} else {
|
||||
block.fences = block.normal.fences;
|
||||
block.paragraph = block.normal.paragraph;
|
||||
}
|
||||
}
|
||||
|
||||
Lexer.lex = function(src, options) {
|
||||
@ -406,7 +414,7 @@ Parser.prototype.compileInline = function(src) {
|
||||
// tag
|
||||
if (cap = inline.tag.exec(src)) {
|
||||
src = src.substring(cap[0].length);
|
||||
out += options.sanitize
|
||||
out += this.options.sanitize
|
||||
? escape(cap[0])
|
||||
: cap[0];
|
||||
continue;
|
||||
@ -522,7 +530,23 @@ Parser.prototype.outputLink = function(cap, link) {
|
||||
function Parser(options) {
|
||||
this.tokens = [];
|
||||
this.token = null;
|
||||
this.options = options || {};
|
||||
this.options = options || marked.defaults;
|
||||
|
||||
if (this.options.gfm) {
|
||||
inline.text = inline.gfm.text;
|
||||
inline.url = inline.gfm.url;
|
||||
} else {
|
||||
inline.text = inline.normal.text;
|
||||
inline.url = inline.normal.url;
|
||||
}
|
||||
|
||||
if (this.options.pedantic) {
|
||||
inline.em = inline.pedantic.em;
|
||||
inline.strong = inline.pedantic.strong;
|
||||
} else {
|
||||
inline.em = inline.normal.em;
|
||||
inline.strong = inline.normal.strong;
|
||||
}
|
||||
}
|
||||
|
||||
Parser.parse = function(src, options) {
|
||||
@ -728,12 +752,11 @@ noop.exec = noop;
|
||||
*/
|
||||
|
||||
function marked(src, opt) {
|
||||
setOptions(opt);
|
||||
try {
|
||||
return Parser.parse(Lexer.lex(src, opt), opt);
|
||||
} catch (e) {
|
||||
e.message += '\nPlease report this to https://github.com/chjj/marked.';
|
||||
if (marked.options.silent) {
|
||||
if ((opt || marked.defaults).silent) {
|
||||
return 'An error occured: ' + e.message;
|
||||
}
|
||||
throw e;
|
||||
@ -744,61 +767,31 @@ function marked(src, opt) {
|
||||
* Options
|
||||
*/
|
||||
|
||||
var options
|
||||
, defaults;
|
||||
|
||||
function setOptions(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.defaults = opt;
|
||||
return marked;
|
||||
};
|
||||
|
||||
marked.setOptions({
|
||||
marked.defaults = {
|
||||
gfm: true,
|
||||
pedantic: false,
|
||||
sanitize: false,
|
||||
highlight: null
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Expose
|
||||
*/
|
||||
|
||||
marked.Parser = Parser;
|
||||
marked.parser = function(src, opt) {
|
||||
setOptions(opt);
|
||||
return parse(src);
|
||||
return Parser.parse(src, opt);
|
||||
};
|
||||
|
||||
marked.Lexer = Lexer;
|
||||
marked.lexer = function(src, opt) {
|
||||
setOptions(opt);
|
||||
return block.lexer(src);
|
||||
return Lexer.lex(src, opt);
|
||||
};
|
||||
|
||||
marked.parse = marked;
|
||||
|
Loading…
x
Reference in New Issue
Block a user