Skip to content

Commit afed95a

Browse files
0b10011marijnh
authored andcommitted
[markdown mode] Fix indentation rules to match CommonMark
See http://spec.commonmark.org/0.24/#list-items Closes codemirror#3833
1 parent 4787735 commit afed95a

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

mode/markdown/markdown.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,8 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
149149
state.list = null;
150150
} else if (state.indentation > 0) {
151151
state.list = null;
152-
state.listDepth = Math.floor(state.indentation / 4);
153152
} else { // No longer a list
154153
state.list = false;
155-
state.listDepth = 0;
156154
}
157155
}
158156

@@ -199,7 +197,17 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
199197
}
200198
state.indentation = stream.column() + stream.current().length;
201199
state.list = true;
202-
state.listDepth++;
200+
201+
// While this list item's marker's indentation
202+
// is less than the deepest list item's content's indentation,
203+
// pop the deepest list item indentation off the stack.
204+
while (state.listStack && stream.column() < state.listStack[state.listStack.length - 1]) {
205+
state.listStack.pop();
206+
}
207+
208+
// Add this list item's content's indentation to the stack
209+
state.listStack.push(state.indentation);
210+
203211
if (modeCfg.taskLists && stream.match(taskListRE, false)) {
204212
state.taskList = true;
205213
}
@@ -321,7 +329,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
321329
}
322330

323331
if (state.list !== false) {
324-
var listMod = (state.listDepth - 1) % 3;
332+
var listMod = (state.listStack.length - 1) % 3;
325333
if (!listMod) {
326334
styles.push(tokenTypes.list1);
327335
} else if (listMod === 1) {
@@ -697,7 +705,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
697705
hr: false,
698706
taskList: false,
699707
list: false,
700-
listDepth: 0,
708+
listStack: [],
701709
quote: 0,
702710
trailingSpace: 0,
703711
trailingSpaceNewLine: false,
@@ -732,7 +740,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
732740
hr: s.hr,
733741
taskList: s.taskList,
734742
list: s.list,
735-
listDepth: s.listDepth,
743+
listStack: s.listStack.slice(0),
736744
quote: s.quote,
737745
indentedCode: s.indentedCode,
738746
trailingSpace: s.trailingSpace,
@@ -772,11 +780,8 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
772780

773781
state.f = state.block;
774782
var indentation = stream.match(/^\s*/, true)[0].replace(/\t/g, ' ').length;
775-
var difference = Math.floor((indentation - state.indentation) / 4) * 4;
776-
if (difference > 4) difference = 4;
777-
var adjustedIndentation = state.indentation + difference;
778-
state.indentationDiff = adjustedIndentation - state.indentation;
779-
state.indentation = adjustedIndentation;
783+
state.indentationDiff = Math.min(indentation - state.indentation, 4);
784+
state.indentation = state.indentation + state.indentationDiff;
780785
if (indentation > 0) return null;
781786
}
782787
return state.f(stream, state);

mode/markdown/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,18 @@
452452
"",
453453
"hello");
454454

455+
MT("listCommonMarkIndentationCode",
456+
"[variable-2 * Code blocks also affect]",
457+
" [variable-3 * The next level starts where the contents start.]",
458+
" [variable-3 * Anything less than that will keep the item on the same level.]",
459+
" [variable-3 * Each list item can indent the first level further and further.]",
460+
" [variable-3 * For the most part, this makes sense while writing a list.]",
461+
" [keyword * This means two items with same indentation can be different levels.]",
462+
" [keyword * Each level has an indent requirement that can change between items.]",
463+
" [keyword * A list item that meets this will be part of the next level.]",
464+
" [variable-3 * Otherwise, it will be part of the level where it does meet this.]",
465+
" [variable-2 * World]");
466+
455467
// Blockquote
456468
MT("blockquote",
457469
"[variable-2 * foo]",

0 commit comments

Comments
 (0)