rehype-custom-toc 1.1.3 → 1.1.5
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/dist/index.js +12 -14
- package/package.json +10 -14
package/dist/index.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import GitHubSlugger from "github-slugger";
|
|
2
2
|
import { fromHtml } from "hast-util-from-html";
|
|
3
3
|
import { h } from "hastscript";
|
|
4
|
-
import { isNonEmptyArray } from "@robot-inventor/ts-utils";
|
|
5
4
|
import { toHtml } from "hast-util-to-html";
|
|
6
5
|
import { toText } from "hast-util-to-text";
|
|
7
6
|
import { visit } from "unist-util-visit";
|
|
@@ -41,7 +40,7 @@ const isHeading = (node) => ["h1", "h2", "h3", "h4", "h5", "h6"].includes(node.t
|
|
|
41
40
|
const addIdToHeading = (node, slugger) => {
|
|
42
41
|
const text = toText(node).trim();
|
|
43
42
|
// Empty-string ids should be replaced with a generated slug
|
|
44
|
-
//
|
|
43
|
+
// oxlint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
45
44
|
const slug = node.properties.id || slugger.slug(text);
|
|
46
45
|
node.properties.id = slug;
|
|
47
46
|
return { slug, text };
|
|
@@ -71,7 +70,7 @@ const rehypeSlugger = () => {
|
|
|
71
70
|
* @param options Options for the plugin
|
|
72
71
|
* @returns The generated table of contents
|
|
73
72
|
*/
|
|
74
|
-
//
|
|
73
|
+
// oxlint-disable-next-line max-statements
|
|
75
74
|
const generateToc = (tree, options) => {
|
|
76
75
|
const toc = {
|
|
77
76
|
children: [],
|
|
@@ -85,7 +84,7 @@ const generateToc = (tree, options) => {
|
|
|
85
84
|
if (!isHeading(node))
|
|
86
85
|
return;
|
|
87
86
|
const { slug, text } = addIdToHeading(node, slugger);
|
|
88
|
-
//
|
|
87
|
+
// oxlint-disable-next-line no-magic-numbers
|
|
89
88
|
const depth = parseInt(node.tagName.slice(1), 10);
|
|
90
89
|
headings.push({
|
|
91
90
|
depth,
|
|
@@ -93,13 +92,13 @@ const generateToc = (tree, options) => {
|
|
|
93
92
|
text
|
|
94
93
|
});
|
|
95
94
|
});
|
|
96
|
-
if (!
|
|
95
|
+
if (!headings[0])
|
|
97
96
|
return [];
|
|
98
97
|
let currentDepth = headings[0].depth;
|
|
99
98
|
let currentParent = toc;
|
|
100
99
|
const parents = [toc];
|
|
101
100
|
for (const heading of headings) {
|
|
102
|
-
//
|
|
101
|
+
// oxlint-disable-next-line no-continue
|
|
103
102
|
if (heading.depth > options.maxDepth)
|
|
104
103
|
continue;
|
|
105
104
|
const li = h("li", h("a", { href: `#${heading.slug}` }, heading.text));
|
|
@@ -118,10 +117,9 @@ const generateToc = (tree, options) => {
|
|
|
118
117
|
}
|
|
119
118
|
else {
|
|
120
119
|
// The current heading is at a shallower level than the previous one.
|
|
121
|
-
// eslint-disable-next-line id-length
|
|
122
120
|
for (let i = 0; i < currentDepth - heading.depth; i++) {
|
|
123
121
|
parents.pop();
|
|
124
|
-
//
|
|
122
|
+
// oxlint-disable-next-line no-magic-numbers
|
|
125
123
|
const parentNode = parents[parents.length - 1];
|
|
126
124
|
if (!parentNode) {
|
|
127
125
|
throw new Error("Parent node not found. Make sure the headings are sorted by depth.");
|
|
@@ -151,24 +149,24 @@ const rehypeCustomToc = (userOptions) => {
|
|
|
151
149
|
visitParents(tree, "comment", (node, ancestors) => {
|
|
152
150
|
if (node.value.trim().toLowerCase() !== "toc")
|
|
153
151
|
return;
|
|
154
|
-
//
|
|
152
|
+
// oxlint-disable-next-line no-magic-numbers, @typescript-eslint/no-non-null-assertion
|
|
155
153
|
const parent = ancestors.at(-1);
|
|
156
154
|
const index = parent.children.indexOf(node);
|
|
157
|
-
//
|
|
155
|
+
// oxlint-disable-next-line no-magic-numbers
|
|
158
156
|
if (parent.type === "element" && parent.tagName === "p" && parent.children.length === 1) {
|
|
159
|
-
//
|
|
157
|
+
// oxlint-disable-next-line no-magic-numbers, @typescript-eslint/no-non-null-assertion
|
|
160
158
|
const grand = ancestors.at(-2);
|
|
161
159
|
const parentIdx = grand.children.indexOf(parent);
|
|
162
|
-
//
|
|
160
|
+
// oxlint-disable-next-line no-magic-numbers
|
|
163
161
|
grand.children.splice(parentIdx, 1, ...tocNodes);
|
|
164
162
|
}
|
|
165
163
|
else {
|
|
166
|
-
//
|
|
164
|
+
// oxlint-disable-next-line no-magic-numbers
|
|
167
165
|
parent.children.splice(index, 1, ...tocNodes);
|
|
168
166
|
}
|
|
169
167
|
tocInserted = true;
|
|
170
168
|
});
|
|
171
|
-
//
|
|
169
|
+
// oxlint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
172
170
|
if (!tocInserted) {
|
|
173
171
|
tree.children.unshift(...tocNodes);
|
|
174
172
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rehype-custom-toc",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.5",
|
|
4
4
|
"description": "rehype plugin to generate a customizable table of contents",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"rehype",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
33
|
"test": "cross-env NODE_ENV=\"production\" vitest",
|
|
34
|
-
"lint": "
|
|
34
|
+
"lint": "oxlint ./src",
|
|
35
35
|
"build": "tsc",
|
|
36
36
|
"format": "prettier --write \"./src/**/*.ts\"",
|
|
37
37
|
"format:check": "prettier --check \"./src/**/*.ts\"",
|
|
@@ -39,7 +39,6 @@
|
|
|
39
39
|
"ci:publish": "npm run build && changeset publish"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@robot-inventor/ts-utils": "^0.8.9",
|
|
43
42
|
"github-slugger": "^2.0.0",
|
|
44
43
|
"hast-util-from-html": "^2.0.3",
|
|
45
44
|
"hast-util-to-html": "^9.0.5",
|
|
@@ -50,23 +49,20 @@
|
|
|
50
49
|
"unist-util-visit-parents": "^6.0.2"
|
|
51
50
|
},
|
|
52
51
|
"devDependencies": {
|
|
53
|
-
"@changesets/changelog-github": "^1.0.
|
|
54
|
-
"@changesets/cli": "^3.0.
|
|
55
|
-
"@robot-inventor/
|
|
56
|
-
"@robot-inventor/tsconfig-base": "^7.
|
|
52
|
+
"@changesets/changelog-github": "^1.0.1",
|
|
53
|
+
"@changesets/cli": "^3.0.3",
|
|
54
|
+
"@robot-inventor/oxlint-config": "^0.5.2",
|
|
55
|
+
"@robot-inventor/tsconfig-base": "^7.2.0",
|
|
57
56
|
"@types/hast": "^3.0.5",
|
|
58
57
|
"cross-env": "^10.1.0",
|
|
59
|
-
"
|
|
60
|
-
"prettier": "^3.9.
|
|
58
|
+
"oxlint": "^1.89.0",
|
|
59
|
+
"prettier": "^3.9.10",
|
|
61
60
|
"rehype-parse": "^9.0.1",
|
|
62
61
|
"rehype-stringify": "^10.0.1",
|
|
63
62
|
"remark-comment": "^1.0.0",
|
|
64
63
|
"remark-parse": "^11.0.0",
|
|
65
64
|
"remark-rehype": "^11.1.2",
|
|
66
|
-
"typescript": "^
|
|
67
|
-
"vitest": "^
|
|
68
|
-
},
|
|
69
|
-
"overrides": {
|
|
70
|
-
"zod-validation-error": "^5.0.0"
|
|
65
|
+
"typescript": "^7.0.2",
|
|
66
|
+
"vitest": "^5.0.5"
|
|
71
67
|
}
|
|
72
68
|
}
|