You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

765 lines
23 KiB

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