@standardnotes/markdown-minimal 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,884 @@
1
+ // CodeMirror, copyright (c) by Marijn Haverbeke and others
2
+ // Distributed under an MIT license: https://codemirror.net/LICENSE
3
+
4
+ (function(mod) {
5
+ if (typeof exports == "object" && typeof module == "object") // CommonJS
6
+ mod(require("../../lib/codemirror"), require("../xml/xml"), require("../meta"));
7
+ else if (typeof define == "function" && define.amd) // AMD
8
+ define(["../../lib/codemirror", "../xml/xml", "../meta"], mod);
9
+ else // Plain browser env
10
+ mod(CodeMirror);
11
+ })(function(CodeMirror) {
12
+ "use strict";
13
+
14
+ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
15
+
16
+ var htmlMode = CodeMirror.getMode(cmCfg, "text/html");
17
+ var htmlModeMissing = htmlMode.name == "null"
18
+
19
+ function getMode(name) {
20
+ if (CodeMirror.findModeByName) {
21
+ var found = CodeMirror.findModeByName(name);
22
+ if (found) name = found.mime || found.mimes[0];
23
+ }
24
+ var mode = CodeMirror.getMode(cmCfg, name);
25
+ return mode.name == "null" ? null : mode;
26
+ }
27
+
28
+ // Should characters that affect highlighting be highlighted separate?
29
+ // Does not include characters that will be output (such as `1.` and `-` for lists)
30
+ if (modeCfg.highlightFormatting === undefined)
31
+ modeCfg.highlightFormatting = false;
32
+
33
+ // Maximum number of nested blockquotes. Set to 0 for infinite nesting.
34
+ // Excess `>` will emit `error` token.
35
+ if (modeCfg.maxBlockquoteDepth === undefined)
36
+ modeCfg.maxBlockquoteDepth = 0;
37
+
38
+ // Turn on task lists? ("- [ ] " and "- [x] ")
39
+ if (modeCfg.taskLists === undefined) modeCfg.taskLists = false;
40
+
41
+ // Turn on strikethrough syntax
42
+ if (modeCfg.strikethrough === undefined)
43
+ modeCfg.strikethrough = false;
44
+
45
+ if (modeCfg.emoji === undefined)
46
+ modeCfg.emoji = false;
47
+
48
+ if (modeCfg.fencedCodeBlockHighlighting === undefined)
49
+ modeCfg.fencedCodeBlockHighlighting = true;
50
+
51
+ if (modeCfg.xml === undefined)
52
+ modeCfg.xml = true;
53
+
54
+ // Allow token types to be overridden by user-provided token types.
55
+ if (modeCfg.tokenTypeOverrides === undefined)
56
+ modeCfg.tokenTypeOverrides = {};
57
+
58
+ var tokenTypes = {
59
+ header: "header",
60
+ code: "comment",
61
+ quote: "quote",
62
+ list1: "variable-2",
63
+ list2: "variable-3",
64
+ list3: "keyword",
65
+ hr: "hr",
66
+ image: "image",
67
+ imageAltText: "image-alt-text",
68
+ imageMarker: "image-marker",
69
+ formatting: "formatting",
70
+ linkInline: "link",
71
+ linkEmail: "link",
72
+ linkText: "link",
73
+ linkHref: "string",
74
+ em: "em",
75
+ strong: "strong",
76
+ strikethrough: "strikethrough",
77
+ emoji: "builtin"
78
+ };
79
+
80
+ for (var tokenType in tokenTypes) {
81
+ if (tokenTypes.hasOwnProperty(tokenType) && modeCfg.tokenTypeOverrides[tokenType]) {
82
+ tokenTypes[tokenType] = modeCfg.tokenTypeOverrides[tokenType];
83
+ }
84
+ }
85
+
86
+ var hrRE = /^([*\-_])(?:\s*\1){2,}\s*$/
87
+ , listRE = /^(?:[*\-+]|^[0-9]+([.)]))\s+/
88
+ , taskListRE = /^\[(x| )\](?=\s)/i // Must follow listRE
89
+ , atxHeaderRE = modeCfg.allowAtxHeaderWithoutSpace ? /^(#+)/ : /^(#+)(?: |$)/
90
+ , setextHeaderRE = /^ *(?:\={1,}|-{1,})\s*$/
91
+ , textRE = /^[^#!\[\]*_\\<>` "'(~:]+/
92
+ , fencedCodeRE = /^(~~~+|```+)[ \t]*([\w+#-]*)[^\n`]*$/
93
+ , linkDefRE = /^\s*\[[^\]]+?\]:.*$/ // naive link-definition
94
+ , punctuation = /[!"#$%&'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~\xA1\xA7\xAB\xB6\xB7\xBB\xBF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u0AF0\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2010-\u2027\u2030-\u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u2308-\u230B\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E42\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F-\uFF65]|\uD800[\uDD00-\uDD02\uDF9F\uDFD0]|\uD801\uDD6F|\uD802[\uDC57\uDD1F\uDD3F\uDE50-\uDE58\uDE7F\uDEF0-\uDEF6\uDF39-\uDF3F\uDF99-\uDF9C]|\uD804[\uDC47-\uDC4D\uDCBB\uDCBC\uDCBE-\uDCC1\uDD40-\uDD43\uDD74\uDD75\uDDC5-\uDDC9\uDDCD\uDDDB\uDDDD-\uDDDF\uDE38-\uDE3D\uDEA9]|\uD805[\uDCC6\uDDC1-\uDDD7\uDE41-\uDE43\uDF3C-\uDF3E]|\uD809[\uDC70-\uDC74]|\uD81A[\uDE6E\uDE6F\uDEF5\uDF37-\uDF3B\uDF44]|\uD82F\uDC9F|\uD836[\uDE87-\uDE8B]/
95
+ , expandedTab = " " // CommonMark specifies tab as 4 spaces
96
+
97
+ function switchInline(stream, state, f) {
98
+ state.f = state.inline = f;
99
+ return f(stream, state);
100
+ }
101
+
102
+ function switchBlock(stream, state, f) {
103
+ state.f = state.block = f;
104
+ return f(stream, state);
105
+ }
106
+
107
+ function lineIsEmpty(line) {
108
+ return !line || !/\S/.test(line.string)
109
+ }
110
+
111
+ // Blocks
112
+
113
+ function blankLine(state) {
114
+ // Reset linkTitle state
115
+ state.linkTitle = false;
116
+ state.linkHref = false;
117
+ state.linkText = false;
118
+ // Reset EM state
119
+ state.em = false;
120
+ // Reset STRONG state
121
+ state.strong = false;
122
+ // Reset strikethrough state
123
+ state.strikethrough = false;
124
+ // Reset state.quote
125
+ state.quote = 0;
126
+ // Reset state.indentedCode
127
+ state.indentedCode = false;
128
+ if (state.f == htmlBlock) {
129
+ var exit = htmlModeMissing
130
+ if (!exit) {
131
+ var inner = CodeMirror.innerMode(htmlMode, state.htmlState)
132
+ exit = inner.mode.name == "xml" && inner.state.tagStart === null &&
133
+ (!inner.state.context && inner.state.tokenize.isInText)
134
+ }
135
+ if (exit) {
136
+ state.f = inlineNormal;
137
+ state.block = blockNormal;
138
+ state.htmlState = null;
139
+ }
140
+ }
141
+ // Reset state.trailingSpace
142
+ state.trailingSpace = 0;
143
+ state.trailingSpaceNewLine = false;
144
+ // Mark this line as blank
145
+ state.prevLine = state.thisLine
146
+ state.thisLine = {stream: null}
147
+ return null;
148
+ }
149
+
150
+ function blockNormal(stream, state) {
151
+ var firstTokenOnLine = stream.column() === state.indentation;
152
+ var prevLineLineIsEmpty = lineIsEmpty(state.prevLine.stream);
153
+ var prevLineIsIndentedCode = state.indentedCode;
154
+ var prevLineIsHr = state.prevLine.hr;
155
+ var prevLineIsList = state.list !== false;
156
+ var maxNonCodeIndentation = (state.listStack[state.listStack.length - 1] || 0) + 3;
157
+
158
+ state.indentedCode = false;
159
+
160
+ var lineIndentation = state.indentation;
161
+ // compute once per line (on first token)
162
+ if (state.indentationDiff === null) {
163
+ state.indentationDiff = state.indentation;
164
+ if (prevLineIsList) {
165
+ // Reset inline styles which shouldn't propagate aross list items
166
+ state.em = false;
167
+ state.strong = false;
168
+ state.code = false;
169
+ state.strikethrough = false;
170
+
171
+ state.list = null;
172
+ // While this list item's marker's indentation is less than the deepest
173
+ // list item's content's indentation,pop the deepest list item
174
+ // indentation off the stack, and update block indentation state
175
+ while (lineIndentation < state.listStack[state.listStack.length - 1]) {
176
+ state.listStack.pop();
177
+ if (state.listStack.length) {
178
+ state.indentation = state.listStack[state.listStack.length - 1];
179
+ // less than the first list's indent -> the line is no longer a list
180
+ } else {
181
+ state.list = false;
182
+ }
183
+ }
184
+ if (state.list !== false) {
185
+ state.indentationDiff = lineIndentation - state.listStack[state.listStack.length - 1]
186
+ }
187
+ }
188
+ }
189
+
190
+ // not comprehensive (currently only for setext detection purposes)
191
+ var allowsInlineContinuation = (
192
+ !prevLineLineIsEmpty && !prevLineIsHr && !state.prevLine.header &&
193
+ (!prevLineIsList || !prevLineIsIndentedCode) &&
194
+ !state.prevLine.fencedCodeEnd
195
+ );
196
+
197
+ var isHr = (state.list === false || prevLineIsHr || prevLineLineIsEmpty) &&
198
+ state.indentation <= maxNonCodeIndentation && stream.match(hrRE);
199
+
200
+ var match = null;
201
+ if (state.indentationDiff >= 4 && (prevLineIsIndentedCode || state.prevLine.fencedCodeEnd ||
202
+ state.prevLine.header || prevLineLineIsEmpty)) {
203
+ stream.skipToEnd();
204
+ state.indentedCode = true;
205
+ return tokenTypes.code;
206
+ } else if (stream.eatSpace()) {
207
+ return null;
208
+ } else if (firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(atxHeaderRE)) && match[1].length <= 6) {
209
+ state.quote = 0;
210
+ state.header = match[1].length;
211
+ state.thisLine.header = true;
212
+ if (modeCfg.highlightFormatting) state.formatting = "header";
213
+ state.f = state.inline;
214
+ return getType(state);
215
+ } else if (state.indentation <= maxNonCodeIndentation && stream.eat('>')) {
216
+ state.quote = firstTokenOnLine ? 1 : state.quote + 1;
217
+ if (modeCfg.highlightFormatting) state.formatting = "quote";
218
+ stream.eatSpace();
219
+ return getType(state);
220
+ } else if (!isHr && !state.setext && firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(listRE))) {
221
+ var listType = match[1] ? "ol" : "ul";
222
+
223
+ state.indentation = lineIndentation + stream.current().length;
224
+ state.list = true;
225
+ state.quote = 0;
226
+
227
+ // Add this list item's content's indentation to the stack
228
+ state.listStack.push(state.indentation);
229
+
230
+ if (modeCfg.taskLists && stream.match(taskListRE, false)) {
231
+ state.taskList = true;
232
+ }
233
+ state.f = state.inline;
234
+ if (modeCfg.highlightFormatting) state.formatting = ["list", "list-" + listType];
235
+ return getType(state);
236
+ } else if (firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(fencedCodeRE, true))) {
237
+ state.quote = 0;
238
+ state.fencedEndRE = new RegExp(match[1] + "+ *$");
239
+ // try switching mode
240
+ state.localMode = modeCfg.fencedCodeBlockHighlighting && getMode(match[2]);
241
+ if (state.localMode) state.localState = CodeMirror.startState(state.localMode);
242
+ state.f = state.block = local;
243
+ if (modeCfg.highlightFormatting) state.formatting = "code-block";
244
+ state.code = -1
245
+ return getType(state);
246
+ // SETEXT has lowest block-scope precedence after HR, so check it after
247
+ // the others (code, blockquote, list...)
248
+ } else if (
249
+ // if setext set, indicates line after ---/===
250
+ state.setext || (
251
+ // line before ---/===
252
+ (!allowsInlineContinuation || !prevLineIsList) && !state.quote && state.list === false &&
253
+ !state.code && !isHr && !linkDefRE.test(stream.string) &&
254
+ (match = stream.lookAhead(1)) && (match = match.match(setextHeaderRE))
255
+ )
256
+ ) {
257
+ if ( !state.setext ) {
258
+ state.header = match[0].charAt(0) == '=' ? 1 : 2;
259
+ state.setext = state.header;
260
+ } else {
261
+ state.header = state.setext;
262
+ // has no effect on type so we can reset it now
263
+ state.setext = 0;
264
+ stream.skipToEnd();
265
+ if (modeCfg.highlightFormatting) state.formatting = "header";
266
+ }
267
+ state.thisLine.header = true;
268
+ state.f = state.inline;
269
+ return getType(state);
270
+ } else if (isHr) {
271
+ stream.skipToEnd();
272
+ state.hr = true;
273
+ state.thisLine.hr = true;
274
+ return tokenTypes.hr;
275
+ } else if (stream.peek() === '[') {
276
+ return switchInline(stream, state, footnoteLink);
277
+ }
278
+
279
+ return switchInline(stream, state, state.inline);
280
+ }
281
+
282
+ function htmlBlock(stream, state) {
283
+ var style = htmlMode.token(stream, state.htmlState);
284
+ if (!htmlModeMissing) {
285
+ var inner = CodeMirror.innerMode(htmlMode, state.htmlState)
286
+ if ((inner.mode.name == "xml" && inner.state.tagStart === null &&
287
+ (!inner.state.context && inner.state.tokenize.isInText)) ||
288
+ (state.md_inside && stream.current().indexOf(">") > -1)) {
289
+ state.f = inlineNormal;
290
+ state.block = blockNormal;
291
+ state.htmlState = null;
292
+ }
293
+ }
294
+ return style;
295
+ }
296
+
297
+ function local(stream, state) {
298
+ var currListInd = state.listStack[state.listStack.length - 1] || 0;
299
+ var hasExitedList = state.indentation < currListInd;
300
+ var maxFencedEndInd = currListInd + 3;
301
+ if (state.fencedEndRE && state.indentation <= maxFencedEndInd && (hasExitedList || stream.match(state.fencedEndRE))) {
302
+ if (modeCfg.highlightFormatting) state.formatting = "code-block";
303
+ var returnType;
304
+ if (!hasExitedList) returnType = getType(state)
305
+ state.localMode = state.localState = null;
306
+ state.block = blockNormal;
307
+ state.f = inlineNormal;
308
+ state.fencedEndRE = null;
309
+ state.code = 0
310
+ state.thisLine.fencedCodeEnd = true;
311
+ if (hasExitedList) return switchBlock(stream, state, state.block);
312
+ return returnType;
313
+ } else if (state.localMode) {
314
+ return state.localMode.token(stream, state.localState);
315
+ } else {
316
+ stream.skipToEnd();
317
+ return tokenTypes.code;
318
+ }
319
+ }
320
+
321
+ // Inline
322
+ function getType(state) {
323
+ var styles = [];
324
+
325
+ if (state.formatting) {
326
+ styles.push(tokenTypes.formatting);
327
+
328
+ if (typeof state.formatting === "string") state.formatting = [state.formatting];
329
+
330
+ for (var i = 0; i < state.formatting.length; i++) {
331
+ styles.push(tokenTypes.formatting + "-" + state.formatting[i]);
332
+
333
+ if (state.formatting[i] === "header") {
334
+ styles.push(tokenTypes.formatting + "-" + state.formatting[i] + "-" + state.header);
335
+ }
336
+
337
+ // Add `formatting-quote` and `formatting-quote-#` for blockquotes
338
+ // Add `error` instead if the maximum blockquote nesting depth is passed
339
+ if (state.formatting[i] === "quote") {
340
+ if (!modeCfg.maxBlockquoteDepth || modeCfg.maxBlockquoteDepth >= state.quote) {
341
+ styles.push(tokenTypes.formatting + "-" + state.formatting[i] + "-" + state.quote);
342
+ } else {
343
+ styles.push("error");
344
+ }
345
+ }
346
+ }
347
+ }
348
+
349
+ if (state.taskOpen) {
350
+ styles.push("meta");
351
+ return styles.length ? styles.join(' ') : null;
352
+ }
353
+ if (state.taskClosed) {
354
+ styles.push("property");
355
+ return styles.length ? styles.join(' ') : null;
356
+ }
357
+
358
+ if (state.linkHref) {
359
+ styles.push(tokenTypes.linkHref, "url");
360
+ } else { // Only apply inline styles to non-url text
361
+ if (state.strong) { styles.push(tokenTypes.strong); }
362
+ if (state.em) { styles.push(tokenTypes.em); }
363
+ if (state.strikethrough) { styles.push(tokenTypes.strikethrough); }
364
+ if (state.emoji) { styles.push(tokenTypes.emoji); }
365
+ if (state.linkText) { styles.push(tokenTypes.linkText); }
366
+ if (state.code) { styles.push(tokenTypes.code); }
367
+ if (state.image) { styles.push(tokenTypes.image); }
368
+ if (state.imageAltText) { styles.push(tokenTypes.imageAltText, "link"); }
369
+ if (state.imageMarker) { styles.push(tokenTypes.imageMarker); }
370
+ }
371
+
372
+ if (state.header) { styles.push(tokenTypes.header, tokenTypes.header + "-" + state.header); }
373
+
374
+ if (state.quote) {
375
+ styles.push(tokenTypes.quote);
376
+
377
+ // Add `quote-#` where the maximum for `#` is modeCfg.maxBlockquoteDepth
378
+ if (!modeCfg.maxBlockquoteDepth || modeCfg.maxBlockquoteDepth >= state.quote) {
379
+ styles.push(tokenTypes.quote + "-" + state.quote);
380
+ } else {
381
+ styles.push(tokenTypes.quote + "-" + modeCfg.maxBlockquoteDepth);
382
+ }
383
+ }
384
+
385
+ if (state.list !== false) {
386
+ var listMod = (state.listStack.length - 1) % 3;
387
+ if (!listMod) {
388
+ styles.push(tokenTypes.list1);
389
+ } else if (listMod === 1) {
390
+ styles.push(tokenTypes.list2);
391
+ } else {
392
+ styles.push(tokenTypes.list3);
393
+ }
394
+ }
395
+
396
+ if (state.trailingSpaceNewLine) {
397
+ styles.push("trailing-space-new-line");
398
+ } else if (state.trailingSpace) {
399
+ styles.push("trailing-space-" + (state.trailingSpace % 2 ? "a" : "b"));
400
+ }
401
+
402
+ return styles.length ? styles.join(' ') : null;
403
+ }
404
+
405
+ function handleText(stream, state) {
406
+ if (stream.match(textRE, true)) {
407
+ return getType(state);
408
+ }
409
+ return undefined;
410
+ }
411
+
412
+ function inlineNormal(stream, state) {
413
+ var style = state.text(stream, state);
414
+ if (typeof style !== 'undefined')
415
+ return style;
416
+
417
+ if (state.list) { // List marker (*, +, -, 1., etc)
418
+ state.list = null;
419
+ return getType(state);
420
+ }
421
+
422
+ if (state.taskList) {
423
+ var taskOpen = stream.match(taskListRE, true)[1] === " ";
424
+ if (taskOpen) state.taskOpen = true;
425
+ else state.taskClosed = true;
426
+ if (modeCfg.highlightFormatting) state.formatting = "task";
427
+ state.taskList = false;
428
+ return getType(state);
429
+ }
430
+
431
+ state.taskOpen = false;
432
+ state.taskClosed = false;
433
+
434
+ if (state.header && stream.match(/^#+$/, true)) {
435
+ if (modeCfg.highlightFormatting) state.formatting = "header";
436
+ return getType(state);
437
+ }
438
+
439
+ var ch = stream.next();
440
+
441
+ // Matches link titles present on next line
442
+ if (state.linkTitle) {
443
+ state.linkTitle = false;
444
+ var matchCh = ch;
445
+ if (ch === '(') {
446
+ matchCh = ')';
447
+ }
448
+ matchCh = (matchCh+'').replace(/([.?*+^\[\]\\(){}|-])/g, "\\$1");
449
+ var regex = '^\\s*(?:[^' + matchCh + '\\\\]+|\\\\\\\\|\\\\.)' + matchCh;
450
+ if (stream.match(new RegExp(regex), true)) {
451
+ return tokenTypes.linkHref;
452
+ }
453
+ }
454
+
455
+ // If this block is changed, it may need to be updated in GFM mode
456
+ if (ch === '`') {
457
+ var previousFormatting = state.formatting;
458
+ if (modeCfg.highlightFormatting) state.formatting = "code";
459
+ stream.eatWhile('`');
460
+ var count = stream.current().length
461
+ if (state.code == 0 && (!state.quote || count == 1)) {
462
+ state.code = count
463
+ return getType(state)
464
+ } else if (count == state.code) { // Must be exact
465
+ var t = getType(state)
466
+ state.code = 0
467
+ return t
468
+ } else {
469
+ state.formatting = previousFormatting
470
+ return getType(state)
471
+ }
472
+ } else if (state.code) {
473
+ return getType(state);
474
+ }
475
+
476
+ if (ch === '\\') {
477
+ stream.next();
478
+ if (modeCfg.highlightFormatting) {
479
+ var type = getType(state);
480
+ var formattingEscape = tokenTypes.formatting + "-escape";
481
+ return type ? type + " " + formattingEscape : formattingEscape;
482
+ }
483
+ }
484
+
485
+ if (ch === '!' && stream.match(/\[[^\]]*\] ?(?:\(|\[)/, false)) {
486
+ state.imageMarker = true;
487
+ state.image = true;
488
+ if (modeCfg.highlightFormatting) state.formatting = "image";
489
+ return getType(state);
490
+ }
491
+
492
+ if (ch === '[' && state.imageMarker && stream.match(/[^\]]*\](\(.*?\)| ?\[.*?\])/, false)) {
493
+ state.imageMarker = false;
494
+ state.imageAltText = true
495
+ if (modeCfg.highlightFormatting) state.formatting = "image";
496
+ return getType(state);
497
+ }
498
+
499
+ if (ch === ']' && state.imageAltText) {
500
+ if (modeCfg.highlightFormatting) state.formatting = "image";
501
+ var type = getType(state);
502
+ state.imageAltText = false;
503
+ state.image = false;
504
+ state.inline = state.f = linkHref;
505
+ return type;
506
+ }
507
+
508
+ if (ch === '[' && !state.image) {
509
+ if (state.linkText && stream.match(/^.*?\]/)) return getType(state)
510
+ state.linkText = true;
511
+ if (modeCfg.highlightFormatting) state.formatting = "link";
512
+ return getType(state);
513
+ }
514
+
515
+ if (ch === ']' && state.linkText) {
516
+ if (modeCfg.highlightFormatting) state.formatting = "link";
517
+ var type = getType(state);
518
+ state.linkText = false;
519
+ state.inline = state.f = stream.match(/\(.*?\)| ?\[.*?\]/, false) ? linkHref : inlineNormal
520
+ return type;
521
+ }
522
+
523
+ if (ch === '<' && stream.match(/^(https?|ftps?):\/\/(?:[^\\>]|\\.)+>/, false)) {
524
+ state.f = state.inline = linkInline;
525
+ if (modeCfg.highlightFormatting) state.formatting = "link";
526
+ var type = getType(state);
527
+ if (type){
528
+ type += " ";
529
+ } else {
530
+ type = "";
531
+ }
532
+ return type + tokenTypes.linkInline;
533
+ }
534
+
535
+ if (ch === '<' && stream.match(/^[^> \\]+@(?:[^\\>]|\\.)+>/, false)) {
536
+ state.f = state.inline = linkInline;
537
+ if (modeCfg.highlightFormatting) state.formatting = "link";
538
+ var type = getType(state);
539
+ if (type){
540
+ type += " ";
541
+ } else {
542
+ type = "";
543
+ }
544
+ return type + tokenTypes.linkEmail;
545
+ }
546
+
547
+ if (modeCfg.xml && ch === '<' && stream.match(/^(!--|\?|!\[CDATA\[|[a-z][a-z0-9-]*(?:\s+[a-z_:.\-]+(?:\s*=\s*[^>]+)?)*\s*(?:>|$))/i, false)) {
548
+ var end = stream.string.indexOf(">", stream.pos);
549
+ if (end != -1) {
550
+ var atts = stream.string.substring(stream.start, end);
551
+ if (/markdown\s*=\s*('|"){0,1}1('|"){0,1}/.test(atts)) state.md_inside = true;
552
+ }
553
+ stream.backUp(1);
554
+ state.htmlState = CodeMirror.startState(htmlMode);
555
+ return switchBlock(stream, state, htmlBlock);
556
+ }
557
+
558
+ if (modeCfg.xml && ch === '<' && stream.match(/^\/\w*?>/)) {
559
+ state.md_inside = false;
560
+ return "tag";
561
+ } else if (ch === "*" || ch === "_") {
562
+ var len = 1, before = stream.pos == 1 ? " " : stream.string.charAt(stream.pos - 2)
563
+ while (len < 3 && stream.eat(ch)) len++
564
+ var after = stream.peek() || " "
565
+ // See http://spec.commonmark.org/0.27/#emphasis-and-strong-emphasis
566
+ var leftFlanking = !/\s/.test(after) && (!punctuation.test(after) || /\s/.test(before) || punctuation.test(before))
567
+ var rightFlanking = !/\s/.test(before) && (!punctuation.test(before) || /\s/.test(after) || punctuation.test(after))
568
+ var setEm = null, setStrong = null
569
+ if (len % 2) { // Em
570
+ if (!state.em && leftFlanking && (ch === "*" || !rightFlanking || punctuation.test(before)))
571
+ setEm = true
572
+ else if (state.em == ch && rightFlanking && (ch === "*" || !leftFlanking || punctuation.test(after)))
573
+ setEm = false
574
+ }
575
+ if (len > 1) { // Strong
576
+ if (!state.strong && leftFlanking && (ch === "*" || !rightFlanking || punctuation.test(before)))
577
+ setStrong = true
578
+ else if (state.strong == ch && rightFlanking && (ch === "*" || !leftFlanking || punctuation.test(after)))
579
+ setStrong = false
580
+ }
581
+ if (setStrong != null || setEm != null) {
582
+ if (modeCfg.highlightFormatting) state.formatting = setEm == null ? "strong" : setStrong == null ? "em" : "strong em"
583
+ if (setEm === true) state.em = ch
584
+ if (setStrong === true) state.strong = ch
585
+ var t = getType(state)
586
+ if (setEm === false) state.em = false
587
+ if (setStrong === false) state.strong = false
588
+ return t
589
+ }
590
+ } else if (ch === ' ') {
591
+ if (stream.eat('*') || stream.eat('_')) { // Probably surrounded by spaces
592
+ if (stream.peek() === ' ') { // Surrounded by spaces, ignore
593
+ return getType(state);
594
+ } else { // Not surrounded by spaces, back up pointer
595
+ stream.backUp(1);
596
+ }
597
+ }
598
+ }
599
+
600
+ if (modeCfg.strikethrough) {
601
+ if (ch === '~' && stream.eatWhile(ch)) {
602
+ if (state.strikethrough) {// Remove strikethrough
603
+ if (modeCfg.highlightFormatting) state.formatting = "strikethrough";
604
+ var t = getType(state);
605
+ state.strikethrough = false;
606
+ return t;
607
+ } else if (stream.match(/^[^\s]/, false)) {// Add strikethrough
608
+ state.strikethrough = true;
609
+ if (modeCfg.highlightFormatting) state.formatting = "strikethrough";
610
+ return getType(state);
611
+ }
612
+ } else if (ch === ' ') {
613
+ if (stream.match(/^~~/, true)) { // Probably surrounded by space
614
+ if (stream.peek() === ' ') { // Surrounded by spaces, ignore
615
+ return getType(state);
616
+ } else { // Not surrounded by spaces, back up pointer
617
+ stream.backUp(2);
618
+ }
619
+ }
620
+ }
621
+ }
622
+
623
+ if (modeCfg.emoji && ch === ":" && stream.match(/^(?:[a-z_\d+][a-z_\d+-]*|\-[a-z_\d+][a-z_\d+-]*):/)) {
624
+ state.emoji = true;
625
+ if (modeCfg.highlightFormatting) state.formatting = "emoji";
626
+ var retType = getType(state);
627
+ state.emoji = false;
628
+ return retType;
629
+ }
630
+
631
+ if (ch === ' ') {
632
+ if (stream.match(/^ +$/, false)) {
633
+ state.trailingSpace++;
634
+ } else if (state.trailingSpace) {
635
+ state.trailingSpaceNewLine = true;
636
+ }
637
+ }
638
+
639
+ return getType(state);
640
+ }
641
+
642
+ function linkInline(stream, state) {
643
+ var ch = stream.next();
644
+
645
+ if (ch === ">") {
646
+ state.f = state.inline = inlineNormal;
647
+ if (modeCfg.highlightFormatting) state.formatting = "link";
648
+ var type = getType(state);
649
+ if (type){
650
+ type += " ";
651
+ } else {
652
+ type = "";
653
+ }
654
+ return type + tokenTypes.linkInline;
655
+ }
656
+
657
+ stream.match(/^[^>]+/, true);
658
+
659
+ return tokenTypes.linkInline;
660
+ }
661
+
662
+ function linkHref(stream, state) {
663
+ // Check if space, and return NULL if so (to avoid marking the space)
664
+ if(stream.eatSpace()){
665
+ return null;
666
+ }
667
+ var ch = stream.next();
668
+ if (ch === '(' || ch === '[') {
669
+ state.f = state.inline = getLinkHrefInside(ch === "(" ? ")" : "]");
670
+ if (modeCfg.highlightFormatting) state.formatting = "link-string";
671
+ state.linkHref = true;
672
+ return getType(state);
673
+ }
674
+ return 'error';
675
+ }
676
+
677
+ var linkRE = {
678
+ ")": /^(?:[^\\\(\)]|\\.|\((?:[^\\\(\)]|\\.)*\))*?(?=\))/,
679
+ "]": /^(?:[^\\\[\]]|\\.|\[(?:[^\\\[\]]|\\.)*\])*?(?=\])/
680
+ }
681
+
682
+ function getLinkHrefInside(endChar) {
683
+ return function(stream, state) {
684
+ var ch = stream.next();
685
+
686
+ if (ch === endChar) {
687
+ state.f = state.inline = inlineNormal;
688
+ if (modeCfg.highlightFormatting) state.formatting = "link-string";
689
+ var returnState = getType(state);
690
+ state.linkHref = false;
691
+ return returnState;
692
+ }
693
+
694
+ stream.match(linkRE[endChar])
695
+ state.linkHref = true;
696
+ return getType(state);
697
+ };
698
+ }
699
+
700
+ function footnoteLink(stream, state) {
701
+ if (stream.match(/^([^\]\\]|\\.)*\]:/, false)) {
702
+ state.f = footnoteLinkInside;
703
+ stream.next(); // Consume [
704
+ if (modeCfg.highlightFormatting) state.formatting = "link";
705
+ state.linkText = true;
706
+ return getType(state);
707
+ }
708
+ return switchInline(stream, state, inlineNormal);
709
+ }
710
+
711
+ function footnoteLinkInside(stream, state) {
712
+ if (stream.match(/^\]:/, true)) {
713
+ state.f = state.inline = footnoteUrl;
714
+ if (modeCfg.highlightFormatting) state.formatting = "link";
715
+ var returnType = getType(state);
716
+ state.linkText = false;
717
+ return returnType;
718
+ }
719
+
720
+ stream.match(/^([^\]\\]|\\.)+/, true);
721
+
722
+ return tokenTypes.linkText;
723
+ }
724
+
725
+ function footnoteUrl(stream, state) {
726
+ // Check if space, and return NULL if so (to avoid marking the space)
727
+ if(stream.eatSpace()){
728
+ return null;
729
+ }
730
+ // Match URL
731
+ stream.match(/^[^\s]+/, true);
732
+ // Check for link title
733
+ if (stream.peek() === undefined) { // End of line, set flag to check next line
734
+ state.linkTitle = true;
735
+ } else { // More content on line, check if link title
736
+ stream.match(/^(?:\s+(?:"(?:[^"\\]|\\\\|\\.)+"|'(?:[^'\\]|\\\\|\\.)+'|\((?:[^)\\]|\\\\|\\.)+\)))?/, true);
737
+ }
738
+ state.f = state.inline = inlineNormal;
739
+ return tokenTypes.linkHref + " url";
740
+ }
741
+
742
+ var mode = {
743
+ startState: function() {
744
+ return {
745
+ f: blockNormal,
746
+
747
+ prevLine: {stream: null},
748
+ thisLine: {stream: null},
749
+
750
+ block: blockNormal,
751
+ htmlState: null,
752
+ indentation: 0,
753
+
754
+ inline: inlineNormal,
755
+ text: handleText,
756
+
757
+ formatting: false,
758
+ linkText: false,
759
+ linkHref: false,
760
+ linkTitle: false,
761
+ code: 0,
762
+ em: false,
763
+ strong: false,
764
+ header: 0,
765
+ setext: 0,
766
+ hr: false,
767
+ taskList: false,
768
+ list: false,
769
+ listStack: [],
770
+ quote: 0,
771
+ trailingSpace: 0,
772
+ trailingSpaceNewLine: false,
773
+ strikethrough: false,
774
+ emoji: false,
775
+ fencedEndRE: null
776
+ };
777
+ },
778
+
779
+ copyState: function(s) {
780
+ return {
781
+ f: s.f,
782
+
783
+ prevLine: s.prevLine,
784
+ thisLine: s.thisLine,
785
+
786
+ block: s.block,
787
+ htmlState: s.htmlState && CodeMirror.copyState(htmlMode, s.htmlState),
788
+ indentation: s.indentation,
789
+
790
+ localMode: s.localMode,
791
+ localState: s.localMode ? CodeMirror.copyState(s.localMode, s.localState) : null,
792
+
793
+ inline: s.inline,
794
+ text: s.text,
795
+ formatting: false,
796
+ linkText: s.linkText,
797
+ linkTitle: s.linkTitle,
798
+ linkHref: s.linkHref,
799
+ code: s.code,
800
+ em: s.em,
801
+ strong: s.strong,
802
+ strikethrough: s.strikethrough,
803
+ emoji: s.emoji,
804
+ header: s.header,
805
+ setext: s.setext,
806
+ hr: s.hr,
807
+ taskList: s.taskList,
808
+ list: s.list,
809
+ listStack: s.listStack.slice(0),
810
+ quote: s.quote,
811
+ indentedCode: s.indentedCode,
812
+ trailingSpace: s.trailingSpace,
813
+ trailingSpaceNewLine: s.trailingSpaceNewLine,
814
+ md_inside: s.md_inside,
815
+ fencedEndRE: s.fencedEndRE
816
+ };
817
+ },
818
+
819
+ token: function(stream, state) {
820
+
821
+ // Reset state.formatting
822
+ state.formatting = false;
823
+
824
+ if (stream != state.thisLine.stream) {
825
+ state.header = 0;
826
+ state.hr = false;
827
+
828
+ if (stream.match(/^\s*$/, true)) {
829
+ blankLine(state);
830
+ return null;
831
+ }
832
+
833
+ state.prevLine = state.thisLine
834
+ state.thisLine = {stream: stream}
835
+
836
+ // Reset state.taskList
837
+ state.taskList = false;
838
+
839
+ // Reset state.trailingSpace
840
+ state.trailingSpace = 0;
841
+ state.trailingSpaceNewLine = false;
842
+
843
+ if (!state.localState) {
844
+ state.f = state.block;
845
+ if (state.f != htmlBlock) {
846
+ var indentation = stream.match(/^\s*/, true)[0].replace(/\t/g, expandedTab).length;
847
+ state.indentation = indentation;
848
+ state.indentationDiff = null;
849
+ if (indentation > 0) return null;
850
+ }
851
+ }
852
+ }
853
+ return state.f(stream, state);
854
+ },
855
+
856
+ innerMode: function(state) {
857
+ if (state.block == htmlBlock) return {state: state.htmlState, mode: htmlMode};
858
+ if (state.localState) return {state: state.localState, mode: state.localMode};
859
+ return {state: state, mode: mode};
860
+ },
861
+
862
+ indent: function(state, textAfter, line) {
863
+ if (state.block == htmlBlock && htmlMode.indent) return htmlMode.indent(state.htmlState, textAfter, line)
864
+ if (state.localState && state.localMode.indent) return state.localMode.indent(state.localState, textAfter, line)
865
+ return CodeMirror.Pass
866
+ },
867
+
868
+ blankLine: blankLine,
869
+
870
+ getType: getType,
871
+
872
+ blockCommentStart: "<!--",
873
+ blockCommentEnd: "-->",
874
+ closeBrackets: "()[]{}''\"\"``",
875
+ fold: "markdown"
876
+ };
877
+ return mode;
878
+ }, "xml");
879
+
880
+ CodeMirror.defineMIME("text/markdown", "markdown");
881
+
882
+ CodeMirror.defineMIME("text/x-markdown", "markdown");
883
+
884
+ });