Skip to content

Commit f8b9eec

Browse files
committed
[markdown mode] support fencedCodeBlocks in base markdown as per CommonMark
1 parent c81346e commit f8b9eec

File tree

5 files changed

+61
-91
lines changed

5 files changed

+61
-91
lines changed

mode/gfm/gfm.js

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ CodeMirror.defineMode("gfm", function(config, modeConfig) {
114114

115115
var markdownConfig = {
116116
taskLists: true,
117-
fencedCodeBlocks: '```',
118117
strikethrough: true,
119118
emoji: true
120119
};

mode/gfm/test.js

-46
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414
FT("doubleBackticks",
1515
"[comment&formatting&formatting-code ``][comment foo ` bar][comment&formatting&formatting-code ``]");
1616

17-
FT("codeBlock",
18-
"[comment&formatting&formatting-code-block ```css]",
19-
"[tag foo]",
20-
"[comment&formatting&formatting-code-block ```]");
21-
2217
FT("taskList",
2318
"[variable-2&formatting&formatting-list&formatting-list-ul - ][meta&formatting&formatting-task [ ]]][variable-2 foo]",
2419
"[variable-2&formatting&formatting-list&formatting-list-ul - ][property&formatting&formatting-task [x]]][variable-2 foo]");
@@ -41,31 +36,6 @@
4136
MT("emStrongUnderscore",
4237
"[em&strong ___foo___] bar");
4338

44-
MT("fencedCodeBlocks",
45-
"[comment ```]",
46-
"[comment foo]",
47-
"",
48-
"[comment ```]",
49-
"bar");
50-
51-
MT("fencedCodeBlockModeSwitching",
52-
"[comment ```javascript]",
53-
"[variable foo]",
54-
"",
55-
"[comment ```]",
56-
"bar");
57-
58-
MT("fencedCodeBlockModeSwitchingObjc",
59-
"[comment ```objective-c]",
60-
"[keyword @property] [variable NSString] [operator *] [variable foo];",
61-
"[comment ```]",
62-
"bar");
63-
64-
MT("fencedCodeBlocksNoTildes",
65-
"~~~",
66-
"foo",
67-
"~~~");
68-
6939
MT("taskListAsterisk",
7040
"[variable-2 * ][link&variable-2 [[]]][variable-2 foo]", // Invalid; must have space or x between []
7141
"[variable-2 * ][link&variable-2 [[ ]]][variable-2 bar]", // Invalid; must have space after ]
@@ -166,11 +136,6 @@
166136
MT("notALink",
167137
"foo asfd:asdf bar");
168138

169-
MT("notALink",
170-
"[comment ```css]",
171-
"[tag foo] {[property color]:[keyword black];}",
172-
"[comment ```][link http://www.example.com/]");
173-
174139
MT("notALink",
175140
"[comment ``foo `bar` http://www.example.com/``] hello");
176141

@@ -181,17 +146,6 @@
181146
"",
182147
"[link http://www.example.com/]");
183148

184-
MT("headerCodeBlockGithub",
185-
"[header&header-1 # heading]",
186-
"",
187-
"[comment ```]",
188-
"[comment code]",
189-
"[comment ```]",
190-
"",
191-
"Commit: [link be6a8cc1c1ecfe9489fb51e4869af15a13fc2cd2]",
192-
"Issue: [link #1]",
193-
"Link: [link http://www.example.com/]");
194-
195149
MT("strikethrough",
196150
"[strikethrough ~~foo~~]");
197151

mode/markdown/index.html

+30-20
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ <h2>Markdown mode</h2>
8787

8888
A First Level Header
8989
====================
90-
90+
9191
A Second Level Header
9292
---------------------
9393

@@ -97,11 +97,11 @@ <h2>Markdown mode</h2>
9797

9898
The quick brown fox jumped over the lazy
9999
dog's back.
100-
100+
101101
### Header 3
102102

103103
&gt; This is a blockquote.
104-
&gt;
104+
&gt;
105105
&gt; This is the second paragraph in the blockquote.
106106
&gt;
107107
&gt; ## This is an H2 in a blockquote
@@ -110,23 +110,23 @@ <h2>Markdown mode</h2>
110110
Output:
111111

112112
&lt;h1&gt;A First Level Header&lt;/h1&gt;
113-
113+
114114
&lt;h2&gt;A Second Level Header&lt;/h2&gt;
115-
115+
116116
&lt;p&gt;Now is the time for all good men to come to
117117
the aid of their country. This is just a
118118
regular paragraph.&lt;/p&gt;
119-
119+
120120
&lt;p&gt;The quick brown fox jumped over the lazy
121121
dog's back.&lt;/p&gt;
122-
122+
123123
&lt;h3&gt;Header 3&lt;/h3&gt;
124-
124+
125125
&lt;blockquote&gt;
126126
&lt;p&gt;This is a blockquote.&lt;/p&gt;
127-
127+
128128
&lt;p&gt;This is the second paragraph in the blockquote.&lt;/p&gt;
129-
129+
130130
&lt;h2&gt;This is an H2 in a blockquote&lt;/h2&gt;
131131
&lt;/blockquote&gt;
132132

@@ -140,18 +140,18 @@ <h2>Markdown mode</h2>
140140

141141
Some of these words *are emphasized*.
142142
Some of these words _are emphasized also_.
143-
143+
144144
Use two asterisks for **strong emphasis**.
145145
Or, if you prefer, __use two underscores instead__.
146146

147147
Output:
148148

149149
&lt;p&gt;Some of these words &lt;em&gt;are emphasized&lt;/em&gt;.
150150
Some of these words &lt;em&gt;are emphasized also&lt;/em&gt;.&lt;/p&gt;
151-
151+
152152
&lt;p&gt;Use two asterisks for &lt;strong&gt;strong emphasis&lt;/strong&gt;.
153153
Or, if you prefer, &lt;strong&gt;use two underscores instead&lt;/strong&gt;.&lt;/p&gt;
154-
154+
155155

156156

157157
## Lists ##
@@ -204,7 +204,7 @@ <h2>Markdown mode</h2>
204204
the paragraphs by 4 spaces or 1 tab:
205205

206206
* A list item.
207-
207+
208208
With multiple paragraphs.
209209

210210
* Another item in the list.
@@ -216,7 +216,7 @@ <h2>Markdown mode</h2>
216216
&lt;p&gt;With multiple paragraphs.&lt;/p&gt;&lt;/li&gt;
217217
&lt;li&gt;&lt;p&gt;Another item in the list.&lt;/p&gt;&lt;/li&gt;
218218
&lt;/ul&gt;
219-
219+
220220

221221

222222
### Links ###
@@ -311,7 +311,7 @@ <h2>Markdown mode</h2>
311311

312312
&lt;p&gt;I strongly recommend against using any
313313
&lt;code&gt;&amp;lt;blink&amp;gt;&lt;/code&gt; tags.&lt;/p&gt;
314-
314+
315315
&lt;p&gt;I wish SmartyPants used named entities like
316316
&lt;code&gt;&amp;amp;mdash;&lt;/code&gt; instead of decimal-encoded
317317
entites like &lt;code&gt;&amp;amp;#8212;&lt;/code&gt;.&lt;/p&gt;
@@ -334,11 +334,20 @@ <h2>Markdown mode</h2>
334334

335335
&lt;p&gt;If you want your page to validate under XHTML 1.0 Strict,
336336
you've got to put paragraph tags in your blockquotes:&lt;/p&gt;
337-
337+
338338
&lt;pre&gt;&lt;code&gt;&amp;lt;blockquote&amp;gt;
339339
&amp;lt;p&amp;gt;For example.&amp;lt;/p&amp;gt;
340340
&amp;lt;/blockquote&amp;gt;
341341
&lt;/code&gt;&lt;/pre&gt;
342+
343+
## Fenced code blocks (and syntax highlighting)
344+
345+
```javascript
346+
for (var i = 0; i < items.length; i++) {
347+
console.log(items[i], i); // log them
348+
}
349+
```
350+
342351
</textarea></form>
343352

344353
<script>
@@ -350,10 +359,11 @@ <h2>Markdown mode</h2>
350359
});
351360
</script>
352361

