Browse Source

feat: normalise paths before compare

pull/2/head
Muthu Kumar 12 months ago
parent
commit
31b3b4d615
Failed to extract signature
  1. 17
      src/index.tsx
  2. 20
      src/util/index.ts

17
src/index.tsx

@ -14,27 +14,30 @@ import Live from "./pages/main/Live";
import NotFound from "./pages/main/404"; import NotFound from "./pages/main/404";
import BlogHome from "./pages/blog/Home"; import BlogHome from "./pages/blog/Home";
import { BlogPost } from "./pages/blog/components/BlogContent"; import { BlogPost } from "./pages/blog/components/BlogContent";
import { normalise } from "./util";
function App() { 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 "/": case "/":
return <Home />; return <Home />;
case "/experience": case "/experience":
case "/experience/":
return <Exp />; return <Exp />;
case "/projects": case "/projects":
case "/projects/":
return <Projects />; return <Projects />;
case "/contact": case "/contact":
case "/contact/":
return <Contact />; return <Contact />;
case "/live": case "/live":
case "/live/":
return <Live />; return <Live />;
case "/blog": case "/blog":
case "/blog/":
// return <BlogHome />; // return <BlogHome />;
default: default:
// if (location.startsWith("/blog")) return <BlogPost />; // if (location.startsWith("/blog")) return <BlogPost />;

20
src/util/index.ts

@ -35,3 +35,23 @@ export function rewriteExtn(filename: string, extn: string) {
split[split.length - 1] = extn; split[split.length - 1] = extn;
return split.join("."); 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<X>(xs: X[], i: number) {
return xs.at((i + 1) % xs.length)!;
},
prev<X>(xs: X[], i: number) {
return xs.at((i - 1) % xs.length)!;
},
};

Loading…
Cancel
Save