Skip to content

Commit

Permalink
fix: Redirect of old URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Jan 30, 2024
1 parent 98c578d commit 2d92ffb
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 541 deletions.
4 changes: 2 additions & 2 deletions packages/mermaid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
13 changes: 7 additions & 6 deletions packages/mermaid/src/docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
};
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
3 changes: 1 addition & 2 deletions packages/mermaid/src/docs/.vitepress/theme/redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 2d92ffb

Please sign in to comment.