|
1 | 1 | --- |
2 | | -type BaseProps<T> = { |
3 | | - toc: (T & { |
4 | | - depth: number; |
5 | | - })[]; |
6 | | - as?: "bullet" | "number" | "menu"; |
7 | | - depth?: number; |
8 | | - maxDepth?: number; |
9 | | - use: (props: any) => any; |
10 | | -}; |
| 2 | +import type { TocProps } from "./types"; |
11 | 3 |
|
12 | | -type ClassicProps = BaseProps<{ |
13 | | - title: string; |
14 | | - url?: string; |
15 | | -}>; |
| 4 | +type Props = TocProps & astroHTML.JSX.HTMLAttributes; |
16 | 5 |
|
17 | | -type UseComponentProps = BaseProps<{ |
18 | | - [key: string]: any; |
19 | | -}>; |
20 | | -
|
21 | | -export type Props = ClassicProps | UseComponentProps; |
22 | | -
|
23 | | -const { toc, depth = 1, ...props } = Astro.props as Props; |
24 | | -const { as = "bullet", use: Cmp, maxDepth, ...styleProps } = props; /* styleProps; `class` & `data-astro-cid-*` */ |
| 6 | +const { toc, depth = 1, ...props } = Astro.props; |
| 7 | +const { as = "bullet", use: Cmp, maxDepth, ...attrs } = props; |
25 | 8 | const headings = toc.filter((it) => it.depth === depth); |
26 | 9 | const Tag = "bullet" === as ? "ul" : "number" === as ? "ol" : "menu"; |
27 | 10 | --- |
28 | 11 |
|
29 | | -<Tag data-astro-toc={depth} {...depth === 1 ? styleProps : {}}> |
| 12 | +<Tag data-astro-toc={depth} {...depth === 1 ? attrs : {}}> |
30 | 13 | { |
31 | 14 | headings.map((it, idx) => { |
32 | 15 | const nextHeading = headings[idx + 1]; |
33 | | - const subHeadings = toc.slice( |
34 | | - (toc as any).indexOf(it) + 1, |
35 | | - nextHeading ? (toc as any).indexOf(nextHeading) : undefined |
36 | | - ); |
| 16 | + const subHeadings = toc.slice(toc.indexOf(it) + 1, nextHeading ? toc.indexOf(nextHeading) : undefined); |
37 | 17 | const shouldRenderSubHeadings = maxDepth ? maxDepth > it.depth : subHeadings.length > 0; |
38 | 18 |
|
39 | 19 | return ( |
|
0 commit comments