scripts/CodeMirror/mode/gfm/gfm.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"), require("../markdown/markdown"), require("../../addon/mode/overlay"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror", "../markdown/markdown", "../../addon/mode/overlay"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11})(function(CodeMirror) {
12"use strict";
13
14var 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
15
16CodeMirror.defineMode("gfm", function(config, modeConfig) {
17 var codeDepth = 0;
18 function blankLine(state) {
19 state.code = false;
20 return null;
21 }
22 var gfmOverlay = {
23 startState: function() {
24 return {
25 code: false,
26 codeBlock: false,
27 ateSpace: false
28 };
29 },
30 copyState: function(s) {
31 return {
32 code: s.code,
33 codeBlock: s.codeBlock,
34 ateSpace: s.ateSpace
35 };
36 },
37 token: function(stream, state) {
38 state.combineTokens = null;
39
40 // Hack to prevent formatting override inside code blocks (block and inline)
41 if (state.codeBlock) {
42 if (stream.match(/^```+/)) {
43 state.codeBlock = false;
44 return null;
45 }
46 stream.skipToEnd();
47 return null;
48 }
49 if (stream.sol()) {
50 state.code = false;
51 }
52 if (stream.sol() && stream.match(/^```+/)) {
53 stream.skipToEnd();
54 state.codeBlock = true;
55 return null;
56 }
57 // If this block is changed, it may need to be updated in Markdown mode
58 if (stream.peek() === '`') {
59 stream.next();
60 var before = stream.pos;
61 stream.eatWhile('`');
62 var difference = 1 + stream.pos - before;
63 if (!state.code) {
64 codeDepth = difference;
65 state.code = true;
66 } else {
67 if (difference === codeDepth) { // Must be exact
68 state.code = false;
69 }
70 }
71 return null;
72 } else if (state.code) {
73 stream.next();
74 return null;
75 }
76 // Check if space. If so, links can be formatted later on
77 if (stream.eatSpace()) {
78 state.ateSpace = true;
79 return null;
80 }
81 if (stream.sol() || state.ateSpace) {
82 state.ateSpace = false;
83 if (modeConfig.gitHubSpice !== false) {
84 if(stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+@)?(?=.{0,6}\d)(?:[a-f0-9]{7,40}\b)/)) {
85 // User/Project@SHA
86 // User@SHA
87 // SHA
88 state.combineTokens = true;
89 return "link";
90 } else if (stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+)?#[0-9]+\b/)) {
91 // User/Project#Num
92 // User#Num
93 // #Num
94 state.combineTokens = true;
95 return "link";
96 }
97 }
98 }
99 if (stream.match(urlRE) &&
100 stream.string.slice(stream.start - 2, stream.start) != "](" &&
101 (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
115 var markdownConfig = {
116 taskLists: true,
117 strikethrough: true,
118 emoji: true
119 };
120 for (var attr in modeConfig) {
121 markdownConfig[attr] = modeConfig[attr];
122 }
123 markdownConfig.name = "markdown";
124 return CodeMirror.overlayMode(CodeMirror.getMode(config, markdownConfig), gfmOverlay);
125
126}, "markdown");
127
128 CodeMirror.defineMIME("text/x-gfm", "gfm");
129});