markdown.js 28 KB

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