| 776 |
lars |
1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
2 |
// Distributed under an MIT license: http://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 htmlFound = CodeMirror.modes.hasOwnProperty("xml");
|
|
|
17 |
var htmlMode = CodeMirror.getMode(cmCfg, htmlFound ? {name: "xml", htmlMode: true} : "text/plain");
|
|
|
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 |
// Should underscores in words open/close em/strong?
|
|
|
39 |
if (modeCfg.underscoresBreakWords === undefined)
|
|
|
40 |
modeCfg.underscoresBreakWords = true;
|
|
|
41 |
|
|
|
42 |
// Turn on fenced code blocks? ("```" to start/end)
|
|
|
43 |
if (modeCfg.fencedCodeBlocks === undefined) modeCfg.fencedCodeBlocks = false;
|
|
|
44 |
|
|
|
45 |
// Turn on task lists? ("- [ ] " and "- [x] ")
|
|
|
46 |
if (modeCfg.taskLists === undefined) modeCfg.taskLists = false;
|
|
|
47 |
|
|
|
48 |
// Turn on strikethrough syntax
|
|
|
49 |
if (modeCfg.strikethrough === undefined)
|
|
|
50 |
modeCfg.strikethrough = false;
|
|
|
51 |
|
|
|
52 |
var codeDepth = 0;
|
|
|
53 |
|
|
|
54 |
var header = 'header'
|
|
|
55 |
, code = 'comment'
|
|
|
56 |
, quote = 'quote'
|
|
|
57 |
, list1 = 'variable-2'
|
|
|
58 |
, list2 = 'variable-3'
|
|
|
59 |
, list3 = 'keyword'
|
|
|
60 |
, hr = 'hr'
|
|
|
61 |
, image = 'tag'
|
|
|
62 |
, formatting = 'formatting'
|
|
|
63 |
, linkinline = 'link'
|
|
|
64 |
, linkemail = 'link'
|
|
|
65 |
, linktext = 'link'
|
|
|
66 |
, linkhref = 'string'
|
|
|
67 |
, em = 'em'
|
|
|
68 |
, strong = 'strong'
|
|
|
69 |
, strikethrough = 'strikethrough';
|
|
|
70 |
|
|
|
71 |
var hrRE = /^([*\-_])(?:\s*\1){2,}\s*$/
|
|
|
72 |
, ulRE = /^[*\-+]\s+/
|
|
|
73 |
, olRE = /^[0-9]+([.)])\s+/
|
|
|
74 |
, taskListRE = /^\[(x| )\](?=\s)/ // Must follow ulRE or olRE
|
|
|
75 |
, atxHeaderRE = /^(#+)(?: |$)/
|
|
|
76 |
, setextHeaderRE = /^ *(?:\={1,}|-{1,})\s*$/
|
|
|
77 |
, textRE = /^[^#!\[\]*_\\<>` "'(~]+/;
|
|
|
78 |
|
|
|
79 |
function switchInline(stream, state, f) {
|
|
|
80 |
state.f = state.inline = f;
|
|
|
81 |
return f(stream, state);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
function switchBlock(stream, state, f) {
|
|
|
85 |
state.f = state.block = f;
|
|
|
86 |
return f(stream, state);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
// Blocks
|
|
|
91 |
|
|
|
92 |
function blankLine(state) {
|
|
|
93 |
// Reset linkTitle state
|
|
|
94 |
state.linkTitle = false;
|
|
|
95 |
// Reset EM state
|
|
|
96 |
state.em = false;
|
|
|
97 |
// Reset STRONG state
|
|
|
98 |
state.strong = false;
|
|
|
99 |
// Reset strikethrough state
|
|
|
100 |
state.strikethrough = false;
|
|
|
101 |
// Reset state.quote
|
|
|
102 |
state.quote = 0;
|
|
|
103 |
// Reset state.indentedCode
|
|
|
104 |
state.indentedCode = false;
|
|
|
105 |
if (!htmlFound && state.f == htmlBlock) {
|
|
|
106 |
state.f = inlineNormal;
|
|
|
107 |
state.block = blockNormal;
|
|
|
108 |
}
|
|
|
109 |
// Reset state.trailingSpace
|
|
|
110 |
state.trailingSpace = 0;
|
|
|
111 |
state.trailingSpaceNewLine = false;
|
|
|
112 |
// Mark this line as blank
|
|
|
113 |
state.thisLineHasContent = false;
|
|
|
114 |
return null;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
function blockNormal(stream, state) {
|
|
|
118 |
|
|
|
119 |
var sol = stream.sol();
|
|
|
120 |
|
|
|
121 |
var prevLineIsList = state.list !== false,
|
|
|
122 |
prevLineIsIndentedCode = state.indentedCode;
|
|
|
123 |
|
|
|
124 |
state.indentedCode = false;
|
|
|
125 |
|
|
|
126 |
if (prevLineIsList) {
|
|
|
127 |
if (state.indentationDiff >= 0) { // Continued list
|
|
|
128 |
if (state.indentationDiff < 4) { // Only adjust indentation if *not* a code block
|
|
|
129 |
state.indentation -= state.indentationDiff;
|
|
|
130 |
}
|
|
|
131 |
state.list = null;
|
|
|
132 |
} else if (state.indentation > 0) {
|
|
|
133 |
state.list = null;
|
|
|
134 |
state.listDepth = Math.floor(state.indentation / 4);
|
|
|
135 |
} else { // No longer a list
|
|
|
136 |
state.list = false;
|
|
|
137 |
state.listDepth = 0;
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
var match = null;
|
|
|
142 |
if (state.indentationDiff >= 4) {
|
|
|
143 |
stream.skipToEnd();
|
|
|
144 |
if (prevLineIsIndentedCode || !state.prevLineHasContent) {
|
|
|
145 |
state.indentation -= 4;
|
|
|
146 |
state.indentedCode = true;
|
|
|
147 |
return code;
|
|
|
148 |
} else {
|
|
|
149 |
return null;
|
|
|
150 |
}
|
|
|
151 |
} else if (stream.eatSpace()) {
|
|
|
152 |
return null;
|
|
|
153 |
} else if ((match = stream.match(atxHeaderRE)) && match[1].length <= 6) {
|
|
|
154 |
state.header = match[1].length;
|
|
|
155 |
if (modeCfg.highlightFormatting) state.formatting = "header";
|
|
|
156 |
state.f = state.inline;
|
|
|
157 |
return getType(state);
|
|
|
158 |
} else if (state.prevLineHasContent && !state.quote && !prevLineIsList && !prevLineIsIndentedCode && (match = stream.match(setextHeaderRE))) {
|
|
|
159 |
state.header = match[0].charAt(0) == '=' ? 1 : 2;
|
|
|
160 |
if (modeCfg.highlightFormatting) state.formatting = "header";
|
|
|
161 |
state.f = state.inline;
|
|
|
162 |
return getType(state);
|
|
|
163 |
} else if (stream.eat('>')) {
|
|
|
164 |
state.quote = sol ? 1 : state.quote + 1;
|
|
|
165 |
if (modeCfg.highlightFormatting) state.formatting = "quote";
|
|
|
166 |
stream.eatSpace();
|
|
|
167 |
return getType(state);
|
|
|
168 |
} else if (stream.peek() === '[') {
|
|
|
169 |
return switchInline(stream, state, footnoteLink);
|
|
|
170 |
} else if (stream.match(hrRE, true)) {
|
|
|
171 |
state.hr = true;
|
|
|
172 |
return hr;
|
|
|
173 |
} else if ((!state.prevLineHasContent || prevLineIsList) && (stream.match(ulRE, false) || stream.match(olRE, false))) {
|
|
|
174 |
var listType = null;
|
|
|
175 |
if (stream.match(ulRE, true)) {
|
|
|
176 |
listType = 'ul';
|
|
|
177 |
} else {
|
|
|
178 |
stream.match(olRE, true);
|
|
|
179 |
listType = 'ol';
|
|
|
180 |
}
|
|
|
181 |
state.indentation += 4;
|
|
|
182 |
state.list = true;
|
|
|
183 |
state.listDepth++;
|
|
|
184 |
if (modeCfg.taskLists && stream.match(taskListRE, false)) {
|
|
|
185 |
state.taskList = true;
|
|
|
186 |
}
|
|
|
187 |
state.f = state.inline;
|
|
|
188 |
if (modeCfg.highlightFormatting) state.formatting = ["list", "list-" + listType];
|
|
|
189 |
return getType(state);
|
|
|
190 |
} else if (modeCfg.fencedCodeBlocks && stream.match(/^```[ \t]*([\w+#]*)/, true)) {
|
|
|
191 |
// try switching mode
|
|
|
192 |
state.localMode = getMode(RegExp.$1);
|
|
|
193 |
if (state.localMode) state.localState = state.localMode.startState();
|
|
|
194 |
state.f = state.block = local;
|
|
|
195 |
if (modeCfg.highlightFormatting) state.formatting = "code-block";
|
|
|
196 |
state.code = true;
|
|
|
197 |
return getType(state);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
return switchInline(stream, state, state.inline);
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
function htmlBlock(stream, state) {
|
|
|
204 |
var style = htmlMode.token(stream, state.htmlState);
|
|
|
205 |
if ((htmlFound && state.htmlState.tagStart === null &&
|
|
|
206 |
(!state.htmlState.context && state.htmlState.tokenize.isInText)) ||
|
|
|
207 |
(state.md_inside && stream.current().indexOf(">") > -1)) {
|
|
|
208 |
state.f = inlineNormal;
|
|
|
209 |
state.block = blockNormal;
|
|
|
210 |
state.htmlState = null;
|
|
|
211 |
}
|
|
|
212 |
return style;
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
function local(stream, state) {
|
|
|
216 |
if (stream.sol() && stream.match("```", false)) {
|
|
|
217 |
state.localMode = state.localState = null;
|
|
|
218 |
state.f = state.block = leavingLocal;
|
|
|
219 |
return null;
|
|
|
220 |
} else if (state.localMode) {
|
|
|
221 |
return state.localMode.token(stream, state.localState);
|
|
|
222 |
} else {
|
|
|
223 |
stream.skipToEnd();
|
|
|
224 |
return code;
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
function leavingLocal(stream, state) {
|
|
|
229 |
stream.match("```");
|
|
|
230 |
state.block = blockNormal;
|
|
|
231 |
state.f = inlineNormal;
|
|
|
232 |
if (modeCfg.highlightFormatting) state.formatting = "code-block";
|
|
|
233 |
state.code = true;
|
|
|
234 |
var returnType = getType(state);
|
|
|
235 |
state.code = false;
|
|
|
236 |
return returnType;
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
// Inline
|
|
|
240 |
function getType(state) {
|
|
|
241 |
var styles = [];
|
|
|
242 |
|
|
|
243 |
if (state.formatting) {
|
|
|
244 |
styles.push(formatting);
|
|
|
245 |
|
|
|
246 |
if (typeof state.formatting === "string") state.formatting = [state.formatting];
|
|
|
247 |
|
|
|
248 |
for (var i = 0; i < state.formatting.length; i++) {
|
|
|
249 |
styles.push(formatting + "-" + state.formatting[i]);
|
|
|
250 |
|
|
|
251 |
if (state.formatting[i] === "header") {
|
|
|
252 |
styles.push(formatting + "-" + state.formatting[i] + "-" + state.header);
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
// Add `formatting-quote` and `formatting-quote-#` for blockquotes
|
|
|
256 |
// Add `error` instead if the maximum blockquote nesting depth is passed
|
|
|
257 |
if (state.formatting[i] === "quote") {
|
|
|
258 |
if (!modeCfg.maxBlockquoteDepth || modeCfg.maxBlockquoteDepth >= state.quote) {
|
|
|
259 |
styles.push(formatting + "-" + state.formatting[i] + "-" + state.quote);
|
|
|
260 |
} else {
|
|
|
261 |
styles.push("error");
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
if (state.taskOpen) {
|
|
|
268 |
styles.push("meta");
|
|
|
269 |
return styles.length ? styles.join(' ') : null;
|
|
|
270 |
}
|
|
|
271 |
if (state.taskClosed) {
|
|
|
272 |
styles.push("property");
|
|
|
273 |
return styles.length ? styles.join(' ') : null;
|
|
|
274 |
}
|
|
|
275 |
|
|
|
276 |
if (state.linkHref) {
|
|
|
277 |
styles.push(linkhref, "url");
|
|
|
278 |
} else { // Only apply inline styles to non-url text
|
|
|
279 |
if (state.strong) { styles.push(strong); }
|
|
|
280 |
if (state.em) { styles.push(em); }
|
|
|
281 |
if (state.strikethrough) { styles.push(strikethrough); }
|
|
|
282 |
|
|
|
283 |
if (state.linkText) { styles.push(linktext); }
|
|
|
284 |
|
|
|
285 |
if (state.code) { styles.push(code); }
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
if (state.header) { styles.push(header); styles.push(header + "-" + state.header); }
|
|
|
289 |
|
|
|
290 |
if (state.quote) {
|
|
|
291 |
styles.push(quote);
|
|
|
292 |
|
|
|
293 |
// Add `quote-#` where the maximum for `#` is modeCfg.maxBlockquoteDepth
|
|
|
294 |
if (!modeCfg.maxBlockquoteDepth || modeCfg.maxBlockquoteDepth >= state.quote) {
|
|
|
295 |
styles.push(quote + "-" + state.quote);
|
|
|
296 |
} else {
|
|
|
297 |
styles.push(quote + "-" + modeCfg.maxBlockquoteDepth);
|
|
|
298 |
}
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
if (state.list !== false) {
|
|
|
302 |
var listMod = (state.listDepth - 1) % 3;
|
|
|
303 |
if (!listMod) {
|
|
|
304 |
styles.push(list1);
|
|
|
305 |
} else if (listMod === 1) {
|
|
|
306 |
styles.push(list2);
|
|
|
307 |
} else {
|
|
|
308 |
styles.push(list3);
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
if (state.trailingSpaceNewLine) {
|
|
|
313 |
styles.push("trailing-space-new-line");
|
|
|
314 |
} else if (state.trailingSpace) {
|
|
|
315 |
styles.push("trailing-space-" + (state.trailingSpace % 2 ? "a" : "b"));
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
return styles.length ? styles.join(' ') : null;
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
function handleText(stream, state) {
|
|
|
322 |
if (stream.match(textRE, true)) {
|
|
|
323 |
return getType(state);
|
|
|
324 |
}
|
|
|
325 |
return undefined;
|
|
|
326 |
}
|
|
|
327 |
|
|
|
328 |
function inlineNormal(stream, state) {
|
|
|
329 |
var style = state.text(stream, state);
|
|
|
330 |
if (typeof style !== 'undefined')
|
|
|
331 |
return style;
|
|
|
332 |
|
|
|
333 |
if (state.list) { // List marker (*, +, -, 1., etc)
|
|
|
334 |
state.list = null;
|
|
|
335 |
return getType(state);
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
if (state.taskList) {
|
|
|
339 |
var taskOpen = stream.match(taskListRE, true)[1] !== "x";
|
|
|
340 |
if (taskOpen) state.taskOpen = true;
|
|
|
341 |
else state.taskClosed = true;
|
|
|
342 |
if (modeCfg.highlightFormatting) state.formatting = "task";
|
|
|
343 |
state.taskList = false;
|
|
|
344 |
return getType(state);
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
state.taskOpen = false;
|
|
|
348 |
state.taskClosed = false;
|
|
|
349 |
|
|
|
350 |
if (state.header && stream.match(/^#+$/, true)) {
|
|
|
351 |
if (modeCfg.highlightFormatting) state.formatting = "header";
|
|
|
352 |
return getType(state);
|
|
|
353 |
}
|
|
|
354 |
|
|
|
355 |
// Get sol() value now, before character is consumed
|
|
|
356 |
var sol = stream.sol();
|
|
|
357 |
|
|
|
358 |
var ch = stream.next();
|
|
|
359 |
|
|
|
360 |
if (ch === '\\') {
|
|
|
361 |
stream.next();
|
|
|
362 |
if (modeCfg.highlightFormatting) {
|
|
|
363 |
var type = getType(state);
|
|
|
364 |
return type ? type + " formatting-escape" : "formatting-escape";
|
|
|
365 |
}
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
// Matches link titles present on next line
|
|
|
369 |
if (state.linkTitle) {
|
|
|
370 |
state.linkTitle = false;
|
|
|
371 |
var matchCh = ch;
|
|
|
372 |
if (ch === '(') {
|
|
|
373 |
matchCh = ')';
|
|
|
374 |
}
|
|
|
375 |
matchCh = (matchCh+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
|
|
|
376 |
var regex = '^\\s*(?:[^' + matchCh + '\\\\]+|\\\\\\\\|\\\\.)' + matchCh;
|
|
|
377 |
if (stream.match(new RegExp(regex), true)) {
|
|
|
378 |
return linkhref;
|
|
|
379 |
}
|
|
|
380 |
}
|
|
|
381 |
|
|
|
382 |
// If this block is changed, it may need to be updated in GFM mode
|
|
|
383 |
if (ch === '`') {
|
|
|
384 |
var previousFormatting = state.formatting;
|
|
|
385 |
if (modeCfg.highlightFormatting) state.formatting = "code";
|
|
|
386 |
var t = getType(state);
|
|
|
387 |
var before = stream.pos;
|
|
|
388 |
stream.eatWhile('`');
|
|
|
389 |
var difference = 1 + stream.pos - before;
|
|
|
390 |
if (!state.code) {
|
|
|
391 |
codeDepth = difference;
|
|
|
392 |
state.code = true;
|
|
|
393 |
return getType(state);
|
|
|
394 |
} else {
|
|
|
395 |
if (difference === codeDepth) { // Must be exact
|
|
|
396 |
state.code = false;
|
|
|
397 |
return t;
|
|
|
398 |
}
|
|
|
399 |
state.formatting = previousFormatting;
|
|
|
400 |
return getType(state);
|
|
|
401 |
}
|
|
|
402 |
} else if (state.code) {
|
|
|
403 |
return getType(state);
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
if (ch === '!' && stream.match(/\[[^\]]*\] ?(?:\(|\[)/, false)) {
|
|
|
407 |
stream.match(/\[[^\]]*\]/);
|
|
|
408 |
state.inline = state.f = linkHref;
|
|
|
409 |
return image;
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
if (ch === '[' && stream.match(/.*\](\(.*\)| ?\[.*\])/, false)) {
|
|
|
413 |
state.linkText = true;
|
|
|
414 |
if (modeCfg.highlightFormatting) state.formatting = "link";
|
|
|
415 |
return getType(state);
|
|
|
416 |
}
|
|
|
417 |
|
|
|
418 |
if (ch === ']' && state.linkText && stream.match(/\(.*\)| ?\[.*\]/, false)) {
|
|
|
419 |
if (modeCfg.highlightFormatting) state.formatting = "link";
|
|
|
420 |
var type = getType(state);
|
|
|
421 |
state.linkText = false;
|
|
|
422 |
state.inline = state.f = linkHref;
|
|
|
423 |
return type;
|
|
|
424 |
}
|
|
|
425 |
|
|
|
426 |
if (ch === '<' && stream.match(/^(https?|ftps?):\/\/(?:[^\\>]|\\.)+>/, false)) {
|
|
|
427 |
state.f = state.inline = linkInline;
|
|
|
428 |
if (modeCfg.highlightFormatting) state.formatting = "link";
|
|
|
429 |
var type = getType(state);
|
|
|
430 |
if (type){
|
|
|
431 |
type += " ";
|
|
|
432 |
} else {
|
|
|
433 |
type = "";
|
|
|
434 |
}
|
|
|
435 |
return type + linkinline;
|
|
|
436 |
}
|
|
|
437 |
|
|
|
438 |
if (ch === '<' && stream.match(/^[^> \\]+@(?:[^\\>]|\\.)+>/, false)) {
|
|
|
439 |
state.f = state.inline = linkInline;
|
|
|
440 |
if (modeCfg.highlightFormatting) state.formatting = "link";
|
|
|
441 |
var type = getType(state);
|
|
|
442 |
if (type){
|
|
|
443 |
type += " ";
|
|
|
444 |
} else {
|
|
|
445 |
type = "";
|
|
|
446 |
}
|
|
|
447 |
return type + linkemail;
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
if (ch === '<' && stream.match(/^(!--|\w)/, false)) {
|
|
|
451 |
var end = stream.string.indexOf(">", stream.pos);
|
|
|
452 |
if (end != -1) {
|
|
|
453 |
var atts = stream.string.substring(stream.start, end);
|
|
|
454 |
if (/markdown\s*=\s*('|"){0,1}1('|"){0,1}/.test(atts)) state.md_inside = true;
|
|
|
455 |
}
|
|
|
456 |
stream.backUp(1);
|
|
|
457 |
state.htmlState = CodeMirror.startState(htmlMode);
|
|
|
458 |
return switchBlock(stream, state, htmlBlock);
|
|
|
459 |
}
|
|
|
460 |
|
|
|
461 |
if (ch === '<' && stream.match(/^\/\w*?>/)) {
|
|
|
462 |
state.md_inside = false;
|
|
|
463 |
return "tag";
|
|
|
464 |
}
|
|
|
465 |
|
|
|
466 |
var ignoreUnderscore = false;
|
|
|
467 |
if (!modeCfg.underscoresBreakWords) {
|
|
|
468 |
if (ch === '_' && stream.peek() !== '_' && stream.match(/(\w)/, false)) {
|
|
|
469 |
var prevPos = stream.pos - 2;
|
|
|
470 |
if (prevPos >= 0) {
|
|
|
471 |
var prevCh = stream.string.charAt(prevPos);
|
|
|
472 |
if (prevCh !== '_' && prevCh.match(/(\w)/, false)) {
|
|
|
473 |
ignoreUnderscore = true;
|
|
|
474 |
}
|
|
|
475 |
}
|
|
|
476 |
}
|
|
|
477 |
}
|
|
|
478 |
if (ch === '*' || (ch === '_' && !ignoreUnderscore)) {
|
|
|
479 |
if (sol && stream.peek() === ' ') {
|
|
|
480 |
// Do nothing, surrounded by newline and space
|
|
|
481 |
} else if (state.strong === ch && stream.eat(ch)) { // Remove STRONG
|
|
|
482 |
if (modeCfg.highlightFormatting) state.formatting = "strong";
|
|
|
483 |
var t = getType(state);
|
|
|
484 |
state.strong = false;
|
|
|
485 |
return t;
|
|
|
486 |
} else if (!state.strong && stream.eat(ch)) { // Add STRONG
|
|
|
487 |
state.strong = ch;
|
|
|
488 |
if (modeCfg.highlightFormatting) state.formatting = "strong";
|
|
|
489 |
return getType(state);
|
|
|
490 |
} else if (state.em === ch) { // Remove EM
|
|
|
491 |
if (modeCfg.highlightFormatting) state.formatting = "em";
|
|
|
492 |
var t = getType(state);
|
|
|
493 |
state.em = false;
|
|
|
494 |
return t;
|
|
|
495 |
} else if (!state.em) { // Add EM
|
|
|
496 |
state.em = ch;
|
|
|
497 |
if (modeCfg.highlightFormatting) state.formatting = "em";
|
|
|
498 |
return getType(state);
|
|
|
499 |
}
|
|
|
500 |
} else if (ch === ' ') {
|
|
|
501 |
if (stream.eat('*') || stream.eat('_')) { // Probably surrounded by spaces
|
|
|
502 |
if (stream.peek() === ' ') { // Surrounded by spaces, ignore
|
|
|
503 |
return getType(state);
|
|
|
504 |
} else { // Not surrounded by spaces, back up pointer
|
|
|
505 |
stream.backUp(1);
|
|
|
506 |
}
|
|
|
507 |
}
|
|
|
508 |
}
|
|
|
509 |
|
|
|
510 |
if (modeCfg.strikethrough) {
|
|
|
511 |
if (ch === '~' && stream.eatWhile(ch)) {
|
|
|
512 |
if (state.strikethrough) {// Remove strikethrough
|
|
|
513 |
if (modeCfg.highlightFormatting) state.formatting = "strikethrough";
|
|
|
514 |
var t = getType(state);
|
|
|
515 |
state.strikethrough = false;
|
|
|
516 |
return t;
|
|
|
517 |
} else if (stream.match(/^[^\s]/, false)) {// Add strikethrough
|
|
|
518 |
state.strikethrough = true;
|
|
|
519 |
if (modeCfg.highlightFormatting) state.formatting = "strikethrough";
|
|
|
520 |
return getType(state);
|
|
|
521 |
}
|
|
|
522 |
} else if (ch === ' ') {
|
|
|
523 |
if (stream.match(/^~~/, true)) { // Probably surrounded by space
|
|
|
524 |
if (stream.peek() === ' ') { // Surrounded by spaces, ignore
|
|
|
525 |
return getType(state);
|
|
|
526 |
} else { // Not surrounded by spaces, back up pointer
|
|
|
527 |
stream.backUp(2);
|
|
|
528 |
}
|
|
|
529 |
}
|
|
|
530 |
}
|
|
|
531 |
}
|
|
|
532 |
|
|
|
533 |
if (ch === ' ') {
|
|
|
534 |
if (stream.match(/ +$/, false)) {
|
|
|
535 |
state.trailingSpace++;
|
|
|
536 |
} else if (state.trailingSpace) {
|
|
|
537 |
state.trailingSpaceNewLine = true;
|
|
|
538 |
}
|
|
|
539 |
}
|
|
|
540 |
|
|
|
541 |
return getType(state);
|
|
|
542 |
}
|
|
|
543 |
|
|
|
544 |
function linkInline(stream, state) {
|
|
|
545 |
var ch = stream.next();
|
|
|
546 |
|
|
|
547 |
if (ch === ">") {
|
|
|
548 |
state.f = state.inline = inlineNormal;
|
|
|
549 |
if (modeCfg.highlightFormatting) state.formatting = "link";
|
|
|
550 |
var type = getType(state);
|
|
|
551 |
if (type){
|
|
|
552 |
type += " ";
|
|
|
553 |
} else {
|
|
|
554 |
type = "";
|
|
|
555 |
}
|
|
|
556 |
return type + linkinline;
|
|
|
557 |
}
|
|
|
558 |
|
|
|
559 |
stream.match(/^[^>]+/, true);
|
|
|
560 |
|
|
|
561 |
return linkinline;
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
function linkHref(stream, state) {
|
|
|
565 |
// Check if space, and return NULL if so (to avoid marking the space)
|
|
|
566 |
if(stream.eatSpace()){
|
|
|
567 |
return null;
|
|
|
568 |
}
|
|
|
569 |
var ch = stream.next();
|
|
|
570 |
if (ch === '(' || ch === '[') {
|
|
|
571 |
state.f = state.inline = getLinkHrefInside(ch === "(" ? ")" : "]");
|
|
|
572 |
if (modeCfg.highlightFormatting) state.formatting = "link-string";
|
|
|
573 |
state.linkHref = true;
|
|
|
574 |
return getType(state);
|
|
|
575 |
}
|
|
|
576 |
return 'error';
|
|
|
577 |
}
|
|
|
578 |
|
|
|
579 |
function getLinkHrefInside(endChar) {
|
|
|
580 |
return function(stream, state) {
|
|
|
581 |
var ch = stream.next();
|
|
|
582 |
|
|
|
583 |
if (ch === endChar) {
|
|
|
584 |
state.f = state.inline = inlineNormal;
|
|
|
585 |
if (modeCfg.highlightFormatting) state.formatting = "link-string";
|
|
|
586 |
var returnState = getType(state);
|
|
|
587 |
state.linkHref = false;
|
|
|
588 |
return returnState;
|
|
|
589 |
}
|
|
|
590 |
|
|
|
591 |
if (stream.match(inlineRE(endChar), true)) {
|
|
|
592 |
stream.backUp(1);
|
|
|
593 |
}
|
|
|
594 |
|
|
|
595 |
state.linkHref = true;
|
|
|
596 |
return getType(state);
|
|
|
597 |
};
|
|
|
598 |
}
|
|
|
599 |
|
|
|
600 |
function footnoteLink(stream, state) {
|
|
|
601 |
if (stream.match(/^[^\]]*\]:/, false)) {
|
|
|
602 |
state.f = footnoteLinkInside;
|
|
|
603 |
stream.next(); // Consume [
|
|
|
604 |
if (modeCfg.highlightFormatting) state.formatting = "link";
|
|
|
605 |
state.linkText = true;
|
|
|
606 |
return getType(state);
|
|
|
607 |
}
|
|
|
608 |
return switchInline(stream, state, inlineNormal);
|
|
|
609 |
}
|
|
|
610 |
|
|
|
611 |
function footnoteLinkInside(stream, state) {
|
|
|
612 |
if (stream.match(/^\]:/, true)) {
|
|
|
613 |
state.f = state.inline = footnoteUrl;
|
|
|
614 |
if (modeCfg.highlightFormatting) state.formatting = "link";
|
|
|
615 |
var returnType = getType(state);
|
|
|
616 |
state.linkText = false;
|
|
|
617 |
return returnType;
|
|
|
618 |
}
|
|
|
619 |
|
|
|
620 |
stream.match(/^[^\]]+/, true);
|
|
|
621 |
|
|
|
622 |
return linktext;
|
|
|
623 |
}
|
|
|
624 |
|
|
|
625 |
function footnoteUrl(stream, state) {
|
|
|
626 |
// Check if space, and return NULL if so (to avoid marking the space)
|
|
|
627 |
if(stream.eatSpace()){
|
|
|
628 |
return null;
|
|
|
629 |
}
|
|
|
630 |
// Match URL
|
|
|
631 |
stream.match(/^[^\s]+/, true);
|
|
|
632 |
// Check for link title
|
|
|
633 |
if (stream.peek() === undefined) { // End of line, set flag to check next line
|
|
|
634 |
state.linkTitle = true;
|
|
|
635 |
} else { // More content on line, check if link title
|
|
|
636 |
stream.match(/^(?:\s+(?:"(?:[^"\\]|\\\\|\\.)+"|'(?:[^'\\]|\\\\|\\.)+'|\((?:[^)\\]|\\\\|\\.)+\)))?/, true);
|
|
|
637 |
}
|
|
|
638 |
state.f = state.inline = inlineNormal;
|
|
|
639 |
return linkhref + " url";
|
|
|
640 |
}
|
|
|
641 |
|
|
|
642 |
var savedInlineRE = [];
|
|
|
643 |
function inlineRE(endChar) {
|
|
|
644 |
if (!savedInlineRE[endChar]) {
|
|
|
645 |
// Escape endChar for RegExp (taken from http://stackoverflow.com/a/494122/526741)
|
|
|
646 |
endChar = (endChar+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
|
|
|
647 |
// Match any non-endChar, escaped character, as well as the closing
|
|
|
648 |
// endChar.
|
|
|
649 |
savedInlineRE[endChar] = new RegExp('^(?:[^\\\\]|\\\\.)*?(' + endChar + ')');
|
|
|
650 |
}
|
|
|
651 |
return savedInlineRE[endChar];
|
|
|
652 |
}
|
|
|
653 |
|
|
|
654 |
var mode = {
|
|
|
655 |
startState: function() {
|
|
|
656 |
return {
|
|
|
657 |
f: blockNormal,
|
|
|
658 |
|
|
|
659 |
prevLineHasContent: false,
|
|
|
660 |
thisLineHasContent: false,
|
|
|
661 |
|
|
|
662 |
block: blockNormal,
|
|
|
663 |
htmlState: null,
|
|
|
664 |
indentation: 0,
|
|
|
665 |
|
|
|
666 |
inline: inlineNormal,
|
|
|
667 |
text: handleText,
|
|
|
668 |
|
|
|
669 |
formatting: false,
|
|
|
670 |
linkText: false,
|
|
|
671 |
linkHref: false,
|
|
|
672 |
linkTitle: false,
|
|
|
673 |
em: false,
|
|
|
674 |
strong: false,
|
|
|
675 |
header: 0,
|
|
|
676 |
hr: false,
|
|
|
677 |
taskList: false,
|
|
|
678 |
list: false,
|
|
|
679 |
listDepth: 0,
|
|
|
680 |
quote: 0,
|
|
|
681 |
trailingSpace: 0,
|
|
|
682 |
trailingSpaceNewLine: false,
|
|
|
683 |
strikethrough: false
|
|
|
684 |
};
|
|
|
685 |
},
|
|
|
686 |
|
|
|
687 |
copyState: function(s) {
|
|
|
688 |
return {
|
|
|
689 |
f: s.f,
|
|
|
690 |
|
|
|
691 |
prevLineHasContent: s.prevLineHasContent,
|
|
|
692 |
thisLineHasContent: s.thisLineHasContent,
|
|
|
693 |
|
|
|
694 |
block: s.block,
|
|
|
695 |
htmlState: s.htmlState && CodeMirror.copyState(htmlMode, s.htmlState),
|
|
|
696 |
indentation: s.indentation,
|
|
|
697 |
|
|
|
698 |
localMode: s.localMode,
|
|
|
699 |
localState: s.localMode ? CodeMirror.copyState(s.localMode, s.localState) : null,
|
|
|
700 |
|
|
|
701 |
inline: s.inline,
|
|
|
702 |
text: s.text,
|
|
|
703 |
formatting: false,
|
|
|
704 |
linkTitle: s.linkTitle,
|
|
|
705 |
em: s.em,
|
|
|
706 |
strong: s.strong,
|
|
|
707 |
strikethrough: s.strikethrough,
|
|
|
708 |
header: s.header,
|
|
|
709 |
hr: s.hr,
|
|
|
710 |
taskList: s.taskList,
|
|
|
711 |
list: s.list,
|
|
|
712 |
listDepth: s.listDepth,
|
|
|
713 |
quote: s.quote,
|
|
|
714 |
indentedCode: s.indentedCode,
|
|
|
715 |
trailingSpace: s.trailingSpace,
|
|
|
716 |
trailingSpaceNewLine: s.trailingSpaceNewLine,
|
|
|
717 |
md_inside: s.md_inside
|
|
|
718 |
};
|
|
|
719 |
},
|
|
|
720 |
|
|
|
721 |
token: function(stream, state) {
|
|
|
722 |
|
|
|
723 |
// Reset state.formatting
|
|
|
724 |
state.formatting = false;
|
|
|
725 |
|
|
|
726 |
if (stream.sol()) {
|
|
|
727 |
var forceBlankLine = !!state.header || state.hr;
|
|
|
728 |
|
|
|
729 |
// Reset state.header and state.hr
|
|
|
730 |
state.header = 0;
|
|
|
731 |
state.hr = false;
|
|
|
732 |
|
|
|
733 |
if (stream.match(/^\s*$/, true) || forceBlankLine) {
|
|
|
734 |
state.prevLineHasContent = false;
|
|
|
735 |
blankLine(state);
|
|
|
736 |
return forceBlankLine ? this.token(stream, state) : null;
|
|
|
737 |
} else {
|
|
|
738 |
state.prevLineHasContent = state.thisLineHasContent;
|
|
|
739 |
state.thisLineHasContent = true;
|
|
|
740 |
}
|
|
|
741 |
|
|
|
742 |
// Reset state.taskList
|
|
|
743 |
state.taskList = false;
|
|
|
744 |
|
|
|
745 |
// Reset state.code
|
|
|
746 |
state.code = false;
|
|
|
747 |
|
|
|
748 |
// Reset state.trailingSpace
|
|
|
749 |
state.trailingSpace = 0;
|
|
|
750 |
state.trailingSpaceNewLine = false;
|
|
|
751 |
|
|
|
752 |
state.f = state.block;
|
|
|
753 |
var indentation = stream.match(/^\s*/, true)[0].replace(/\t/g, ' ').length;
|
|
|
754 |
var difference = Math.floor((indentation - state.indentation) / 4) * 4;
|
|
|
755 |
if (difference > 4) difference = 4;
|
|
|
756 |
var adjustedIndentation = state.indentation + difference;
|
|
|
757 |
state.indentationDiff = adjustedIndentation - state.indentation;
|
|
|
758 |
state.indentation = adjustedIndentation;
|
|
|
759 |
if (indentation > 0) return null;
|
|
|
760 |
}
|
|
|
761 |
return state.f(stream, state);
|
|
|
762 |
},
|
|
|
763 |
|
|
|
764 |
innerMode: function(state) {
|
|
|
765 |
if (state.block == htmlBlock) return {state: state.htmlState, mode: htmlMode};
|
|
|
766 |
if (state.localState) return {state: state.localState, mode: state.localMode};
|
|
|
767 |
return {state: state, mode: mode};
|
|
|
768 |
},
|
|
|
769 |
|
|
|
770 |
blankLine: blankLine,
|
|
|
771 |
|
|
|
772 |
getType: getType,
|
|
|
773 |
|
|
|
774 |
fold: "markdown"
|
|
|
775 |
};
|
|
|
776 |
return mode;
|
|
|
777 |
}, "xml");
|
|
|
778 |
|
|
|
779 |
CodeMirror.defineMIME("text/x-markdown", "markdown");
|
|
|
780 |
|
|
|
781 |
});
|