fix: fix route

This commit is contained in:
p-sw 2024-06-04 11:43:20 +09:00
parent 0c67ee5c62
commit 10c17bb51e
4 changed files with 39 additions and 5 deletions

View File

@ -11,6 +11,13 @@ import DocsLayout from "./DocsLayout";
import ErrorBoundary from "./ErrorHandler"; import ErrorBoundary from "./ErrorHandler";
import DynamicLayout from "./DynamicLayout"; import DynamicLayout from "./DynamicLayout";
import DocsIntroduction, {
tableOfContents as docsIntroductionToc,
} from "./docs/introduction.mdx";
import DocsInstallation, {
tableOfContents as docsInstallationToc,
} from "./docs/installation.mdx";
import { HeadingContext } from "./HeadingContext"; import { HeadingContext } from "./HeadingContext";
import { ForwardedRef, forwardRef, useContext, useEffect, useRef } from "react"; import { ForwardedRef, forwardRef, useContext, useEffect, useRef } from "react";
@ -96,7 +103,7 @@ const overrideComponents = {
h6: forwardRef<HTMLHeadingElement, any>(HashedHeaders("h6")), h6: forwardRef<HTMLHeadingElement, any>(HashedHeaders("h6")),
}; };
const docsModules = import.meta.glob("./docs/**/*.mdx"); const docsModules = import.meta.glob("./docs/components/*.mdx");
const routes = Object.keys(docsModules).map((path) => { const routes = Object.keys(docsModules).map((path) => {
const sfPath = path.replace("./docs", "").replace(".mdx", ""); const sfPath = path.replace("./docs", "").replace(".mdx", "");
@ -126,6 +133,23 @@ const router = createBrowserRouter(
<Route path="/" element={<MainLayout />} errorElement={<ErrorBoundary />}> <Route path="/" element={<MainLayout />} errorElement={<ErrorBoundary />}>
<Route index element={<Home />} /> <Route index element={<Home />} />
<Route path="docs" element={<DocsLayout />}> <Route path="docs" element={<DocsLayout />}>
<Route index loader={() => redirect("/docs/introduction")} />
<Route
path="introduction"
element={
<DynamicLayout toc={docsIntroductionToc}>
<DocsIntroduction />
</DynamicLayout>
}
/>
<Route
path="installation"
element={
<DynamicLayout toc={docsInstallationToc}>
<DocsInstallation />
</DynamicLayout>
}
/>
<Route <Route
path="components" path="components"
loader={() => redirect("/docs/components/button")} loader={() => redirect("/docs/components/button")}

View File

@ -1,4 +1,4 @@
const docsModules = import.meta.glob('./docs/**/*.mdx'); const docsModules = import.meta.glob('./docs/components/*.mdx');
const mainNav = [ const mainNav = [
{ {
@ -19,14 +19,24 @@ const mainNav = [
]; ];
const sideNav: Record<string, ({ path: string; name: string; eq: (path: string) => boolean })[]> = { const sideNav: Record<string, ({ path: string; name: string; eq: (path: string) => boolean })[]> = {
"Documents": [], "Documents": [
{
path: "/docs/introduction",
name: "Introduction",
eq: (pathname: string) => pathname === "/docs/introduction"
},
{
path: "/docs/installation",
name: "Installation",
eq: (pathname: string) => pathname === "/docs/installation"
}
],
"Components": [] "Components": []
}; };
Object.keys(docsModules).forEach((path) => { Object.keys(docsModules).forEach((path) => {
const name = (path.split('/').pop() ?? '').replace('.mdx', ''); const name = (path.split('/').pop() ?? '').replace('.mdx', '');
const section = path.includes('/components/') ? "Components" : "Documents"; sideNav["Components"].push({
sideNav[section].push({
path: path.replace('./docs', '/docs').replace('.mdx', ''), path: path.replace('./docs', '/docs').replace('.mdx', ''),
name: name.charAt(0).toUpperCase() + name.slice(1), name: name.charAt(0).toUpperCase() + name.slice(1),
eq: (pathname: string) => pathname === path.replace('./docs', '/docs').replace('.mdx', '') eq: (pathname: string) => pathname === path.replace('./docs', '/docs').replace('.mdx', '')