Browse Source

Added backticks parse

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
172e77c318
  1. 3
      lib/lexer_block/code.js
  2. 1
      lib/lexer_inline.js
  3. 6
      lib/lexer_inline/autolink.js
  4. 42
      lib/lexer_inline/backticks.js
  5. 4
      lib/renderer.js

3
lib/lexer_block/code.js

@ -35,7 +35,8 @@ module.exports = function code(state, startLine, endLine, silent) {
state.tokens.push({
type: 'code',
content: getLines(state, startLine, last, 4 + state.blkIndent, true)
content: getLines(state, startLine, last, 4 + state.blkIndent, true),
block: true
});
state.line = nextLine;

1
lib/lexer_inline.js

@ -15,6 +15,7 @@ var rules = [];
rules.push(require('./lexer_inline/text'));
rules.push(require('./lexer_inline/newline'));
rules.push(require('./lexer_inline/escape'));
rules.push(require('./lexer_inline/backticks'));
//
//
rules.push(require('./lexer_inline/autolink'));

6
lib/lexer_inline/autolink.js

@ -22,15 +22,15 @@ module.exports = function autolink(state) {
if (linkMatch) {
if (linkPrefix.indexOf(linkMatch[1]) < 0) { return false; }
state.tokens.push({
state.push({
type: 'link_open',
href: escape(linkMatch[0].slice(1, -1))
});
state.tokens.push({
state.push({
type: 'text',
content: escape(linkMatch[0].slice(1, -1))
});
state.tokens.push({ type: 'link_close' });
state.push({ type: 'link_close' });
state.pos += linkMatch[0].length;
return true;

42
lib/lexer_inline/backticks.js

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

4
lib/renderer.js

@ -28,7 +28,11 @@ rules.blockquote_close = function (tokens, idx /*, options*/) {
rules.code = function (tokens, idx /*, options*/) {
if (tokens[idx].block) {
return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>' + getBreak(tokens, idx);
}
return '<code>' + escapeHtml(tokens[idx].content) + '</code>';
};

Loading…
Cancel
Save