@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 CHANGED
@@ -1,21 +1,41 @@
1
1
  # rehype-toc
2
2
 
3
- ## Development
3
+ [![CI on main](https://github.com/mehm8128/rehype-toc/actions/workflows/main.yml/badge.svg)](https://github.com/mehm8128/rehype-toc/actions/workflows/main.yml)
4
+ [![NPM Version](https://img.shields.io/npm/v/@mehm8128/rehype-toc)](https://www.npmjs.com/package/@mehm8128/rehype-toc)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4
6
 
5
- - Install dependencies:
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
+ ![A table of contents whose each items are anchor link.](assets/toc.png)
12
+
13
+ ## Installation
6
14
 
7
15
  ```bash
8
- pnpm install
16
+ pnpm install @mehm8128/rehype-toc
9
17
  ```
10
18
 
11
- - Run the unit tests:
19
+ ## Usage
12
20
 
13
- ```bash
14
- pnpm run test
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
- - Build the library:
33
+ ### rehype
18
34
 
19
- ```bash
20
- pnpm run build
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 { 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,7 +1,7 @@
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
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 = createCollapsableToc(rootUlElement);
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
- if (rootLiElement.children[1] === void 0) return;
48
- const childUlElement = assertElementNode(rootLiElement.children[1]);
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
- if (!rootLiElement.children[1]) return rootLiElement;
57
- const olElement = assertElementNode(rootLiElement.children[1]);
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 createCollapsableToc = (rootUlElement) => {
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 { rehypeCollapsableToc, visitorCallback };
129
+ export { rehypeCollapsibleToc };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mehm8128/rehype-toc",
3
- "version": "1.0.0",
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 collapsable table of contents",
11
+ "description": "rehype plugin to generate a collapsible table of contents",
12
12
  "keywords": [
13
13
  "rehype",
14
14
  "toc",