markdown.js 25 KB

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