Skip to content

Commit 1c21aaf

Browse files
authored
Merge pull request #9 from ueberdosis/toc-scroll-parent-update
update tableofcontents docs describing scrollParent options
2 parents 8c82e02 + f649211 commit 1c21aaf

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

src/content/editor/extensions/functionality/table-of-contents.mdx

+32-20
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ title: Table of Contents extension
33
tags:
44
- type: pro
55
meta:
6-
title: Contents extension | Tiptap Editor Docs
7-
description: Integrate a list of anchors to your document and collect all headlines in a nice TOC (Table of Contents). Learn more in the docs!
8-
category: Editor
6+
title: Contents extension | Tiptap Editor Docs
7+
description: Integrate a list of anchors to your document and collect all headlines in a nice TOC (Table of Contents). Learn more in the docs!
8+
category: Editor
99
extension:
10-
name: Table of contents
11-
description: Add a table of contents of all your anchors or headlines.
12-
type: extension
13-
icon: List
10+
name: Table of contents
11+
description: Add a table of contents of all your anchors or headlines.
12+
type: extension
13+
icon: List
1414
---
15+
1516
import { CodeDemo } from '@/components/CodeDemo'
1617
import { Callout } from '@/components/ui/Callout'
1718

@@ -20,10 +21,11 @@ The `TableOfContents` extension lets you get a list of anchors from your documen
2021
<CodeDemo isPro path="/Extensions/TableOfContents" />
2122

2223
## Install
24+
2325
<Callout title="Set up access to Tiptap's private repository" variant="info">
24-
Integrate this pro extension by registering for a free [Tiptap
25-
account](https://cloud.tiptap.dev/register) and following our [access
26-
guide](/guides/pro-extensions) to Tiptap’s private repository.
26+
Integrate this pro extension by registering for a free [Tiptap
27+
account](https://cloud.tiptap.dev/register) and following our [access
28+
guide](/guides/pro-extensions) to Tiptap’s private repository.
2729
</Callout>
2830

2931
Once done, you can install the extension from our private registry:
@@ -35,6 +37,7 @@ npm install @tiptap-pro/extension-table-of-contents
3537
## Settings
3638

3739
### anchorTypes
40+
3841
The types of the nodes you want to use for your Table of Content. By default this is `["heading"]` but in case you create your own custom Heading extension OR extend the existing one and use a different name, you can pass that name here.
3942

4043
Default: `["heading"]`
@@ -46,14 +49,15 @@ TableOfContents.configure({
4649
```
4750

4851
### getIndex
52+
4953
This option can be used to customize how the item indexes are calculated. By default this is using an internal function but it can be overwritten to do some custom logic.
5054

5155
```js
5256
TableOfContents.configure({
5357
getIndex: (anchor, previousAnchors, level) => {
5458
// do some custom logic, but for this example we will just return 1
5559
return 1
56-
}
60+
},
5761
})
5862
```
5963

@@ -74,18 +78,20 @@ TableOfContents.configure({
7478
```
7579

7680
### getLevel
81+
7782
This option can be used to customize how item levels are generated. By default the normal level generation is used that checks for heading element level attributes. If you want to customize this because for example you want to include custom anchors in your heading generation, you can use this to do so.
7883

7984
```js
8085
TableOfContents.configure({
8186
getLevel: (anchor, previousAnchors) => {
8287
// do some custom logic, but for this example we will just return 1
8388
return 1
84-
}
89+
},
8590
})
8691
```
8792

8893
### getId
94+
8995
A builder function that returns a unique ID for each heading. Inside the argument you get access to the headings text content (for example you want to generate IDs based on the text content of the heading).
9096

9197
By default this is a function that uses the [uuid](https://www.npmjs.com/package/uuid) package to generate a unique ID.
@@ -96,23 +102,25 @@ Default: `() => uuid()`
96102
// here we use an imaginary "slugify" function
97103
// you should probably also add a unique identifier to the slug
98104
TableOfContents.configure({
99-
getId: (content) => slugify(content)
105+
getId: (content) => slugify(content),
100106
})
101107
```
102108

103109
### scrollParent
104-
The scroll parent you want to attach to. This is used to determine which heading currently is active or was already scrolled over. By default this is the window but you can pass any HTML element here.
105110

106-
Default: `window`
111+
The scroll parent you want to attach to. This is used to determine which heading currently is active or was already scrolled over. By default this is a callback function that returns the window but you can pass a callback that returns any HTML element here.
112+
113+
Default: `() => window`
107114

108115
```js
109116
// For example the editors DOM element itself is the scrolling element
110117
TableOfContents.configure({
111-
scrollParent: editor.view.dom
118+
scrollParent: () => editor.view.dom,
112119
})
113120
```
114121

115122
### onUpdate
123+
116124
The most important option that you must set to use this extension. This is a callback function that gets called whenever the Table of Content updates. You get access to an array of heading data (see below) which you can use to render your own Table of Content.
117125

118126
To render the table of content you can render it by any means you want. You can use a framework like Vue, React or Svelte or you can use a simple templating engine like Handlebars or Pug. You can also use a simple `document.createElement` to render the table of content.
@@ -145,7 +153,7 @@ TableOfContents.configure({
145153

146154
tocElement.appendChild(anchorElement)
147155
})
148-
}
156+
},
149157
})
150158
```
151159

@@ -157,7 +165,7 @@ const [anchors, setAnchors] = useState([])
157165
TableOfContents.configure({
158166
onUpdate: (anchors) => {
159167
setAnchors(anchors)
160-
}
168+
},
161169
})
162170
```
163171

@@ -168,27 +176,30 @@ const anchors = ref([])
168176
TableOfContents.configure({
169177
onUpdate: (anchors) => {
170178
anchors.value = anchors
171-
}
179+
},
172180
})
173181
```
174182

175183
## Storage
176184

177185
### content
186+
178187
The heading content of the current document
179188

180189
```js
181190
editor.storage.tableOfContents.content
182191
```
183192

184193
### anchors
194+
185195
An array of HTML nodes
186196

187197
```js
188198
editor.storage.tableOfContents.anchors
189199
```
190200

191201
### scrollHandler
202+
192203
The scrollHandler used by the scroll function. Should not be changed or edited but
193204
could be used to manually bind this function somewhere else
194205

@@ -197,6 +208,7 @@ editor.storage.tableOfContents.scrollHandler()
197208
```
198209

199210
### scrollPosition
211+
200212
The current scrollPosition inside the scrollParent.
201213

202214
```js
@@ -223,4 +235,4 @@ The array returned by the storage or the `onUpdate` function includes objects st
223235
}
224236
```
225237

226-
This should give you enough flexibility to render your own table of content.
238+
This should give you enough flexibility to render your own table of content.

0 commit comments

Comments
 (0)