Browse Source

Pass token index to renderer fn + use it for br magick

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
abc2900ce6
  1. 2
      lib/lexer_block.js
  2. 2
      lib/lexer_block/code.js
  3. 9
      lib/lexer_block/fences.js
  4. 51
      lib/renderer.js

2
lib/lexer_block.js

@ -13,11 +13,11 @@ var rules = [];
// `list` should be after `hr`, but before `heading`
rules.push(require('./lexer_block/code'));
rules.push(require('./lexer_block/fences'));
rules.push(require('./lexer_block/blockquote'));
rules.push(require('./lexer_block/hr'));
rules.push(require('./lexer_block/list'));
rules.push(require('./lexer_block/heading'));
rules.push(require('./lexer_block/lheading'));
rules.push(require('./lexer_block/blockquote'));
rules.push(require('./lexer_block/table'));
rules.push(require('./lexer_block/paragraph'));

2
lib/lexer_block/code.js

@ -34,7 +34,7 @@ module.exports = function code(state, startLine, endLine, silent) {
state.tokens.push({
type: 'code',
content: getLines(state, startLine, last, true).replace(/^ {4}/gm, '')
content: getLines(state, startLine, last, true).replace(/^ {1,4}/gm, '')
});
state.line = nextLine;

9
lib/lexer_block/fences.js

@ -66,10 +66,17 @@ module.exports = function fences(state, startLine, endLine, silent) {
if (silent) { return true; }
// If fense has heading spases, those should be removed from inner block
len = state.tShift[startLine];
state.tokens.push({
type: 'fence',
params: params ? params.split(/\s+/g) : [],
content: getLines(state, startLine + 1, nextLine, true)
content: len === 0 ?
getLines(state, startLine + 1, nextLine, true)
:
getLines(state, startLine + 1, nextLine, true)
.replace(RegExp('^ {1,' + len + '}', 'mg'), '')
});
state.line = nextLine + 1;

51
lib/renderer.js

@ -18,14 +18,25 @@ function unescapeMd(str) {
}
// check if we need to hide '\n' before next token
function getBreak(state, idx) {
if (++idx < state.tokens.length &&
state.tokens[idx].type === 'list_item_close') {
return '';
}
return '\n';
}
var rules = {};
rules.blockquote_open = function (state /*, token*/) {
rules.blockquote_open = function (state /*, token, idx*/) {
state.result += '<blockquote>\n';
};
rules.blockquote_close = function (state /*, token*/) {
state.result += '</blockquote>\n';
rules.blockquote_close = function (state, token, idx) {
state.result += '</blockquote>' + getBreak(state, idx);
};
@ -54,21 +65,21 @@ rules.heading_close = function (state, token) {
};
rules.hr = function (state/*, token*/) {
state.result += state.options.xhtml ? '<hr />\n' : '<hr>\n';
rules.hr = function (state, token, idx) {
state.result += (state.options.xhtml ? '<hr />' : '<hr>') + getBreak(state, idx);
};
rules.bullet_list_open = function (state /*, token*/) {
rules.bullet_list_open = function (state /*, token, idx*/) {
state.result += '<ul>\n';
};
rules.bullet_list_close = function (state /*, token*/) {
rules.bullet_list_close = function (state /*, token, idx*/) {
state.result += '</ul>\n';
};
rules.list_item_open = function (state /*, token*/) {
rules.list_item_open = function (state /*, token, idx*/) {
state.result += '<li>';
};
rules.list_item_close = function (state /*, token*/) {
rules.list_item_close = function (state /*, token, idx*/) {
state.result += '</li>\n';
};
rules.ordered_list_open = function (state, token) {
@ -76,29 +87,29 @@ rules.ordered_list_open = function (state, token) {
+ (token.order > 1 ? ' start="' + token.order + '"' : '')
+ '>\n';
};
rules.ordered_list_close = function (state /*, token*/) {
rules.ordered_list_close = function (state /*, token, idx*/) {
state.result += '</ol>\n';
};
rules.paragraph_open = function (state /*, token*/) {
rules.paragraph_open = function (state /*, token, idx*/) {
state.result += '<p>';
};
rules.paragraph_close = function (state /*, token*/) {
state.result += '</p>\n';
rules.paragraph_close = function (state, token, idx) {
state.result += '</p>' + getBreak(state, idx);
};
rules.table_open = function (state /*, token*/) {
rules.table_open = function (state /*, token, idx*/) {
state.result += '<table>\n';
};
rules.table_close = function (state /*, token*/) {
rules.table_close = function (state /*, token, idx*/) {
state.result += '</table>\n';
};
rules.tr_open = function (state /*, token*/) {
rules.tr_open = function (state /*, token, idx*/) {
state.result += '<tr>\n';
};
rules.tr_close = function (state /*, token*/) {
rules.tr_close = function (state /*, token, idx*/) {
state.result += '</tr>\n';
};
rules.th_open = function (state, token) {
@ -106,7 +117,7 @@ rules.th_open = function (state, token) {
+ (token.align ? ' align="' + token.align + '"' : '')
+ '>';
};
rules.th_close = function (state /*, token*/) {
rules.th_close = function (state /*, token, idx*/) {
state.result += '</th>\n';
};
rules.td_open = function (state, token) {
@ -114,7 +125,7 @@ rules.td_open = function (state, token) {
+ (token.align ? ' align="' + token.align + '"' : '')
+ '>';
};
rules.td_close = function (state /*, token*/) {
rules.td_close = function (state /*, token, idx*/) {
state.result += '</td>\n';
};
@ -143,7 +154,7 @@ Renderer.prototype.render = function (state) {
throw new Error('Renderer error: unknown token ' + tokens[i].type);
}
rule(state, tokens[i]);
rule(state, tokens[i], i);
}
return state.result;

Loading…
Cancel
Save