Skip to content

Commit 1dea6ae

Browse files
committed
feat: improve versioning jump and 404 page
1 parent b644599 commit 1dea6ae

File tree

6 files changed

+131
-6
lines changed

6 files changed

+131
-6
lines changed

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Documentation for CommandAPI.
66

77
## License
88

9-
All things in this repository **EXPECT** `docs/.vitepress/theme/components/Authors.vue`(this file is licensed under CC BY-NC-SA 4.0 License) is licensed under MIT License.
9+
All things in this repository **EXPECT** `docs/.vitepress/theme/author/PageAuthors.vue`(this file is licensed under CC BY-NC-SA 4.0 License) is licensed under MIT License.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<script setup lang="ts">
2+
import {useData} from 'vitepress'
3+
import {changingVersion, currentVersion, isLatest} from "../versioning/version";
4+
import getRandomNotFoundMsg from "./notFoundMsg";
5+
6+
const {theme} = useData()
7+
</script>
8+
9+
<template>
10+
<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>
15+
16+
<div class="action" v-if="!changingVersion">
17+
<a
18+
class="link"
19+
:href="isLatest ? '/' : `/${currentVersion}/`"
20+
:aria-label="theme.notFound?.linkLabel ?? 'go to home'"
21+
>
22+
{{ theme.notFound?.linkText ?? 'Take me home' }}
23+
</a>
24+
</div>
25+
</div>
26+
</template>
27+
28+
<style scoped>
29+
.NotFound {
30+
padding: 64px 24px 96px;
31+
text-align: center;
32+
}
33+
34+
@media (min-width: 768px) {
35+
.NotFound {
36+
padding: 96px 32px 168px;
37+
}
38+
}
39+
40+
.code {
41+
line-height: 64px;
42+
font-size: 64px;
43+
font-weight: 600;
44+
}
45+
46+
.title {
47+
padding-top: 12px;
48+
letter-spacing: 2px;
49+
line-height: 20px;
50+
font-size: 20px;
51+
font-weight: 700;
52+
}
53+
54+
.divider {
55+
margin: 24px auto 18px;
56+
width: 64px;
57+
height: 1px;
58+
background-color: var(--vp-c-divider);
59+
}
60+
61+
.quote {
62+
margin: 0 auto;
63+
max-width: 256px;
64+
font-size: 14px;
65+
font-weight: 500;
66+
color: var(--vp-c-text-2);
67+
}
68+
69+
.action {
70+
padding-top: 20px;
71+
}
72+
73+
.link {
74+
display: inline-block;
75+
border: 1px solid var(--vp-c-brand-1);
76+
border-radius: 16px;
77+
padding: 3px 16px;
78+
font-size: 14px;
79+
font-weight: 500;
80+
color: var(--vp-c-brand-1);
81+
transition: border-color 0.25s,
82+
color 0.25s;
83+
}
84+
85+
.link:hover {
86+
border-color: var(--vp-c-brand-2);
87+
color: var(--vp-c-brand-2);
88+
}
89+
</style>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const notFoundMessages = [
2+
"{} experienced kinetic energy",
3+
"{} blew up",
4+
"{} was killed by [Intentional Game Design]",
5+
"{} hit the ground too hard",
6+
"{} fell out of the water",
7+
"{} was doomed to fall",
8+
"{} went off with a bang",
9+
"{} tried to swim in lava",
10+
"{} was obliterated by a sonically-charged shriek",
11+
"{} suffocated in a wall",
12+
"{} was squished too much",
13+
"{} left the confines of this world",
14+
"{} withered away",
15+
];
16+
17+
export default function getRandomNotFoundMsg(): string {
18+
return notFoundMessages[Math.floor(Math.random() * notFoundMessages.length)]
19+
.replace("{}", "This page");
20+
}

docs/.vitepress/theme/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {Theme} from 'vitepress'
2-
import {useRoute} from 'vitepress'
2+
import {useData, useRoute} from 'vitepress'
33
import DefaultTheme from 'vitepress/theme'
44
import PluginTabs from './tabs/PluginTabs.vue'
55
import PluginTabsTab from './tabs/PluginTabsTab.vue'
@@ -14,6 +14,7 @@ import './style/global.css'
1414
import '@nolebase/vitepress-plugin-enhanced-readabilities/client/style.css'
1515
import VersionSwitcher from "./versioning/VersionSwitcher.vue";
1616
import TitleAnchor from "./anchor/TitleAnchor.vue";
17+
import NotFoundComponent from "./404/NotFoundComponent.vue";
1718

1819
// noinspection JSUnusedGlobalSymbols
1920
export default {
@@ -34,6 +35,10 @@ export default {
3435
'nav-screen-content-after': () => h(NolebaseEnhancedReadabilitiesScreenMenu),
3536
};
3637

38+
if (useData().page.value.isNotFound) {
39+
children["not-found"] = () => h(NotFoundComponent);
40+
}
41+
3742
return h(DefaultTheme.Layout, null, children);
3843
},
3944
setup() {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import VPMenuLink from 'vitepress/dist/client/theme-default/components/VPMenuLin
66
import VPFlyout from 'vitepress/dist/client/theme-default/components/VPFlyout.vue'
77
import {parse} from "yaml";
88
import {useRoute} from "vitepress";
9-
import {currentVersion, latestVersion} from "./version";
9+
import {changingVersion, currentVersion, isLatest, latestVersion} from "./version";
1010
1111
// noinspection JSUnusedGlobalSymbols
1212
const props = defineProps<{
@@ -16,7 +16,6 @@ const route = useRoute();
1616
1717
let versionList: string[] = [];
1818
const versions = ref<string[]>([]);
19-
const isLatest = ref(true);
2019
2120
function refresh() {
2221
let version = latestVersion.value;
@@ -33,6 +32,7 @@ function refresh() {
3332
3433
if (currentVersion.value !== version) {
3534
refreshPage = true;
35+
changingVersion.value = true;
3636
}
3737
3838
currentVersion.value = version;
@@ -45,6 +45,7 @@ function refresh() {
4545
}
4646
4747
async function init() {
48+
changingVersion.value = false;
4849
const versionDataFileUrl = `${window.location.origin}/versions.yml`;
4950
const versionDataFileContent = await (await fetch(versionDataFileUrl)).text();
5051
const versionData = parse(versionDataFileContent);

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@ import {inBrowser} from "vitepress";
33

44
function getString(key: string, defaultValue: string) {
55
if (inBrowser) {
6-
return localStorage.getItem(key) || defaultValue;
6+
return sessionStorage.getItem(key) || defaultValue;
77
} else {
88
return defaultValue;
99
}
1010
}
1111

12+
function getBoolean(key: string, defaultValue: boolean) {
13+
if (inBrowser) {
14+
return sessionStorage.getItem(key) === 'true';
15+
} else {
16+
return defaultValue;
17+
}
18+
}
19+
20+
export const isLatest = ref(true);
1221
export const currentVersion = ref(getString('current-version', ''));
1322
export const latestVersion = ref('');
23+
export const changingVersion = ref(getBoolean('changing-version', true));
1424

1525
watch(currentVersion, (value: string) => {
1626
if (inBrowser) {
17-
localStorage.setItem('current-version', value);
27+
sessionStorage.setItem('current-version', value);
1828
}
1929
});

0 commit comments

Comments
 (0)