Skip to content

Commit 57d38df

Browse files
authored
feat: replace fence indent space with ZWSP to preserve indent information (#10)
1 parent abab6ae commit 57d38df

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/rules_block/fence.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ module.exports = function fence(state, startLine, endLine, silent) {
9191

9292
token = state.push('fence', 'code', 0);
9393
token.info = params;
94-
token.content = state.getLines(startLine + 1, nextLine, len, true);
94+
token.content = state.getLines(startLine + 1, nextLine, len, true, true);
9595
token.markup = markup;
9696
token.map = [ startLine, state.line ];
9797
token.position = originalPos;

lib/rules_block/state_block.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,12 @@ StateBlock.prototype.skipCharsBack = function skipCharsBack(pos, code, min) {
176176
return pos;
177177
};
178178

179+
const ZWSP = '\u200B'; // zero width space
179180
// cut lines range from source.
180-
StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF) {
181+
StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF, replaceIndentSpaceWithZWSP) {
181182
var i, lineIndent, ch, first, last, queue, lineStart,
182-
line = begin;
183+
line = begin,
184+
replaceIndentSpaceWithZWSP = replaceIndentSpaceWithZWSP || false;
183185

184186
if (begin >= end) {
185187
return '';
@@ -224,6 +226,10 @@ StateBlock.prototype.getLines = function getLines(begin, end, indent, keepLastLF
224226
} else {
225227
queue[i] = this.src.slice(first, last);
226228
}
229+
230+
if (replaceIndentSpaceWithZWSP) {
231+
queue[i] = new Array(lineIndent + 1).join(ZWSP) + queue[i];
232+
}
227233
}
228234

229235
return queue.join('');

0 commit comments

Comments
 (0)