Skip to content

Commit 8c6f40a

Browse files
committed
fix: replace invalid url
1 parent 50e67aa commit 8c6f40a

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

docs/.vitepress/config.mts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ export default defineConfig({
88
locales: {
99
root: {
1010
label: "한국어",
11+
link: "/ko/",
1112
...ko,
12-
link: "/ko",
1313
},
14+
1415
en: {
1516
label: "English",
17+
link: "/en/",
1618
...en,
17-
link: "/en",
1819
},
1920
},
20-
rewrites: {
21-
"/ko": "/",
22-
},
2321
});

docs/.vitepress/libs/nextPath.mts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function nextPath(to: "en" | "ko", path: string): string {
2+
if (path === "/" || path === "/en/" || path === "/ko/") {
3+
return to === "en" ? "/en/" : "/";
4+
}
5+
6+
return to === "en"
7+
? path.replace(/^\/ko(?=\/|$)/, "/en")
8+
: path.replace(/^\/en(?=\/|$)/, "/ko");
9+
}
10+
11+
export function fixLangLinks(pathname: string) {
12+
document
13+
.querySelectorAll<HTMLAnchorElement>('a.VPLink.link[href^="/en"]')
14+
.forEach((a) => a.setAttribute("href", nextPath("en", pathname)));
15+
16+
document
17+
.querySelectorAll<HTMLAnchorElement>(
18+
'a.VPLink.link[href^="/ko"], a.VPLink.link[href="/"]'
19+
)
20+
.forEach((a) => a.setAttribute("href", nextPath("ko", pathname)));
21+
}

docs/.vitepress/theme/index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// https://vitepress.dev/guide/custom-theme
2-
import { h } from "vue";
32
import type { Theme } from "vitepress";
43
import DefaultTheme from "vitepress/theme";
4+
import { h } from "vue";
5+
import { fixLangLinks } from "../libs/nextPath.mts";
56
import "./style.css";
67

78
export default {
@@ -11,5 +12,11 @@ export default {
1112
// https://vitepress.dev/guide/extending-default-theme#layout-slots
1213
});
1314
},
14-
enhanceApp({ app, router, siteData }) {},
15+
enhanceApp({ app, router, siteData }) {
16+
if (typeof window === "undefined") return;
17+
fixLangLinks(window.location.pathname);
18+
router.onAfterRouteChanged = (to) => {
19+
fixLangLinks(to);
20+
};
21+
},
1522
} satisfies Theme;

0 commit comments

Comments
 (0)