@mehm8128/rehype-toc 1.0.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -9
- package/dist/index.d.ts +3 -4
- package/dist/index.js +8 -8
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,21 +1,41 @@
|
|
|
1
1
|
# rehype-toc
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/mehm8128/rehype-toc/actions/workflows/main.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@mehm8128/rehype-toc)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
rehype plugin to generate a table of contents from headings in your markdown.
|
|
8
|
+
|
|
9
|
+
You can create the table of contents as follows.
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## Installation
|
|
6
14
|
|
|
7
15
|
```bash
|
|
8
|
-
pnpm install
|
|
16
|
+
pnpm install @mehm8128/rehype-toc
|
|
9
17
|
```
|
|
10
18
|
|
|
11
|
-
|
|
19
|
+
## Usage
|
|
12
20
|
|
|
13
|
-
|
|
14
|
-
|
|
21
|
+
### astro.config.mjs
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { rehypeCollapsibleToc } from "@mehm8128/rehype-toc";
|
|
25
|
+
|
|
26
|
+
export default defineConfig({
|
|
27
|
+
markdown: {
|
|
28
|
+
rehypePlugins: [rehypeCollapsibleToc],
|
|
29
|
+
},
|
|
30
|
+
});
|
|
15
31
|
```
|
|
16
32
|
|
|
17
|
-
|
|
33
|
+
### rehype
|
|
18
34
|
|
|
19
|
-
```
|
|
20
|
-
|
|
35
|
+
```ts
|
|
36
|
+
import { rehype } from "rehype"
|
|
37
|
+
import { rehypeCollapsibleToc } from "@mehm8128/rehype-toc";
|
|
38
|
+
|
|
39
|
+
const input = "<h2 id="heading-id"><a href="#heading-id">Heading</a></h2>";
|
|
40
|
+
const { value } = await rehype().use(rehypeCollapsibleToc).process(input);
|
|
21
41
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Root } from "hast";
|
|
2
2
|
|
|
3
3
|
//#region src/index.d.ts
|
|
4
|
-
declare const
|
|
5
|
-
declare const visitorCallback: (node: Element, rootUlElement: Element) => void;
|
|
4
|
+
declare const rehypeCollapsibleToc: () => (tree: Root) => void;
|
|
6
5
|
//#endregion
|
|
7
|
-
export {
|
|
6
|
+
export { rehypeCollapsibleToc };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { visit } from "unist-util-visit";
|
|
2
2
|
|
|
3
3
|
//#region src/index.ts
|
|
4
|
-
const
|
|
4
|
+
const rehypeCollapsibleToc = () => {
|
|
5
5
|
return (tree) => {
|
|
6
6
|
const rootUlElement = {
|
|
7
7
|
type: "element",
|
|
@@ -12,7 +12,7 @@ const rehypeCollapsableToc = () => {
|
|
|
12
12
|
visit(tree, "element", (node) => {
|
|
13
13
|
visitorCallback(node, rootUlElement);
|
|
14
14
|
});
|
|
15
|
-
const detailsElement =
|
|
15
|
+
const detailsElement = createCollapsibleToc(rootUlElement);
|
|
16
16
|
tree.children.unshift(detailsElement);
|
|
17
17
|
};
|
|
18
18
|
};
|
|
@@ -44,8 +44,8 @@ const searchSameLevelUlElement = (rootUlElement, level) => {
|
|
|
44
44
|
const headingTextElement = assertElementText(headingAnchorElement.children[0]);
|
|
45
45
|
const rootElementHeadingLevel = getHeadingLevelFromText(headingTextElement);
|
|
46
46
|
if (rootElementHeadingLevel === level) return rootLiElement;
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const childUlElement = assertElementNodeList(rootLiElement.children)[1];
|
|
48
|
+
if (childUlElement === void 0) return;
|
|
49
49
|
return searchSameLevelUlElement(assertElementNodeList(childUlElement.children), level);
|
|
50
50
|
};
|
|
51
51
|
/**
|
|
@@ -53,8 +53,8 @@ const searchSameLevelUlElement = (rootUlElement, level) => {
|
|
|
53
53
|
*/
|
|
54
54
|
const getDeepestLiElement = (rootUlElement) => {
|
|
55
55
|
const rootLiElement = assertElementNode(rootUlElement[rootUlElement.length - 1]);
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
const olElement = assertElementNodeList(rootLiElement.children)[1];
|
|
57
|
+
if (!olElement) return rootLiElement;
|
|
58
58
|
return getDeepestLiElement(assertElementNodeList(olElement.children));
|
|
59
59
|
};
|
|
60
60
|
const getHeadingLevelFromElement = (headingElement) => {
|
|
@@ -94,7 +94,7 @@ const createListItemElement = (node) => {
|
|
|
94
94
|
children: [anchorElement]
|
|
95
95
|
};
|
|
96
96
|
};
|
|
97
|
-
const
|
|
97
|
+
const createCollapsibleToc = (rootUlElement) => {
|
|
98
98
|
const summaryElement = {
|
|
99
99
|
type: "element",
|
|
100
100
|
tagName: "summary",
|
|
@@ -126,4 +126,4 @@ const assertElementText = (node) => {
|
|
|
126
126
|
};
|
|
127
127
|
|
|
128
128
|
//#endregion
|
|
129
|
-
export {
|
|
129
|
+
export { rehypeCollapsibleToc };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mehm8128/rehype-toc",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"homepage": "https://github.com/mehm8128/rehype-toc",
|
|
10
10
|
"license": "MIT",
|
|
11
|
-
"description": "rehype plugin to generate a
|
|
11
|
+
"description": "rehype plugin to generate a collapsible table of contents",
|
|
12
12
|
"keywords": [
|
|
13
13
|
"rehype",
|
|
14
14
|
"toc",
|