@mehm8128/rehype-toc 1.0.0
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/LICENSE +21 -0
- package/README.md +21 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +129 -0
- package/package.json +48 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 mehm8128
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Element, Root } from "hast";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare const rehypeCollapsableToc: () => (tree: Root) => void;
|
|
5
|
+
declare const visitorCallback: (node: Element, rootUlElement: Element) => void;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { rehypeCollapsableToc, visitorCallback };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { visit } from "unist-util-visit";
|
|
2
|
+
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
const rehypeCollapsableToc = () => {
|
|
5
|
+
return (tree) => {
|
|
6
|
+
const rootUlElement = {
|
|
7
|
+
type: "element",
|
|
8
|
+
tagName: "ol",
|
|
9
|
+
properties: {},
|
|
10
|
+
children: []
|
|
11
|
+
};
|
|
12
|
+
visit(tree, "element", (node) => {
|
|
13
|
+
visitorCallback(node, rootUlElement);
|
|
14
|
+
});
|
|
15
|
+
const detailsElement = createCollapsableToc(rootUlElement);
|
|
16
|
+
tree.children.unshift(detailsElement);
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
const visitorCallback = (node, rootUlElement) => {
|
|
20
|
+
if (!/^h[2-6]$/.test(node.tagName)) return;
|
|
21
|
+
const headingLevel = getHeadingLevelFromElement(node);
|
|
22
|
+
const liElement = createListItemElement(node);
|
|
23
|
+
const rootUlElementChildren = assertElementNodeList(rootUlElement.children);
|
|
24
|
+
if (headingLevel === 2) {
|
|
25
|
+
rootUlElement.children.push(liElement);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const sameLevelUlElement = searchSameLevelUlElement(rootUlElementChildren, headingLevel);
|
|
29
|
+
if (sameLevelUlElement) {
|
|
30
|
+
sameLevelUlElement.children.push(liElement);
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const deepestLiElement = getDeepestLiElement(rootUlElementChildren);
|
|
34
|
+
const newUlElement = createUlElement();
|
|
35
|
+
newUlElement.children.push(liElement);
|
|
36
|
+
deepestLiElement.children.push(newUlElement);
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* 引数のolに入っている一番新しいliの中で、levelと同じ見出しレベルのli要素を返す
|
|
40
|
+
*/
|
|
41
|
+
const searchSameLevelUlElement = (rootUlElement, level) => {
|
|
42
|
+
const rootLiElement = assertElementNode(rootUlElement[rootUlElement.length - 1]);
|
|
43
|
+
const headingAnchorElement = assertElementNode(rootLiElement.children[0]);
|
|
44
|
+
const headingTextElement = assertElementText(headingAnchorElement.children[0]);
|
|
45
|
+
const rootElementHeadingLevel = getHeadingLevelFromText(headingTextElement);
|
|
46
|
+
if (rootElementHeadingLevel === level) return rootLiElement;
|
|
47
|
+
if (rootLiElement.children[1] === void 0) return;
|
|
48
|
+
const childUlElement = assertElementNode(rootLiElement.children[1]);
|
|
49
|
+
return searchSameLevelUlElement(assertElementNodeList(childUlElement.children), level);
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* 引数のolに入っている一番新しいliの中で、一番深いli要素を取得する
|
|
53
|
+
*/
|
|
54
|
+
const getDeepestLiElement = (rootUlElement) => {
|
|
55
|
+
const rootLiElement = assertElementNode(rootUlElement[rootUlElement.length - 1]);
|
|
56
|
+
if (!rootLiElement.children[1]) return rootLiElement;
|
|
57
|
+
const olElement = assertElementNode(rootLiElement.children[1]);
|
|
58
|
+
return getDeepestLiElement(assertElementNodeList(olElement.children));
|
|
59
|
+
};
|
|
60
|
+
const getHeadingLevelFromElement = (headingElement) => {
|
|
61
|
+
const headingLevel = Number(headingElement.tagName.charAt(1));
|
|
62
|
+
return headingLevel;
|
|
63
|
+
};
|
|
64
|
+
const getHeadingLevelFromText = (headingElement) => {
|
|
65
|
+
const headingLevel = Number(headingElement.value.charAt(1));
|
|
66
|
+
return headingLevel;
|
|
67
|
+
};
|
|
68
|
+
const createUlElement = () => {
|
|
69
|
+
return {
|
|
70
|
+
type: "element",
|
|
71
|
+
tagName: "ol",
|
|
72
|
+
properties: {},
|
|
73
|
+
children: []
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
const createListItemElement = (node) => {
|
|
77
|
+
const headingChildren = node.children.flatMap((child) => child.type === "element" ? child.children : []);
|
|
78
|
+
const headingTextElement = headingChildren.find((child) => child.type === "text");
|
|
79
|
+
if (!headingTextElement) throw new Error("見出しにテキストがありません");
|
|
80
|
+
const headingText = headingTextElement.value;
|
|
81
|
+
const anchorElement = {
|
|
82
|
+
type: "element",
|
|
83
|
+
tagName: "a",
|
|
84
|
+
properties: { href: `#${headingText}` },
|
|
85
|
+
children: [{
|
|
86
|
+
type: "text",
|
|
87
|
+
value: headingText
|
|
88
|
+
}]
|
|
89
|
+
};
|
|
90
|
+
return {
|
|
91
|
+
type: "element",
|
|
92
|
+
tagName: "li",
|
|
93
|
+
properties: {},
|
|
94
|
+
children: [anchorElement]
|
|
95
|
+
};
|
|
96
|
+
};
|
|
97
|
+
const createCollapsableToc = (rootUlElement) => {
|
|
98
|
+
const summaryElement = {
|
|
99
|
+
type: "element",
|
|
100
|
+
tagName: "summary",
|
|
101
|
+
properties: {},
|
|
102
|
+
children: [{
|
|
103
|
+
type: "text",
|
|
104
|
+
value: "目次"
|
|
105
|
+
}]
|
|
106
|
+
};
|
|
107
|
+
const detailsElement = {
|
|
108
|
+
type: "element",
|
|
109
|
+
tagName: "details",
|
|
110
|
+
properties: {},
|
|
111
|
+
children: [summaryElement, rootUlElement]
|
|
112
|
+
};
|
|
113
|
+
return detailsElement;
|
|
114
|
+
};
|
|
115
|
+
const assertElementNode = (node) => {
|
|
116
|
+
if (node.type !== "element") throw new Error("Elementノードではありません");
|
|
117
|
+
return node;
|
|
118
|
+
};
|
|
119
|
+
const assertElementNodeList = (nodeList) => {
|
|
120
|
+
if (nodeList.some((node) => node.type !== "element")) throw new Error("Elementノードではありません");
|
|
121
|
+
return nodeList;
|
|
122
|
+
};
|
|
123
|
+
const assertElementText = (node) => {
|
|
124
|
+
if (node.type !== "text") throw new Error("Textノードではありません");
|
|
125
|
+
return node;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
export { rehypeCollapsableToc, visitorCallback };
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mehm8128/rehype-toc",
|
|
3
|
+
"version": "1.0.0",
|
|
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 collapsable 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
|
+
"scripts": {
|
|
32
|
+
"build": "tsdown",
|
|
33
|
+
"dev": "tsdown --watch",
|
|
34
|
+
"test": "vitest",
|
|
35
|
+
"typecheck": "tsc --noEmit"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@biomejs/biome": "^2.2.4",
|
|
39
|
+
"@types/hast": "^3.0.4",
|
|
40
|
+
"rehype": "^13.0.2",
|
|
41
|
+
"tsdown": "^0.11.9",
|
|
42
|
+
"typescript": "^5.8.3",
|
|
43
|
+
"vitest": "^3.1.3"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"unist-util-visit": "^5.0.0"
|
|
47
|
+
}
|
|
48
|
+
}
|