Theme Inspinia
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.

markdown.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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")));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../xml/xml"], 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. var aliases = {
  16. html: "htmlmixed",
  17. js: "javascript",
  18. json: "application/json",
  19. c: "text/x-csrc",
  20. "c++": "text/x-c++src",
  21. java: "text/x-java",
  22. csharp: "text/x-csharp",
  23. "c#": "text/x-csharp",
  24. scala: "text/x-scala"
  25. };
  26. var getMode = (function () {
  27. var i, modes = {}, mimes = {}, mime;
  28. var list = [];
  29. for (var m in CodeMirror.modes)
  30. if (CodeMirror.modes.propertyIsEnumerable(m)) list.push(m);
  31. for (i = 0; i < list.length; i++) {
  32. modes[list[i]] = list[i];
  33. }
  34. var mimesList = [];
  35. for (var m in CodeMirror.mimeModes)
  36. if (CodeMirror.mimeModes.propertyIsEnumerable(m))
  37. mimesList.push({mime: m, mode: CodeMirror.mimeModes[m]});
  38. for (i = 0; i < mimesList.length; i++) {
  39. mime = mimesList[i].mime;
  40. mimes[mime] = mimesList[i].mime;
  41. }
  42. for (var a in aliases) {
  43. if (aliases[a] in modes || aliases[a] in mimes)
  44. modes[a] = aliases[a];
  45. }
  46. return function (lang) {
  47. return modes[lang] ? CodeMirror.getMode(cmCfg, modes[lang]) : null;
  48. };
  49. }());
  50. // Should characters that affect highlighting be highlighted separate?
  51. // Does not include characters that will be output (such as `1.` and `-` for lists)
  52. if (modeCfg.highlightFormatting === undefined)
  53. modeCfg.highlightFormatting = false;
  54. // Maximum number of nested blockquotes. Set to 0 for infinite nesting.
  55. // Excess `>` will emit `error` token.
  56. if (modeCfg.maxBlockquoteDepth === undefined)
  57. modeCfg.maxBlockquoteDepth = 0;
  58. // Should underscores in words open/close em/strong?
  59. if (modeCfg.underscoresBreakWords === undefined)
  60. modeCfg.underscoresBreakWords = true;
  61. // Turn on fenced code blocks? ("```" to start/end)
  62. if (modeCfg.fencedCodeBlocks === undefined) modeCfg.fencedCodeBlocks = false;
  63. // Turn on task lists? ("- [ ] " and "- [x] ")
  64. if (modeCfg.taskLists === undefined) modeCfg.taskLists = false;
  65. var codeDepth = 0;
  66. var header = 'header'
  67. , code = 'comment'
  68. , quote = 'quote'
  69. , list1 = 'variable-2'
  70. , list2 = 'variable-3'
  71. , list3 = 'keyword'
  72. , hr = 'hr'
  73. , image = 'tag'
  74. , formatting = 'formatting'
  75. , linkinline = 'link'
  76. , linkemail = 'link'
  77. , linktext = 'link'
  78. , linkhref = 'string'
  79. , em = 'em'
  80. , strong = 'strong';
  81. var hrRE = /^([*\-=_])(?:\s*\1){2,}\s*$/
  82. , ulRE = /^[*\-+]\s+/
  83. , olRE = /^[0-9]+\.\s+/
  84. , taskListRE = /^\[(x| )\](?=\s)/ // Must follow ulRE or olRE
  85. , atxHeaderRE = /^#+/
  86. , setextHeaderRE = /^(?:\={1,}|-{1,})$/
  87. , textRE = /^[^#!\[\]*_\\<>` "'(]+/;
  88. function switchInline(stream, state, f) {
  89. state.f = state.inline = f;
  90. return f(stream, state);
  91. }
  92. function switchBlock(stream, state, f) {
  93. state.f = state.block = f;
  94. return f(stream, state);
  95. }
  96. // Blocks
  97. function blankLine(state) {
  98. // Reset linkTitle state
  99. state.linkTitle = false;
  100. // Reset EM state
  101. state.em = false;
  102. // Reset STRONG state
  103. state.strong = false;
  104. // Reset state.quote
  105. state.quote = 0;
  106. if (!htmlFound && state.f == htmlBlock) {
  107. state.f = inlineNormal;
  108. state.block = blockNormal;
  109. }
  110. // Reset state.trailingSpace
  111. state.trailingSpace = 0;
  112. state.trailingSpaceNewLine = false;
  113. // Mark this line as blank
  114. state.thisLineHasContent = false;
  115. return null;
  116. }
  117. function blockNormal(stream, state) {
  118. var sol = stream.sol();
  119. var prevLineIsList = (state.list !== false);
  120. if (state.list !== false && state.indentationDiff >= 0) { // Continued list
  121. if (state.indentationDiff < 4) { // Only adjust indentation if *not* a code block
  122. state.indentation -= state.indentationDiff;
  123. }
  124. state.list = null;
  125. } else if (state.list !== false && state.indentation > 0) {
  126. state.list = null;
  127. state.listDepth = Math.floor(state.indentation / 4);
  128. } else if (state.list !== false) { // No longer a list
  129. state.list = false;
  130. state.listDepth = 0;
  131. }
  132. var match = null;
  133. if (state.indentationDiff >= 4) {
  134. state.indentation -= 4;
  135. stream.skipToEnd();
  136. return code;
  137. } else if (stream.eatSpace()) {
  138. return null;
  139. } else if (match = stream.match(atxHeaderRE)) {
  140. state.header = match[0].length <= 6 ? match[0].length : 6;
  141. if (modeCfg.highlightFormatting) state.formatting = "header";
  142. state.f = state.inline;
  143. return getType(state);
  144. } else if (state.prevLineHasContent && (match = stream.match(setextHeaderRE))) {
  145. state.header = match[0].charAt(0) == '=' ? 1 : 2;
  146. if (modeCfg.highlightFormatting) state.formatting = "header";
  147. state.f = state.inline;
  148. return getType(state);
  149. } else if (stream.eat('>')) {
  150. state.indentation++;
  151. state.quote = sol ? 1 : state.quote + 1;
  152. if (modeCfg.highlightFormatting) state.formatting = "quote";
  153. stream.eatSpace();
  154. return getType(state);
  155. } else if (stream.peek() === '[') {
  156. return switchInline(stream, state, footnoteLink);
  157. } else if (stream.match(hrRE, true)) {
  158. return hr;
  159. } else if ((!state.prevLineHasContent || prevLineIsList) && (stream.match(ulRE, false) || stream.match(olRE, false))) {
  160. var listType = null;
  161. if (stream.match(ulRE, true)) {
  162. listType = 'ul';
  163. } else {
  164. stream.match(olRE, true);
  165. listType = 'ol';
  166. }
  167. state.indentation += 4;
  168. state.list = true;
  169. state.listDepth++;
  170. if (modeCfg.taskLists && stream.match(taskListRE, false)) {
  171. state.taskList = true;
  172. }
  173. state.f = state.inline;
  174. if (modeCfg.highlightFormatting) state.formatting = ["list", "list-" + listType];
  175. return getType(state);
  176. } else if (modeCfg.fencedCodeBlocks && stream.match(/^```([\w+#]*)/, true)) {
  177. // try switching mode
  178. state.localMode = getMode(RegExp.$1);
  179. if (state.localMode) state.localState = state.localMode.startState();
  180. switchBlock(stream, state, local);
  181. if (modeCfg.highlightFormatting) state.formatting = "code-block";
  182. state.code = true;
  183. return getType(state);
  184. }
  185. return switchInline(stream, state, state.inline);
  186. }
  187. function htmlBlock(stream, state) {
  188. var style = htmlMode.token(stream, state.htmlState);
  189. if ((htmlFound && state.htmlState.tagStart === null && !state.htmlState.context) ||
  190. (state.md_inside && stream.current().indexOf(">") > -1)) {
  191. state.f = inlineNormal;
  192. state.block = blockNormal;
  193. state.htmlState = null;
  194. }
  195. return style;
  196. }
  197. function local(stream, state) {
  198. if (stream.sol() && stream.match(/^```/, true)) {
  199. state.localMode = state.localState = null;
  200. state.f = inlineNormal;
  201. state.block = blockNormal;
  202. if (modeCfg.highlightFormatting) state.formatting = "code-block";
  203. state.code = true;
  204. var returnType = getType(state);
  205. state.code = false;
  206. return returnType;
  207. } else if (state.localMode) {
  208. return state.localMode.token(stream, state.localState);
  209. } else {
  210. stream.skipToEnd();
  211. return code;
  212. }
  213. }
  214. // Inline
  215. function getType(state) {
  216. var styles = [];
  217. if (state.formatting) {
  218. styles.push(formatting);
  219. if (typeof state.formatting === "string") state.formatting = [state.formatting];
  220. for (var i = 0; i < state.formatting.length; i++) {
  221. styles.push(formatting + "-" + state.formatting[i]);
  222. if (state.formatting[i] === "header") {
  223. styles.push(formatting + "-" + state.formatting[i] + "-" + state.header);
  224. }
  225. // Add `formatting-quote` and `formatting-quote-#` for blockquotes
  226. // Add `error` instead if the maximum blockquote nesting depth is passed
  227. if (state.formatting[i] === "quote") {
  228. if (!modeCfg.maxBlockquoteDepth || modeCfg.maxBlockquoteDepth >= state.quote) {
  229. styles.push(formatting + "-" + state.formatting[i] + "-" + state.quote);
  230. } else {
  231. styles.push("error");
  232. }
  233. }
  234. }
  235. }
  236. if (state.taskOpen) {
  237. styles.push("meta");
  238. return styles.length ? styles.join(' ') : null;
  239. }
  240. if (state.taskClosed) {
  241. styles.push("property");
  242. return styles.length ? styles.join(' ') : null;
  243. }
  244. if (state.linkHref) {
  245. styles.push(linkhref);
  246. return styles.length ? styles.join(' ') : null;
  247. }
  248. if (state.strong) { styles.push(strong); }
  249. if (state.em) { styles.push(em); }
  250. if (state.linkText) { styles.push(linktext); }
  251. if (state.code) { styles.push(code); }
  252. if (state.header) { styles.push(header); styles.push(header + "-" + state.header); }
  253. if (state.quote) {
  254. styles.push(quote);
  255. // Add `quote-#` where the maximum for `#` is modeCfg.maxBlockquoteDepth
  256. if (!modeCfg.maxBlockquoteDepth || modeCfg.maxBlockquoteDepth >= state.quote) {
  257. styles.push(quote + "-" + state.quote);
  258. } else {
  259. styles.push(quote + "-" + modeCfg.maxBlockquoteDepth);
  260. }
  261. }
  262. if (state.list !== false) {
  263. var listMod = (state.listDepth - 1) % 3;
  264. if (!listMod) {
  265. styles.push(list1);
  266. } else if (listMod === 1) {
  267. styles.push(list2);
  268. } else {
  269. styles.push(list3);
  270. }
  271. }
  272. if (state.trailingSpaceNewLine) {
  273. styles.push("trailing-space-new-line");
  274. } else if (state.trailingSpace) {
  275. styles.push("trailing-space-" + (state.trailingSpace % 2 ? "a" : "b"));
  276. }
  277. return styles.length ? styles.join(' ') : null;
  278. }
  279. function handleText(stream, state) {
  280. if (stream.match(textRE, true)) {
  281. return getType(state);
  282. }
  283. return undefined;
  284. }
  285. function inlineNormal(stream, state) {
  286. var style = state.text(stream, state);
  287. if (typeof style !== 'undefined')
  288. return style;
  289. if (state.list) { // List marker (*, +, -, 1., etc)
  290. state.list = null;
  291. return getType(state);
  292. }
  293. if (state.taskList) {
  294. var taskOpen = stream.match(taskListRE, true)[1] !== "x";
  295. if (taskOpen) state.taskOpen = true;
  296. else state.taskClosed = true;
  297. if (modeCfg.highlightFormatting) state.formatting = "task";
  298. state.taskList = false;
  299. return getType(state);
  300. }
  301. state.taskOpen = false;
  302. state.taskClosed = false;
  303. if (state.header && stream.match(/^#+$/, true)) {
  304. if (modeCfg.highlightFormatting) state.formatting = "header";
  305. return getType(state);
  306. }
  307. // Get sol() value now, before character is consumed
  308. var sol = stream.sol();
  309. var ch = stream.next();
  310. if (ch === '\\') {
  311. stream.next();
  312. if (modeCfg.highlightFormatting) {
  313. var type = getType(state);
  314. return type ? type + " formatting-escape" : "formatting-escape";
  315. }
  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. formatting: false,
  566. linkText: false,
  567. linkHref: false,
  568. linkTitle: false,
  569. em: false,
  570. strong: false,
  571. header: 0,
  572. taskList: false,
  573. list: false,
  574. listDepth: 0,
  575. quote: 0,
  576. trailingSpace: 0,
  577. trailingSpaceNewLine: false
  578. };
  579. },
  580. copyState: function(s) {
  581. return {
  582. f: s.f,
  583. prevLineHasContent: s.prevLineHasContent,
  584. thisLineHasContent: s.thisLineHasContent,
  585. block: s.block,
  586. htmlState: s.htmlState && CodeMirror.copyState(htmlMode, s.htmlState),
  587. indentation: s.indentation,
  588. localMode: s.localMode,
  589. localState: s.localMode ? CodeMirror.copyState(s.localMode, s.localState) : null,
  590. inline: s.inline,
  591. text: s.text,
  592. formatting: false,
  593. linkTitle: s.linkTitle,
  594. em: s.em,
  595. strong: s.strong,
  596. header: s.header,
  597. taskList: s.taskList,
  598. list: s.list,
  599. listDepth: s.listDepth,
  600. quote: s.quote,
  601. trailingSpace: s.trailingSpace,
  602. trailingSpaceNewLine: s.trailingSpaceNewLine,
  603. md_inside: s.md_inside
  604. };
  605. },
  606. token: function(stream, state) {
  607. // Reset state.formatting
  608. state.formatting = false;
  609. if (stream.sol()) {
  610. var forceBlankLine = !!state.header;
  611. // Reset state.header
  612. state.header = 0;
  613. if (stream.match(/^\s*$/, true) || forceBlankLine) {
  614. state.prevLineHasContent = false;
  615. blankLine(state);
  616. return forceBlankLine ? this.token(stream, state) : null;
  617. } else {
  618. state.prevLineHasContent = state.thisLineHasContent;
  619. state.thisLineHasContent = true;
  620. }
  621. // Reset state.taskList
  622. state.taskList = false;
  623. // Reset state.code
  624. state.code = false;
  625. // Reset state.trailingSpace
  626. state.trailingSpace = 0;
  627. state.trailingSpaceNewLine = false;
  628. state.f = state.block;
  629. var indentation = stream.match(/^\s*/, true)[0].replace(/\t/g, ' ').length;
  630. var difference = Math.floor((indentation - state.indentation) / 4) * 4;
  631. if (difference > 4) difference = 4;
  632. var adjustedIndentation = state.indentation + difference;
  633. state.indentationDiff = adjustedIndentation - state.indentation;
  634. state.indentation = adjustedIndentation;
  635. if (indentation > 0) return null;
  636. }
  637. var result = state.f(stream, state);
  638. if (stream.start == stream.pos) return this.token(stream, state);
  639. else return result;
  640. },
  641. innerMode: function(state) {
  642. if (state.block == htmlBlock) return {state: state.htmlState, mode: htmlMode};
  643. if (state.localState) return {state: state.localState, mode: state.localMode};
  644. return {state: state, mode: mode};
  645. },
  646. blankLine: blankLine,
  647. getType: getType,
  648. fold: "markdown"
  649. };
  650. return mode;
  651. }, "xml");
  652. CodeMirror.defineMIME("text/x-markdown", "markdown");
  653. });