-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuepress-redirect.patch
169 lines (168 loc) · 5.97 KB
/
vuepress-redirect.patch
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
167
168
169
// Replace node_modules/vuepress-plugin-redirect/lib/enhanceApp.js with this
import { join } from 'path';
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
import pluginRedirectOptions from '@dynamic/plugin-redirect-options';
const options = pluginRedirectOptions;
const enhanceApp = ({ router, siteData }) => {
const { routes = [] } = router.options;
const { redirectors: rawRedirectors = [] } = options;
// if a path has corresponding route
function hasRoute(path) {
return routes.some((route) => route.path.toLowerCase() === path.toLowerCase());
}
// get the route or fallback route of a path
function getFallbackRoute(path) {
// if has exact route
if (hasRoute(path))
return path;
// if has route with /
if (!/\/$/.test(path)) {
const endingSlashUrl = path + '/';
if (hasRoute(endingSlashUrl))
return endingSlashUrl;
}
// if has route with .html
if (!/\.html$/.test(path)) {
const endingHtmlUrl = path.replace(/\/$/, '') + '.html';
if (hasRoute(endingHtmlUrl))
return endingHtmlUrl;
}
return null;
}
// locales redirector
if (options.locales && siteData.locales) {
// resolve locales config from siteData
const siteLocales = siteData.locales;
const localeKeys = Object.keys(siteLocales);
const locales = localeKeys.map((key) => ({
key: key.replace(/^\/|\/$/, ''),
lang: siteLocales[key].lang,
}));
// resolve locales config from plugin options
if (typeof options.locales !== 'object') {
options.locales = {};
}
const { fallback, storage = true } = options.locales;
if (fallback) {
localeKeys.unshift(fallback);
}
// add locales redirector
rawRedirectors.unshift({
storage,
base: '/',
alternative() {
if (typeof window !== 'undefined' && window.navigator) {
const langs = window.navigator.languages || [
window.navigator.language,
];
let locale = locales.find(({ lang }) => langs.includes(lang))
if (!locale) {
// no exact match found, ignore region-specific dialects as a fallback
const shortLangs = langs.map(language => language.substring(0, 2))
locale = locales.find(({ lang }) =>
shortLangs.includes(lang.substring(0, 2))
)
}
if (locale) {
return locale.key;
}
}
return localeKeys;
},
});
}
// all redirectors
const redirectors = rawRedirectors.map(({ base = '/', storage: rawStorage = false, alternative }) => {
let storage = false;
if (rawStorage) {
if (typeof rawStorage !== 'object') {
const key = typeof rawStorage !== 'string'
? `vuepress:redirect:${base}`
: rawStorage;
storage = {
get() {
if (typeof localStorage === 'undefined')
return null;
return localStorage.getItem(key);
},
set(val) {
if (typeof localStorage === 'undefined')
return;
localStorage.setItem(key, val);
},
};
}
else if (rawStorage.get && rawStorage.set) {
// warning
storage = rawStorage;
}
}
return {
base,
storage,
alternative,
};
});
router.beforeEach((to, from, next) => {
// if router exists, skip redirection
const fallback = getFallbackRoute(to.path);
if (fallback)
return next();
let target;
for (const redirector of redirectors) {
const { base = '/', storage = false } = redirector;
let { alternative } = redirector;
if (!to.path.startsWith(base))
continue;
// get rest of the path
// ensure ending slash at root
const rest = to.path.slice(base.length) || '/';
if (storage) {
const alt = storage.get(redirector);
if (alt) {
const path = getFallbackRoute(join(base, alt, rest));
if (path) {
target = path;
break;
}
}
}
// resolve alternatives
if (typeof alternative === 'function') {
alternative = alternative(rest);
}
if (!alternative)
continue;
if (typeof alternative === 'string') {
alternative = [alternative];
}
for (const alt of alternative) {
const path = getFallbackRoute(join(base, alt, rest));
if (path) {
target = path;
break;
}
}
if (target)
break;
}
next(target);
});
router.afterEach((to) => {
// if router doesn't exist, skip storage
if (!hasRoute(to.path))
return;
for (const redirector of redirectors) {
const { base, storage } = redirector;
if (!storage || !to.path.startsWith(base))
continue;
const alt = to.path.slice(base.length).split('/')[0];
if (alt) {
storage.set(alt, redirector);
}
}
});
};
export default enhanceApp;
//# sourceMappingURL=enhanceApp.js.map