Skip to content

Commit 29fb280

Browse files
dwellemarijnh
authored andcommitted
[markdown mode] improve header, list & fencedCode behavior around blockquote & indentation
1 parent d600b94 commit 29fb280

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

mode/markdown/markdown.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
139139

140140
function blockNormal(stream, state) {
141141
var sol = stream.sol();
142+
var firstTokenOnLine = stream.column() === state.indentation;
142143
var prevLineLineIsEmpty = lineIsEmpty(state.prevLine);
143144
var prevLineIsIndentedCode = state.indentedCode;
144145
var prevLineIsHr = state.hr;
@@ -182,7 +183,8 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
182183
return tokenTypes.code;
183184
} else if (stream.eatSpace()) {
184185
return null;
185-
} else if ((match = stream.match(atxHeaderRE)) && match[1].length <= 6) {
186+
} else if (firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(atxHeaderRE)) && match[1].length <= 6) {
187+
state.quote = 0;
186188
state.header = match[1].length;
187189
if (modeCfg.highlightFormatting) state.formatting = "header";
188190
state.f = state.inline;
@@ -192,11 +194,12 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
192194
if (modeCfg.highlightFormatting) state.formatting = "quote";
193195
stream.eatSpace();
194196
return getType(state);
195-
} else if (!isHr && !state.quote && (match = stream.match(listRE))) {
197+
} else if (!isHr && firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(listRE))) {
196198
var listType = match[1] ? "ol" : "ul";
197199

198200
state.indentation = lineIndentation + stream.current().length;
199201
state.list = true;
202+
state.quote = 0;
200203

201204
// Add this list item's content's indentation to the stack
202205
state.listStack.push(state.indentation);
@@ -207,7 +210,8 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
207210
state.f = state.inline;
208211
if (modeCfg.highlightFormatting) state.formatting = ["list", "list-" + listType];
209212
return getType(state);
210-
} else if (modeCfg.fencedCodeBlocks && !state.quote && (match = stream.match(fencedCodeRE, true))) {
213+
} else if (modeCfg.fencedCodeBlocks && firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(fencedCodeRE, true))) {
214+
state.quote = 0;
211215
state.fencedChars = match[1]
212216
// try switching mode
213217
state.localMode = getMode(match[2]);

mode/markdown/test.js

+39
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,19 @@
228228
MT("atxH1inline",
229229
"[header&header-1 # foo ][header&header-1&em *bar*]");
230230

231+
MT("atxIndentedTooMuch",
232+
"[header&header-1 # foo]",
233+
" # bar");
234+
235+
// disable atx inside blockquote until we implement proper blockquote inner mode
236+
// TODO: fix to be CommonMark-compliant
237+
MT("atxNestedInsideBlockquote",
238+
"[quote&quote-1 > # foo]");
239+
240+
MT("atxAfterBlockquote",
241+
"[quote&quote-1 > foo]",
242+
"[header&header-1 # bar]");
243+
231244
// Setext headers - H1, H2
232245
// Per documentation, "Any number of underlining =’s or -’s will work."
233246
// http://daringfireball.net/projects/markdown/syntax#header
@@ -588,6 +601,19 @@
588601
"",
589602
"\t\t[variable-3 part of list2]");
590603

604+
MT("listAfterBlockquote",
605+
"[quote&quote-1 > foo]",
606+
"[variable-2 - bar]");
607+
608+
// shouldn't create sublist if it's indented more than allowed
609+
MT("nestedListIndentedTooMuch",
610+
"[variable-2 - foo]",
611+
" [variable-2 - bar]");
612+
613+
MT("listIndentedTooMuchAfterParagraph",
614+
"foo",
615+
" - bar");
616+
591617
// Blockquote
592618
MT("blockquote",
593619
"[variable-2 * foo]",
@@ -1096,6 +1122,19 @@
10961122
"[comment ~~~]",
10971123
"bar");
10981124

1125+
FencedTest("fencedCodeBlocksAfterBlockquote",
1126+
"[quote&quote-1 > foo]",
1127+
"[comment ```]",
1128+
"[comment bar]",
1129+
"[comment ```]");
1130+
1131+
// fencedCode indented too much should act as simple indentedCode
1132+
// (hence has no highlight formatting)
1133+
FT("tooMuchIndentedFencedCode",
1134+
" [comment ```]",
1135+
" [comment code]",
1136+
" [comment ```]");
1137+
10991138
// Tests that require XML mode
11001139

11011140
MT("xmlMode",

0 commit comments

Comments
 (0)