markdown.js 41 KB

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