markdown.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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 = /[!"#$%&'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~\xA1\xA7\xAB\xB6\xB7\xBB\xBF\u037E\u0387\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u0AF0\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F14\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1360-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CC0-\u1CC7\u1CD3\u2010-\u2027\u2030-\u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u2308-\u230B\u2329\u232A\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30-\u2E42\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA8FC\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uAAF0\uAAF1\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uFF3F\uFF5B\uFF5D\uFF5F-\uFF65]|\uD800[\uDD00-\uDD02\uDF9F\uDFD0]|\uD801\uDD6F|\uD802[\uDC57\uDD1F\uDD3F\uDE50-\uDE58\uDE7F\uDEF0-\uDEF6\uDF39-\uDF3F\uDF99-\uDF9C]|\uD804[\uDC47-\uDC4D\uDCBB\uDCBC\uDCBE-\uDCC1\uDD40-\uDD43\uDD74\uDD75\uDDC5-\uDDC9\uDDCD\uDDDB\uDDDD-\uDDDF\uDE38-\uDE3D\uDEA9]|\uD805[\uDCC6\uDDC1-\uDDD7\uDE41-\uDE43\uDF3C-\uDF3E]|\uD809[\uDC70-\uDC74]|\uD81A[\uDE6E\uDE6F\uDEF5\uDF37-\uDF3B\uDF44]|\uD82F\uDC9F|\uD836[\uDE87-\uDE8B]/
  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. var exit = htmlModeMissing
  110. if (!exit) {
  111. var inner = CodeMirror.innerMode(htmlMode, state.htmlState)
  112. exit = inner.mode.name == "xml" && inner.state.tagStart === null &&
  113. (!inner.state.context && inner.state.tokenize.isInText)
  114. }
  115. if (exit) {
  116. state.f = inlineNormal;
  117. state.block = blockNormal;
  118. state.htmlState = null;
  119. }
  120. }
  121. // Reset state.trailingSpace
  122. state.trailingSpace = 0;
  123. state.trailingSpaceNewLine = false;
  124. // Mark this line as blank
  125. state.prevLine = state.thisLine
  126. state.thisLine = {stream: null}
  127. return null;
  128. }
  129. function blockNormal(stream, state) {
  130. var firstTokenOnLine = stream.column() === state.indentation;
  131. var prevLineLineIsEmpty = lineIsEmpty(state.prevLine.stream);
  132. var prevLineIsIndentedCode = state.indentedCode;
  133. var prevLineIsHr = state.prevLine.hr;
  134. var prevLineIsList = state.list !== false;
  135. var maxNonCodeIndentation = (state.listStack[state.listStack.length - 1] || 0) + 3;
  136. state.indentedCode = false;
  137. var lineIndentation = state.indentation;
  138. // compute once per line (on first token)
  139. if (state.indentationDiff === null) {
  140. state.indentationDiff = state.indentation;
  141. if (prevLineIsList) {
  142. // Reset inline styles which shouldn't propagate aross list items
  143. state.em = false;
  144. state.strong = false;
  145. state.code = false;
  146. state.strikethrough = false;
  147. state.list = null;
  148. // While this list item's marker's indentation is less than the deepest
  149. // list item's content's indentation,pop the deepest list item
  150. // indentation off the stack, and update block indentation state
  151. while (lineIndentation < state.listStack[state.listStack.length - 1]) {
  152. state.listStack.pop();
  153. if (state.listStack.length) {
  154. state.indentation = state.listStack[state.listStack.length - 1];
  155. // less than the first list's indent -> the line is no longer a list
  156. } else {
  157. state.list = false;
  158. }
  159. }
  160. if (state.list !== false) {
  161. state.indentationDiff = lineIndentation - state.listStack[state.listStack.length - 1]
  162. }
  163. }
  164. }
  165. // not comprehensive (currently only for setext detection purposes)
  166. var allowsInlineContinuation = (
  167. !prevLineLineIsEmpty && !prevLineIsHr && !state.prevLine.header &&
  168. (!prevLineIsList || !prevLineIsIndentedCode) &&
  169. !state.prevLine.fencedCodeEnd
  170. );
  171. var isHr = (state.list === false || prevLineIsHr || prevLineLineIsEmpty) &&
  172. state.indentation <= maxNonCodeIndentation && stream.match(hrRE);
  173. var match = null;
  174. if (state.indentationDiff >= 4 && (prevLineIsIndentedCode || state.prevLine.fencedCodeEnd ||
  175. state.prevLine.header || prevLineLineIsEmpty)) {
  176. stream.skipToEnd();
  177. state.indentedCode = true;
  178. return tokenTypes.code;
  179. } else if (stream.eatSpace()) {
  180. return null;
  181. } else if (firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(atxHeaderRE)) && match[1].length <= 6) {
  182. state.quote = 0;
  183. state.header = match[1].length;
  184. state.thisLine.header = true;
  185. if (modeCfg.highlightFormatting) state.formatting = "header";
  186. state.f = state.inline;
  187. return getType(state);
  188. } else if (state.indentation <= maxNonCodeIndentation && stream.eat('>')) {
  189. state.quote = firstTokenOnLine ? 1 : state.quote + 1;
  190. if (modeCfg.highlightFormatting) state.formatting = "quote";
  191. stream.eatSpace();
  192. return getType(state);
  193. } else if (!isHr && !state.setext && firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(listRE))) {
  194. var listType = match[1] ? "ol" : "ul";
  195. state.indentation = lineIndentation + stream.current().length;
  196. state.list = true;
  197. state.quote = 0;
  198. // Add this list item's content's indentation to the stack
  199. state.listStack.push(state.indentation);
  200. if (modeCfg.taskLists && stream.match(taskListRE, false)) {
  201. state.taskList = true;
  202. }
  203. state.f = state.inline;
  204. if (modeCfg.highlightFormatting) state.formatting = ["list", "list-" + listType];
  205. return getType(state);
  206. } else if (firstTokenOnLine && state.indentation <= maxNonCodeIndentation && (match = stream.match(fencedCodeRE, true))) {
  207. state.quote = 0;
  208. state.fencedEndRE = new RegExp(match[1] + "+ *$");
  209. // try switching mode
  210. state.localMode = modeCfg.fencedCodeBlockHighlighting && getMode(match[2]);
  211. if (state.localMode) state.localState = CodeMirror.startState(state.localMode);
  212. state.f = state.block = local;
  213. if (modeCfg.highlightFormatting) state.formatting = "code-block";
  214. state.code = -1
  215. return getType(state);
  216. // SETEXT has lowest block-scope precedence after HR, so check it after
  217. // the others (code, blockquote, list...)
  218. } else if (
  219. // if setext set, indicates line after ---/===
  220. state.setext || (
  221. // line before ---/===
  222. (!allowsInlineContinuation || !prevLineIsList) && !state.quote && state.list === false &&
  223. !state.code && !isHr && !linkDefRE.test(stream.string) &&
  224. (match = stream.lookAhead(1)) && (match = match.match(setextHeaderRE))
  225. )
  226. ) {
  227. if ( !state.setext ) {
  228. state.header = match[0].charAt(0) == '=' ? 1 : 2;
  229. state.setext = state.header;
  230. } else {
  231. state.header = state.setext;
  232. // has no effect on type so we can reset it now
  233. state.setext = 0;
  234. stream.skipToEnd();
  235. if (modeCfg.highlightFormatting) state.formatting = "header";
  236. }
  237. state.thisLine.header = true;
  238. state.f = state.inline;
  239. return getType(state);
  240. } else if (isHr) {
  241. stream.skipToEnd();
  242. state.hr = true;
  243. state.thisLine.hr = true;
  244. return tokenTypes.hr;
  245. } else if (stream.peek() === '[') {
  246. return switchInline(stream, state, footnoteLink);
  247. }
  248. return switchInline(stream, state, state.inline);
  249. }
  250. function htmlBlock(stream, state) {
  251. var style = htmlMode.token(stream, state.htmlState);
  252. if (!htmlModeMissing) {
  253. var inner = CodeMirror.innerMode(htmlMode, state.htmlState)
  254. if ((inner.mode.name == "xml" && inner.state.tagStart === null &&
  255. (!inner.state.context && inner.state.tokenize.isInText)) ||
  256. (state.md_inside && stream.current().indexOf(">") > -1)) {
  257. state.f = inlineNormal;
  258. state.block = blockNormal;
  259. state.htmlState = null;
  260. }
  261. }
  262. return style;
  263. }
  264. function local(stream, state) {
  265. var currListInd = state.listStack[state.listStack.length - 1] || 0;
  266. var hasExitedList = state.indentation < currListInd;
  267. var maxFencedEndInd = currListInd + 3;
  268. if (state.fencedEndRE && state.indentation <= maxFencedEndInd && (hasExitedList || stream.match(state.fencedEndRE))) {
  269. if (modeCfg.highlightFormatting) state.formatting = "code-block";
  270. var returnType;
  271. if (!hasExitedList) returnType = getType(state)
  272. state.localMode = state.localState = null;
  273. state.block = blockNormal;
  274. state.f = inlineNormal;
  275. state.fencedEndRE = null;
  276. state.code = 0
  277. state.thisLine.fencedCodeEnd = true;
  278. if (hasExitedList) return switchBlock(stream, state, state.block);
  279. return returnType;
  280. } else if (state.localMode) {
  281. return state.localMode.token(stream, state.localState);
  282. } else {
  283. stream.skipToEnd();
  284. return tokenTypes.code;
  285. }
  286. }
  287. // Inline
  288. function getType(state) {
  289. var styles = [];
  290. if (state.formatting) {
  291. styles.push(tokenTypes.formatting);
  292. if (typeof state.formatting === "string") state.formatting = [state.formatting];
  293. for (var i = 0; i < state.formatting.length; i++) {
  294. styles.push(tokenTypes.formatting + "-" + state.formatting[i]);
  295. if (state.formatting[i] === "header") {
  296. styles.push(tokenTypes.formatting + "-" + state.formatting[i] + "-" + state.header);
  297. }
  298. // Add `formatting-quote` and `formatting-quote-#` for blockquotes
  299. // Add `error` instead if the maximum blockquote nesting depth is passed
  300. if (state.formatting[i] === "quote") {
  301. if (!modeCfg.maxBlockquoteDepth || modeCfg.maxBlockquoteDepth >= state.quote) {
  302. styles.push(tokenTypes.formatting + "-" + state.formatting[i] + "-" + state.quote);
  303. } else {
  304. styles.push("error");
  305. }
  306. }
  307. }
  308. }
  309. if (state.taskOpen) {
  310. styles.push("meta");
  311. return styles.length ? styles.join(' ') : null;
  312. }
  313. if (state.taskClosed) {
  314. styles.push("property");
  315. return styles.length ? styles.join(' ') : null;
  316. }
  317. if (state.linkHref) {
  318. styles.push(tokenTypes.linkHref, "url");
  319. } else { // Only apply inline styles to non-url text
  320. if (state.strong) { styles.push(tokenTypes.strong); }
  321. if (state.em) { styles.push(tokenTypes.em); }
  322. if (state.strikethrough) { styles.push(tokenTypes.strikethrough); }
  323. if (state.emoji) { styles.push(tokenTypes.emoji); }
  324. if (state.linkText) { styles.push(tokenTypes.linkText); }
  325. if (state.code) { styles.push(tokenTypes.code); }
  326. if (state.image) { styles.push(tokenTypes.image); }
  327. if (state.imageAltText) { styles.push(tokenTypes.imageAltText, "link"); }
  328. if (state.imageMarker) { styles.push(tokenTypes.imageMarker); }
  329. }
  330. if (state.header) { styles.push(tokenTypes.header, tokenTypes.header + "-" + state.header); }
  331. if (state.quote) {
  332. styles.push(tokenTypes.quote);
  333. // Add `quote-#` where the maximum for `#` is modeCfg.maxBlockquoteDepth
  334. if (!modeCfg.maxBlockquoteDepth || modeCfg.maxBlockquoteDepth >= state.quote) {
  335. styles.push(tokenTypes.quote + "-" + state.quote);
  336. } else {
  337. styles.push(tokenTypes.quote + "-" + modeCfg.maxBlockquoteDepth);
  338. }
  339. }
  340. if (state.list !== false) {
  341. var listMod = (state.listStack.length - 1) % 3;
  342. if (!listMod) {
  343. styles.push(tokenTypes.list1);
  344. } else if (listMod === 1) {
  345. styles.push(tokenTypes.list2);
  346. } else {
  347. styles.push(tokenTypes.list3);
  348. }
  349. }
  350. if (state.trailingSpaceNewLine) {
  351. styles.push("trailing-space-new-line");
  352. } else if (state.trailingSpace) {
  353. styles.push("trailing-space-" + (state.trailingSpace % 2 ? "a" : "b"));
  354. }
  355. return styles.length ? styles.join(' ') : null;
  356. }
  357. function handleText(stream, state) {
  358. if (stream.match(textRE, true)) {
  359. return getType(state);
  360. }
  361. return undefined;
  362. }
  363. function inlineNormal(stream, state) {
  364. var style = state.text(stream, state);
  365. if (typeof style !== 'undefined')
  366. return style;
  367. if (state.list) { // List marker (*, +, -, 1., etc)
  368. state.list = null;
  369. return getType(state);
  370. }
  371. if (state.taskList) {
  372. var taskOpen = stream.match(taskListRE, true)[1] === " ";
  373. if (taskOpen) state.taskOpen = true;
  374. else state.taskClosed = true;
  375. if (modeCfg.highlightFormatting) state.formatting = "task";
  376. state.taskList = false;
  377. return getType(state);
  378. }
  379. state.taskOpen = false;
  380. state.taskClosed = false;
  381. if (state.header && stream.match(/^#+$/, true)) {
  382. if (modeCfg.highlightFormatting) state.formatting = "header";
  383. return getType(state);
  384. }
  385. var ch = stream.next();
  386. // Matches link titles present on next line
  387. if (state.linkTitle) {
  388. state.linkTitle = false;
  389. var matchCh = ch;
  390. if (ch === '(') {
  391. matchCh = ')';
  392. }
  393. matchCh = (matchCh+'').replace(/([.?*+^\[\]\\(){}|-])/g, "\\$1");
  394. var regex = '^\\s*(?:[^' + matchCh + '\\\\]+|\\\\\\\\|\\\\.)' + matchCh;
  395. if (stream.match(new RegExp(regex), true)) {
  396. return tokenTypes.linkHref;
  397. }
  398. }
  399. // If this block is changed, it may need to be updated in GFM mode
  400. if (ch === '`') {
  401. var previousFormatting = state.formatting;
  402. if (modeCfg.highlightFormatting) state.formatting = "code";
  403. stream.eatWhile('`');
  404. var count = stream.current().length
  405. if (state.code == 0 && (!state.quote || count == 1)) {
  406. state.code = count
  407. return getType(state)
  408. } else if (count == state.code) { // Must be exact
  409. var t = getType(state)
  410. state.code = 0
  411. return t
  412. } else {
  413. state.formatting = previousFormatting
  414. return getType(state)
  415. }
  416. } else if (state.code) {
  417. return getType(state);
  418. }
  419. if (ch === '\\') {
  420. stream.next();
  421. if (modeCfg.highlightFormatting) {
  422. var type = getType(state);
  423. var formattingEscape = tokenTypes.formatting + "-escape";
  424. return type ? type + " " + formattingEscape : formattingEscape;
  425. }
  426. }
  427. if (ch === '!' && stream.match(/\[[^\]]*\] ?(?:\(|\[)/, false)) {
  428. state.imageMarker = true;
  429. state.image = true;
  430. if (modeCfg.highlightFormatting) state.formatting = "image";
  431. return getType(state);
  432. }
  433. if (ch === '[' && state.imageMarker && stream.match(/[^\]]*\](\(.*?\)| ?\[.*?\])/, false)) {
  434. state.imageMarker = false;
  435. state.imageAltText = true
  436. if (modeCfg.highlightFormatting) state.formatting = "image";
  437. return getType(state);
  438. }
  439. if (ch === ']' && state.imageAltText) {
  440. if (modeCfg.highlightFormatting) state.formatting = "image";
  441. var type = getType(state);
  442. state.imageAltText = false;
  443. state.image = false;
  444. state.inline = state.f = linkHref;
  445. return type;
  446. }
  447. if (ch === '[' && !state.image) {
  448. if (state.linkText && stream.match(/^.*?\]/)) return getType(state)
  449. state.linkText = true;
  450. if (modeCfg.highlightFormatting) state.formatting = "link";
  451. return getType(state);
  452. }
  453. if (ch === ']' && state.linkText) {
  454. if (modeCfg.highlightFormatting) state.formatting = "link";
  455. var type = getType(state);
  456. state.linkText = false;
  457. state.inline = state.f = stream.match(/\(.*?\)| ?\[.*?\]/, false) ? linkHref : inlineNormal
  458. return type;
  459. }
  460. if (ch === '<' && stream.match(/^(https?|ftps?):\/\/(?:[^\\>]|\\.)+>/, false)) {
  461. state.f = state.inline = linkInline;
  462. if (modeCfg.highlightFormatting) state.formatting = "link";
  463. var type = getType(state);
  464. if (type){
  465. type += " ";
  466. } else {
  467. type = "";
  468. }
  469. return type + tokenTypes.linkInline;
  470. }
  471. if (ch === '<' && stream.match(/^[^> \\]+@(?:[^\\>]|\\.)+>/, false)) {
  472. state.f = state.inline = linkInline;
  473. if (modeCfg.highlightFormatting) state.formatting = "link";
  474. var type = getType(state);
  475. if (type){
  476. type += " ";
  477. } else {
  478. type = "";
  479. }
  480. return type + tokenTypes.linkEmail;
  481. }
  482. if (modeCfg.xml && ch === '<' && stream.match(/^(!--|\?|!\[CDATA\[|[a-z][a-z0-9-]*(?:\s+[a-z_:.\-]+(?:\s*=\s*[^>]+)?)*\s*(?:>|$))/i, false)) {
  483. var end = stream.string.indexOf(">", stream.pos);
  484. if (end != -1) {
  485. var atts = stream.string.substring(stream.start, end);
  486. if (/markdown\s*=\s*('|"){0,1}1('|"){0,1}/.test(atts)) state.md_inside = true;
  487. }
  488. stream.backUp(1);
  489. state.htmlState = CodeMirror.startState(htmlMode);
  490. return switchBlock(stream, state, htmlBlock);
  491. }
  492. if (modeCfg.xml && ch === '<' && stream.match(/^\/\w*?>/)) {
  493. state.md_inside = false;
  494. return "tag";
  495. } else if (ch === "*" || ch === "_") {
  496. var len = 1, before = stream.pos == 1 ? " " : stream.string.charAt(stream.pos - 2)
  497. while (len < 3 && stream.eat(ch)) len++
  498. var after = stream.peek() || " "
  499. // See http://spec.commonmark.org/0.27/#emphasis-and-strong-emphasis
  500. var leftFlanking = !/\s/.test(after) && (!punctuation.test(after) || /\s/.test(before) || punctuation.test(before))
  501. var rightFlanking = !/\s/.test(before) && (!punctuation.test(before) || /\s/.test(after) || punctuation.test(after))
  502. var setEm = null, setStrong = null
  503. if (len % 2) { // Em
  504. if (!state.em && leftFlanking && (ch === "*" || !rightFlanking || punctuation.test(before)))
  505. setEm = true
  506. else if (state.em == ch && rightFlanking && (ch === "*" || !leftFlanking || punctuation.test(after)))
  507. setEm = false
  508. }
  509. if (len > 1) { // Strong
  510. if (!state.strong && leftFlanking && (ch === "*" || !rightFlanking || punctuation.test(before)))
  511. setStrong = true
  512. else if (state.strong == ch && rightFlanking && (ch === "*" || !leftFlanking || punctuation.test(after)))
  513. setStrong = false
  514. }
  515. if (setStrong != null || setEm != null) {
  516. if (modeCfg.highlightFormatting) state.formatting = setEm == null ? "strong" : setStrong == null ? "em" : "strong em"
  517. if (setEm === true) state.em = ch
  518. if (setStrong === true) state.strong = ch
  519. var t = getType(state)
  520. if (setEm === false) state.em = false
  521. if (setStrong === false) state.strong = false
  522. return t
  523. }
  524. } else if (ch === ' ') {
  525. if (stream.eat('*') || stream.eat('_')) { // Probably surrounded by spaces
  526. if (stream.peek() === ' ') { // Surrounded by spaces, ignore
  527. return getType(state);
  528. } else { // Not surrounded by spaces, back up pointer
  529. stream.backUp(1);
  530. }
  531. }
  532. }
  533. if (modeCfg.strikethrough) {
  534. if (ch === '~' && stream.eatWhile(ch)) {
  535. if (state.strikethrough) {// Remove strikethrough
  536. if (modeCfg.highlightFormatting) state.formatting = "strikethrough";
  537. var t = getType(state);
  538. state.strikethrough = false;
  539. return t;
  540. } else if (stream.match(/^[^\s]/, false)) {// Add strikethrough
  541. state.strikethrough = true;
  542. if (modeCfg.highlightFormatting) state.formatting = "strikethrough";
  543. return getType(state);
  544. }
  545. } else if (ch === ' ') {
  546. if (stream.match(/^~~/, true)) { // Probably surrounded by space
  547. if (stream.peek() === ' ') { // Surrounded by spaces, ignore
  548. return getType(state);
  549. } else { // Not surrounded by spaces, back up pointer
  550. stream.backUp(2);
  551. }
  552. }
  553. }
  554. }
  555. if (modeCfg.emoji && ch === ":" && stream.match(/^(?:[a-z_\d+][a-z_\d+-]*|\-[a-z_\d+][a-z_\d+-]*):/)) {
  556. state.emoji = true;
  557. if (modeCfg.highlightFormatting) state.formatting = "emoji";
  558. var retType = getType(state);
  559. state.emoji = false;
  560. return retType;
  561. }
  562. if (ch === ' ') {
  563. if (stream.match(/^ +$/, false)) {
  564. state.trailingSpace++;
  565. } else if (state.trailingSpace) {
  566. state.trailingSpaceNewLine = true;
  567. }
  568. }
  569. return getType(state);
  570. }
  571. function linkInline(stream, state) {
  572. var ch = stream.next();
  573. if (ch === ">") {
  574. state.f = state.inline = inlineNormal;
  575. if (modeCfg.highlightFormatting) state.formatting = "link";
  576. var type = getType(state);
  577. if (type){
  578. type += " ";
  579. } else {
  580. type = "";
  581. }
  582. return type + tokenTypes.linkInline;
  583. }
  584. stream.match(/^[^>]+/, true);
  585. return tokenTypes.linkInline;
  586. }
  587. function linkHref(stream, state) {
  588. // Check if space, and return NULL if so (to avoid marking the space)
  589. if(stream.eatSpace()){
  590. return null;
  591. }
  592. var ch = stream.next();
  593. if (ch === '(' || ch === '[') {
  594. state.f = state.inline = getLinkHrefInside(ch === "(" ? ")" : "]");
  595. if (modeCfg.highlightFormatting) state.formatting = "link-string";
  596. state.linkHref = true;
  597. return getType(state);
  598. }
  599. return 'error';
  600. }
  601. var linkRE = {
  602. ")": /^(?:[^\\\(\)]|\\.|\((?:[^\\\(\)]|\\.)*\))*?(?=\))/,
  603. "]": /^(?:[^\\\[\]]|\\.|\[(?:[^\\\[\]]|\\.)*\])*?(?=\])/
  604. }
  605. function getLinkHrefInside(endChar) {
  606. return function(stream, state) {
  607. var ch = stream.next();
  608. if (ch === endChar) {
  609. state.f = state.inline = inlineNormal;
  610. if (modeCfg.highlightFormatting) state.formatting = "link-string";
  611. var returnState = getType(state);
  612. state.linkHref = false;
  613. return returnState;
  614. }
  615. stream.match(linkRE[endChar])
  616. state.linkHref = true;
  617. return getType(state);
  618. };
  619. }
  620. function footnoteLink(stream, state) {
  621. if (stream.match(/^([^\]\\]|\\.)*\]:/, false)) {
  622. state.f = footnoteLinkInside;
  623. stream.next(); // Consume [
  624. if (modeCfg.highlightFormatting) state.formatting = "link";
  625. state.linkText = true;
  626. return getType(state);
  627. }
  628. return switchInline(stream, state, inlineNormal);
  629. }
  630. function footnoteLinkInside(stream, state) {
  631. if (stream.match(/^\]:/, true)) {
  632. state.f = state.inline = footnoteUrl;
  633. if (modeCfg.highlightFormatting) state.formatting = "link";
  634. var returnType = getType(state);
  635. state.linkText = false;
  636. return returnType;
  637. }
  638. stream.match(/^([^\]\\]|\\.)+/, true);
  639. return tokenTypes.linkText;
  640. }
  641. function footnoteUrl(stream, state) {
  642. // Check if space, and return NULL if so (to avoid marking the space)
  643. if(stream.eatSpace()){
  644. return null;
  645. }
  646. // Match URL
  647. stream.match(/^[^\s]+/, true);
  648. // Check for link title
  649. if (stream.peek() === undefined) { // End of line, set flag to check next line
  650. state.linkTitle = true;
  651. } else { // More content on line, check if link title
  652. stream.match(/^(?:\s+(?:"(?:[^"\\]|\\\\|\\.)+"|'(?:[^'\\]|\\\\|\\.)+'|\((?:[^)\\]|\\\\|\\.)+\)))?/, true);
  653. }
  654. state.f = state.inline = inlineNormal;
  655. return tokenTypes.linkHref + " url";
  656. }
  657. var mode = {
  658. startState: function() {
  659. return {
  660. f: blockNormal,
  661. prevLine: {stream: null},
  662. thisLine: {stream: null},
  663. block: blockNormal,
  664. htmlState: null,
  665. indentation: 0,
  666. inline: inlineNormal,
  667. text: handleText,
  668. formatting: false,
  669. linkText: false,
  670. linkHref: false,
  671. linkTitle: false,
  672. code: 0,
  673. em: false,
  674. strong: false,
  675. header: 0,
  676. setext: 0,
  677. hr: false,
  678. taskList: false,
  679. list: false,
  680. listStack: [],
  681. quote: 0,
  682. trailingSpace: 0,
  683. trailingSpaceNewLine: false,
  684. strikethrough: false,
  685. emoji: false,
  686. fencedEndRE: null
  687. };
  688. },
  689. copyState: function(s) {
  690. return {
  691. f: s.f,
  692. prevLine: s.prevLine,
  693. thisLine: s.thisLine,
  694. block: s.block,
  695. htmlState: s.htmlState && CodeMirror.copyState(htmlMode, s.htmlState),
  696. indentation: s.indentation,
  697. localMode: s.localMode,
  698. localState: s.localMode ? CodeMirror.copyState(s.localMode, s.localState) : null,
  699. inline: s.inline,
  700. text: s.text,
  701. formatting: false,
  702. linkText: s.linkText,
  703. linkTitle: s.linkTitle,
  704. linkHref: s.linkHref,
  705. code: s.code,
  706. em: s.em,
  707. strong: s.strong,
  708. strikethrough: s.strikethrough,
  709. emoji: s.emoji,
  710. header: s.header,
  711. setext: s.setext,
  712. hr: s.hr,
  713. taskList: s.taskList,
  714. list: s.list,
  715. listStack: s.listStack.slice(0),
  716. quote: s.quote,
  717. indentedCode: s.indentedCode,
  718. trailingSpace: s.trailingSpace,
  719. trailingSpaceNewLine: s.trailingSpaceNewLine,
  720. md_inside: s.md_inside,
  721. fencedEndRE: s.fencedEndRE
  722. };
  723. },
  724. token: function(stream, state) {
  725. // Reset state.formatting
  726. state.formatting = false;
  727. if (stream != state.thisLine.stream) {
  728. state.header = 0;
  729. state.hr = false;
  730. if (stream.match(/^\s*$/, true)) {
  731. blankLine(state);
  732. return null;
  733. }
  734. state.prevLine = state.thisLine
  735. state.thisLine = {stream: stream}
  736. // Reset state.taskList
  737. state.taskList = false;
  738. // Reset state.trailingSpace
  739. state.trailingSpace = 0;
  740. state.trailingSpaceNewLine = false;
  741. if (!state.localState) {
  742. state.f = state.block;
  743. if (state.f != htmlBlock) {
  744. var indentation = stream.match(/^\s*/, true)[0].replace(/\t/g, expandedTab).length;
  745. state.indentation = indentation;
  746. state.indentationDiff = null;
  747. if (indentation > 0) return null;
  748. }
  749. }
  750. }
  751. return state.f(stream, state);
  752. },
  753. innerMode: function(state) {
  754. if (state.block == htmlBlock) return {state: state.htmlState, mode: htmlMode};
  755. if (state.localState) return {state: state.localState, mode: state.localMode};
  756. return {state: state, mode: mode};
  757. },
  758. indent: function(state, textAfter, line) {
  759. if (state.block == htmlBlock && htmlMode.indent) return htmlMode.indent(state.htmlState, textAfter, line)
  760. if (state.localState && state.localMode.indent) return state.localMode.indent(state.localState, textAfter, line)
  761. return CodeMirror.Pass
  762. },
  763. blankLine: blankLine,
  764. getType: getType,
  765. blockCommentStart: "<!--",
  766. blockCommentEnd: "-->",
  767. closeBrackets: "()[]{}''\"\"``",
  768. fold: "markdown"
  769. };
  770. return mode;
  771. }, "xml");
  772. CodeMirror.defineMIME("text/markdown", "markdown");
  773. CodeMirror.defineMIME("text/x-markdown", "markdown");
  774. });