markdown_math.js 27 KB

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