-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirect.js
More file actions
51 lines (43 loc) · 1.7 KB
/
Copy pathredirect.js
File metadata and controls
51 lines (43 loc) · 1.7 KB
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
// Helper function to get URL from redirect entry
function getRedirectUrl(entry) {
return typeof entry === 'string' ? entry : entry.url;
}
// Helper function to get description from redirect entry
function getRedirectDescription(entry) {
return typeof entry === 'string' ? '' : (entry.description || '');
}
// Get address in url bar
function getShortlink() {
const hostname = window.location.hostname;
const pathname = window.location.pathname;
// If accessing via custom domain
if (hostname === 'sih.tools') {
// Path is /shortlink, so remove leading slash
return pathname.substring(1).replace(/\/$/, '').toLowerCase();
}
// If accessing via GitHub Pages (my-org.github.io)
if (hostname.includes('github.io')) {
// Path is /links/shortlink, so remove /links/ prefix
const parts = pathname.split('/');
if (parts.length >= 3 && parts[1] === 'links') {
return parts[2].replace(/\/$/, '').toLowerCase();
}
// If somehow accessing /links/ directly, redirect to catch-all
return '';
}
// Fallback
return pathname.substring(1).replace(/\/$/, '').toLowerCase();
}
const shortlink = getShortlink();
const redirectEntry = REDIRECTS[shortlink];
let redirectUrl;
if (redirectEntry) {
redirectUrl = getRedirectUrl(redirectEntry);
} else {
// No match — send to directory search so user can find what they wanted
const base = window.location.hostname.includes('github.io')
? `/${window.location.pathname.split('/')[1]}/`
: '/';
redirectUrl = `${window.location.protocol}//${window.location.hostname}${base}?q=${encodeURIComponent(shortlink)}`;
}
window.location.replace(redirectUrl);