Skip to content

Commit 9f7d6a8

Browse files
committed
feat: record line offset in block state
1 parent 2703f16 commit 9f7d6a8

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

lib/rules_block/blockquote.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,27 @@ module.exports = function blockquote(state, startLine, endLine, silent) {
272272
state.parentType = oldParentType;
273273
lines[1] = state.line;
274274

275+
let totalLineOffset = 0;
275276
// Restore original tShift; this might not be necessary since the parser
276277
// has already been here, but just to make sure we can do that.
277278
for (i = 0; i < oldTShift.length; i++) {
278-
state.bMarks[i + startLine] = oldBMarks[i];
279-
state.tShift[i + startLine] = oldTShift[i];
280-
state.sCount[i + startLine] = oldSCount[i];
281-
state.bsCount[i + startLine] = oldBSCount[i];
279+
const lineNumber = i + startLine;
280+
if (state.lineOffsets[lineNumber] === null) {
281+
state.lineOffsets[lineNumber] = totalLineOffset;
282+
totalLineOffset += calcLineOffset(state, lineNumber);
283+
}
284+
285+
state.bMarks[lineNumber] = oldBMarks[i];
286+
state.tShift[lineNumber] = oldTShift[i];
287+
state.sCount[lineNumber] = oldSCount[i];
288+
state.bsCount[lineNumber] = oldBSCount[i];
282289
}
283290
state.blkIndent = oldIndent;
284291

285292
return true;
286293
};
294+
295+
function calcLineOffset (state, lineNumber) {
296+
const previousLineEnd = state.eMarks[lineNumber - 1] + 1 || 0;
297+
return state.bMarks[lineNumber] - previousLineEnd;
298+
}

lib/rules_block/state_block.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function StateBlock(src, md, env, tokens) {
3131
this.eMarks = []; // line end offsets for fast jumps
3232
this.tShift = []; // offsets of the first non-space characters (tabs not expanded)
3333
this.sCount = []; // indents for each line (tabs expanded)
34+
this.lineOffsets = [];
3435

3536
// An amount of virtual spaces (tabs expanded) between beginning
3637
// of each line (bMarks) and real beginning of that line.
@@ -92,6 +93,7 @@ function StateBlock(src, md, env, tokens) {
9293
this.tShift.push(indent);
9394
this.sCount.push(offset);
9495
this.bsCount.push(0);
96+
this.lineOffsets.push(null);
9597

9698
indent_found = false;
9799
indent = 0;
@@ -106,6 +108,7 @@ function StateBlock(src, md, env, tokens) {
106108
this.tShift.push(0);
107109
this.sCount.push(0);
108110
this.bsCount.push(0);
111+
this.lineOffsets.push(null);
109112

110113
this.lineMax = this.bMarks.length - 1; // don't count last fake line
111114
}

0 commit comments

Comments
 (0)