markdown.js 28 KB

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