Skip to content

Commit 7e518e3

Browse files
committed
update README.md
1 parent b80dab4 commit 7e518e3

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,134 @@
11
# react-markdown-toc
2+
3+
`react-markdown-toc` is a library designed for React applications, focused on generating a Table of Contents (TOC) from Markdown text.
4+
5+
[Preview Link](https://zhangyu1818.github.io/react-markdown-toc/)
6+
7+
## Features
8+
9+
- **Fast and Accurate**: Optimized for runtime performance, ensuring quick and accurate identification of the currently active section in the document. This feature is particularly beneficial for interactive documents where users might scroll frequently.
10+
- **Fully Customizable**: Supports a wide range of customization options, including list style and link behavior, suitable for various design needs.
11+
- **Server and Client Side Support**: Compatible with React server components and client components, enabling it to adapt to different rendering strategies.
12+
13+
## Installation
14+
15+
You can install `react-markdown-toc` using the following commands:
16+
17+
```bash
18+
npm install react-markdown-toc
19+
```
20+
21+
## Usage
22+
23+
### fromMarkdown
24+
25+
The `fromMarkdown` function is used to generate a TOC tree from a Markdown text.
26+
27+
**Example Usage:**
28+
29+
```js
30+
import { fromMarkdown } from 'react-markdown-toc'
31+
32+
const [result, map] = fromMarkdown(`## Heading 1\n\n### Heading 1.1\n\n## Heading 2`)
33+
```
34+
35+
The `fromMarkdown` function returns a tuple containing the TOC tree and a map linking IDs to keys within the tree.
36+
37+
### Server Component
38+
39+
The server component does not support custom rendering functions; it renders the ul, li, and a tags in a fixed manner.
40+
41+
**Example Usage:**
42+
43+
```jsx
44+
import fs from 'node:fs/promises'
45+
import { TOC } from 'react-markdown-toc/server'
46+
47+
async function App() {
48+
const markdown = await fs.readFile('.example.md', 'utf-8')
49+
return <TOC markdown={markdown} className='ml-4' ul='pl-4' />
50+
}
51+
```
52+
53+
#### Props
54+
55+
- `markdown`: `string`, `required`
56+
- The Markdown text to generate the TOC from.
57+
- `className`: `string`, `optional`
58+
- Adds a custom class name to the outermost `<ul>` for style customization.
59+
- `scrollAlign`: `start`|`center`|`end`, `optional`, default: `center`
60+
- Determines which TOC item is considered active based on the scroll position, especially useful when multiple headings are visible in the viewport simultaneously.
61+
- The options are `start` (the element closest to the top of the viewport), `center` (the element closest to the center of the viewport), and `end` (the element closest to the bottom of the viewport).
62+
- `throttleTime`: `number`, `optional`, default: `1000`
63+
- Controls the throttling time for the scroll event to improve performance.
64+
65+
### Client Component
66+
67+
The client component allows full customization of rendering, including the functions for rendering the `list`, `list items`, and `links`.
68+
69+
**Example Usage:**
70+
71+
Use `shadcn/ui` to implement a collapsible TOC list.
72+
73+
[Preview Link](https://zhangyu1818.github.io/react-markdown-toc/)
74+
75+
```jsx
76+
import { useRouter } from 'next/navigation'
77+
import { TOC } from 'react-markdown-toc/client'
78+
import { Collapsible, CollapsibleTrigger, CollapsibleContent } from '@/components/ui/collapsible'
79+
80+
function CustomTOC({ tocData }) {
81+
const router = useRouter()
82+
return (
83+
<TOC
84+
toc={toc}
85+
scrollAlign='start'
86+
renderList={children => (
87+
<CollapsibleContent className='pl-4 overflow-hidden data-[state=open]:animate-collapsible-down data-[state=closed]:animate-collapsible-up'>
88+
{children}
89+
</CollapsibleContent>
90+
)}
91+
renderListItem={(children, open) => <Collapsible open={open}>{children}</Collapsible>}
92+
renderLink={(children, href, active) => (
93+
<CollapsibleTrigger>
94+
<span
95+
data-active={active}
96+
role='button'
97+
onClick={() => {
98+
router.push(href, { scroll: false })
99+
const target = document.querySelector(href)
100+
target?.scrollIntoView({ behavior: 'smooth' })
101+
}}
102+
>
103+
{children}
104+
</span>
105+
</CollapsibleTrigger>
106+
)}
107+
/>
108+
)
109+
}
110+
```
111+
112+
#### Props
113+
114+
- `toc`: `readonly [Result, Map<string, string>]`, `required`
115+
- Obtained through the `fromMarkdown` method. This tuple contains the TOC tree and a map linking IDs to keys within the tree.
116+
- `scrollAlign`: `start`|`center`|`end`, `optional`, default: `center`
117+
- Determines which TOC item is considered active based on the scroll position, especially useful when multiple headings are visible in the viewport simultaneously.
118+
- The options are `start` (the element closest to the top of the viewport), `center` (the element closest to the center of the viewport), and `end` (the element closest to the bottom of the viewport).
119+
- `throttleTime`: `number`, `optional`, default: `1000`
120+
- Controls the throttling time for the scroll event to improve performance.
121+
- `renderList`: `(children: React.ReactNode, active: boolean) => React.ReactNode`, `required`
122+
- A custom rendering function for the list component, providing full control over the list's appearance and behavior.
123+
- `renderListItem`: `(children: React.ReactNode, active: boolean) => React.ReactNode`, `required`
124+
- A custom rendering function for each list item, allowing for individual customization of list items within the TOC.
125+
- `renderLink`: `(children: React.ReactNode, url: string, active: boolean) => React.ReactNode`, `required`
126+
- A custom function for rendering links, where you can specify the content, the target URL, and the active state styling or behavior.
127+
128+
## Contribution
129+
130+
Contributions are always welcome!
131+
132+
## License
133+
134+
[MIT](LICENSE)

0 commit comments

Comments
 (0)