Browse Source

parse backticks without regex exec

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
199976d09e
  1. 25
      lib/rules_inline/backticks.js

25
lib/rules_inline/backticks.js

@ -1,9 +1,7 @@
// Parse backticks // Parse backticks
var END_RE = /`+/g;
module.exports = function backticks(state) { module.exports = function backticks(state) {
var start, code, max, marker, match, var start, max, marker, matchStart, matchEnd,
pos = state.pos, pos = state.pos,
ch = state.src.charCodeAt(pos); ch = state.src.charCodeAt(pos);
@ -17,22 +15,23 @@ module.exports = function backticks(state) {
marker = state.src.slice(start, pos); marker = state.src.slice(start, pos);
END_RE = /`+/g; matchStart = matchEnd = pos;
END_RE.lastIndex = pos;
while ((matchStart = state.src.indexOf('`', matchEnd)) !== -1) {
matchEnd = matchStart + 1;
while ((match = END_RE.exec(state.src)) !== null) { while (matchEnd < max && state.src.charCodeAt(matchEnd) === 0x60/* ` */) { matchEnd++; }
if (match[0].length === marker.length) {
code = state.src.slice(pos, END_RE.lastIndex - marker.length); if (matchEnd - matchStart === marker.length) {
state.push({ state.push({
type: 'code', type: 'code',
content: code content: state.src.slice(pos, matchStart)
.replace(/[ \n]+/g,' ') .replace(/[ \n]+/g,' ')
.trim(), .trim(),
block: false, block: false,
level: state.level level: state.level
}); });
state.pos = matchEnd;
state.pos += marker.length * 2 + code.length;
return true; return true;
} }
} }

Loading…
Cancel
Save