@vuepress/plugin-redirect 2.0.0-rc.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/lib/cli/index.d.ts +2 -0
- package/lib/cli/index.js +74 -0
- package/lib/client/components/LanguageSwitch.d.ts +4 -0
- package/lib/client/components/LanguageSwitch.js +99 -0
- package/lib/client/composables/index.d.ts +1 -0
- package/lib/client/composables/index.js +1 -0
- package/lib/client/composables/setupDevServerRedirect.d.ts +1 -0
- package/lib/client/composables/setupDevServerRedirect.js +80 -0
- package/lib/client/config.d.ts +3 -0
- package/lib/client/config.js +10 -0
- package/lib/client/define.d.ts +3 -0
- package/lib/client/define.js +3 -0
- package/lib/client/styles/language-switch.css +57 -0
- package/lib/node/ensureRootHomePage.d.ts +3 -0
- package/lib/node/ensureRootHomePage.js +68 -0
- package/lib/node/generate/generateAutoLocaleRedirectFiles.d.ts +3 -0
- package/lib/node/generate/generateAutoLocaleRedirectFiles.js +28 -0
- package/lib/node/generate/generateRedirectFiles.d.ts +2 -0
- package/lib/node/generate/generateRedirectFiles.js +21 -0
- package/lib/node/generate/getLocaleRedirectHTML.d.ts +2 -0
- package/lib/node/generate/getLocaleRedirectHTML.js +77 -0
- package/lib/node/generate/getRedirectHTML.d.ts +1 -0
- package/lib/node/generate/getRedirectHTML.js +18 -0
- package/lib/node/generate/index.d.ts +2 -0
- package/lib/node/generate/index.js +2 -0
- package/lib/node/getRedirectLocaleConfig.d.ts +4 -0
- package/lib/node/getRedirectLocaleConfig.js +35 -0
- package/lib/node/getRedirectMap.d.ts +3 -0
- package/lib/node/getRedirectMap.js +25 -0
- package/lib/node/handleRedirectTo.d.ts +2 -0
- package/lib/node/handleRedirectTo.js +19 -0
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +1 -0
- package/lib/node/locales.d.ts +3 -0
- package/lib/node/locales.js +129 -0
- package/lib/node/logger.d.ts +3 -0
- package/lib/node/logger.js +3 -0
- package/lib/node/redirectPlugin.d.ts +3 -0
- package/lib/node/redirectPlugin.js +53 -0
- package/lib/node/types/frontmatter.d.ts +4 -0
- package/lib/node/types/frontmatter.js +1 -0
- package/lib/node/types/index.d.ts +2 -0
- package/lib/node/types/index.js +1 -0
- package/lib/node/types/options.d.ts +22 -0
- package/lib/node/types/options.js +1 -0
- package/lib/shared/index.d.ts +3 -0
- package/lib/shared/index.js +3 -0
- package/lib/shared/localeConfig.d.ts +54 -0
- package/lib/shared/localeConfig.js +1 -0
- package/lib/shared/locales.d.ts +24 -0
- package/lib/shared/locales.js +1 -0
- package/lib/shared/normalizePath.d.ts +1 -0
- package/lib/shared/normalizePath.js +4 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-present, VuePress Community
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/lib/cli/index.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from 'node:module';
|
|
3
|
+
import { removeEndingSlash, removeLeadingSlash } from '@vuepress/helper';
|
|
4
|
+
import { cac } from 'cac';
|
|
5
|
+
import { loadUserConfig, resolveAppConfig, resolveCliAppConfig, resolveUserConfigConventionalPath, transformUserConfigToPlugin, } from 'vuepress/cli';
|
|
6
|
+
import { createBuildApp } from 'vuepress/core';
|
|
7
|
+
import { fs, logger, path } from 'vuepress/utils';
|
|
8
|
+
import { getRedirectHTML } from '../node/generate/getRedirectHTML.js';
|
|
9
|
+
const require = createRequire(import.meta.url);
|
|
10
|
+
const cli = cac('vp-redirect');
|
|
11
|
+
const { version } = require('@vuepress/plugin-redirect/package.json');
|
|
12
|
+
cli
|
|
13
|
+
.command('generate [source-dir]', 'Generate redirect site using VuePress project under source folder')
|
|
14
|
+
.option('--hostname <hostname>', 'Hostname to redirect to (E.g.: https://new.example.com/)', { default: '/' })
|
|
15
|
+
.option('-c, --config <config>', 'Set path to config file')
|
|
16
|
+
.option('-o, --output <output>', 'Set the output directory (default: .vuepress/redirect)')
|
|
17
|
+
.option('--cache <cache>', 'Set the directory of the cache files')
|
|
18
|
+
.option('-t, --temp <temp>', 'Set the directory of the temporary files')
|
|
19
|
+
.option('--clean-cache', 'Clean the cache files before generation')
|
|
20
|
+
.option('--clean-temp', 'Clean the temporary files before generation')
|
|
21
|
+
.action(async (sourceDir, commandOptions) => {
|
|
22
|
+
if (!sourceDir)
|
|
23
|
+
return cli.outputHelp();
|
|
24
|
+
// ensure NODE_ENV is set
|
|
25
|
+
process.env.NODE_ENV ??= 'production';
|
|
26
|
+
// resolve app config from cli options
|
|
27
|
+
const cliAppConfig = resolveCliAppConfig(sourceDir, {});
|
|
28
|
+
// resolve user config file
|
|
29
|
+
const userConfigPath = resolveUserConfigConventionalPath(cliAppConfig.source);
|
|
30
|
+
const { userConfig } = await loadUserConfig(userConfigPath);
|
|
31
|
+
// resolve the final app config to use
|
|
32
|
+
const appConfig = resolveAppConfig({
|
|
33
|
+
defaultAppConfig: {},
|
|
34
|
+
cliAppConfig,
|
|
35
|
+
userConfig,
|
|
36
|
+
});
|
|
37
|
+
if (appConfig === null)
|
|
38
|
+
return;
|
|
39
|
+
// create vuepress app
|
|
40
|
+
const app = createBuildApp(appConfig);
|
|
41
|
+
// use user-config plugin
|
|
42
|
+
app.use(transformUserConfigToPlugin(userConfig, cliAppConfig.source));
|
|
43
|
+
// clean temp and cache
|
|
44
|
+
if (commandOptions.cleanTemp === true) {
|
|
45
|
+
logger.info('Cleaning temp...');
|
|
46
|
+
await fs.remove(app.dir.temp());
|
|
47
|
+
}
|
|
48
|
+
if (commandOptions.cleanCache === true) {
|
|
49
|
+
logger.info('Cleaning cache...');
|
|
50
|
+
await fs.remove(app.dir.cache());
|
|
51
|
+
}
|
|
52
|
+
const outputFolder = commandOptions.output
|
|
53
|
+
? path.join(process.cwd(), commandOptions.output)
|
|
54
|
+
: path.join(app.dir.source(), '.vuepress', 'redirect');
|
|
55
|
+
// empty output directory
|
|
56
|
+
await fs.emptyDir(outputFolder);
|
|
57
|
+
// initialize vuepress app to get pages
|
|
58
|
+
logger.info('Initializing VuePress and preparing data...');
|
|
59
|
+
await app.init();
|
|
60
|
+
// redirect all pages
|
|
61
|
+
// initialize vuepress app to get pages
|
|
62
|
+
logger.info('Generating redirect pages...');
|
|
63
|
+
await Promise.all(app.pages.map((page) => {
|
|
64
|
+
const redirectUrl = `${removeEndingSlash(commandOptions.hostname)}${app.options.base}${removeLeadingSlash(page.path)}`;
|
|
65
|
+
const destLocation = path.join(outputFolder, removeLeadingSlash(page.path.replace(/\/$/, '/index.html')));
|
|
66
|
+
return fs
|
|
67
|
+
.ensureDir(path.dirname(destLocation))
|
|
68
|
+
.then(() => fs.writeFile(destLocation, getRedirectHTML(redirectUrl)));
|
|
69
|
+
}));
|
|
70
|
+
});
|
|
71
|
+
cli.command('').action(() => cli.outputHelp());
|
|
72
|
+
cli.help();
|
|
73
|
+
cli.version(version);
|
|
74
|
+
cli.parse();
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { VNode } from 'vue';
|
|
2
|
+
import '../styles/language-switch.css';
|
|
3
|
+
declare const _default: import("vue").DefineComponent<{}, () => VNode | null, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{}>>, {}, {}>;
|
|
4
|
+
export default _default;
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { usePreferredLanguages, useScrollLock, useSessionStorage, } from '@vueuse/core';
|
|
2
|
+
import { computed, defineComponent, h, onMounted, onUnmounted, ref, TransitionGroup, watch, } from 'vue';
|
|
3
|
+
import { useRoute, useRouter } from 'vue-router';
|
|
4
|
+
import { useRouteLocale } from 'vuepress/client';
|
|
5
|
+
import { redirectLocaleConfig, redirectLocaleEntries } from '../define.js';
|
|
6
|
+
import '../styles/language-switch.css';
|
|
7
|
+
const redirectLocales = __REDIRECT_LOCALES__;
|
|
8
|
+
const { switchLocale } = redirectLocaleConfig;
|
|
9
|
+
const REDIRECT_LOCALE_STORAGE = useSessionStorage('VUEPRESS_REDIRECT_LOCALES', {});
|
|
10
|
+
export default defineComponent({
|
|
11
|
+
name: 'LanguageSwitch',
|
|
12
|
+
setup() {
|
|
13
|
+
const languages = usePreferredLanguages();
|
|
14
|
+
const route = useRoute();
|
|
15
|
+
const router = useRouter();
|
|
16
|
+
const routeLocale = useRouteLocale();
|
|
17
|
+
const showModal = ref(false);
|
|
18
|
+
const info = computed(() => {
|
|
19
|
+
if (redirectLocaleEntries.some(([key]) => routeLocale.value === key))
|
|
20
|
+
for (const language of languages.value)
|
|
21
|
+
for (const [localePath, langs] of redirectLocaleEntries)
|
|
22
|
+
if (langs.includes(language)) {
|
|
23
|
+
if (localePath === routeLocale.value)
|
|
24
|
+
return null;
|
|
25
|
+
return {
|
|
26
|
+
lang: language,
|
|
27
|
+
localePath,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
});
|
|
32
|
+
const locale = computed(() => {
|
|
33
|
+
if (info.value) {
|
|
34
|
+
const { lang, localePath } = info.value;
|
|
35
|
+
const locales = [
|
|
36
|
+
redirectLocales[routeLocale.value],
|
|
37
|
+
redirectLocales[localePath],
|
|
38
|
+
];
|
|
39
|
+
return {
|
|
40
|
+
hint: locales.map(({ hint }) => hint.replace('$1', lang)),
|
|
41
|
+
switch: locales
|
|
42
|
+
.map(({ switch: switchText }) => switchText.replace('$1', lang))
|
|
43
|
+
.join(' / '),
|
|
44
|
+
cancel: locales.map(({ cancel }) => cancel).join(' / '),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
});
|
|
49
|
+
const targetRoute = computed(() => info.value
|
|
50
|
+
? route.path.replace(routeLocale.value, info.value.localePath)
|
|
51
|
+
: null);
|
|
52
|
+
const updateStatus = () => {
|
|
53
|
+
REDIRECT_LOCALE_STORAGE.value[routeLocale.value] = true;
|
|
54
|
+
showModal.value = false;
|
|
55
|
+
};
|
|
56
|
+
onMounted(() => {
|
|
57
|
+
const isLocked = useScrollLock(document.body);
|
|
58
|
+
if (!REDIRECT_LOCALE_STORAGE.value[routeLocale.value])
|
|
59
|
+
if (info.value)
|
|
60
|
+
if (switchLocale === 'direct')
|
|
61
|
+
router.replace(targetRoute.value);
|
|
62
|
+
else if (switchLocale === 'modal')
|
|
63
|
+
showModal.value = true;
|
|
64
|
+
else
|
|
65
|
+
showModal.value = false;
|
|
66
|
+
else
|
|
67
|
+
showModal.value = false;
|
|
68
|
+
watch(showModal, (value) => {
|
|
69
|
+
isLocked.value = value;
|
|
70
|
+
}, { immediate: true });
|
|
71
|
+
onUnmounted(() => {
|
|
72
|
+
isLocked.value = false;
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
return () => showModal.value
|
|
76
|
+
? h(TransitionGroup, { name: 'lang-modal-fade' }, () => showModal.value
|
|
77
|
+
? h('div', { key: 'mask', class: 'lang-modal-mask' }, h('div', {
|
|
78
|
+
key: 'popup',
|
|
79
|
+
class: 'lang-modal-wrapper',
|
|
80
|
+
}, [
|
|
81
|
+
h('div', { class: 'lang-modal-content' }, locale.value?.hint.map((text) => h('p', text))),
|
|
82
|
+
h('button', {
|
|
83
|
+
type: 'button',
|
|
84
|
+
class: 'lang-modal-action primary',
|
|
85
|
+
onClick: () => {
|
|
86
|
+
updateStatus();
|
|
87
|
+
router.replace(targetRoute.value);
|
|
88
|
+
},
|
|
89
|
+
}, locale.value?.switch),
|
|
90
|
+
h('button', {
|
|
91
|
+
type: 'button',
|
|
92
|
+
class: 'lang-modal-action',
|
|
93
|
+
onClick: () => updateStatus(),
|
|
94
|
+
}, locale.value?.cancel),
|
|
95
|
+
]))
|
|
96
|
+
: null)
|
|
97
|
+
: null;
|
|
98
|
+
},
|
|
99
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './setupDevServerRedirect.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './setupDevServerRedirect.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const setupDevServerRedirect: () => void;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { redirectMap } from '@temp/redirect/map.js';
|
|
2
|
+
import { entries, isLinkHttp } from '@vuepress/helper/client';
|
|
3
|
+
import { usePreferredLanguages } from '@vueuse/core';
|
|
4
|
+
import { computed, watch } from 'vue';
|
|
5
|
+
import { useRoute, useRouter } from 'vue-router';
|
|
6
|
+
import { useRouteLocale } from 'vuepress/client';
|
|
7
|
+
import { normalizePath } from '../../shared/index.js';
|
|
8
|
+
import { redirectLocaleConfig, redirectLocaleEntries } from '../define.js';
|
|
9
|
+
const { autoLocale, defaultBehavior, defaultLocale: defaultLocalePath, localeFallback, } = redirectLocaleConfig;
|
|
10
|
+
export const setupDevServerRedirect = () => {
|
|
11
|
+
const languages = usePreferredLanguages();
|
|
12
|
+
const route = useRoute();
|
|
13
|
+
const router = useRouter();
|
|
14
|
+
const routeLocale = useRouteLocale();
|
|
15
|
+
const isRootLocale = computed(() => routeLocale.value === '/');
|
|
16
|
+
const handleLocaleRedirect = () => {
|
|
17
|
+
const routes = router.getRoutes();
|
|
18
|
+
const defaultLocale = defaultLocalePath &&
|
|
19
|
+
routes.some(({ path }) => path === route.path.replace('/', defaultLocalePath))
|
|
20
|
+
? defaultLocalePath
|
|
21
|
+
: routes.find(({ path }) => route.path.split('/').length >= 3 &&
|
|
22
|
+
path === route.path.replace(/^\/[^/]+\//, '/'))?.path;
|
|
23
|
+
let matchedLocalePath = null;
|
|
24
|
+
// get matched locale
|
|
25
|
+
// eslint-disable-next-line no-labels
|
|
26
|
+
findLanguage: for (const lang of languages.value)
|
|
27
|
+
for (const [localePath, langs] of redirectLocaleEntries)
|
|
28
|
+
if (langs.includes(lang)) {
|
|
29
|
+
if (localeFallback &&
|
|
30
|
+
routes.every(({ path }) => path !== route.path.replace('/', path)))
|
|
31
|
+
continue;
|
|
32
|
+
matchedLocalePath = localePath;
|
|
33
|
+
// eslint-disable-next-line no-labels
|
|
34
|
+
break findLanguage;
|
|
35
|
+
}
|
|
36
|
+
// default link
|
|
37
|
+
const defaultRoute = defaultLocale
|
|
38
|
+
? route.fullPath.replace('/', defaultLocale)
|
|
39
|
+
: null;
|
|
40
|
+
// a locale matches
|
|
41
|
+
if (matchedLocalePath) {
|
|
42
|
+
const hasLocalePage = routes.some(({ path }) => route.path.replace('/', matchedLocalePath) === path);
|
|
43
|
+
const localeRoute = route.fullPath.replace('/', matchedLocalePath);
|
|
44
|
+
const routePath =
|
|
45
|
+
// the locale page exists
|
|
46
|
+
hasLocalePage
|
|
47
|
+
? localeRoute
|
|
48
|
+
: // the page does not exist
|
|
49
|
+
defaultBehavior === 'homepage'
|
|
50
|
+
? // locale homepage
|
|
51
|
+
matchedLocalePath
|
|
52
|
+
: defaultBehavior === 'defaultLocale' && defaultRoute
|
|
53
|
+
? // default locale page
|
|
54
|
+
defaultRoute
|
|
55
|
+
: // as is to get a 404 page of that locale
|
|
56
|
+
localeRoute;
|
|
57
|
+
router.replace(routePath);
|
|
58
|
+
}
|
|
59
|
+
// we have a default page
|
|
60
|
+
else if (defaultRoute) {
|
|
61
|
+
router.replace(defaultRoute);
|
|
62
|
+
}
|
|
63
|
+
else if (route.path !== '/404.html') {
|
|
64
|
+
router.replace('/404.html');
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
watch(() => route.path, (path) => {
|
|
68
|
+
// handle redirects
|
|
69
|
+
for (const [from, to] of entries(redirectMap))
|
|
70
|
+
if (normalizePath(path.toLowerCase()) === from.toLowerCase()) {
|
|
71
|
+
if (isLinkHttp(to))
|
|
72
|
+
window.open(to);
|
|
73
|
+
else
|
|
74
|
+
router.replace(to);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (autoLocale && isRootLocale.value)
|
|
78
|
+
handleLocaleRedirect();
|
|
79
|
+
}, { immediate: true });
|
|
80
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { defineClientConfig } from 'vuepress/client';
|
|
2
|
+
import LanguageSwitch from './components/LanguageSwitch.js';
|
|
3
|
+
import { setupDevServerRedirect } from './composables/setupDevServerRedirect.js';
|
|
4
|
+
export default defineClientConfig({
|
|
5
|
+
setup() {
|
|
6
|
+
if (__VUEPRESS_DEV__)
|
|
7
|
+
setupDevServerRedirect();
|
|
8
|
+
},
|
|
9
|
+
rootComponents: __REDIRECT_LOCALE_SWITCH__ ? [LanguageSwitch] : [],
|
|
10
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
.lang-modal-fade-enter-active,
|
|
2
|
+
.lang-modal-fade-leave-active {
|
|
3
|
+
transition: opacity 0.5s;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
.lang-modal-fade-enter,
|
|
7
|
+
.lang-modal-fade-leave-to {
|
|
8
|
+
opacity: 0;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
.lang-modal-mask {
|
|
12
|
+
position: fixed;
|
|
13
|
+
inset: 0;
|
|
14
|
+
z-index: 1499;
|
|
15
|
+
display: flex;
|
|
16
|
+
align-items: center;
|
|
17
|
+
justify-content: center;
|
|
18
|
+
backdrop-filter: blur(10px);
|
|
19
|
+
}
|
|
20
|
+
@media print {
|
|
21
|
+
.lang-modal-mask {
|
|
22
|
+
display: none;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.lang-modal-wrapper {
|
|
27
|
+
position: relative;
|
|
28
|
+
z-index: 1500;
|
|
29
|
+
overflow: hidden;
|
|
30
|
+
max-width: 80vw;
|
|
31
|
+
padding: 1rem 2rem;
|
|
32
|
+
border-radius: 8px;
|
|
33
|
+
background: var(--vp-bg);
|
|
34
|
+
box-shadow: 0 2px 6px 0 var(--card-shadow);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
.lang-modal-action {
|
|
38
|
+
display: block;
|
|
39
|
+
width: 100%;
|
|
40
|
+
margin: 1rem 0;
|
|
41
|
+
padding: 0.5rem 0.75rem;
|
|
42
|
+
border: none;
|
|
43
|
+
border-radius: 8px;
|
|
44
|
+
background-color: var(--vp-bglt);
|
|
45
|
+
color: var(--vp-c);
|
|
46
|
+
cursor: pointer;
|
|
47
|
+
}
|
|
48
|
+
.lang-modal-action:hover {
|
|
49
|
+
background-color: var(--vp-bgl);
|
|
50
|
+
}
|
|
51
|
+
.lang-modal-action.primary {
|
|
52
|
+
background-color: var(--vp-tc);
|
|
53
|
+
color: var(--white);
|
|
54
|
+
}
|
|
55
|
+
.lang-modal-action.primary:hover {
|
|
56
|
+
background-color: var(--vp-tcl);
|
|
57
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { removeEndingSlash } from '@vuepress/helper';
|
|
2
|
+
import { createPage } from 'vuepress/core';
|
|
3
|
+
export const ensureRootHomePage = async (app, localeOptions) => {
|
|
4
|
+
const { options: { base }, pages, } = app;
|
|
5
|
+
if (
|
|
6
|
+
// root homepage not exists
|
|
7
|
+
pages.every(({ path }) => path !== '/')) {
|
|
8
|
+
const availableLocales = pages
|
|
9
|
+
.filter(({ pathLocale, path }) => pathLocale === path)
|
|
10
|
+
.map(({ pathLocale }) => pathLocale);
|
|
11
|
+
pages.push(await createPage(app, {
|
|
12
|
+
path: '/',
|
|
13
|
+
frontmatter: { title: 'Home' },
|
|
14
|
+
// set markdown content
|
|
15
|
+
content: `\
|
|
16
|
+
Redirecting to the correct locale...
|
|
17
|
+
|
|
18
|
+
<script setup>
|
|
19
|
+
import { onMounted } from "vue";
|
|
20
|
+
|
|
21
|
+
if(!__VUEPRESS_DEV__)
|
|
22
|
+
onMounted(() => {
|
|
23
|
+
const { languages } = window.navigator;
|
|
24
|
+
const { hash, origin } = window.location;
|
|
25
|
+
const anchor = hash.substring(1);
|
|
26
|
+
|
|
27
|
+
const localeConfig = ${JSON.stringify(localeOptions.localeConfig)};
|
|
28
|
+
const availableLocales = ${JSON.stringify(availableLocales)};
|
|
29
|
+
const defaultLocale = availableLocales.pop();
|
|
30
|
+
|
|
31
|
+
let matchedLocalePath = null;
|
|
32
|
+
|
|
33
|
+
// get matched locale
|
|
34
|
+
findLanguage:
|
|
35
|
+
for (const lang of languages)
|
|
36
|
+
for (const [localePath, langs] of Object.entries(localeConfig))
|
|
37
|
+
if (langs.includes(lang)) {
|
|
38
|
+
${localeOptions.localeFallback
|
|
39
|
+
? `\
|
|
40
|
+
if (!availableLocales.includes(localePath))
|
|
41
|
+
continue;
|
|
42
|
+
`
|
|
43
|
+
: ``}\
|
|
44
|
+
matchedLocalePath = localePath;
|
|
45
|
+
break findLanguage;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// default link
|
|
49
|
+
const defaultLink = defaultLocale? \`\${origin}${removeEndingSlash(base)}\${defaultLocale}\${anchor? \`#\${anchor}\`: ""}\`: null;
|
|
50
|
+
|
|
51
|
+
// a locale homepage exists
|
|
52
|
+
if (matchedLocalePath && availableLocales.includes(matchedLocalePath)) {
|
|
53
|
+
location.href = \`\${origin}${removeEndingSlash(base)}\${matchedLocalePath}\${anchor? \`#\${anchor}\`: ""}\`;
|
|
54
|
+
}
|
|
55
|
+
// we have a default page
|
|
56
|
+
else if (defaultLink) {
|
|
57
|
+
location.href = defaultLink;
|
|
58
|
+
}
|
|
59
|
+
// no homepage? WTF, just go to 404
|
|
60
|
+
else {
|
|
61
|
+
location.href = \`\${origin}${removeEndingSlash(base)}/404.html\`;
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
</script>
|
|
65
|
+
`,
|
|
66
|
+
}));
|
|
67
|
+
}
|
|
68
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { entries, removeLeadingSlash } from '@vuepress/helper';
|
|
2
|
+
import { fs, path } from 'vuepress/utils';
|
|
3
|
+
import { logger } from '../logger.js';
|
|
4
|
+
import { getLocaleRedirectHTML } from './getLocaleRedirectHTML.js';
|
|
5
|
+
export const generateAutoLocaleRedirectFiles = async ({ dir, options, pages }, localeOptions) => {
|
|
6
|
+
const rootPaths = pages
|
|
7
|
+
.filter(({ pathLocale }) => pathLocale === '/')
|
|
8
|
+
.map(({ path }) => path);
|
|
9
|
+
const localeRedirectMap = {};
|
|
10
|
+
pages
|
|
11
|
+
.filter(({ pathLocale }) => pathLocale !== '/')
|
|
12
|
+
.forEach(({ path, pathLocale }) => {
|
|
13
|
+
const rootPath = path
|
|
14
|
+
.replace(pathLocale, '/')
|
|
15
|
+
.replace(/\/$/, '/index.html');
|
|
16
|
+
if (!rootPaths.includes(rootPath))
|
|
17
|
+
(localeRedirectMap[rootPath] ??= []).push(pathLocale);
|
|
18
|
+
});
|
|
19
|
+
const { succeed } = logger.load('Generating locale redirect files');
|
|
20
|
+
await Promise.all(entries(localeRedirectMap).map(async ([rootPath, availableLocales]) => {
|
|
21
|
+
const filePath = dir.dest(removeLeadingSlash(rootPath));
|
|
22
|
+
if (!fs.existsSync(filePath)) {
|
|
23
|
+
await fs.ensureDir(path.dirname(filePath));
|
|
24
|
+
await fs.writeFile(filePath, getLocaleRedirectHTML(localeOptions, availableLocales, options.base));
|
|
25
|
+
}
|
|
26
|
+
}));
|
|
27
|
+
succeed();
|
|
28
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { entries, isLinkAbsolute, isLinkHttp, removeEndingSlash, removeLeadingSlash, } from '@vuepress/helper';
|
|
2
|
+
import { fs, path } from 'vuepress/utils';
|
|
3
|
+
import { logger } from '../logger.js';
|
|
4
|
+
import { getRedirectHTML } from './getRedirectHTML.js';
|
|
5
|
+
export const generateRedirectFiles = async ({ dir, options }, config, hostname = '') => {
|
|
6
|
+
const resolvedHostname = hostname
|
|
7
|
+
? removeEndingSlash(isLinkHttp(hostname) ? hostname : `https://${hostname}`)
|
|
8
|
+
: '';
|
|
9
|
+
const { succeed } = logger.load('Generating redirect files');
|
|
10
|
+
await Promise.all(entries(config).map(async ([from, to]) => {
|
|
11
|
+
const filePath = dir.dest(removeLeadingSlash(from));
|
|
12
|
+
if (!fs.existsSync(filePath)) {
|
|
13
|
+
const redirectUrl = isLinkAbsolute(to)
|
|
14
|
+
? `${resolvedHostname}${options.base}${removeLeadingSlash(to)}`
|
|
15
|
+
: to;
|
|
16
|
+
await fs.ensureDir(path.dirname(filePath));
|
|
17
|
+
await fs.writeFile(filePath, getRedirectHTML(redirectUrl));
|
|
18
|
+
}
|
|
19
|
+
}));
|
|
20
|
+
succeed();
|
|
21
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { removeEndingSlash } from '@vuepress/helper';
|
|
2
|
+
export const getLocaleRedirectHTML = ({ localeConfig, defaultBehavior, defaultLocale, localeFallback, }, availableLocales, base) => `<!DOCTYPE html>
|
|
3
|
+
<html lang="en">
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="UTF-8">
|
|
6
|
+
<meta name="robots" content="noindex">
|
|
7
|
+
<title>Redirecting...</title>
|
|
8
|
+
<script>
|
|
9
|
+
const { hash, origin, pathname } = window.location;
|
|
10
|
+
const routePath = pathname.substring(${base.length});
|
|
11
|
+
const { languages } = window.navigator;
|
|
12
|
+
const anchor = hash.substring(1);
|
|
13
|
+
|
|
14
|
+
const localeConfig = ${JSON.stringify(localeConfig)};
|
|
15
|
+
const availableLocales = ${JSON.stringify(availableLocales)};
|
|
16
|
+
const defaultLocale = ${availableLocales.includes(defaultLocale)
|
|
17
|
+
? JSON.stringify(defaultLocale)
|
|
18
|
+
: 'availableLocales.pop()'};
|
|
19
|
+
const defaultBehavior = ${JSON.stringify(defaultBehavior)}
|
|
20
|
+
|
|
21
|
+
let matchedLocalePath = null;
|
|
22
|
+
|
|
23
|
+
// get matched locale
|
|
24
|
+
findLanguage:
|
|
25
|
+
for (const lang of languages)
|
|
26
|
+
for (const [localePath, langs] of Object.entries(localeConfig))
|
|
27
|
+
if (langs.includes(lang)) {
|
|
28
|
+
${localeFallback
|
|
29
|
+
? `\
|
|
30
|
+
if (!availableLocales.includes(localePath))
|
|
31
|
+
continue;
|
|
32
|
+
`
|
|
33
|
+
: ``}\
|
|
34
|
+
matchedLocalePath = localePath;
|
|
35
|
+
break findLanguage;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// default link
|
|
39
|
+
const defaultLink = defaultLocale? \`\${origin}${removeEndingSlash(base)}\${defaultLocale}\${routePath}\${anchor? \`#\${anchor}\`: ""}\`: null;
|
|
40
|
+
|
|
41
|
+
// a locale matches
|
|
42
|
+
if (matchedLocalePath) {
|
|
43
|
+
const localeLink = \`\${origin}${removeEndingSlash(base)}\${matchedLocalePath}\${routePath}\${anchor? \`#\${anchor}\`: ""}\`;
|
|
44
|
+
|
|
45
|
+
if (availableLocales.includes(matchedLocalePath)) {
|
|
46
|
+
location.href = localeLink;
|
|
47
|
+
}
|
|
48
|
+
// the page does not exist
|
|
49
|
+
else {
|
|
50
|
+
// locale homepage
|
|
51
|
+
if (defaultBehavior === "homepage") {
|
|
52
|
+
location.href = \`\${origin}${removeEndingSlash(base)}\${matchedLocalePath}\`;
|
|
53
|
+
}
|
|
54
|
+
// default locale page
|
|
55
|
+
else if (defaultBehavior === "defaultLocale" && defaultLink) {
|
|
56
|
+
location.href = defaultLink;
|
|
57
|
+
}
|
|
58
|
+
// as is to get a 404 page of that locale
|
|
59
|
+
else {
|
|
60
|
+
location.href = localeLink;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// we have a default page
|
|
65
|
+
else if (defaultLink) {
|
|
66
|
+
location.href = defaultLink;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
location.href = \`\${origin}${removeEndingSlash(base)}/404.html\`;
|
|
70
|
+
}
|
|
71
|
+
</script>
|
|
72
|
+
</head>
|
|
73
|
+
<body>
|
|
74
|
+
<p>Redirecting...</p>
|
|
75
|
+
</body>
|
|
76
|
+
</html>
|
|
77
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getRedirectHTML: (redirectUrl: string) => string;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const getRedirectHTML = (redirectUrl) => `<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="robots" content="noindex">
|
|
6
|
+
<meta http-equiv="refresh" content="0; url=${redirectUrl}">
|
|
7
|
+
<link rel="canonical" href="${redirectUrl}">
|
|
8
|
+
<title>Redirecting...</title>
|
|
9
|
+
<script>
|
|
10
|
+
const anchor = window.location.hash.substring(1);
|
|
11
|
+
location.href = \`${redirectUrl}\${anchor? \`#\${anchor}\`: ""}\`;
|
|
12
|
+
</script>
|
|
13
|
+
</head>
|
|
14
|
+
<body>
|
|
15
|
+
<p>Redirecting...</p>
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
|
18
|
+
`;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { App } from 'vuepress/core';
|
|
2
|
+
import type { RedirectLocaleConfig } from '../shared/index.js';
|
|
3
|
+
import type { RedirectPluginOptions } from './types/index.js';
|
|
4
|
+
export declare const getRedirectLocaleConfig: (app: App, options: RedirectPluginOptions) => RedirectLocaleConfig;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { deepAssign, entries, fromEntries, isArray, isPlainObject, keys, } from '@vuepress/helper';
|
|
2
|
+
import { colors } from 'vuepress/utils';
|
|
3
|
+
import { logger } from './logger.js';
|
|
4
|
+
const AVAILABLE_FALLBACK = ['defaultLocale', 'homepage', '404'];
|
|
5
|
+
export const getRedirectLocaleConfig = (app, options) => {
|
|
6
|
+
const { locales } = app.options;
|
|
7
|
+
const localeConfig = deepAssign(fromEntries(entries(locales)
|
|
8
|
+
.filter(([key, { lang }]) => {
|
|
9
|
+
if (key === '/')
|
|
10
|
+
return false;
|
|
11
|
+
if (!lang) {
|
|
12
|
+
logger.error(`Missing ${colors.magenta('lang')} option for locale "${key}", this locale will be ignored!`);
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
})
|
|
17
|
+
.map(([key, { lang }]) => [key, [lang]])), isPlainObject(options.localeConfig)
|
|
18
|
+
? fromEntries(entries(options.localeConfig).map(([routePath, lang]) => [
|
|
19
|
+
routePath,
|
|
20
|
+
isArray(lang) ? lang : [lang],
|
|
21
|
+
]))
|
|
22
|
+
: {});
|
|
23
|
+
const defaultLocale = options.defaultLocale || keys(localeConfig).pop();
|
|
24
|
+
return {
|
|
25
|
+
autoLocale: options.autoLocale ?? false,
|
|
26
|
+
switchLocale: options.switchLocale ?? false,
|
|
27
|
+
localeConfig,
|
|
28
|
+
defaultLocale,
|
|
29
|
+
localeFallback: options.localeFallback ?? true,
|
|
30
|
+
defaultBehavior: options.defaultBehavior &&
|
|
31
|
+
AVAILABLE_FALLBACK.includes(options.defaultBehavior)
|
|
32
|
+
? options.defaultBehavior
|
|
33
|
+
: 'defaultLocale',
|
|
34
|
+
};
|
|
35
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { entries, fromEntries, isArray, isFunction, isPlainObject, } from '@vuepress/helper';
|
|
2
|
+
import { normalizePath } from '../shared/normalizePath.js';
|
|
3
|
+
export const getRedirectMap = (app, options) => {
|
|
4
|
+
const config = isFunction(options.config)
|
|
5
|
+
? options.config(app)
|
|
6
|
+
: isPlainObject(options.config)
|
|
7
|
+
? options.config
|
|
8
|
+
: {};
|
|
9
|
+
return {
|
|
10
|
+
...fromEntries(app.pages
|
|
11
|
+
.map(({ frontmatter, path }) => isArray(frontmatter.redirectFrom)
|
|
12
|
+
? frontmatter.redirectFrom.map((from) => [
|
|
13
|
+
normalizePath(from, true),
|
|
14
|
+
path,
|
|
15
|
+
])
|
|
16
|
+
: frontmatter.redirectFrom
|
|
17
|
+
? [[normalizePath(frontmatter.redirectFrom, true), path]]
|
|
18
|
+
: [])
|
|
19
|
+
.flat()),
|
|
20
|
+
...fromEntries(entries(config).map(([from, to]) => [
|
|
21
|
+
normalizePath(from, true),
|
|
22
|
+
normalizePath(to),
|
|
23
|
+
])),
|
|
24
|
+
};
|
|
25
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { isLinkAbsolute, removeLeadingSlash } from '@vuepress/helper';
|
|
2
|
+
import { normalizePath } from '../shared/normalizePath.js';
|
|
3
|
+
export const handleRedirectTo = ({ frontmatter }, app) => {
|
|
4
|
+
const { base } = app.options;
|
|
5
|
+
const { redirectTo } = frontmatter;
|
|
6
|
+
if (redirectTo) {
|
|
7
|
+
const redirectUrl = normalizePath(isLinkAbsolute(redirectTo)
|
|
8
|
+
? `${base}${removeLeadingSlash(redirectTo)}`
|
|
9
|
+
: redirectTo);
|
|
10
|
+
(frontmatter.head ??= []).unshift([
|
|
11
|
+
'script',
|
|
12
|
+
{},
|
|
13
|
+
`{\
|
|
14
|
+
const anchor = window.location.hash.substring(1);\
|
|
15
|
+
location.href=\`${redirectUrl}\${anchor? \`#\${anchor}\`: ""}\`;\
|
|
16
|
+
}`,
|
|
17
|
+
]);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './redirectPlugin.js';
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/** Multi language config for redirect popup */
|
|
2
|
+
export const redirectLocales = {
|
|
3
|
+
'/en/': {
|
|
4
|
+
name: 'English',
|
|
5
|
+
hint: 'Your primary language is $1, do you want to switch to it?',
|
|
6
|
+
switch: 'Switch to $1',
|
|
7
|
+
cancel: 'Cancel',
|
|
8
|
+
},
|
|
9
|
+
'/zh/': {
|
|
10
|
+
name: '简体中文',
|
|
11
|
+
hint: '您的首选语言是 $1,是否切换到该语言?',
|
|
12
|
+
switch: '切换到 $1',
|
|
13
|
+
cancel: '取消',
|
|
14
|
+
},
|
|
15
|
+
'/zh-tw/': {
|
|
16
|
+
name: '繁體中文',
|
|
17
|
+
hint: '您的首選語言是 $1,是否切換到該語言?',
|
|
18
|
+
switch: '切換到 $1',
|
|
19
|
+
cancel: '取消',
|
|
20
|
+
},
|
|
21
|
+
'/de/': {
|
|
22
|
+
name: 'Deutsch',
|
|
23
|
+
hint: 'Ihre bevorzugte Sprache ist $1, möchten Sie zu dieser wechseln?',
|
|
24
|
+
switch: 'Zu $1 wechseln',
|
|
25
|
+
cancel: 'Abbrechen',
|
|
26
|
+
},
|
|
27
|
+
'/de-at/': {
|
|
28
|
+
name: 'Deutsch (Österreich)',
|
|
29
|
+
hint: 'Ihre bevorzugte Sprache ist $1, möchten Sie zu dieser wechseln?',
|
|
30
|
+
switch: 'Zu $1 wechseln',
|
|
31
|
+
cancel: 'Abbrechen',
|
|
32
|
+
},
|
|
33
|
+
'/vi/': {
|
|
34
|
+
name: 'Tiếng Việt',
|
|
35
|
+
hint: 'Ngôn ngữ chính của bạn là $1, bạn có muốn chuyển sang nó?',
|
|
36
|
+
switch: 'Chuyển sang $1',
|
|
37
|
+
cancel: 'Hủy',
|
|
38
|
+
},
|
|
39
|
+
'/uk/': {
|
|
40
|
+
name: 'Українська',
|
|
41
|
+
hint: 'Вашою основною мовою є $1, чи бажаєте ви переключитися на неї?',
|
|
42
|
+
switch: 'Переключитися на $1',
|
|
43
|
+
cancel: 'Скасувати',
|
|
44
|
+
},
|
|
45
|
+
'/ru/': {
|
|
46
|
+
name: 'Русский',
|
|
47
|
+
hint: 'Ваш основной язык - $1, вы хотите переключиться на него?',
|
|
48
|
+
switch: 'Переключиться на $1',
|
|
49
|
+
cancel: 'Отмена',
|
|
50
|
+
},
|
|
51
|
+
'/br/': {
|
|
52
|
+
name: 'Português (Brasil)',
|
|
53
|
+
hint: 'A língua principal é $1, deseja mudar para ela?',
|
|
54
|
+
switch: 'Mudar para $1',
|
|
55
|
+
cancel: 'Cancelar',
|
|
56
|
+
},
|
|
57
|
+
'/pl/': {
|
|
58
|
+
name: 'Polski',
|
|
59
|
+
hint: 'Twoim głównym językiem jest $1, czy chcesz przełączyć się na niego?',
|
|
60
|
+
switch: 'Przełącz na $1',
|
|
61
|
+
cancel: 'Anuluj',
|
|
62
|
+
},
|
|
63
|
+
'/sk/': {
|
|
64
|
+
name: 'Slovenčina',
|
|
65
|
+
hint: 'Vašou hlavnou jazykom je $1, chcete prepnúť naň?',
|
|
66
|
+
switch: 'Prepnúť na $1',
|
|
67
|
+
cancel: 'Zrušiť',
|
|
68
|
+
},
|
|
69
|
+
'/fr/': {
|
|
70
|
+
name: 'Français',
|
|
71
|
+
hint: 'Votre langue principale est $1, voulez-vous la changer ?',
|
|
72
|
+
switch: 'Changer pour $1',
|
|
73
|
+
cancel: 'Annuler',
|
|
74
|
+
},
|
|
75
|
+
'/tr/': {
|
|
76
|
+
name: 'Türkçe',
|
|
77
|
+
hint: 'Ana diliniz $1, ona geçmek ister misiniz?',
|
|
78
|
+
switch: "$1'e geç",
|
|
79
|
+
cancel: 'İptal',
|
|
80
|
+
},
|
|
81
|
+
'/fi/': {
|
|
82
|
+
name: 'Suomi',
|
|
83
|
+
hint: 'Pääkielenäsi on $1, haluatko vaihtaa siihen?',
|
|
84
|
+
switch: 'Vaihda $1:een',
|
|
85
|
+
cancel: 'Peruuta',
|
|
86
|
+
},
|
|
87
|
+
'/hu/': {
|
|
88
|
+
name: 'Magyar',
|
|
89
|
+
hint: 'A fő nyelvét $1, szeretné váltani?',
|
|
90
|
+
switch: 'Váltás $1',
|
|
91
|
+
cancel: 'Mégse',
|
|
92
|
+
},
|
|
93
|
+
'/id/': {
|
|
94
|
+
name: 'Bahasa Indonesia',
|
|
95
|
+
hint: 'Bahasa utama Anda adalah $1, apakah Anda ingin beralih ke sana?',
|
|
96
|
+
switch: 'Beralih ke $1',
|
|
97
|
+
cancel: 'Batal',
|
|
98
|
+
},
|
|
99
|
+
'/nl/': {
|
|
100
|
+
name: 'Nederlands',
|
|
101
|
+
hint: 'Uw primaire taal is $1, wilt u overschakelen?',
|
|
102
|
+
switch: 'Overschakelen naar $1',
|
|
103
|
+
cancel: 'Annuleren',
|
|
104
|
+
},
|
|
105
|
+
'/ja/': {
|
|
106
|
+
name: '日本語',
|
|
107
|
+
hint: 'あなたの主要な言語は $1 です。それに切り替えますか?',
|
|
108
|
+
switch: '$1 に切り替える',
|
|
109
|
+
cancel: 'キャンセル',
|
|
110
|
+
},
|
|
111
|
+
'/ko/': {
|
|
112
|
+
name: '한국어',
|
|
113
|
+
hint: '당신의 기본 언어는 $1입니다. 그것으로 전환 하시겠습니까?',
|
|
114
|
+
switch: '$1로 전환',
|
|
115
|
+
cancel: '취소',
|
|
116
|
+
},
|
|
117
|
+
'/es/': {
|
|
118
|
+
name: 'Español',
|
|
119
|
+
hint: 'Su idioma principal es $1, ¿desea cambiarlo?',
|
|
120
|
+
switch: 'Cambiar a $1',
|
|
121
|
+
cancel: 'Cancelar',
|
|
122
|
+
},
|
|
123
|
+
'/pt/': {
|
|
124
|
+
name: 'Português',
|
|
125
|
+
hint: 'Sua língua principal é $1, deseja mudar para ela?',
|
|
126
|
+
switch: 'Mudar para $1',
|
|
127
|
+
cancel: 'Cancelar',
|
|
128
|
+
},
|
|
129
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { addViteSsrNoExternal, getLocaleConfig } from '@vuepress/helper';
|
|
2
|
+
import { getDirname, path } from 'vuepress/utils';
|
|
3
|
+
import { ensureRootHomePage } from './ensureRootHomePage.js';
|
|
4
|
+
import { generateAutoLocaleRedirectFiles, generateRedirectFiles, } from './generate/index.js';
|
|
5
|
+
import { getRedirectLocaleConfig } from './getRedirectLocaleConfig.js';
|
|
6
|
+
import { getRedirectMap } from './getRedirectMap.js';
|
|
7
|
+
import { handleRedirectTo } from './handleRedirectTo.js';
|
|
8
|
+
import { redirectLocales } from './locales.js';
|
|
9
|
+
import { logger, PLUGIN_NAME } from './logger.js';
|
|
10
|
+
const __dirname = getDirname(import.meta.url);
|
|
11
|
+
export const redirectPlugin = (options = {}) => (app) => {
|
|
12
|
+
if (app.env.isDebug)
|
|
13
|
+
logger.info('Options:', options);
|
|
14
|
+
const redirectLocaleConfig = getRedirectLocaleConfig(app, options);
|
|
15
|
+
let redirectMap;
|
|
16
|
+
return {
|
|
17
|
+
name: PLUGIN_NAME,
|
|
18
|
+
define: {
|
|
19
|
+
__REDIRECT_LOCALE_CONFIG__: redirectLocaleConfig,
|
|
20
|
+
__REDIRECT_LOCALE_SWITCH__: Boolean(redirectLocaleConfig.switchLocale),
|
|
21
|
+
__REDIRECT_LOCALES__: getLocaleConfig({
|
|
22
|
+
app,
|
|
23
|
+
name: 'redirect',
|
|
24
|
+
config: options.locales,
|
|
25
|
+
default: redirectLocales,
|
|
26
|
+
}),
|
|
27
|
+
},
|
|
28
|
+
extendsBundlerOptions: (bundlerOptions, app) => {
|
|
29
|
+
addViteSsrNoExternal(bundlerOptions, app, '@vuepress/helper');
|
|
30
|
+
},
|
|
31
|
+
extendsPage: (page, app) => {
|
|
32
|
+
handleRedirectTo(page, app);
|
|
33
|
+
},
|
|
34
|
+
onInitialized: async (app) => {
|
|
35
|
+
redirectMap = getRedirectMap(app, options);
|
|
36
|
+
if (app.env.isDebug)
|
|
37
|
+
logger.info('Redirect Map:', redirectMap);
|
|
38
|
+
if (redirectLocaleConfig.autoLocale)
|
|
39
|
+
await ensureRootHomePage(app, redirectLocaleConfig);
|
|
40
|
+
},
|
|
41
|
+
onPrepared: async (app) => {
|
|
42
|
+
await app.writeTemp('redirect/map.js', `\
|
|
43
|
+
export const redirectMap = ${app.env.isDev ? JSON.stringify(redirectMap, null, 2) : '{}'};
|
|
44
|
+
`);
|
|
45
|
+
},
|
|
46
|
+
onGenerated: async (app) => {
|
|
47
|
+
await generateRedirectFiles(app, redirectMap);
|
|
48
|
+
if (redirectLocaleConfig.autoLocale)
|
|
49
|
+
await generateAutoLocaleRedirectFiles(app, redirectLocaleConfig);
|
|
50
|
+
},
|
|
51
|
+
clientConfigFile: path.join(__dirname, '../client/config.js'),
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { App, LocaleConfig } from 'vuepress/core';
|
|
2
|
+
import type { RedirectLocaleConfig, RedirectPluginLocaleData } from '../../shared/index.js';
|
|
3
|
+
export interface RedirectPluginOptions extends Partial<Omit<RedirectLocaleConfig, 'localeConfig'>> {
|
|
4
|
+
/**
|
|
5
|
+
* Redirect mapping
|
|
6
|
+
*
|
|
7
|
+
* 重定向映射
|
|
8
|
+
*/
|
|
9
|
+
config?: Record<string, string> | ((app: App) => Record<string, string>);
|
|
10
|
+
/**
|
|
11
|
+
* Locale language config
|
|
12
|
+
*
|
|
13
|
+
* 多语言语言配置
|
|
14
|
+
*/
|
|
15
|
+
localeConfig?: Record<string, string | string[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Locales config
|
|
18
|
+
*
|
|
19
|
+
* 多语言选项
|
|
20
|
+
*/
|
|
21
|
+
locales?: LocaleConfig<RedirectPluginLocaleData>;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface RedirectLocaleConfig {
|
|
2
|
+
/**
|
|
3
|
+
* Whether enable locales redirection
|
|
4
|
+
*
|
|
5
|
+
* 是否启用语言重定向
|
|
6
|
+
*
|
|
7
|
+
* @default false
|
|
8
|
+
*/
|
|
9
|
+
autoLocale: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Whether switch locales
|
|
12
|
+
*
|
|
13
|
+
* 是否启用重定向语言
|
|
14
|
+
*
|
|
15
|
+
* @default false
|
|
16
|
+
*/
|
|
17
|
+
switchLocale: 'direct' | 'modal' | false;
|
|
18
|
+
/**
|
|
19
|
+
* Locale language config
|
|
20
|
+
*
|
|
21
|
+
* 多语言语言配置
|
|
22
|
+
*/
|
|
23
|
+
localeConfig: Record<string, string[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Whether fallback to other locales user defined
|
|
26
|
+
*
|
|
27
|
+
* 是否回退到用户定义的其他语言
|
|
28
|
+
*
|
|
29
|
+
* @default true
|
|
30
|
+
*/
|
|
31
|
+
localeFallback: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Behavior when a locale version is not available for current link
|
|
34
|
+
*
|
|
35
|
+
* @description `"homepage"` and `"404"` is only available when a locale is assigned to current language
|
|
36
|
+
*
|
|
37
|
+
* 当前链接没有可用的语言版本时的行为
|
|
38
|
+
*
|
|
39
|
+
* @description 只有当语言分配给当前语言时,`"homepage"` 和 `"404"` 才可用
|
|
40
|
+
*
|
|
41
|
+
* @default "defaultLocale"
|
|
42
|
+
*/
|
|
43
|
+
defaultBehavior: 'defaultLocale' | 'homepage' | '404';
|
|
44
|
+
/**
|
|
45
|
+
* Default locale path
|
|
46
|
+
*
|
|
47
|
+
* @description the first locale will be used if absent
|
|
48
|
+
*
|
|
49
|
+
* 默认语言路径
|
|
50
|
+
*
|
|
51
|
+
* @description 如果缺失,则使用第一个语言
|
|
52
|
+
*/
|
|
53
|
+
defaultLocale: string;
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ExactLocaleConfig } from '@vuepress/helper/shared';
|
|
2
|
+
export interface RedirectPluginLocaleData {
|
|
3
|
+
/**
|
|
4
|
+
* Language name
|
|
5
|
+
*/
|
|
6
|
+
name: string;
|
|
7
|
+
/**
|
|
8
|
+
* Switch hint
|
|
9
|
+
*
|
|
10
|
+
* 切换提示
|
|
11
|
+
*/
|
|
12
|
+
hint: string;
|
|
13
|
+
/**
|
|
14
|
+
* Switch button text
|
|
15
|
+
*/
|
|
16
|
+
switch: string;
|
|
17
|
+
/**
|
|
18
|
+
* Cancel button text
|
|
19
|
+
*
|
|
20
|
+
* 取消按钮文字
|
|
21
|
+
*/
|
|
22
|
+
cancel: string;
|
|
23
|
+
}
|
|
24
|
+
export type RedirectPluginLocaleConfig = ExactLocaleConfig<RedirectPluginLocaleData>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const normalizePath: (path: string, removeHash?: boolean) => string;
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vuepress/plugin-redirect",
|
|
3
|
+
"version": "2.0.0-rc.10",
|
|
4
|
+
"description": "VuePress plugin - redirect",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"vuepress-plugin",
|
|
7
|
+
"vuepress",
|
|
8
|
+
"plugin",
|
|
9
|
+
"redirect"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://ecosystem.vuejs.press/plugins/redirect.html",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/vuepress/ecosystem/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/vuepress/ecosystem.git",
|
|
18
|
+
"directory": "plugins/plugin-redirect"
|
|
19
|
+
},
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"author": {
|
|
22
|
+
"name": "Mr.Hope",
|
|
23
|
+
"email": "mister-hope@outlook.com",
|
|
24
|
+
"url": "https://mister-hope.com"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./lib/node/index.js",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"main": "./lib/node/index.js",
|
|
32
|
+
"types": "./lib/node/index.d.ts",
|
|
33
|
+
"bin": {
|
|
34
|
+
"vp-redirect": "./lib/cli/index.js"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"lib"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -b tsconfig.build.json",
|
|
41
|
+
"clean": "rimraf --glob ./lib ./*.tsbuildinfo",
|
|
42
|
+
"style": "sass src:lib --no-source-map"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@vuepress/helper": "~2.0.0-rc.10",
|
|
46
|
+
"@vueuse/core": "^10.7.2",
|
|
47
|
+
"cac": "^6.7.14",
|
|
48
|
+
"vue": "^3.4.15",
|
|
49
|
+
"vue-router": "^4.2.5"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"vuepress": "2.0.0-rc.5"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"gitHead": "2e4d75617b030b584fb59346087790c982dbf411"
|
|
58
|
+
}
|