-
Notifications
You must be signed in to change notification settings - Fork 24
/
links.js
138 lines (73 loc) · 2.18 KB
/
links.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
/*
* Codeit Link API
* Creates and decodes Codeit links
*/
// create link
function createLink(linkData) {
// save link
let base = 'https://cde.run';
if (window.location.hostname === 'dev.codeit.codes') base = 'https://dev.cde.run';
let link = '';
if (linkData.dir) {
const [user, repo, contents] = linkData.dir;
let [repoName, branch] = repo.split(':');
if (user && repo) {
const modRepo = modifiedRepos[user + '/' + repoName];
// if selected branch is the default branch
if (modRepo &&
(modRepo.defaultBranch === branch)) {
branch = '';
} else if (branch) {
branch = ':' + branch;
} else {
branch = '';
}
link += '/' + encodeURI(user) +
'/' + encodeURI(repoName + branch);
if (contents) {
link += encodeURI(contents);
}
if (linkData.file) {
const fileName = linkData.file.name;
link += '/' + encodeURI(fileName);
if (linkData.openLive === false &&
(fileName.endsWith('.html') || fileName.endsWith('.svg') ||
fileName.endsWith('.md'))) {
link += '?l=f';
}
}
}
}
return base + link;
}
// decode link
function decodeLink(url) {
// save link data
let link = new URL(url);
let linkData = {};
if (link.origin === window.location.origin) {
link = link.searchParams;
if (link.get('dir')) {
const dir = link.get('dir').split(',');
if (dir.length === 3) {
linkData.dir = dir;
if (link.get('file')) {
const file = link.get('file');
if (file) {
linkData.file = file;
if (link.get('openLive') === 'true') {
linkData.openLive = true;
}
}
}
}
}
if (link.get('embed')) {
linkData.embed = true;
}
if (link.get('gitCode')) {
linkData.gitCode = link.get('gitCode');
}
}
return linkData;
}