Skip to content

Commit 1dd1721

Browse files
authored
fix(types): correct TypeScript type resolution (fixes #14) (#16)
Fixes TS type resolution & improves DX. Updates `demo`. Adds `.astro/` to .gitignore. Fixes #14
1 parent 813c4cf commit 1dd1721

File tree

8 files changed

+66
-33
lines changed

8 files changed

+66
-33
lines changed

.changeset/bitter-cats-camp.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro-toc": patch
3+
---
4+
5+
Fix TypeScript resolution by centralizing definitions in `types.ts` and refactoring the entry point (`index.ts`). Adds type safety and improves DX for TS users.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
22
dist
33
*.log
4+
.astro/
45
packages/**/env.d.ts

demo/src/components/Card.astro

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
---
2-
import type { TOC } from "astro-toc";
2+
import type { TocItem } from "astro-toc";
33
4-
export interface Props {
5-
title: string;
6-
url?: string;
7-
}
4+
type Props = TocItem;
85
96
const { title, url, depth, ...attrs } = Astro.props;
107
---

demo/src/pages/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const toc = [
4444
</div>
4545
<hr />
4646
<h2>Max Depth (maxDepth="1")</h2>
47-
<TOC toc={toc} maxDepth="1" />
47+
<TOC toc={toc} maxDepth={1} />
4848
<style is:global>
4949
* {
5050
box-sizing: border-box;

packages/astro-toc/lib/TOC.astro

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,19 @@
11
---
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";
113
12-
type ClassicProps = BaseProps<{
13-
title: string;
14-
url?: string;
15-
}>;
4+
type Props = TocProps & astroHTML.JSX.HTMLAttributes;
165
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;
258
const headings = toc.filter((it) => it.depth === depth);
269
const Tag = "bullet" === as ? "ul" : "number" === as ? "ol" : "menu";
2710
---
2811

29-
<Tag data-astro-toc={depth} {...depth === 1 ? styleProps : {}}>
12+
<Tag data-astro-toc={depth} {...depth === 1 ? attrs : {}}>
3013
{
3114
headings.map((it, idx) => {
3215
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);
3717
const shouldRenderSubHeadings = maxDepth ? maxDepth > it.depth : subHeadings.length > 0;
3818

3919
return (
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export { default as TOC } from "./TOC.astro";
2+
export type * from "./types.ts";

packages/astro-toc/lib/types.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Represents a single item in a Table of Contents (TOC).
3+
*/
4+
export interface TocItem {
5+
/**
6+
* Source heading level (1=&lt;h1&gt;, 2=&lt;h2&gt;, ...).
7+
*/
8+
depth: number;
9+
/**
10+
* The visible text of the heading.
11+
*/
12+
title: string;
13+
/**
14+
* Optional URL fragment (`#id`) for linking to the heading.
15+
*/
16+
url?: string;
17+
}
18+
19+
/**
20+
* Props for a component rendering a Table of Contents (TOC).
21+
*
22+
* @typeParam T - The shape of items in the TOC array, extending {@link TocItem}.
23+
*/
24+
export type TocProps<T extends TocItem = TocItem> = {
25+
/**
26+
* List style type: unordered list (`bullet`), ordered list (`number`), or semantic menu.
27+
*
28+
* Default is `bullet`.
29+
*/
30+
as?: "bullet" | "number" | "menu";
31+
/**
32+
* Minimum heading depth to include (inclusive).
33+
*
34+
* Default is `1`.
35+
*/
36+
depth?: number;
37+
/**
38+
* Maximum heading depth to include (inclusive).
39+
*/
40+
maxDepth?: number;
41+
/**
42+
* Array of TOC items to render.
43+
*/
44+
toc: T[];
45+
/**
46+
* Optional custom render function or component for TOC items.
47+
*/
48+
use?: (item: T) => any;
49+
};

packages/astro-toc/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"version": "0.1.3",
55
"type": "module",
66
"exports": {
7-
".": "./lib/index.js"
7+
".": "./lib/index.ts"
88
},
99
"scripts": {
1010
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)