Skip to content

Commit

Permalink
🐛 Take baseurl into account for the TOC nav
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Nov 14, 2023
1 parent 8a58b8a commit b3fed5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-flowers-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@myst-theme/site': patch
---

Ensure baseurl is accounted for when the nav is opened
12 changes: 6 additions & 6 deletions packages/site/src/components/Navigation/TableOfContentsItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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];
})
Expand Down Expand Up @@ -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 (
<LinkItem
Expand Down

0 comments on commit b3fed5b

Please sign in to comment.