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