Skip to content

Commit 6989c63

Browse files
authored
Merge pull request #9 from MC-XiaoHei/master
Fix: jump to the corresponding page when switch version
2 parents 65e7272 + 5a20e22 commit 6989c63

File tree

4 files changed

+61
-31
lines changed

4 files changed

+61
-31
lines changed

docs/.vitepress/theme/404/NotFoundComponent.vue

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
<script setup lang="ts">
2-
import {useData} from 'vitepress'
3-
import {changingVersion, currentVersion, isLatest} from "../versioning/version";
2+
import { useData } from "vitepress";
3+
import { computed } from "vue";
4+
import { changingVersion, currentVersion, isLatest, isOld } from "../versioning/version";
45
import getRandomNotFoundMsg from "./notFoundMsg";
56
6-
const {theme} = useData()
7+
const {theme} = useData();
8+
const redirectFromAnotherVersion = computed(() =>
9+
window.location.search.startsWith("?from=") && !changingVersion.value,
10+
);
11+
const redirectFromVersion = computed(() => {
12+
if (redirectFromAnotherVersion.value) {
13+
return window.location.search.split("?from=")[1].split("&")[0];
14+
}
15+
return "";
16+
});
717
</script>
818

919
<template>
1020
<div class="NotFound">
11-
<p class="code">{{ changingVersion ? '' : '404' }}</p>
12-
<h1 class="title">{{ changingVersion ? 'Redirecting' : 'PAGE NOT FOUND' }}</h1>
13-
<div class="divider"/>
14-
<div class="quote">{{ changingVersion ? 'Please wait......' : getRandomNotFoundMsg() }}</div>
21+
<p class="code">{{ changingVersion ? "" : "404" }}</p>
22+
<h1 class="title">{{ changingVersion ? "Redirecting" : "PAGE NOT FOUND" }}</h1>
23+
<div class="divider" />
24+
<div v-if="redirectFromAnotherVersion">
25+
You switched here from version {{ redirectFromVersion }}, but this version does not have a corresponding page.
26+
</div>
27+
<div v-else class="quote">{{ changingVersion ? "Please wait......" : getRandomNotFoundMsg() }}</div>
1528

1629
<div class="action" v-if="!changingVersion">
1730
<a
1831
class="link"
1932
:href="isLatest ? '/' : `/${currentVersion}/`"
2033
:aria-label="theme.notFound?.linkLabel ?? 'go to home'"
34+
:onclick="isOld ? 'location.reload(true);location.href=this.href;return false;' : undefined"
2135
>
22-
{{ theme.notFound?.linkText ?? 'Take me home' }}
36+
{{ theme.notFound?.linkText ?? "Take me home" }}
2337
</a>
2438
</div>
2539
</div>

docs/.vitepress/theme/versioning/VersionSwitcher.vue

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<!--modified from https://github.com/IMB11/vitepress-versioning-plugin , original license is MIT-->
22

