@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 CHANGED
@@ -13,7 +13,7 @@ You can create the table of contents as follows.
13
13
  ## Installation
14
14
 
15
15
  ```bash
16
- pnpm install @mehm8128/rehype-toc
16
+ pnpm add @mehm8128/rehype-toc
17
17
  ```
18
18
 
19
19
  ## Usage
package/dist/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
- import { Element, Root } from "hast";
1
+ import { Root } from "hast";
2
2
 
3
3
  //#region src/index.d.ts
4
- declare const rehypeCollapsableToc: () => (tree: Root) => void;
5
- declare const visitorCallback: (node: Element, rootUlElement: Element) => void;
4
+ declare const rehypeCollapsibleToc: () => (tree: Root) => void;
6
5
  //#endregion
7
- export { rehypeCollapsableToc, visitorCallback };
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 rehypeCollapsableToc = () => {
4
+ const rehypeCollapsibleToc = () => {
5
5
  return (tree) => {
6
- const rootUlElement = {
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, rootUlElement);
13
+ visitorCallback(node, rootOlElement);
14
14
  });
15
- const detailsElement = createCollapsableToc(rootUlElement);
15
+ const detailsElement = createCollapsibleToc(rootOlElement);
16
16
  tree.children.unshift(detailsElement);
17
17
  };
18
18
  };
19
- const visitorCallback = (node, rootUlElement) => {
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 rootUlElementChildren = assertElementNodeList(rootUlElement.children);
23
+ const rootOlElementChildren = assertElementNodeList(rootOlElement.children);
24
24
  if (headingLevel === 2) {
25
- rootUlElement.children.push(liElement);
25
+ rootOlElement.children.push(liElement);
26
26
  return;
27
27
  }
28
- const sameLevelUlElement = searchSameLevelUlElement(rootUlElementChildren, headingLevel);
29
- if (sameLevelUlElement) {
30
- sameLevelUlElement.children.push(liElement);
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(rootUlElementChildren);
34
- const newUlElement = createUlElement();
35
- newUlElement.children.push(liElement);
36
- deepestLiElement.children.push(newUlElement);
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 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);
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 = (rootUlElement) => {
55
- const rootLiElement = assertElementNode(rootUlElement[rootUlElement.length - 1]);
56
- if (!rootLiElement.children[1]) return rootLiElement;
57
- const olElement = assertElementNode(rootLiElement.children[1]);
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 getHeadingLevelFromText = (headingElement) => {
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 createCollapsableToc = (rootUlElement) => {
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, rootUlElement]
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 { rehypeCollapsableToc, visitorCallback };
119
+ export { rehypeCollapsibleToc };
package/package.json CHANGED
@@ -1,48 +1,49 @@
1
1
  {
2
- "name": "@mehm8128/rehype-toc",
3
- "version": "1.1.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 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
- "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
- }
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
+ }