From abc2900ce66b2ad41decd31948e8a38b410c14f0 Mon Sep 17 00:00:00 2001
From: Vitaly Puzrin
Date: Wed, 10 Sep 2014 08:10:41 +0400
Subject: [PATCH] Pass token index to renderer fn + use it for br magick
---
lib/lexer_block.js | 2 +-
lib/lexer_block/code.js | 2 +-
lib/lexer_block/fences.js | 9 ++++++-
lib/renderer.js | 51 ++++++++++++++++++++++++---------------
4 files changed, 41 insertions(+), 23 deletions(-)
diff --git a/lib/lexer_block.js b/lib/lexer_block.js
index 0fbfc50..e5bada7 100644
--- a/lib/lexer_block.js
+++ b/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'));
diff --git a/lib/lexer_block/code.js b/lib/lexer_block/code.js
index 4480075..6d00e35 100644
--- a/lib/lexer_block/code.js
+++ b/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;
diff --git a/lib/lexer_block/fences.js b/lib/lexer_block/fences.js
index 27bd99d..8b56bcb 100644
--- a/lib/lexer_block/fences.js
+++ b/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;
diff --git a/lib/renderer.js b/lib/renderer.js
index 8ac9dea..91cce50 100644
--- a/lib/renderer.js
+++ b/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 += '\n';
};
-rules.blockquote_close = function (state /*, token*/) {
- state.result += '
\n';
+rules.blockquote_close = function (state, token, idx) {
+ state.result += '' + getBreak(state, idx);
};
@@ -54,21 +65,21 @@ rules.heading_close = function (state, token) {
};
-rules.hr = function (state/*, token*/) {
- state.result += state.options.xhtml ? '
\n' : '
\n';
+rules.hr = function (state, token, idx) {
+ state.result += (state.options.xhtml ? '
' : '
') + getBreak(state, idx);
};
-rules.bullet_list_open = function (state /*, token*/) {
+rules.bullet_list_open = function (state /*, token, idx*/) {
state.result += '\n';
};
-rules.bullet_list_close = function (state /*, token*/) {
+rules.bullet_list_close = function (state /*, token, idx*/) {
state.result += '
\n';
};
-rules.list_item_open = function (state /*, token*/) {
+rules.list_item_open = function (state /*, token, idx*/) {
state.result += '';
};
-rules.list_item_close = function (state /*, token*/) {
+rules.list_item_close = function (state /*, token, idx*/) {
state.result += '\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 += '\n';
};
-rules.paragraph_open = function (state /*, token*/) {
+rules.paragraph_open = function (state /*, token, idx*/) {
state.result += '';
};
-rules.paragraph_close = function (state /*, token*/) {
- state.result += '
\n';
+rules.paragraph_close = function (state, token, idx) {
+ state.result += '
' + getBreak(state, idx);
};
-rules.table_open = function (state /*, token*/) {
+rules.table_open = function (state /*, token, idx*/) {
state.result += '\n';
};
-rules.table_close = function (state /*, token*/) {
+rules.table_close = function (state /*, token, idx*/) {
state.result += '
\n';
};
-rules.tr_open = function (state /*, token*/) {
+rules.tr_open = function (state /*, token, idx*/) {
state.result += '\n';
};
-rules.tr_close = function (state /*, token*/) {
+rules.tr_close = function (state /*, token, idx*/) {
state.result += '
\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 += '\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 += '\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;