'use client' import type { TOCItemType } from './server/get-toc' import * as Primitive from './toc-primitive' import { type ComponentProps, Fragment, type HTMLAttributes, type ReactNode, useRef } from 'react' import { TocThumb } from './toc-thumb' import { cn, ScrollArea, ScrollViewport } from 'ui' import { removeAnchor } from 'ui/src/components/CustomHTMLElements/CustomHTMLElements.utils' export interface TOCProps { /** * Custom content in TOC container, before the main TOC */ header?: ReactNode /** * Custom content in TOC container, after the main TOC */ footer?: ReactNode children: ReactNode } export function Toc(props: HTMLAttributes) { return (
{props.children}
) } export function TOCScrollArea({ isMenu, ...props }: ComponentProps & { isMenu?: boolean }) { const viewRef = useRef(null) return ( {props.children} ) } export function TOCItems({ items, showTrack = false, }: { items: TOCItemType[] showTrack?: boolean }) { const containerRef = useRef(null) if (items.length === 0) return null return ( <>
{items.map((item) => ( ))}
) } const formatSlug = (slug: string) => { // [Joshen] We will still provide support for headers declared like this: // ## REST API {#rest-api-overview} // At least for now, this was a docusaurus thing. if (slug.includes('#')) return slug.split('#')[1] return slug } function formatTOCHeader(content: string) { let insideInlineCode = false const res: Array<{ type: 'text'; value: string } | { type: 'code'; value: string }> = [] for (const x of content) { if (x === '`') { if (!insideInlineCode) { insideInlineCode = true res.push({ type: 'code', value: '' }) } else { insideInlineCode = false } } else { if (insideInlineCode) { res[res.length - 1].value += x } else { if (res.length === 0 || res[res.length - 1].type === 'code') { res.push({ type: 'text', value: x }) } else { res[res.length - 1].value += x } } } } return res } function TOCItem({ item }: { item: TOCItemType }) { return ( = 4 && 'ps-8' )} > {formatTOCHeader(removeAnchor(item.title)).map((x, index) => ( {x.type === 'code' ? ( {x.value} ) : ( x.value )} ))} ) }