markdown.js 39 KB

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