diff --git a/src/index.tsx b/src/index.tsx index 987c952..4e2caa2 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -14,27 +14,30 @@ import Live from "./pages/main/Live"; import NotFound from "./pages/main/404"; import BlogHome from "./pages/blog/Home"; import { BlogPost } from "./pages/blog/components/BlogContent"; +import { normalise } from "./util"; function App() { - const [location] = useLocation(); + const [location, navigate] = useLocation(); - switch (location) { + const normalised = normalise(location); + + if (location !== normalised) { + navigate(normalised, { replace: true }); + return null; + } + + switch (normalised) { case "/": return ; case "/experience": - case "/experience/": return ; case "/projects": - case "/projects/": return ; case "/contact": - case "/contact/": return ; case "/live": - case "/live/": return ; case "/blog": - case "/blog/": // return ; default: // if (location.startsWith("/blog")) return ; diff --git a/src/util/index.ts b/src/util/index.ts index 80cc21e..21cab9b 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -35,3 +35,23 @@ export function rewriteExtn(filename: string, extn: string) { split[split.length - 1] = extn; return split.join("."); } + +export function normalise(path: string) { + return ( + (path.startsWith("/") ? "/" : "") + + path.trim().split("/").filter(Boolean).join("/") + ); +} + +export function comparePaths(p1: string, p2: string) { + return normalise(p1) === normalise(p2); +} + +export const get = { + next(xs: X[], i: number) { + return xs.at((i + 1) % xs.length)!; + }, + prev(xs: X[], i: number) { + return xs.at((i - 1) % xs.length)!; + }, +};