markdown.js 31 KB

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