gfm.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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('../markdown/markdown'), require('../../addon/mode/overlay'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror', '../markdown/markdown', '../../addon/mode/overlay'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. var urlRE =
  15. /^((?:(?:aaas?|about|acap|adiumxtra|af[ps]|aim|apt|attachment|aw|beshare|bitcoin|bolo|callto|cap|chrome(?:-extension)?|cid|coap|com-eventbrite-attendee|content|crid|cvs|data|dav|dict|dlna-(?:playcontainer|playsingle)|dns|doi|dtn|dvb|ed2k|facetime|feed|file|finger|fish|ftp|geo|gg|git|gizmoproject|go|gopher|gtalk|h323|hcp|https?|iax|icap|icon|im|imap|info|ipn|ipp|irc[6s]?|iris(?:\.beep|\.lwz|\.xpc|\.xpcs)?|itms|jar|javascript|jms|keyparc|lastfm|ldaps?|magnet|mailto|maps|market|message|mid|mms|ms-help|msnim|msrps?|mtqp|mumble|mupdate|mvn|news|nfs|nih?|nntp|notes|oid|opaquelocktoken|palm|paparazzi|platform|pop|pres|proxy|psyc|query|res(?:ource)?|rmi|rsync|rtmp|rtsp|secondlife|service|session|sftp|sgn|shttp|sieve|sips?|skype|sm[bs]|snmp|soap\.beeps?|soldat|spotify|ssh|steam|svn|tag|teamspeak|tel(?:net)?|tftp|things|thismessage|tip|tn3270|tv|udp|unreal|urn|ut2004|vemmi|ventrilo|view-source|webcal|wss?|wtai|wyciwyg|xcon(?:-userid)?|xfire|xmlrpc\.beeps?|xmpp|xri|ymsgr|z39\.50[rs]?):(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]|\([^\s()<>]*\))+(?:\([^\s()<>]*\)|[^\s`*!()\[\]{};:'".,<>?«»“”‘’]))/i
  16. CodeMirror.defineMode(
  17. 'gfm',
  18. function (config, modeConfig) {
  19. var codeDepth = 0
  20. function blankLine(state) {
  21. state.code = false
  22. return null
  23. }
  24. var gfmOverlay = {
  25. startState: function () {
  26. return {
  27. code: false,
  28. codeBlock: false,
  29. ateSpace: false,
  30. }
  31. },
  32. copyState: function (s) {
  33. return {
  34. code: s.code,
  35. codeBlock: s.codeBlock,
  36. ateSpace: s.ateSpace,
  37. }
  38. },
  39. token: function (stream, state) {
  40. state.combineTokens = null
  41. // Hack to prevent formatting override inside code blocks (block and inline)
  42. if (state.codeBlock) {
  43. if (stream.match(/^```+/)) {
  44. state.codeBlock = false
  45. return null
  46. }
  47. stream.skipToEnd()
  48. return null
  49. }
  50. if (stream.sol()) {
  51. state.code = false
  52. }
  53. if (stream.sol() && stream.match(/^```+/)) {
  54. stream.skipToEnd()
  55. state.codeBlock = true
  56. return null
  57. }
  58. // If this block is changed, it may need to be updated in Markdown mode
  59. if (stream.peek() === '`') {
  60. stream.next()
  61. var before = stream.pos
  62. stream.eatWhile('`')
  63. var difference = 1 + stream.pos - before
  64. if (!state.code) {
  65. codeDepth = difference
  66. state.code = true
  67. } else {
  68. if (difference === codeDepth) {
  69. // Must be exact
  70. state.code = false
  71. }
  72. }
  73. return null
  74. } else if (state.code) {
  75. stream.next()
  76. return null
  77. }
  78. // Check if space. If so, links can be formatted later on
  79. if (stream.eatSpace()) {
  80. state.ateSpace = true
  81. return null
  82. }
  83. if (stream.sol() || state.ateSpace) {
  84. state.ateSpace = false
  85. if (modeConfig.gitHubSpice !== false) {
  86. if (stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+@)?(?=.{0,6}\d)(?:[a-f0-9]{7,40}\b)/)) {
  87. // User/Project@SHA
  88. // User@SHA
  89. // SHA
  90. state.combineTokens = true
  91. return 'link'
  92. } else if (stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+)?#[0-9]+\b/)) {
  93. // User/Project#Num
  94. // User#Num
  95. // #Num
  96. state.combineTokens = true
  97. return 'link'
  98. }
  99. }
  100. }
  101. if (stream.match(urlRE) && stream.string.slice(stream.start - 2, stream.start) != '](' && (stream.start == 0 || /\W/.test(stream.string.charAt(stream.start - 1)))) {
  102. // URLs
  103. // Taken from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
  104. // And then (issue #1160) simplified to make it not crash the Chrome Regexp engine
  105. // And then limited url schemes to the CommonMark list, so foo:bar isn't matched as a URL
  106. state.combineTokens = true
  107. return 'link'
  108. }
  109. stream.next()
  110. return null
  111. },
  112. blankLine: blankLine,
  113. }
  114. var markdownConfig = {
  115. taskLists: true,
  116. strikethrough: true,
  117. emoji: true,
  118. }
  119. for (var attr in modeConfig) {
  120. markdownConfig[attr] = modeConfig[attr]
  121. }
  122. markdownConfig.name = 'markdown'
  123. return CodeMirror.overlayMode(CodeMirror.getMode(config, markdownConfig), gfmOverlay)
  124. },
  125. 'markdown'
  126. )
  127. CodeMirror.defineMIME('text/x-gfm', 'gfm')
  128. })