| 776 |
lars |
1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
2 |
// Distributed under an MIT license: http://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 |
|
|
|
14 |
CodeMirror.defineMode("gfm", function(config, modeConfig) {
|
|
|
15 |
var codeDepth = 0;
|
|
|
16 |
function blankLine(state) {
|
|
|
17 |
state.code = false;
|
|
|
18 |
return null;
|
|
|
19 |
}
|
|
|
20 |
var gfmOverlay = {
|
|
|
21 |
startState: function() {
|
|
|
22 |
return {
|
|
|
23 |
code: false,
|
|
|
24 |
codeBlock: false,
|
|
|
25 |
ateSpace: false
|
|
|
26 |
};
|
|
|
27 |
},
|
|
|
28 |
copyState: function(s) {
|
|
|
29 |
return {
|
|
|
30 |
code: s.code,
|
|
|
31 |
codeBlock: s.codeBlock,
|
|
|
32 |
ateSpace: s.ateSpace
|
|
|
33 |
};
|
|
|
34 |
},
|
|
|
35 |
token: function(stream, state) {
|
|
|
36 |
state.combineTokens = null;
|
|
|
37 |
|
|
|
38 |
// Hack to prevent formatting override inside code blocks (block and inline)
|
|
|
39 |
if (state.codeBlock) {
|
|
|
40 |
if (stream.match(/^```/)) {
|
|
|
41 |
state.codeBlock = false;
|
|
|
42 |
return null;
|
|
|
43 |
}
|
|
|
44 |
stream.skipToEnd();
|
|
|
45 |
return null;
|
|
|
46 |
}
|
|
|
47 |
if (stream.sol()) {
|
|
|
48 |
state.code = false;
|
|
|
49 |
}
|
|
|
50 |
if (stream.sol() && stream.match(/^```/)) {
|
|
|
51 |
stream.skipToEnd();
|
|
|
52 |
state.codeBlock = true;
|
|
|
53 |
return null;
|
|
|
54 |
}
|
|
|
55 |
// If this block is changed, it may need to be updated in Markdown mode
|
|
|
56 |
if (stream.peek() === '`') {
|
|
|
57 |
stream.next();
|
|
|
58 |
var before = stream.pos;
|
|
|
59 |
stream.eatWhile('`');
|
|
|
60 |
var difference = 1 + stream.pos - before;
|
|
|
61 |
if (!state.code) {
|
|
|
62 |
codeDepth = difference;
|
|
|
63 |
state.code = true;
|
|
|
64 |
} else {
|
|
|
65 |
if (difference === codeDepth) { // Must be exact
|
|
|
66 |
state.code = false;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
return null;
|
|
|
70 |
} else if (state.code) {
|
|
|
71 |
stream.next();
|
|
|
72 |
return null;
|
|
|
73 |
}
|
|
|
74 |
// Check if space. If so, links can be formatted later on
|
|
|
75 |
if (stream.eatSpace()) {
|
|
|
76 |
state.ateSpace = true;
|
|
|
77 |
return null;
|
|
|
78 |
}
|
|
|
79 |
if (stream.sol() || state.ateSpace) {
|
|
|
80 |
state.ateSpace = false;
|
|
|
81 |
if(stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+@)?(?:[a-f0-9]{7,40}\b)/)) {
|
|
|
82 |
// User/Project@SHA
|
|
|
83 |
// User@SHA
|
|
|
84 |
// SHA
|
|
|
85 |
state.combineTokens = true;
|
|
|
86 |
return "link";
|
|
|
87 |
} else if (stream.match(/^(?:[a-zA-Z0-9\-_]+\/)?(?:[a-zA-Z0-9\-_]+)?#[0-9]+\b/)) {
|
|
|
88 |
// User/Project#Num
|
|
|
89 |
// User#Num
|
|
|
90 |
// #Num
|
|
|
91 |
state.combineTokens = true;
|
|
|
92 |
return "link";
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
if (stream.match(/^((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]|\([^\s()<>]*\))+(?:\([^\s()<>]*\)|[^\s`*!()\[\]{};:'".,<>?«»“”‘’]))/i) &&
|
|
|
96 |
stream.string.slice(stream.start - 2, stream.start) != "](") {
|
|
|
97 |
// URLs
|
|
|
98 |
// Taken from http://daringfireball.net/2010/07/improved_regex_for_matching_urls
|
|
|
99 |
// And then (issue #1160) simplified to make it not crash the Chrome Regexp engine
|
|
|
100 |
state.combineTokens = true;
|
|
|
101 |
return "link";
|
|
|
102 |
}
|
|
|
103 |
stream.next();
|
|
|
104 |
return null;
|
|
|
105 |
},
|
|
|
106 |
blankLine: blankLine
|
|
|
107 |
};
|
|
|
108 |
|
|
|
109 |
var markdownConfig = {
|
|
|
110 |
underscoresBreakWords: false,
|
|
|
111 |
taskLists: true,
|
|
|
112 |
fencedCodeBlocks: true,
|
|
|
113 |
strikethrough: 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 |
|
|
|
121 |
}, "markdown");
|
|
|
122 |
|
|
|
123 |
CodeMirror.defineMIME("text/x-gfm", "gfm");
|
|
|
124 |
});
|