353-
<p>You might want to use the <a href="../gfm/index.html">Github-Flavored Markdown mode</a> instead, which adds support for fenced code blocks and a few other things.</p>
362+
<p>If you also want support <code>strikethrough</code>, <code>emoji</code> and few other goodies, check out <a href="../gfm/index.html">Github-Flavored Markdown mode</a>.</p>
363+
364+
<p>Optionally depends on other modes for properly highlighted code blocks,
365+
and XML mode for properly highlighted inline XML blocks.</p>
354366

355-
<p>Optionally depends on the XML mode for properly highlighted inline XML blocks.</p>
356-
357367
<p><strong>MIME types defined:</strong> <code>text/x-markdown</code>.</p>
358368

359369
<p><strong>Parsing/Highlighting Tests:</strong> <a href="../../test/index.html#markdown_*">normal</a>, <a href="../../test/index.html#verbose,markdown_*">verbose</a>.</p>

mode/markdown/markdown.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
3535
if (modeCfg.maxBlockquoteDepth === undefined)
3636
modeCfg.maxBlockquoteDepth = 0;
3737

38-
// Use `fencedCodeBlocks` to configure fenced code blocks. false to
39-
// disable, string to specify a precise regexp that the fence should
40-
// match, and true to allow three or more backticks or tildes (as
41-
// per CommonMark).
42-
4338
// Turn on task lists? ("- [ ] " and "- [x] ")
4439
if (modeCfg.taskLists === undefined) modeCfg.taskLists = false;
4540

