-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathapi-link-parser.js
167 lines (99 loc) · 3.61 KB
/
api-link-parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// decodes cde.run links
// eg.
// https://cde.run/mrdoob/three.js/examples/physics_ammo_break.html
// or
// https://cde.run/https://github.com/mrdoob/three.js/blob/dev/examples/physics_ammo_break.html
function decodeLink(url) {
// save link data
const urlQuery = new URL(url).searchParams;
url = decodeURIComponent(url);
const isEmbed = urlQuery.has('embed');
const isLiveViewDisabled = (urlQuery.get('live') === 'false' || urlQuery.get('l') === 'f');
// remove query from URL
url = url.replace('?' + urlQuery.toString(), '');
const isDev = url.startsWith('https://dev.cde.run/');
if (!isDev) url = url.slice('https://cde.run/'.length);
else url = url.slice('https://dev.cde.run/'.length);
const isGithub = url.startsWith('https://github.com/');
if (isGithub) url = url.slice('https://github.com/'.length);
// if link is a Git URL
if (isGithub && url.endsWith('.git')) {
// slice .git ending
url = url.slice(0, -('.git'.length));
}
let baseURL = 'https://codeit.codes';
if (isDev) baseURL = 'https://dev.codeit.codes';
let linkData = {};
let link = url.split('/');
// if link exists
if (link.length > 1) {
linkData.user = link[0];
linkData.repo = link[1];
linkData.contents = url.slice((linkData.user + '/' + linkData.repo).length);
if (linkData.contents.endsWith('/')) {
linkData.contents = linkData.contents.slice(0, -1);
}
// if link includes a Github URL
if (isGithub) {
linkData.contents = linkData.contents.slice('/blob'.length);
// if link includes a branch
if (link[3]) {
linkData.repo += ':' + link[3];
linkData.contents = linkData.contents.slice(('/' + link[3]).length);
}
}
const lastEntry = link[link.length - 1];
// if linking to file
if (lastEntry !== linkData.repo
&& lastEntry.split('.').length > 1) {
linkData.file = lastEntry;
linkData.contents = linkData.contents.slice(0, (-lastEntry.length - 1));
// if linked file can be viewed live
// and live view not disabled
if ((lastEntry.endsWith('.html') || lastEntry.endsWith('.svg') ||
lastEntry.endsWith('.md'))
&& (isLiveViewDisabled === false)) {
// show file in live view
linkData.openLive = true;
} // else, show the file's code
} else if (isEmbed) { // if linking to directory
// and link is embed
// show directory link
linkData.redirect = encodeURIComponent(
baseURL + '/full?dir=' +
linkData.user + ',' + linkData.repo +
',' + linkData.contents
);
linkData.redirectText = encodeURI(
'Open ' +
linkData.user + '/' +
linkData.repo.split(':')[0] +
' with Codeit'
);
}
} else {
// show codeit link
linkData.redirect = encodeURIComponent(baseURL + '/full');
linkData.redirectText = encodeURI('Open Codeit');
}
// build link from data
let resp = baseURL;
// if redirect exists
if (linkData.redirect) {
resp += '/redirect?to=' + linkData.redirect +
'&text=' + linkData.redirectText;
} else {
resp += '/full?dir=' +
linkData.user + ',' + linkData.repo +
',' + linkData.contents;
// if file exists
if (linkData.file) {
resp += '&file=' + linkData.file;
// if live view flag exists
if (linkData.openLive) {
resp += '&openLive=true';
}
}
}
return resp;
}