本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
5.0KB

  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("../markdown/markdown"), require("../../addon/mode/overlay"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "../markdown/markdown", "../../addon/mode/overlay"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. var urlRE = /^((?:(?: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
  13. CodeMirror.defineMode("gfm", function(config, modeConfig) {
  14. var codeDepth = 0;
  15. function blankLine(state) {
  16. state.code = false;
  17. return null;
  18. }
  19. var gfmOverlay = {
  20. startState: function() {
  21. return {
  22. code: false,
  23. codeBlock: false,
  24. ateSpace: false
  25. };
  26. },
  27. copyState: function(s) {
  28. return {
  29. code: s.code,
  30. codeBlock: s.codeBlock,
  31. ateSpace: s.ateSpace
  32. };
  33. },
  34. token: function(stream, state) {
  35. state.combineTokens = null;
  36. // Hack to prevent formatting override inside code blocks (block and inline)
  37. if (state.codeBlock) {
  38. if (stream.match(/^```+/)) {
  39. state.codeBlock = false;
  40. return null;
  41. }
  42. stream.skipToEnd();
  43. return null;
  44. }
  45. if (stream.sol()) {
  46. state.code = false;
  47. }
  48. if (stream.sol() && stream.match(/^```+/)) {
  49. stream.skipToEnd();
  50. state.codeBlock = true;
  51. return null;
  52. }
  53. // If this block is changed, it may need to be updated in Markdown mode
  54. if (stream.peek() === '`') {
  55. stream.next();
  56. var before = stream.pos;
  57. stream.eatWhile('`');
  58. var difference = 1 + stream.pos - before;
  59. if (!state.code) {
  60. codeDepth = difference;
  61. state.code = true;
  62. } else {
  63. if (difference === codeDepth) { // Must be exact
  64. state.code = false;
  65. }
  66. }
  67. return null;
  68. } else if (state.code) {
  69. stream.next();
  70. return null;
  71. }
  72. // Check if space. If so, links can be formatted later on
  73. if (stream.eatSpace()) {
  74. state.ateSpace = true;
  75. return null;
  76. }
  77. if (stream.sol() || state.ateSpace) {
  78. state.ateSpace = false;
  79. if (modeConfig.gitHubSpice !== false) {
  80. if(stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+@)?(?=.{0,6}\d)(?:[a-f0-9]{7,40}\b)/)) {
  81. // User/Project@SHA
  82. // User@SHA
  83. // SHA
  84. state.combineTokens = true;
  85. return "link";
  86. } else if (stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+)?#[0-9]+\b/)) {
  87. // User/Project#Num
  88. // User#Num
  89. // #Num
  90. state.combineTokens = true;
  91. return "link";
  92. }
  93. }
  94. }
  95. if (stream.match(urlRE) &&
  96. stream.string.slice(stream.start - 2, stream.start) != "](" &&
  97. (stream.start == 0 || /\W/.test(stream.string.charAt(stream.start - 1)))) {
  98. // URLs
  99. // Taken from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
  100. // And then (issue #1160) simplified to make it not crash the Chrome Regexp engine
  101. // And then limited url schemes to the CommonMark list, so foo:bar isn't matched as a URL
  102. state.combineTokens = true;
  103. return "link";
  104. }
  105. stream.next();
  106. return null;
  107. },
  108. blankLine: blankLine
  109. };
  110. var markdownConfig = {
  111. taskLists: true,
  112. strikethrough: true,
  113. emoji: true
  114. };
  115. for (var attr in modeConfig) {
  116. markdownConfig[attr] = modeConfig[attr];
  117. }
  118. markdownConfig.name = "markdown";
  119. return CodeMirror.overlayMode(CodeMirror.getMode(config, markdownConfig), gfmOverlay);
  120. }, "markdown");
  121. CodeMirror.defineMIME("text/x-gfm", "gfm");
  122. });
上海开阖软件有限公司 沪ICP备12045867号-1