@@ -88,8 +83,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
8883
, atxHeaderRE = modeCfg.allowAtxHeaderWithoutSpace ? /^(#+)/ : /^(#+)(?: |$)/
8984
, setextHeaderRE = /^ *(?:\={1,}|-{1,})\s*$/
9085
, textRE = /^[^#!\[\]*_\\<>` "'(~:]+/
91-
, fencedCodeRE = new RegExp("^(" + (modeCfg.fencedCodeBlocks === true ? "~~~+|```+" : modeCfg.fencedCodeBlocks) +
92-
")[ \\t]*([\\w+#\-]*)")
86+
, fencedCodeRE = /^(~~~+|```+)[ \t]*([\w+#-]*)/
9387
, linkDefRE = /^\s*\[[^\]]+?\]:\s*\S+(\s*\S*\s*)?$/ // naive link-definition
9488
, punctuation = /[!\"#$%&\'()*+,\-\.\/:;<=>?@\[\\\]^_`{|}~]/
9589
, expandedTab = " " // CommonMark specifies tab as 4 spaces
@@ -209,7 +203,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
209203
state.f = state.inline;
210204
if (modeCfg.highlightFormatting) state.formatting = ["list", "list-" + listType];
211205
return getType(state);
212-
} else if (modeCfg.fencedCodeBlocks && firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(fencedCodeRE, true))) {
206+
} else if (firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(fencedCodeRE, true))) {
213207
state.quote = 0;
214208
state.fencedChars = match[1]
215209
// try switching mode

mode/markdown/test.js

+29-16
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
function FT(name) { test.mode(name, modeHighlightFormatting, Array.prototype.slice.call(arguments, 1)); }
1010
var modeAtxNoSpace = CodeMirror.getMode(config, {name: "markdown", allowAtxHeaderWithoutSpace: true});
1111
function AtxNoSpaceTest(name) { test.mode(name, modeAtxNoSpace, Array.prototype.slice.call(arguments, 1)); }
12-
var modeFenced = CodeMirror.getMode(config, {name: "markdown", fencedCodeBlocks: true});
13-
function FencedTest(name) { test.mode(name, modeFenced, Array.prototype.slice.call(arguments, 1)); }
1412
var modeOverrideClasses = CodeMirror.getMode(config, {
1513
name: "markdown",
1614
strikethrough: true,
@@ -97,6 +95,11 @@
9795
FT("formatting_image",
9896
"[formatting&formatting-image&image&image-marker !][formatting&formatting-image&image&image-alt-text&link [[][image&image-alt-text&link alt text][formatting&formatting-image&image&image-alt-text&link ]]][formatting&formatting-link-string&string&url (][url&string http://link.to/image.jpg][formatting&formatting-link-string&string&url )]");
9997

98+
FT("codeBlock",
99+
"[comment&formatting&formatting-code-block ```css]",
100+
"[tag foo]",
101+
"[comment&formatting&formatting-code-block ```]");
102+
100103
MT("plainText",
101104
"foo");
102105

@@ -587,7 +590,7 @@
587590
" [variable-2 de-indented text part of list1 again]",
588591
"",
589592
" [variable-2&comment ```]",
590-
" [variable-2&comment code]",
593+
" [comment code]",
591594
" [variable-2&comment ```]",
592595
"",
593596
" [variable-2 text after fenced code]");
@@ -1085,54 +1088,64 @@
10851088
MT("taskList",
10861089
"[variable-2 * ][link&variable-2 [[ ]]][variable-2 bar]");
10871090

1088-
MT("noFencedCodeBlocks",
1089-
"~~~",
1090-
"foo",
1091-
"~~~");
1092-
1093-
FencedTest("fencedCodeBlocks",
1091+
MT("fencedCodeBlocks",
10941092
"[comment ```]",
10951093
"[comment foo]",
1094+
"",
1095+
"[comment bar]",
1096+
"[comment ```]",
1097+
"baz");
1098+
1099+
MT("fencedCodeBlockModeSwitching",
1100+
"[comment ```javascript]",
1101+
"[variable foo]",
1102+
"",
1103+
"[comment ```]",
1104+
"bar");
1105+
1106+
MT("fencedCodeBlockModeSwitchingObjc",
1107+
"[comment ```objective-c]",
1108+
"[keyword @property] [variable NSString] [operator *] [variable foo];",
10961109
"[comment ```]",
10971110
"bar");
10981111

1099-
FencedTest("fencedCodeBlocksMultipleChars",
1112+
MT("fencedCodeBlocksMultipleChars",
11001113
"[comment `````]",
11011114
"[comment foo]",
11021115
"[comment ```]",
11031116
"[comment foo]",
11041117
"[comment `````]",
11051118
"bar");
11061119

1107-
FencedTest("fencedCodeBlocksTildes",
1120+
MT("fencedCodeBlocksTildes",
11081121
"[comment ~~~]",
11091122
"[comment foo]",
11101123
"[comment ~~~]",
11111124
"bar");
11121125

1113-
FencedTest("fencedCodeBlocksTildesMultipleChars",
1126+
MT("fencedCodeBlocksTildesMultipleChars",
11141127
"[comment ~~~~~]",
11151128
"[comment ~~~]",
11161129
"[comment foo]",
11171130
"[comment ~~~~~]",
11181131
"bar");
11191132

1120-
FencedTest("fencedCodeBlocksMultipleChars",
1133+
MT("fencedCodeBlocksMultipleChars",
11211134
"[comment `````]",
11221135
"[comment foo]",
11231136
"[comment ```]",
11241137
"[comment foo]",
11251138
"[comment `````]",
11261139
"bar");
11271140

1128-
FencedTest("fencedCodeBlocksMixed",
1141+
MT("fencedCodeBlocksMixed",
11291142
"[comment ~~~]",
11301143
"[comment ```]",
11311144
"[comment foo]",
11321145
"[comment ~~~]",
11331146
"bar");
11341147

1135-
FencedTest("fencedCodeBlocksAfterBlockquote",
1148+
MT("fencedCodeBlocksAfterBlockquote",
11361149
"[quote&quote-1 > foo]",
11371150
"[comment ```]",
11381151
"[comment bar]",
@@ -1145,7 +1158,7 @@
11451158
" [comment code]",
11461159
" [comment ```]");
11471160

1148-
FencedTest("autoTerminateFencedCodeWhenLeavingList",
1161+
MT("autoTerminateFencedCodeWhenLeavingList",
11491162
"[variable-2 - list1]",
11501163
" [variable-3 - list2]",
11511164
" [variable-3&comment ```]",

0 commit comments

Comments
 (0)