Browse Source

Changes to improve speed on node 0.12 & iojs

pull/82/head
Vitaly Puzrin 9 years ago
parent
commit
00117cb2e7
  1. 13
      lib/parser_inline.js
  2. 16
      lib/renderer.js
  3. 4
      lib/rules_block/heading.js
  4. 5
      lib/rules_core/linkify.js
  5. 5
      lib/rules_core/smartquotes.js
  6. 9
      lib/rules_inline/autolink.js
  7. 22
      lib/rules_inline/state_inline.js
  8. 4
      lib/token.js

13
lib/parser_inline.js

@ -48,14 +48,15 @@ function ParserInline() {
// returns `true` if any rule reported success
//
ParserInline.prototype.skipToken = function (state) {
var i, cached_pos, pos = state.pos,
var i, pos = state.pos,
rules = this.ruler.getRules(''),
len = rules.length,
maxNesting = state.md.options.maxNesting;
maxNesting = state.md.options.maxNesting,
cache = state.cache;
if ((cached_pos = state.cacheGet(pos)) > 0) {
state.pos = cached_pos;
if (typeof cache[pos] !== 'undefined') {
state.pos = cache[pos];
return;
}
@ -63,14 +64,14 @@ ParserInline.prototype.skipToken = function (state) {
if (state.level < maxNesting) {
for (i = 0; i < len; i++) {
if (rules[i](state, true)) {
state.cacheSet(pos, state.pos);
cache[pos] = state.pos;
return;
}
}
}
state.pos++;
state.cacheSet(pos, state.pos);
cache[pos] = state.pos;
};

16
lib/renderer.js

@ -18,6 +18,11 @@ var escapeHtml = require('./common/utils').escapeHtml;
var default_rules = {};
default_rules.code_inline = function (tokens, idx /*, options, env */) {
return '<code>' + escapeHtml(tokens[idx].content) + '</code>';
};
default_rules.code_block = function (tokens, idx /*, options, env */) {
return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>\n';
};
@ -176,10 +181,12 @@ Renderer.prototype.renderToken = function renderToken(tokens, idx, options) {
result += (token.nesting === -1 ? '</' : '<') + token.tag;
// Encode attributes, e.g. `<img src="foo"`
result += this.renderAttrs(token.attrs);
if (typeof token.attrs !== 'undefined') {
result += this.renderAttrs(token.attrs);
}
// Add a slash for self-closing tags, e.g. `<img src="foo" /`
if (token.nesting === 0 && options.xhtmlOut && token.content === null) {
if (token.nesting === 0 && options.xhtmlOut) {
result += ' /';
}
@ -205,11 +212,6 @@ Renderer.prototype.renderToken = function renderToken(tokens, idx, options) {
}
}
// If it's self-contained token, add its contents + closing tag
if (token.nesting === 0 && token.content !== null) {
result += '>' + escapeHtml(token.content) + '</' + token.tag;
}
result += needLf ? '>\n' : '>';
return result;

4
lib/rules_block/heading.js

@ -35,7 +35,7 @@ module.exports = function heading(state, startLine, endLine, silent) {
state.line = startLine + 1;
token = state.push('heading_open', 'h' + String(level), 1);
token.markup = Array(level + 1).join('#');
token.markup = '########'.slice(0, level);
token.map = [ startLine, state.line ];
token = state.push('inline', '', 0);
@ -44,7 +44,7 @@ module.exports = function heading(state, startLine, endLine, silent) {
token.children = [];
token = state.push('heading_close', 'h' + String(level), -1);
token.markup = Array(level + 1).join('#');
token.markup = '########'.slice(0, level);
return true;
};

5
lib/rules_core/linkify.js

@ -26,7 +26,10 @@ module.exports = function linkify(state) {
if (!state.md.options.linkify) { return; }
for (j = 0, l = blockTokens.length; j < l; j++) {
if (blockTokens[j].type !== 'inline') { continue; }
if (blockTokens[j].type !== 'inline' ||
!state.md.linkify.test(blockTokens[j].content)) {
continue;
}
tokens = blockTokens[j].children;

5
lib/rules_core/smartquotes.js

@ -29,7 +29,10 @@ module.exports = function smartquotes(state) {
for (blkIdx = state.tokens.length - 1; blkIdx >= 0; blkIdx--) {
if (state.tokens[blkIdx].type !== 'inline') { continue; }
if (state.tokens[blkIdx].type !== 'inline' ||
!QUOTE_TEST_RE.test(state.tokens[blkIdx].content)) {
continue;
}
tokens = state.tokens[blkIdx].children;
stack.length = 0;

9
lib/rules_inline/autolink.js

@ -21,9 +21,9 @@ module.exports = function autolink(state, silent) {
if (tail.indexOf('>') < 0) { return false; }
linkMatch = tail.match(AUTOLINK_RE);
if (AUTOLINK_RE.test(tail)) {
linkMatch = tail.match(AUTOLINK_RE);
if (linkMatch) {
if (url_schemas.indexOf(linkMatch[1].toLowerCase()) < 0) { return false; }
url = linkMatch[0].slice(1, -1);
@ -44,9 +44,8 @@ module.exports = function autolink(state, silent) {
return true;
}
emailMatch = tail.match(EMAIL_RE);
if (emailMatch) {
if (EMAIL_RE.test(tail)) {
emailMatch = tail.match(EMAIL_RE);
url = emailMatch[0].slice(1, -1);

22
lib/rules_inline/state_inline.js

@ -17,7 +17,7 @@ function StateInline(src, md, env, outTokens) {
this.pending = '';
this.pendingLevel = 0;
this.cache = []; // Stores { start: end } pairs. Useful for backtrack
this.cache = {}; // Stores { start: end } pairs. Useful for backtrack
// optimization of pairs parse (emphasis, strikes).
}
@ -54,24 +54,4 @@ StateInline.prototype.push = function (type, tag, nesting) {
};
// Store value to cache.
// !!! Implementation has parser-specific optimizations
// !!! keys MUST be integer, >= 0; values MUST be integer, > 0
//
StateInline.prototype.cacheSet = function (key, val) {
for (var i = this.cache.length; i <= key; i++) {
this.cache.push(0);
}
this.cache[key] = val;
};
// Get cache value
//
StateInline.prototype.cacheGet = function (key) {
return key < this.cache.length ? this.cache[key] : 0;
};
module.exports = StateInline;

4
lib/token.js

@ -72,7 +72,7 @@ function Token(type, tag, nesting) {
* In a case of self-closing tag (code, html, fence, etc.),
* it has contents of this tag.
**/
this.content = null;
this.content = '';
/**
* Token#markup -> String
@ -86,7 +86,7 @@ function Token(type, tag, nesting) {
*
* fence infostring
**/
this.info = null;
this.info = '';
/**
* Token#block -> Boolean

Loading…
Cancel
Save