'use client';
import {Divider, PickerItem} from '@react-spectrum/s2';
import {MarkdownMenu} from './MarkdownMenu';
import {MobileOnPageNav, OnPageNav, SideNav, SideNavItem, SideNavLink} from './Nav';
import React from 'react';
import {ScrollableToc} from './ScrollableToc';
import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
import type {TocNode} from '@parcel/rsc';
import {useRouter} from './Router';
function anchorId(children) {
return children
.replace(/\s/g, '-')
.replace(/[^a-zA-Z0-9-_]/g, '')
.toLowerCase();
}
let tocLinkTextStyle = style({
overflowWrap: 'break-word',
wordBreak: 'break-word',
minWidth: 0
});
function Toc({toc, isNested = false}: {toc: TocNode[]; isNested?: boolean}) {
return (
{toc.map((c, i) => (
{c.title}
{c.children.length > 0 && }
))}
);
}
function renderMobileToc(toc: TocNode[], seen = new Map()) {
return toc.map(c => {
let href = c.level === 1 ? '#top' : '#' + anchorId(c.title);
if (seen.has(href)) {
seen.set(href, seen.get(href) + 1);
href += '-' + seen.get(href);
} else {
seen.set(href, 1);
}
return (
{c.title}
{c.children.length > 0 && renderMobileToc(c.children, seen)}
);
});
}
export function OptimisticToc() {
let {currentPage} = useRouter();
let section = currentPage.exports?.section;
let hasToC =
(!currentPage.exports?.hideNav || section === 'Blog' || section === 'Releases') &&
currentPage.tableOfContents?.[0]?.children &&
currentPage.tableOfContents?.[0]?.children?.length > 0;
if (!hasToC) {
return null;
}
return (
);
}
function isExternalUrl(url: string): boolean {
return url.startsWith('http://') || url.startsWith('https://');
}
function RelatedPages({pages}: {pages: Array<{title: string; url: string}>}) {
return (
Related pages
{pages.map((page, i) => (
{page.title}
))}
);
}
export function OptimisticMobileToc() {
let {currentPage} = useRouter();
if ((currentPage.tableOfContents?.[0]?.children?.length ?? 0) <= 1) {
return null;
}
let withRelatedPages = currentPage.exports?.relatedPages
? [
...(currentPage.tableOfContents ?? []),
{
level: 2,
title: 'Related pages',
children: []
}
]
: currentPage.tableOfContents!;
return {renderMobileToc(withRelatedPages)};
}