fix: Redirect of old URLs

This commit is contained in:
Sidharth Vinod 2024-01-30 13:41:47 +05:30
parent 98c578da02
commit 2d92ffb085
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
6 changed files with 419 additions and 569 deletions

View File

@ -122,8 +122,8 @@
"typescript": "^5.0.4",
"unist-util-flatmap": "^1.0.0",
"unist-util-visit": "^4.1.2",
"vitepress": "^1.0.0-alpha.72",
"vitepress-plugin-search": "^1.0.4-alpha.20"
"vitepress": "^1.0.0-rc.40",
"vitepress-plugin-search": "^1.0.4-alpha.22"
},
"files": [
"dist/",

View File

@ -8,14 +8,12 @@ import Contributors from '../components/Contributors.vue';
import HomePage from '../components/HomePage.vue';
// @ts-ignore
import TopBar from '../components/TopBar.vue';
import { getRedirect } from './redirect.js';
import { h } from 'vue';
import Theme from 'vitepress/theme';
import '../style/main.css';
import 'uno.css';
import type { EnhanceAppContext } from 'vitepress';
export default {
...DefaultTheme,
@ -26,19 +24,22 @@ export default {
'home-features-after': () => h(HomePage),
});
},
enhanceApp({ app, router }) {
enhanceApp({ app, router }: EnhanceAppContext) {
// register global components
app.component('Mermaid', Mermaid);
app.component('Contributors', Contributors);
router.onBeforeRouteChange = (to) => {
try {
const newPath = getRedirect(to);
const url = new URL(window.location.origin + to);
const newPath = getRedirect(url);
if (newPath) {
console.log(`Redirecting to ${newPath} from ${window.location}`);
// router.go isn't loading the ID properly.
window.location.href = `/${newPath}`;
}
} catch (e) {}
} catch (e) {
console.error(e);
}
};
},
};

View File

@ -43,5 +43,5 @@ test.each([
'configure/faq.html#frequently-asked-questions',
], // with hash
])('should process url %s to %s', (link: string, path: string) => {
expect(getRedirect(link)).toBe(path);
expect(getRedirect(new URL(link))).toBe(path);
});

View File

@ -99,8 +99,7 @@ const urlRedirectMap: Record<string, string> = {
* @param link - The old documentation URL.
* @returns The new documentation path.
*/
export const getRedirect = (link: string): string | undefined => {
const url = new URL(link);
export const getRedirect = (url: URL): string | undefined => {
// Redirects for deprecated vitepress URLs
if (url.pathname in urlRedirectMap) {
return `${urlRedirectMap[url.pathname]}${url.hash}`;

View File

@ -31,8 +31,8 @@
"unocss": "^0.58.0",
"unplugin-vue-components": "^0.26.0",
"vite": "^4.4.12",
"vite-plugin-pwa": "^0.17.0",
"vitepress": "1.0.0-rc.31",
"vite-plugin-pwa": "^0.17.5",
"vitepress": "1.0.0-rc.40",
"workbox-window": "^7.0.0"
}
}

File diff suppressed because it is too large Load Diff