33
<script setup lang="ts">
4-
import {onMounted, ref, watch} from 'vue'
5-
import VPMenuLink from 'vitepress/dist/client/theme-default/components/VPMenuLink.vue'
6-
import VPFlyout from 'vitepress/dist/client/theme-default/components/VPFlyout.vue'
7-
import {parse} from "yaml";
8-
import {useRoute} from "vitepress";
9-
import {changingVersion, currentVersion, isLatest, latestVersion} from "./version";
4+
import { useRoute } from "vitepress";
5+
import VPFlyout from "vitepress/dist/client/theme-default/components/VPFlyout.vue";
6+
import VPMenuLink from "vitepress/dist/client/theme-default/components/VPMenuLink.vue";
7+
import { computed, onMounted, ref, watch } from "vue";
8+
import { parse } from "yaml";
9+
import { changingVersion, currentVersion, isLatest, isOld, latestVersion } from "./version";
1010
1111
// noinspection JSUnusedGlobalSymbols
1212
const props = defineProps<{
@@ -15,55 +15,65 @@ const props = defineProps<{
1515
const route = useRoute();
1616
1717
let versionList: string[] = [];
18+
let oldVersionList: string[] = [];
1819
const versions = ref<string[]>([]);
20+
const docsPath = computed(() => {
21+
if (isLatest.value) {
22+
return window.location.pathname;
23+
} else {
24+
return window.location.pathname.split(`/${ currentVersion.value }/`)[1];
25+
}
26+
});
1927
2028
function refresh() {
2129
let version = latestVersion.value;
2230
let refreshPage = false;
2331
isLatest.value = true;
2432
2533
for (const v of versionList) {
26-
if (window.location.pathname.startsWith(`/${v}/`)) {
34+
if (window.location.pathname.startsWith(`/${ v }/`)) {
2735
version = v;
2836
isLatest.value = false;
2937
break;
3038
}
3139
}
3240
33-
if (currentVersion.value !== '' && currentVersion.value !== version) {
41+
if (currentVersion.value !== "" && currentVersion.value !== version) {
3442
refreshPage = true;
3543
changingVersion.value = true;
3644
}
3745
3846
currentVersion.value = version;
3947
versions.value = versionList;
48+
isOld.value = oldVersionList.includes(version);
4049
4150
if (refreshPage) {
42-
window.location.pathname = isLatest.value ? '/' : `/${version}/`;
51+
window.location.pathname = isLatest.value ? "/" : `/${ version }/`;
4352
window.location.reload();
4453
}
4554
}
4655
4756
async function init() {
4857
changingVersion.value = false;
49-
const versionDataFileUrl = `${window.location.origin}/versions.yml`;
58+
const versionDataFileUrl = `${ window.location.origin }/versions.yml`;
5059
const versionDataFileContent = await (await fetch(versionDataFileUrl)).text();
5160
const versionData = parse(versionDataFileContent);
5261
versionList = versionData.versions;
62+
oldVersionList = versionData["old-versions"];
5363
latestVersion.value = versionList[0];
5464
versionList.shift();
55-
refresh()
65+
refresh();
5666
}
5767
5868
const isOpen = ref(false);
5969
const toggle = () => {
6070
isOpen.value = !isOpen.value;
6171
};
6272
63-
onMounted(async () => init())
73+
onMounted(async () => init());
6474
watch(
6575
() => route.path,
66-
() => refresh()
76+
() => refresh(),
6777
);
6878
</script>
6979

@@ -73,32 +83,32 @@ watch(
7383
<div class="items">
7484
<VPMenuLink v-if="!isLatest" :item="{
7585
text: latestVersion,
76-
link: `/../`,
77-
}"/>
86+
link: `/../${docsPath}?from=${currentVersion}`,
87+
}" />
7888
<template v-for="version in versions" :key="version">
7989
<VPMenuLink v-if="currentVersion != version" :item="{
8090
text: version,
81-
link: `${isLatest? '' : '/..'}/${version}/`,
82-
}"/>
91+
link: `${isLatest? '' : '/..'}/${version}/${docsPath}?from=${currentVersion}`,
92+
}" />
8393
</template>
8494
</div>
8595
</VPFlyout>
8696
<div v-else class="VPScreenVersionSwitcher" :class="{ open: isOpen }">
8797
<button class="button" aria-controls="navbar-group-version" :aria-expanded="isOpen" @click="toggle">
88-
<span class="button-text"><span class="vpi-versioning icon"/>Switch Version</span>
89-
<span class="vpi-plus button-icon"/>
98+
<span class="button-text"><span class="vpi-versioning icon" />Switch Version</span>
99+
<span class="vpi-plus button-icon" />
90100
</button>
91101

92102
<div id="navbar-group-version" class="items">
93103
<VPMenuLink :item="{
94104
text: latestVersion,
95105
link: `${isLatest? '' : '/..'}/`,
96-
}"/>
106+
}" />
97107
<template v-for="version in versions" :key="version">
98108
<VPMenuLink :item="{
99109
text: version,
100110
link: `${isLatest? '' : '/..'}/${version}/`,
101-
}"/>
111+
}" />
102112
</template>
103113
</div>
104114
</div>
@@ -116,11 +126,11 @@ watch(
116126

117127
<style scoped>
118128
.VPMenuLink {
119-
font-family: 'Helvetica Neue', sans-serif;
129+
font-family: "Helvetica Neue", sans-serif;
120130
}
121131
122132
.VPVersionSwitcher {
123-
font-family: 'Helvetica Neue', sans-serif;
133+
font-family: "Helvetica Neue", sans-serif;
124134
display: flex;
125135
align-items: center;
126136
}

docs/.vitepress/theme/versioning/version.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function getBoolean(key: string, defaultValue: boolean) {
1818
}
1919

2020
export const isLatest = ref(true);
21+
export const isOld = ref(false);
2122
export const currentVersion = ref(getString('current-version', ''));
2223
export const latestVersion = ref('');
2324
export const changingVersion = ref(getBoolean('changing-version', true));

docs/public/versions.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
versions:
22
- 10.0.0
33
- 9.7.0
4+
- 9.6.1
5+
- 9.4.2
6+
- 9.3.0
7+
- 8.8.0
8+
old-versions:
49
- 9.6.1
510
- 9.4.2
611
- 9.3.0

0 commit comments

Comments
 (0)