astro-custom-toc 2.0.45 → 3.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/README.md +2 -40
- package/dist/index.d.ts +10 -1
- package/dist/rehype-custom-toc.d.ts +2 -53
- package/dist/rehype-custom-toc.js +6 -107
- package/package.json +9 -11
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# astro-custom-toc
|
|
2
2
|
|
|
3
|
-
Astro Integration to generate a customizable table of contents.
|
|
3
|
+
Astro Integration to generate a customizable table of contents. This is a convenient wrapper for [rehype-custom-toc](https://github.com/Robot-Inventor/rehype-custom-toc/). If you are not using Astro, you can directly use rehype-custom-toc instead.
|
|
4
4
|
|
|
5
5
|
> [!WARNING]
|
|
6
6
|
> This plugin uses [remark-comment](https://github.com/leebyron/remark-comment). It may break other plugins that use comments.
|
|
@@ -56,45 +56,7 @@ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
|
|
|
56
56
|
|
|
57
57
|
## Options
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
type RemarkCustomTocTemplate = (html: string) => string;
|
|
61
|
-
|
|
62
|
-
interface RemarkCustomTocOptions {
|
|
63
|
-
template?: RemarkCustomTocTemplate;
|
|
64
|
-
maxDepth?: number;
|
|
65
|
-
ordered?: boolean;
|
|
66
|
-
}
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
### `template`
|
|
70
|
-
|
|
71
|
-
A function that takes the generated HTML and returns the final HTML. This can be used to wrap the generated HTML in a custom template.
|
|
72
|
-
|
|
73
|
-
Default:
|
|
74
|
-
|
|
75
|
-
```javascript
|
|
76
|
-
const defaultTemplate = (html) => {
|
|
77
|
-
return `
|
|
78
|
-
<aside class="toc">
|
|
79
|
-
<h2>Contents</h2>
|
|
80
|
-
<nav>
|
|
81
|
-
${html}
|
|
82
|
-
</nav>
|
|
83
|
-
</aside>`.trim();
|
|
84
|
-
};
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
### `maxDepth`
|
|
88
|
-
|
|
89
|
-
The maximum depth of headings to include in the table of contents.
|
|
90
|
-
|
|
91
|
-
Default: `3`
|
|
92
|
-
|
|
93
|
-
### `ordered`
|
|
94
|
-
|
|
95
|
-
Whether to use an ordered list (`<ol>`) or an unordered list (`<ul>`).
|
|
96
|
-
|
|
97
|
-
Default: `false`
|
|
59
|
+
All options of rehype-custom-toc can be used in this plugin. Please refer to the [rehype-custom-toc documentation](https://github.com/Robot-Inventor/rehype-custom-toc/) for options.
|
|
98
60
|
|
|
99
61
|
## Development
|
|
100
62
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { AstroIntegration } from "astro";
|
|
2
|
-
import { type RehypeCustomTocOptions } from "./rehype-custom-toc.js";
|
|
2
|
+
import { type Comment, type RehypeCustomTocOptions, type RehypeCustomTocTemplate } from "./rehype-custom-toc.js";
|
|
3
|
+
declare module "mdast" {
|
|
4
|
+
interface RootContentMap {
|
|
5
|
+
comment: Comment;
|
|
6
|
+
}
|
|
7
|
+
interface Data {
|
|
8
|
+
id: string;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
3
11
|
/**
|
|
4
12
|
* Create the astro-custom-toc integration.
|
|
5
13
|
* @param options Options for the integration
|
|
@@ -7,3 +15,4 @@ import { type RehypeCustomTocOptions } from "./rehype-custom-toc.js";
|
|
|
7
15
|
*/
|
|
8
16
|
declare const astroCustomToc: (options?: RehypeCustomTocOptions) => AstroIntegration;
|
|
9
17
|
export default astroCustomToc;
|
|
18
|
+
export type { RehypeCustomTocOptions, RehypeCustomTocTemplate };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type RehypeCustomTocOptions, type RehypeCustomTocTemplate } from "rehype-custom-toc";
|
|
2
2
|
import type { Plugin } from "unified";
|
|
3
|
-
import type {
|
|
3
|
+
import type { Root } from "hast";
|
|
4
4
|
/**
|
|
5
5
|
* A comment node generated by the `remark-comment` plugin.
|
|
6
6
|
*/
|
|
@@ -9,57 +9,6 @@ interface Comment extends Node {
|
|
|
9
9
|
value: "";
|
|
10
10
|
commentValue: string;
|
|
11
11
|
}
|
|
12
|
-
declare module "mdast" {
|
|
13
|
-
interface RootContentMap {
|
|
14
|
-
comment: Comment;
|
|
15
|
-
}
|
|
16
|
-
interface Data {
|
|
17
|
-
id: string;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Custom TOC template function.
|
|
22
|
-
* @param html HTML content of the TOC list
|
|
23
|
-
* @returns Wrapped HTML content
|
|
24
|
-
*/
|
|
25
|
-
type RehypeCustomTocTemplate = (html: string) => string;
|
|
26
|
-
/**
|
|
27
|
-
* Options for the rehypeCustomToc plugin.
|
|
28
|
-
*/
|
|
29
|
-
interface RehypeCustomTocOptions {
|
|
30
|
-
/**
|
|
31
|
-
* A function that takes the generated HTML and returns the final HTML.
|
|
32
|
-
* This can be used to wrap the generated HTML in a custom template.
|
|
33
|
-
* @default
|
|
34
|
-
* ```javascript
|
|
35
|
-
* const defaultTemplate = (html) => {
|
|
36
|
-
* return `
|
|
37
|
-
* <aside class="toc">
|
|
38
|
-
* <h2>Contents</h2>
|
|
39
|
-
* <nav>
|
|
40
|
-
* ${html}
|
|
41
|
-
* </nav>
|
|
42
|
-
* </aside>`.trim();
|
|
43
|
-
* };
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
template?: RehypeCustomTocTemplate;
|
|
47
|
-
/**
|
|
48
|
-
* The maximum depth of headings to include in the table of contents.
|
|
49
|
-
* @default 3
|
|
50
|
-
*/
|
|
51
|
-
maxDepth?: number;
|
|
52
|
-
/**
|
|
53
|
-
* Whether to use an ordered list (`<ol>`) or an unordered list (`<ul>`).
|
|
54
|
-
* @default false
|
|
55
|
-
*/
|
|
56
|
-
ordered?: boolean;
|
|
57
|
-
}
|
|
58
|
-
declare module "vfile" {
|
|
59
|
-
interface DataMap {
|
|
60
|
-
__astroHeadings: MarkdownHeading[];
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
12
|
/**
|
|
64
13
|
* Rehype plugin to generate a table of contents.
|
|
65
14
|
* @param userOptions Options for the plugin
|
|
@@ -1,117 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { fromHtml } from "hast-util-from-html";
|
|
3
|
-
import { h } from "hastscript";
|
|
4
|
-
import { toHtml } from "hast-util-to-html";
|
|
5
|
-
import { visit } from "unist-util-visit";
|
|
6
|
-
/**
|
|
7
|
-
* Default TOC template function for {@link RehypeCustomTocTemplate}.
|
|
8
|
-
* @param html HTML content of the TOC list
|
|
9
|
-
* @returns Wrapped HTML content
|
|
10
|
-
*/
|
|
11
|
-
const defaultTemplate = (html) => `
|
|
12
|
-
<aside class="toc">
|
|
13
|
-
<h2>Contents</h2>
|
|
14
|
-
<nav>
|
|
15
|
-
${html}
|
|
16
|
-
</nav>
|
|
17
|
-
</aside>`.trim();
|
|
18
|
-
/**
|
|
19
|
-
* Default options for the rehypeCustomToc plugin.
|
|
20
|
-
*/
|
|
21
|
-
const DEFAULT_OPTIONS = {
|
|
22
|
-
maxDepth: 3,
|
|
23
|
-
ordered: false,
|
|
24
|
-
template: defaultTemplate
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* Generate the table of contents from the headings data.
|
|
28
|
-
* @param options Options for the plugin
|
|
29
|
-
* @param headings Headings data
|
|
30
|
-
* @returns The generated table of contents
|
|
31
|
-
*/
|
|
32
|
-
// eslint-disable-next-line max-statements, max-lines-per-function
|
|
33
|
-
const generateToc = (options, headings) => {
|
|
34
|
-
const toc = {
|
|
35
|
-
children: [],
|
|
36
|
-
properties: {},
|
|
37
|
-
tagName: options.ordered ? "ol" : "ul",
|
|
38
|
-
type: "element"
|
|
39
|
-
};
|
|
40
|
-
let currentDepth = headings[0].depth;
|
|
41
|
-
let currentParent = toc;
|
|
42
|
-
const parents = [toc];
|
|
43
|
-
for (const heading of headings) {
|
|
44
|
-
// eslint-disable-next-line no-continue
|
|
45
|
-
if (heading.depth > options.maxDepth)
|
|
46
|
-
continue;
|
|
47
|
-
const li = h("li", h("a", { href: `#${heading.slug}` }, heading.text));
|
|
48
|
-
if (heading.depth === currentDepth) {
|
|
49
|
-
// The current heading is at the same level as the previous one.
|
|
50
|
-
currentParent.children.push(li);
|
|
51
|
-
currentDepth = heading.depth;
|
|
52
|
-
}
|
|
53
|
-
else if (heading.depth > currentDepth) {
|
|
54
|
-
// The current heading is at a deeper level than the previous one.
|
|
55
|
-
const ul = h(options.ordered ? "ol" : "ul", li);
|
|
56
|
-
currentParent.children.push(ul);
|
|
57
|
-
currentParent = ul;
|
|
58
|
-
parents.push(currentParent);
|
|
59
|
-
currentDepth = heading.depth;
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
// The current heading is at a shallower level than the previous one.
|
|
63
|
-
// eslint-disable-next-line id-length
|
|
64
|
-
for (let i = 0; i < currentDepth - heading.depth; i++) {
|
|
65
|
-
parents.pop();
|
|
66
|
-
// eslint-disable-next-line no-magic-numbers
|
|
67
|
-
const parentNode = parents[parents.length - 1];
|
|
68
|
-
if (!parentNode) {
|
|
69
|
-
throw new Error("Parent node not found. Make sure the headings are sorted by depth.");
|
|
70
|
-
}
|
|
71
|
-
currentParent = parentNode;
|
|
72
|
-
}
|
|
73
|
-
currentParent.children.push(li);
|
|
74
|
-
currentDepth = heading.depth;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return fromHtml(options.template(toHtml(toc)), { fragment: true }).children;
|
|
78
|
-
};
|
|
1
|
+
import rehypeCustomTocBase, {} from "rehype-custom-toc";
|
|
79
2
|
/**
|
|
80
3
|
* Rehype plugin to generate a table of contents.
|
|
81
4
|
* @param userOptions Options for the plugin
|
|
82
5
|
* @returns The plugin
|
|
83
6
|
*/
|
|
84
7
|
const rehypeCustomToc = (userOptions) => {
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
* @param vFile VFile data
|
|
90
|
-
* @param vFile.data The VFile data
|
|
91
|
-
*/
|
|
92
|
-
const transformer = (tree, { data }) => {
|
|
93
|
-
if (!data.astro?.frontmatter || data.astro.frontmatter["showToc"] !== true)
|
|
94
|
-
return;
|
|
95
|
-
const { headings } = data.astro;
|
|
96
|
-
if (!headings)
|
|
97
|
-
throw new Error("Headings data not found in the file data.");
|
|
98
|
-
if (!isNonEmptyArray(headings))
|
|
8
|
+
const createBaseTransformer = rehypeCustomTocBase;
|
|
9
|
+
const transformer = createBaseTransformer(userOptions);
|
|
10
|
+
return (tree, file) => {
|
|
11
|
+
if (!file.data.astro?.frontmatter || file.data.astro.frontmatter["showToc"] !== true)
|
|
99
12
|
return;
|
|
100
|
-
|
|
101
|
-
visit(tree, "comment", (node, index) => {
|
|
102
|
-
if (node.type === "comment" && node.value.trim().toLowerCase() === "toc" && typeof index !== "undefined") {
|
|
103
|
-
tocIndex = index;
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
const toc = generateToc(options, headings);
|
|
107
|
-
if (tocIndex) {
|
|
108
|
-
// eslint-disable-next-line no-magic-numbers
|
|
109
|
-
tree.children.splice(tocIndex, 1, ...toc);
|
|
110
|
-
}
|
|
111
|
-
else {
|
|
112
|
-
tree.children.unshift(...toc);
|
|
113
|
-
}
|
|
13
|
+
void transformer(tree, file);
|
|
114
14
|
};
|
|
115
|
-
return transformer;
|
|
116
15
|
};
|
|
117
16
|
export { rehypeCustomToc };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-custom-toc",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Astro Integration to generate a customizable table of contents",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"provenance": true
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"build": "tsc",
|
|
10
10
|
"format": "prettier ./src/ --write",
|
|
11
11
|
"format:check": "prettier ./src/ --check",
|
|
12
|
-
"lint": "npx eslint ./src/**/*.ts",
|
|
12
|
+
"lint": "knip && npx eslint ./src/**/*.ts",
|
|
13
13
|
"version": "npm run build && git add .",
|
|
14
14
|
"changeset-version": "changeset version && npm run version",
|
|
15
15
|
"changeset-publish": "npm run build && changeset publish"
|
|
@@ -40,23 +40,21 @@
|
|
|
40
40
|
"homepage": "https://github.com/Robot-Inventor/astro-custom-toc#readme",
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@astrojs/markdown-remark": "^6.0.0",
|
|
43
|
-
"@robot-inventor/ts-utils": "^0.8.0",
|
|
44
43
|
"@types/hast": "^3.0.4",
|
|
45
|
-
"
|
|
46
|
-
"hast-util-to-html": "^9.0.1",
|
|
47
|
-
"hastscript": "^9.0.0",
|
|
44
|
+
"rehype-custom-toc": "^1.0.0",
|
|
48
45
|
"remark-comment": "^1.0.0",
|
|
49
|
-
"unified": "^11.0.4"
|
|
50
|
-
"unist-util-visit": "^5.0.0"
|
|
46
|
+
"unified": "^11.0.4"
|
|
51
47
|
},
|
|
52
48
|
"devDependencies": {
|
|
53
49
|
"@changesets/changelog-github": "^0.5.0",
|
|
54
50
|
"@changesets/cli": "^2.27.1",
|
|
55
|
-
"@robot-inventor/eslint-config": "^
|
|
56
|
-
"@robot-inventor/tsconfig-base": "^
|
|
51
|
+
"@robot-inventor/eslint-config": "^11.0.0",
|
|
52
|
+
"@robot-inventor/tsconfig-base": "^7.0.0",
|
|
57
53
|
"astro": "^5.1.1",
|
|
58
54
|
"eslint": "^9.0.0",
|
|
55
|
+
"knip": "^5.80.2",
|
|
59
56
|
"prettier": "^3.2.5",
|
|
60
|
-
"typescript": "^5.9.2"
|
|
57
|
+
"typescript": "^5.9.2",
|
|
58
|
+
"vfile": "^6.0.3"
|
|
61
59
|
}
|
|
62
60
|
}
|