Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to adding simple block rules #1003

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 65 additions & 3 deletions lib/marked.js
Expand Up @@ -111,6 +111,11 @@ function Lexer(options) {
}
}

/**
* Array of RegExp.
*/
Lexer.simpleRules = [];

/**
* Expose Block Rules
*/
Expand Down Expand Up @@ -156,6 +161,7 @@ Lexer.prototype.token = function(src, top, bq) {
, i
, l;

mainLoop:
while (src) {
// newline
if (cap = this.rules.newline.exec(src)) {
Expand Down Expand Up @@ -408,6 +414,18 @@ Lexer.prototype.token = function(src, top, bq) {
continue;
}

// simple rules
if (Lexer.simpleRules.length) {
var simpleRules = Lexer.simpleRules;
for (var i = 0; i < simpleRules.length; i++) {
if (cap = simpleRules[i].exec(src)) {
src = src.substring(cap[0].length);
this.tokens.push({ type: 'simpleRule' + (i + 1), cap: cap });
continue mainLoop;
}
}
}

// top-level paragraph
if (top && (cap = this.rules.paragraph.exec(src))) {
src = src.substring(cap[0].length);
Expand Down Expand Up @@ -927,6 +945,18 @@ Parser.parse = function(src, options, renderer) {
return parser.parse(src);
};


/**
* Array of simple renderer callbacks, like this:
*
* ```js
* function (cap) {
* return cap[1];
* }
* ```
*/
Parser.prototype.simpleRenderers = [];

/**
* Parse Loop
*/
Expand Down Expand Up @@ -1082,6 +1112,14 @@ Parser.prototype.tok = function() {
case 'text': {
return this.renderer.paragraph(this.parseText());
}
default: {
if (this.simpleRenderers.length)
for (var i = 0; i < this.simpleRenderers.length; i++) {
if (this.token.type == 'simpleRule' + (i + 1)) {
return this.simpleRenderers[i].call(this.renderer, this.token.cap);
}
}
}
}
};

Expand Down Expand Up @@ -1204,7 +1242,7 @@ function marked(src, opt, callback) {
var out;

try {
out = Parser.parse(tokens, opt);
out = marked.parser(tokens, opt);
} catch (e) {
err = e;
}
Expand Down Expand Up @@ -1245,7 +1283,7 @@ function marked(src, opt, callback) {
}
try {
if (opt) opt = merge({}, marked.defaults, opt);
return Parser.parse(Lexer.lex(src, opt), opt);
return marked.parser(Lexer.lex(src, opt), opt);
} catch (e) {
e.message += '\nPlease report this to https://github.com/chjj/marked.';
if ((opt || marked.defaults).silent) {
Expand All @@ -1257,6 +1295,30 @@ function marked(src, opt, callback) {
}
}

/**
* Staticaly saved `simpleRenderers`. They are used to inject in instance of Parser.
*/
marked.simpleRenderers = [];

marked.parser = function (src, options, renderer) {
if (marked.simpleRenderers.length) {
var parser = new Parser(options);
parser.simpleRenderers = marked.simpleRenderers;
return parser.parse(src, options, renderer);
}
else {
return Parser.parse(src, options, renderer);
}
};

marked.setBlockRule = function(regexp, renderer) {
renderer = renderer || function(){};
Lexer.simpleRules.push(regexp);
this.simpleRenderers.push(renderer);

return marked;
};

/**
* Options
*/
Expand Down Expand Up @@ -1291,7 +1353,7 @@ marked.defaults = {
*/

marked.Parser = Parser;
marked.parser = Parser.parse;
marked.escape = escape;

marked.Renderer = Renderer;

Expand Down