@mehm8128/rehype-toc 1.1.0 → 1.2.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 +1 -1
- package/dist/index.d.ts +3 -4
- package/dist/index.js +29 -39
- package/package.json +48 -47
package/README.md
CHANGED
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,71 +1,65 @@
|
|
|
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
|
-
const
|
|
6
|
+
const rootOlElement = {
|
|
7
7
|
type: "element",
|
|
8
8
|
tagName: "ol",
|
|
9
9
|
properties: {},
|
|
10
10
|
children: []
|
|
11
11
|
};
|
|
12
12
|
visit(tree, "element", (node) => {
|
|
13
|
-
visitorCallback(node,
|
|
13
|
+
visitorCallback(node, rootOlElement);
|
|
14
14
|
});
|
|
15
|
-
const detailsElement =
|
|
15
|
+
const detailsElement = createCollapsibleToc(rootOlElement);
|
|
16
16
|
tree.children.unshift(detailsElement);
|
|
17
17
|
};
|
|
18
18
|
};
|
|
19
|
-
const visitorCallback = (node,
|
|
19
|
+
const visitorCallback = (node, rootOlElement) => {
|
|
20
20
|
if (!/^h[2-6]$/.test(node.tagName)) return;
|
|
21
21
|
const headingLevel = getHeadingLevelFromElement(node);
|
|
22
22
|
const liElement = createListItemElement(node);
|
|
23
|
-
const
|
|
23
|
+
const rootOlElementChildren = assertElementNodeList(rootOlElement.children);
|
|
24
24
|
if (headingLevel === 2) {
|
|
25
|
-
|
|
25
|
+
rootOlElement.children.push(liElement);
|
|
26
26
|
return;
|
|
27
27
|
}
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
const rootElementHeadingLevel = 2;
|
|
29
|
+
const sameLevelOlElement = searchSameLevelOlElement(rootOlElement, headingLevel, rootElementHeadingLevel);
|
|
30
|
+
if (sameLevelOlElement) {
|
|
31
|
+
sameLevelOlElement.children.push(liElement);
|
|
31
32
|
return;
|
|
32
33
|
}
|
|
33
|
-
const deepestLiElement = getDeepestLiElement(
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
deepestLiElement.children.push(
|
|
34
|
+
const deepestLiElement = getDeepestLiElement(rootOlElementChildren);
|
|
35
|
+
const newOlElement = createOlElement();
|
|
36
|
+
newOlElement.children.push(liElement);
|
|
37
|
+
deepestLiElement.children.push(newOlElement);
|
|
37
38
|
};
|
|
38
39
|
/**
|
|
39
40
|
* 引数のolに入っている一番新しいliの中で、levelと同じ見出しレベルのli要素を返す
|
|
40
41
|
*/
|
|
41
|
-
const
|
|
42
|
-
const rootLiElement = assertElementNode(
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (rootLiElement.children[1] === void 0) return;
|
|
48
|
-
const childUlElement = assertElementNode(rootLiElement.children[1]);
|
|
49
|
-
return searchSameLevelUlElement(assertElementNodeList(childUlElement.children), level);
|
|
42
|
+
const searchSameLevelOlElement = (rootOlElement, level, rootElementHeadingLevel) => {
|
|
43
|
+
const rootLiElement = assertElementNode(rootOlElement.children[rootOlElement.children.length - 1]);
|
|
44
|
+
if (level === rootElementHeadingLevel) return rootOlElement;
|
|
45
|
+
const childOlElement = assertElementNodeList(rootLiElement.children)[1];
|
|
46
|
+
if (childOlElement === void 0) return;
|
|
47
|
+
return searchSameLevelOlElement(childOlElement, level, rootElementHeadingLevel + 1);
|
|
50
48
|
};
|
|
51
49
|
/**
|
|
52
50
|
* 引数のolに入っている一番新しいliの中で、一番深いli要素を取得する
|
|
53
51
|
*/
|
|
54
|
-
const getDeepestLiElement = (
|
|
55
|
-
const rootLiElement = assertElementNode(
|
|
56
|
-
|
|
57
|
-
|
|
52
|
+
const getDeepestLiElement = (rootOlElement) => {
|
|
53
|
+
const rootLiElement = assertElementNode(rootOlElement[rootOlElement.length - 1]);
|
|
54
|
+
const olElement = assertElementNodeList(rootLiElement.children)[1];
|
|
55
|
+
if (!olElement) return rootLiElement;
|
|
58
56
|
return getDeepestLiElement(assertElementNodeList(olElement.children));
|
|
59
57
|
};
|
|
60
58
|
const getHeadingLevelFromElement = (headingElement) => {
|
|
61
59
|
const headingLevel = Number(headingElement.tagName.charAt(1));
|
|
62
60
|
return headingLevel;
|
|
63
61
|
};
|
|
64
|
-
const
|
|
65
|
-
const headingLevel = Number(headingElement.value.charAt(1));
|
|
66
|
-
return headingLevel;
|
|
67
|
-
};
|
|
68
|
-
const createUlElement = () => {
|
|
62
|
+
const createOlElement = () => {
|
|
69
63
|
return {
|
|
70
64
|
type: "element",
|
|
71
65
|
tagName: "ol",
|
|
@@ -94,7 +88,7 @@ const createListItemElement = (node) => {
|
|
|
94
88
|
children: [anchorElement]
|
|
95
89
|
};
|
|
96
90
|
};
|
|
97
|
-
const
|
|
91
|
+
const createCollapsibleToc = (rootOlElement) => {
|
|
98
92
|
const summaryElement = {
|
|
99
93
|
type: "element",
|
|
100
94
|
tagName: "summary",
|
|
@@ -108,7 +102,7 @@ const createCollapsableToc = (rootUlElement) => {
|
|
|
108
102
|
type: "element",
|
|
109
103
|
tagName: "details",
|
|
110
104
|
properties: {},
|
|
111
|
-
children: [summaryElement,
|
|
105
|
+
children: [summaryElement, rootOlElement]
|
|
112
106
|
};
|
|
113
107
|
return detailsElement;
|
|
114
108
|
};
|
|
@@ -120,10 +114,6 @@ const assertElementNodeList = (nodeList) => {
|
|
|
120
114
|
if (nodeList.some((node) => node.type !== "element")) throw new Error("Elementノードではありません");
|
|
121
115
|
return nodeList;
|
|
122
116
|
};
|
|
123
|
-
const assertElementText = (node) => {
|
|
124
|
-
if (node.type !== "text") throw new Error("Textノードではありません");
|
|
125
|
-
return node;
|
|
126
|
-
};
|
|
127
117
|
|
|
128
118
|
//#endregion
|
|
129
|
-
export {
|
|
119
|
+
export { rehypeCollapsibleToc };
|
package/package.json
CHANGED
|
@@ -1,48 +1,49 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
2
|
+
"name": "@mehm8128/rehype-toc",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+ssh://git@github.com/mehm8128/rehype-toc.git"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/mehm8128/rehype-toc",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"description": "rehype plugin to generate a collapsible table of contents",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"rehype",
|
|
14
|
+
"toc",
|
|
15
|
+
"table of contents"
|
|
16
|
+
],
|
|
17
|
+
"author": "mehm8128 <mehm8128@mail.com>",
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"package.json"
|
|
23
|
+
],
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./dist/index.js",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@biomejs/biome": "^2.2.4",
|
|
33
|
+
"@types/hast": "^3.0.4",
|
|
34
|
+
"@types/node": "^24.10.1",
|
|
35
|
+
"rehype": "^13.0.2",
|
|
36
|
+
"tsdown": "^0.11.9",
|
|
37
|
+
"typescript": "^5.8.3",
|
|
38
|
+
"vitest": "^3.1.3"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"unist-util-visit": "^5.0.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsdown",
|
|
45
|
+
"dev": "tsdown --watch",
|
|
46
|
+
"test": "vitest",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
}
|
|
49
|
+
}
|