markdown.js 22 KB

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