automatically derive current page in RootNav
This commit is contained in:
		
							parent
							
								
									49fed51ced
								
							
						
					
					
						commit
						5865251560
					
				
					 2 changed files with 32 additions and 25 deletions
				
			
		| 
						 | 
					@ -5,8 +5,6 @@ export interface Props {
 | 
				
			||||||
  currentClass?: string;
 | 
					  currentClass?: string;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const { currentRoute, currentModule, currentClass } = Astro.props;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import { getModulesData } from "@config/io/generateTypeData";
 | 
					import { getModulesData } from "@config/io/generateTypeData";
 | 
				
			||||||
import type { TreeEntry } from "./Tree.astro";
 | 
					import type { TreeEntry } from "./Tree.astro";
 | 
				
			||||||
import Tree from "./Tree.astro";
 | 
					import Tree from "./Tree.astro";
 | 
				
			||||||
| 
						 | 
					@ -18,42 +16,56 @@ const modules = await getModulesData();
 | 
				
			||||||
import { getCollection } from "astro:content";
 | 
					import { getCollection } from "astro:content";
 | 
				
			||||||
const guidePages = await getCollection("guide");
 | 
					const guidePages = await getCollection("guide");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function genGuideNav(base: string): TreeEntry[] | undefined {
 | 
					interface NavTree {
 | 
				
			||||||
 | 
					  title: string,
 | 
				
			||||||
 | 
					  slug: string,
 | 
				
			||||||
 | 
					  entries?: NavTree[],
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const currentPath = Astro.url.pathname.slice(1).split('/');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function mkTree(mount: string, pathIdx: number, { title, slug, entries }: NavTree): TreeEntry {
 | 
				
			||||||
 | 
					  const link = `${mount}/${slug}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return {
 | 
				
			||||||
 | 
					    title,
 | 
				
			||||||
 | 
					    link,
 | 
				
			||||||
 | 
					    current: currentPath[pathIdx] === slug,
 | 
				
			||||||
 | 
					    entries: entries?.map(entry => mkTree(link, pathIdx + 1, entry)),
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function genGuideNav(base: string): NavTree[] | undefined {
 | 
				
			||||||
  const pages = guidePages
 | 
					  const pages = guidePages
 | 
				
			||||||
    .filter(page => page.id.match(`^${base}[^/]*$`) !== null && page.id !== "index")
 | 
					    .filter(page => page.id.match(`^${base}[^/]*$`) !== null && page.id !== "index")
 | 
				
			||||||
    .sort((a, b) => a.data.index - b.data.index)
 | 
					    .sort((a, b) => a.data.index - b.data.index)
 | 
				
			||||||
    .map(page => ({
 | 
					    .map(page => ({
 | 
				
			||||||
      title: page.data.title,
 | 
					      title: page.data.title,
 | 
				
			||||||
      link: `/docs/guide/${page.id}`,
 | 
					      slug: page.id,
 | 
				
			||||||
      current: currentModule === page.id,
 | 
					 | 
				
			||||||
      entries: genGuideNav(page.id + "/"),
 | 
					      entries: genGuideNav(page.id + "/"),
 | 
				
			||||||
    }));
 | 
					    }));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return pages.length === 0 ? undefined : pages;
 | 
					  return pages.length === 0 ? undefined : pages;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const guide = {
 | 
					const guide = mkTree("/docs", 1, {
 | 
				
			||||||
  title: "Usage Guide",
 | 
					  title: "Usage Guide",
 | 
				
			||||||
  link: "/docs/guide",
 | 
					  slug: "guide",
 | 
				
			||||||
  current: currentRoute?.startsWith("guide") ?? false,
 | 
					 | 
				
			||||||
  entries: genGuideNav(""),
 | 
					  entries: genGuideNav(""),
 | 
				
			||||||
}
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const types = {
 | 
					const types = mkTree("/docs", 1, {
 | 
				
			||||||
  title: "Quickshell Types",
 | 
					  title: "Quickshell Types",
 | 
				
			||||||
  link: "/docs/types",
 | 
					  slug: "types",
 | 
				
			||||||
  current: currentRoute?.startsWith("types") ?? false,
 | 
					 | 
				
			||||||
  entries: modules.map(module => ({
 | 
					  entries: modules.map(module => ({
 | 
				
			||||||
    title: module.name,
 | 
					    title: module.name,
 | 
				
			||||||
    link: `/docs/types/${module.name}`,
 | 
					    slug: module.name,
 | 
				
			||||||
    current: currentModule === module.name,
 | 
					 | 
				
			||||||
    entries: module.types.map(type => ({
 | 
					    entries: module.types.map(type => ({
 | 
				
			||||||
      title: type.name,
 | 
					      title: type.name,
 | 
				
			||||||
      link: `/docs/types/${module.name}/${type.name}`,
 | 
					      slug: type.name,
 | 
				
			||||||
      current: currentClass === type.name,
 | 
					 | 
				
			||||||
    }))
 | 
					    }))
 | 
				
			||||||
  }))
 | 
					  }))
 | 
				
			||||||
};
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
---
 | 
					---
 | 
				
			||||||
<nav class="navtree">
 | 
					<nav class="navtree">
 | 
				
			||||||
| 
						 | 
					@ -61,7 +73,7 @@ const types = {
 | 
				
			||||||
  <Link
 | 
					  <Link
 | 
				
			||||||
    title="About Quickshell"
 | 
					    title="About Quickshell"
 | 
				
			||||||
    link="/about"
 | 
					    link="/about"
 | 
				
			||||||
    current={currentRoute === "about"}
 | 
					    current={Astro.url.pathname === "/about"}
 | 
				
			||||||
  />
 | 
					  />
 | 
				
			||||||
  <Tree {...guide}/>
 | 
					  <Tree {...guide}/>
 | 
				
			||||||
  <Tree {...types}/>
 | 
					  <Tree {...types}/>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,11 +2,6 @@
 | 
				
			||||||
import SidebarWrapper from "./SidebarWrapper.tsx";
 | 
					import SidebarWrapper from "./SidebarWrapper.tsx";
 | 
				
			||||||
import RootNav from "./RootNav.astro";
 | 
					import RootNav from "./RootNav.astro";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const url = Astro.url.pathname.split("/");
 | 
					 | 
				
			||||||
const currentRoute = url[2];
 | 
					 | 
				
			||||||
const currentModule = url[3];
 | 
					 | 
				
			||||||
const currentClass = url[4];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
export interface Props {
 | 
					export interface Props {
 | 
				
			||||||
  mobile: boolean;
 | 
					  mobile: boolean;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -16,9 +11,9 @@ const { mobile } = Astro.props;
 | 
				
			||||||
<aside class=`nav-wrapper${mobile ? "-mobile" : ""} id="nav"`>
 | 
					<aside class=`nav-wrapper${mobile ? "-mobile" : ""} id="nav"`>
 | 
				
			||||||
  { mobile ? (
 | 
					  { mobile ? (
 | 
				
			||||||
    <SidebarWrapper client:load>
 | 
					    <SidebarWrapper client:load>
 | 
				
			||||||
      <RootNav currentRoute={currentRoute} currentModule={currentModule} currentClass={currentClass}/>
 | 
					      <RootNav/>
 | 
				
			||||||
    </SidebarWrapper>
 | 
					    </SidebarWrapper>
 | 
				
			||||||
  ) : (
 | 
					  ) : (
 | 
				
			||||||
    <RootNav currentRoute={currentRoute} currentModule={currentModule} currentClass={currentClass}/>
 | 
					    <RootNav/>
 | 
				
			||||||
  )}
 | 
					  )}
 | 
				
			||||||
</aside>
 | 
					</aside>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue