From b3fed5bc71f78c2118c7ff53693545f0e32b39b1 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Tue, 14 Nov 2023 12:55:22 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Take=20baseurl=20into=20account?= =?UTF-8?q?=20for=20the=20TOC=20nav?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/beige-flowers-thank.md | 5 +++++ .../components/Navigation/TableOfContentsItems.tsx | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) create mode 100644 .changeset/beige-flowers-thank.md diff --git a/.changeset/beige-flowers-thank.md b/.changeset/beige-flowers-thank.md new file mode 100644 index 000000000..cfee11c87 --- /dev/null +++ b/.changeset/beige-flowers-thank.md @@ -0,0 +1,5 @@ +--- +'@myst-theme/site': patch +--- + +Ensure baseurl is accounted for when the nav is opened diff --git a/packages/site/src/components/Navigation/TableOfContentsItems.tsx b/packages/site/src/components/Navigation/TableOfContentsItems.tsx index 5084b33c8..2d704a82c 100644 --- a/packages/site/src/components/Navigation/TableOfContentsItems.tsx +++ b/packages/site/src/components/Navigation/TableOfContentsItems.tsx @@ -35,11 +35,11 @@ function nestToc(toc: Heading[]): NestedHeading[] { return items; } -function childrenOpen(headings: NestedHeading[], pathname: string): string[] { +function childrenOpen(headings: NestedHeading[], pathname: string, baseurl?: string): string[] { return headings .map((heading) => { - if (heading.path === pathname) return [heading.id]; - const open = childrenOpen(heading.children, pathname); + if (withBaseurl(heading.path, baseurl) === pathname) return [heading.id]; + const open = childrenOpen(heading.children, pathname, baseurl); if (open.length === 0) return []; return [heading.id, ...open]; }) @@ -103,14 +103,14 @@ function LinkItem({ const NestedToc = ({ heading }: { heading: NestedHeading }) => { const { pathname } = useLocation(); - const startOpen = childrenOpen([heading], pathname).includes(heading.id); + const baseurl = useBaseurl(); + const startOpen = childrenOpen([heading], pathname, baseurl).includes(heading.id); const nav = useNavigation(); - nav.state; const [open, setOpen] = React.useState(startOpen); useEffect(() => { if (nav.state === 'idle') setOpen(startOpen); }, [nav.state]); - const exact = pathname === heading.path; + const exact = pathname === withBaseurl(heading.path, baseurl); if (!heading.children || heading.children.length === 0) { return (