astro-custom-toc 1.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/LICENSE +21 -0
- package/README.md +97 -0
- package/index.ts +44 -0
- package/package.json +42 -0
- package/remark-custom-toc.ts +139 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 ろぼいん
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# astro-custom-toc
|
|
2
|
+
|
|
3
|
+
Astro Integration to generate a customizable table of contents.
|
|
4
|
+
|
|
5
|
+
> [!WARNING]
|
|
6
|
+
> This plugin uses [remark-comment](https://github.com/leebyron/remark-comment). It may break other plug-ins that use comments.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npx astro add astro-custom-toc
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
### Manual Install
|
|
15
|
+
|
|
16
|
+
Install the package
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install astro-custom-toc
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Add the plugin to your `astro.config.mjs`. **This plugin must be inserted before the [mdx()](https://github.com/withastro/astro/tree/main/packages/integrations/mdx/) plugin if you are using it.**
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
import { defineConfig } from "astro/config";
|
|
26
|
+
import { remarkCustomToc } from "./src/plugins/remark-toc";
|
|
27
|
+
|
|
28
|
+
// https://astro.build/config
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
// ... other config
|
|
31
|
+
integrations: [remarkCustomToc(), mdx()]
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
To include a table of contents in your markdown file, add ``showToc: true`` to the frontmatter of the markdown file. The table of contents will be inserted at the location of the `<!-- toc -->` comment or at the beginning of the file if no comment is found.
|
|
38
|
+
|
|
39
|
+
```markdown
|
|
40
|
+
---
|
|
41
|
+
showToc: true
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
|
|
45
|
+
|
|
46
|
+
<!-- toc -->
|
|
47
|
+
|
|
48
|
+
## Section 1
|
|
49
|
+
|
|
50
|
+
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
|
51
|
+
|
|
52
|
+
### Subsection 1.1
|
|
53
|
+
|
|
54
|
+
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Options
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
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`
|
package/index.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { AstroConfig, AstroIntegration } from "astro";
|
|
2
|
+
import { remarkCustomToc, RemarkCustomTocOptions } from "./remark-custom-toc";
|
|
3
|
+
import remarkComment from "remark-comment";
|
|
4
|
+
|
|
5
|
+
const astroCustomToc = (options?: RemarkCustomTocOptions) => {
|
|
6
|
+
const integration: AstroIntegration = {
|
|
7
|
+
name: "astro-custom-toc",
|
|
8
|
+
hooks: {
|
|
9
|
+
"astro:config:setup": ({ config, updateConfig }) => {
|
|
10
|
+
checkOrder(config);
|
|
11
|
+
updateConfig({
|
|
12
|
+
markdown: {
|
|
13
|
+
remarkPlugins: [
|
|
14
|
+
[
|
|
15
|
+
remarkComment,
|
|
16
|
+
{
|
|
17
|
+
ast: true
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
[remarkCustomToc, options]
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return integration;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const checkOrder = (config: AstroConfig) => {
|
|
32
|
+
const customTocIndex = config.integrations.findIndex((i) => i.name === "astro-custom-toc");
|
|
33
|
+
const mdxIndex = config.integrations.findIndex((i) => i.name === "@astrojs/mdx");
|
|
34
|
+
if (mdxIndex > -1 && customTocIndex > mdxIndex) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
`
|
|
37
|
+
MDX integration configured before astro-custom-toc.
|
|
38
|
+
\`astroCustomToc()\` must be loaded before \`mdx()\`.
|
|
39
|
+
`.trim()
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export default astroCustomToc;
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "astro-custom-toc",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Astro Integration to generate a customizable table of contents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.ts"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"index.ts",
|
|
11
|
+
"remark-custom-toc.ts"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/Robot-Inventor/astro-custom-toc.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"astro",
|
|
19
|
+
"remark",
|
|
20
|
+
"markdown",
|
|
21
|
+
"astro-integration"
|
|
22
|
+
],
|
|
23
|
+
"author": "Robot-Inventor",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/Robot-Inventor/astro-custom-toc/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/Robot-Inventor/astro-custom-toc#readme",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"github-slugger": "^2.0.0",
|
|
31
|
+
"jsdom": "^24.0.0",
|
|
32
|
+
"remark-comment": "^1.0.0",
|
|
33
|
+
"typescript": "^5.3.3",
|
|
34
|
+
"unist-util-visit": "^5.0.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"astro": "^4.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/jsdom": "^21.1.6"
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { AstroUserConfig } from "astro";
|
|
2
|
+
import { visit } from "unist-util-visit";
|
|
3
|
+
import Slugger from "github-slugger";
|
|
4
|
+
import { JSDOM } from "jsdom";
|
|
5
|
+
|
|
6
|
+
interface Heading {
|
|
7
|
+
slug: string;
|
|
8
|
+
text: string;
|
|
9
|
+
depth: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type RemarkCustomTocTemplate = (html: string) => string;
|
|
13
|
+
|
|
14
|
+
export interface RemarkCustomTocOptions {
|
|
15
|
+
template?: RemarkCustomTocTemplate;
|
|
16
|
+
maxDepth?: number;
|
|
17
|
+
ordered?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type RemarkPlugin = NonNullable<NonNullable<AstroUserConfig["markdown"]>["remarkPlugins"]>[number];
|
|
21
|
+
|
|
22
|
+
interface VFile {
|
|
23
|
+
data: {
|
|
24
|
+
astro?: {
|
|
25
|
+
frontmatter: Record<string, any>;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface CommentNode {
|
|
31
|
+
type: "comment";
|
|
32
|
+
value: "";
|
|
33
|
+
commentValue: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const defaultTemplate = (html: string) => {
|
|
37
|
+
return `
|
|
38
|
+
<aside class="toc">
|
|
39
|
+
<h2>Contents</h2>
|
|
40
|
+
<nav>
|
|
41
|
+
${html}
|
|
42
|
+
</nav>
|
|
43
|
+
</aside>`.trim();
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const remarkCustomToc: RemarkPlugin = ({
|
|
47
|
+
template = defaultTemplate,
|
|
48
|
+
maxDepth = 3,
|
|
49
|
+
ordered = false
|
|
50
|
+
}: RemarkCustomTocOptions = {}) => {
|
|
51
|
+
return (tree, { data }: VFile) => {
|
|
52
|
+
if (data.astro && data.astro.frontmatter.showToc !== true) return;
|
|
53
|
+
|
|
54
|
+
const slugs = new Slugger();
|
|
55
|
+
slugs.reset();
|
|
56
|
+
|
|
57
|
+
const headings: Heading[] = [];
|
|
58
|
+
visit(tree, "heading", (node) => {
|
|
59
|
+
const { depth } = node;
|
|
60
|
+
const textNode = node.children[0];
|
|
61
|
+
// @ts-ignore
|
|
62
|
+
const text = textNode ? textNode.value : "";
|
|
63
|
+
const slug = slugs.slug(text);
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
node.data = { id: slug };
|
|
66
|
+
headings.push({ slug, text, depth });
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
let tocIndex = 0;
|
|
70
|
+
visit(tree, "comment", (node: CommentNode, index) => {
|
|
71
|
+
if (node.commentValue.trim().toLowerCase() === "toc" && index !== undefined) {
|
|
72
|
+
tocIndex = index;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
visit(tree, "html", (node, index) => {
|
|
77
|
+
if (node.value.trim().toLowerCase() === "<!-- toc -->" && index !== undefined) {
|
|
78
|
+
tocIndex = index;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const toc = generateToc(template, maxDepth, ordered, headings);
|
|
83
|
+
if (tocIndex === 0) {
|
|
84
|
+
// @ts-ignore
|
|
85
|
+
tree.children.unshift(toc);
|
|
86
|
+
} else {
|
|
87
|
+
// @ts-ignore
|
|
88
|
+
tree.children.splice(tocIndex, 1, toc);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const generateToc = (template: RemarkCustomTocTemplate, maxDepth: number, ordered: boolean, headings: Heading[]) => {
|
|
94
|
+
const { document } = new JSDOM().window;
|
|
95
|
+
|
|
96
|
+
const toc: HTMLElement = document.createElement(ordered ? "ol" : "ul");
|
|
97
|
+
let currentDepth = 2;
|
|
98
|
+
let currentParent = toc;
|
|
99
|
+
for (const heading of headings) {
|
|
100
|
+
if (heading.depth > maxDepth) continue;
|
|
101
|
+
|
|
102
|
+
if (heading.depth === currentDepth) {
|
|
103
|
+
const li = document.createElement("li");
|
|
104
|
+
const a = document.createElement("a");
|
|
105
|
+
a.href = `#${heading.slug}`;
|
|
106
|
+
a.textContent = heading.text;
|
|
107
|
+
li.appendChild(a);
|
|
108
|
+
currentParent.appendChild(li);
|
|
109
|
+
currentDepth = heading.depth;
|
|
110
|
+
} else if (heading.depth > currentDepth) {
|
|
111
|
+
const ul = document.createElement(ordered ? "ol" : "ul");
|
|
112
|
+
const li = document.createElement("li");
|
|
113
|
+
const a = document.createElement("a");
|
|
114
|
+
a.href = `#${heading.slug}`;
|
|
115
|
+
a.textContent = heading.text;
|
|
116
|
+
li.appendChild(a);
|
|
117
|
+
ul.appendChild(li);
|
|
118
|
+
currentParent.appendChild(ul);
|
|
119
|
+
currentParent = ul;
|
|
120
|
+
currentDepth = heading.depth;
|
|
121
|
+
} else {
|
|
122
|
+
for (let i = 0; i < currentDepth - heading.depth; i++) {
|
|
123
|
+
currentParent = currentParent.parentElement!;
|
|
124
|
+
}
|
|
125
|
+
const li = document.createElement("li");
|
|
126
|
+
const a = document.createElement("a");
|
|
127
|
+
a.href = `#${heading.slug}`;
|
|
128
|
+
a.textContent = heading.text;
|
|
129
|
+
li.appendChild(a);
|
|
130
|
+
currentParent.appendChild(li);
|
|
131
|
+
currentDepth = heading.depth;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
type: "html",
|
|
137
|
+
value: template(toc.outerHTML)
|
|
138
|
+
};
|
|
139
|
+
};
|