pretty-toc 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 樛木
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,67 @@
1
+ A Table of Contents generation plugin for the Satteri markdown parser library.
2
+
3
+ ## Install
4
+
5
+ ```
6
+ npm install pretty-toc
7
+ ```
8
+
9
+ ## Example
10
+
11
+ ```javascript
12
+ import prettyToc from 'pretty-toc'
13
+
14
+ satteri({
15
+ hastPlugins: [prettyToc({title: "Table of Contents", listStyle: "decimal"})]
16
+ })
17
+ ```
18
+
19
+ ## options
20
+
21
+ ```javascript
22
+ // config file
23
+ processor: satteri({
24
+ features: {
25
+ directive: true,
26
+ math: true,
27
+ },
28
+ // mdastPlugins: [mdastPlugin],
29
+ hastPlugins: [
30
+ prettyToc({
31
+ title: "Table of Contents",
32
+ listStyle: "decimal", // disc, circle, decimal, none...
33
+ lightThemeHighlightColor: "oklch(0.75 0.1229 12.71)",
34
+ darkThemeHighlightColor: "oklch(0.81 0.1004 305.04)",
35
+ class: {
36
+ summary: "",
37
+ ul: "",
38
+ li: "",
39
+ a: "",
40
+ },
41
+ globalStyle: "",
42
+ style: {
43
+ summary: "",
44
+ ul: "",
45
+ li: "",
46
+ a: "",
47
+ },
48
+ locale: "zh-CN",
49
+ languageMap: {
50
+ "de-DE": "Inhaltsverzeichnis",
51
+ "el-GR": "Περιεχόμενα",
52
+ "en-US": "Contents",
53
+ "es-ES": "Índice",
54
+ "fr-FR": "Sommaire",
55
+ "it-IT": "Indice",
56
+ "ja-JP": "目次",
57
+ "ko-KR": "목차",
58
+ "ru-RU": "Оглавление",
59
+ "th-TH": "สารบัญ",
60
+ "tr-TR": "İçindekiler",
61
+ "zh-CN": "目录",
62
+ "zh-Hant": "目錄",
63
+ },
64
+ }),
65
+ ],
66
+ }),
67
+ ```
@@ -0,0 +1,39 @@
1
+ import { HastPluginDefinition } from "satteri";
2
+ //#region app.d.ts
3
+ interface tagHasProperty {
4
+ summary?: string;
5
+ ul?: string;
6
+ li?: string;
7
+ a?: string;
8
+ }
9
+ interface HastOption {
10
+ /** TOC标题 */
11
+ title?: string;
12
+ /** 列表形式 */
13
+ listStyle?: string;
14
+ /** 图标 */
15
+ icon?: string | string[];
16
+ /** 图片路径 */
17
+ image?: string | string[];
18
+ /** 标记大小 */
19
+ markerCssSize?: string;
20
+ /** 浅色主题高亮颜色 */
21
+ lightThemeHighlightColor?: string;
22
+ /** 深色主题高亮颜色 */
23
+ darkThemeHighlightColor?: string;
24
+ /** 类名 */
25
+ class?: tagHasProperty;
26
+ /** 全局样式 */
27
+ globalStyle?: string;
28
+ /** 样式 */
29
+ style?: tagHasProperty;
30
+ /** 语言环境 */
31
+ locale?: string;
32
+ /** 标题文本映射 */
33
+ languageMap?: Record<string, string>;
34
+ }
35
+ //#endregion
36
+ //#region src/index.d.ts
37
+ declare function prettyToc(option?: HastOption): HastPluginDefinition;
38
+ //#endregion
39
+ export { prettyToc as default };
package/dist/index.mjs ADDED
@@ -0,0 +1,236 @@
1
+ import { defineHastPlugin } from "satteri";
2
+ import slug from "slug";
3
+ //#region src/assets/svg/image-not-found.svg
4
+ var image_not_found_default = "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%221em%22%20height%3D%221em%22%20viewBox%3D%220%200%2024%2024%22%3E%20%3Cpath%20d%3D%22M0%200h24v24H0z%22%20fill%3D%22none%22%20%2F%3E%20%3Cpath%20fill%3D%22currentColor%22%20d%3D%22M19%205c1.1%200%202%20.9%202%202v10c0%201.1-.9%202-2%202H5c-1.1%200-2-.9-2-2V7c0-1.1.9-2%202-2zM5%206.5c-.3%200-.5.2-.5.5v10c0%20.3.2.5.5.5h14c.3%200%20.5-.2.5-.5V7c0-.3-.2-.5-.5-.5zm7.01%202.75q.711%200%201.24.364q.533.364.824%201.012q.296.645.296%201.488q0%20.887-.296%201.556q-.292.664-.824%201.036q-.528.368-1.24.368q-.708%200-1.24-.368q-.527-.372-.824-1.036q-.296-.668-.296-1.556q0-.848.296-1.492q.296-.648.824-1.008a2.14%202.14%200%200%201%201.24-.364m-3.484%203.6h.72v.832h-.72v1.28h-.984v-1.28H4.75l3.08-4.32h.696zm9.522%200h.72v.832h-.72v1.28h-.983v-1.28h-2.793l3.08-4.32h.696zm-6.038-2.696q-.568%200-.952.48q-.384.475-.384%201.48q0%20.716.176%201.168q.176.45.476.66q.304.212.684.212t.68-.208q.304-.207.48-.656q.176-.451.176-1.176q0-.996-.384-1.476q-.38-.484-.952-.484M6.33%2012.85h1.212v-1.722zm9.523%200h1.211v-1.722z%22%20%2F%3E%3C%2Fsvg%3E";
5
+ //#endregion
6
+ //#region src/index.ts
7
+ function prettyToc(option) {
8
+ return defineHastPlugin({
9
+ name: "prettyToc",
10
+ element: [{
11
+ filter: [
12
+ "h1",
13
+ "h2",
14
+ "h3",
15
+ "h4",
16
+ "h5",
17
+ "h6"
18
+ ],
19
+ visit(node, ctx) {
20
+ const opt = option ?? {
21
+ title: "Table of Contents",
22
+ listStyle: "decimal",
23
+ lightThemeHighlightColor: "oklch(0.75 0.1229 12.71)",
24
+ darkThemeHighlightColor: "oklch(0.81 0.1004 305.04)",
25
+ class: {
26
+ summary: "",
27
+ ul: "",
28
+ li: "",
29
+ a: ""
30
+ },
31
+ globalStyle: "",
32
+ style: {
33
+ summary: "",
34
+ ul: "",
35
+ li: "",
36
+ a: ""
37
+ },
38
+ locale: "zh-CN",
39
+ languageMap: {
40
+ "de-DE": "Inhaltsverzeichnis",
41
+ "el-GR": "Περιεχόμενα",
42
+ "en-US": "Contents",
43
+ "es-ES": "Índice",
44
+ "fr-FR": "Sommaire",
45
+ "it-IT": "Indice",
46
+ "ja-JP": "目次",
47
+ "ko-KR": "목차",
48
+ "ru-RU": "Оглавление",
49
+ "th-TH": "สารบัญ",
50
+ "tr-TR": "İçindekiler",
51
+ "zh-CN": "目录",
52
+ "zh-Hant": "目錄"
53
+ }
54
+ };
55
+ try {
56
+ const defaultTitle = "Table of Contents";
57
+ const depth = parseInt(node.tagName.slice(-1), 10);
58
+ const content = ctx.textContent(node);
59
+ const contentSlug = slug(content) + (/* @__PURE__ */ new Date()).getTime();
60
+ const lightThemeHighlightColor = opt.lightThemeHighlightColor ?? "oklch(0.75 0.1229 12.71)";
61
+ const darkThemeHighlightColor = opt.darkThemeHighlightColor ?? "oklch(0.81 0.1004 305.04)";
62
+ let marker = "";
63
+ let icon = "🍎";
64
+ let image = image_not_found_default;
65
+ if (opt.icon && typeof opt.icon === "string") icon = opt.icon;
66
+ else if (Array.isArray(opt.icon)) icon = opt.icon[depth - 1] ?? "🍎";
67
+ if (opt.image && typeof opt.image === "string") image = opt.image;
68
+ else if (Array.isArray(opt.image)) image = opt.image[depth - 1] ?? "data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%221em%22%20height%3D%221em%22%20viewBox%3D%220%200%2024%2024%22%3E%20%3Cpath%20d%3D%22M0%200h24v24H0z%22%20fill%3D%22none%22%20%2F%3E%20%3Cpath%20fill%3D%22currentColor%22%20d%3D%22M19%205c1.1%200%202%20.9%202%202v10c0%201.1-.9%202-2%202H5c-1.1%200-2-.9-2-2V7c0-1.1.9-2%202-2zM5%206.5c-.3%200-.5.2-.5.5v10c0%20.3.2.5.5.5h14c.3%200%20.5-.2.5-.5V7c0-.3-.2-.5-.5-.5zm7.01%202.75q.711%200%201.24.364q.533.364.824%201.012q.296.645.296%201.488q0%20.887-.296%201.556q-.292.664-.824%201.036q-.528.368-1.24.368q-.708%200-1.24-.368q-.527-.372-.824-1.036q-.296-.668-.296-1.556q0-.848.296-1.492q.296-.648.824-1.008a2.14%202.14%200%200%201%201.24-.364m-3.484%203.6h.72v.832h-.72v1.28h-.984v-1.28H4.75l3.08-4.32h.696zm9.522%200h.72v.832h-.72v1.28h-.983v-1.28h-2.793l3.08-4.32h.696zm-6.038-2.696q-.568%200-.952.48q-.384.475-.384%201.48q0%20.716.176%201.168q.176.45.476.66q.304.212.684.212t.68-.208q.304-.207.48-.656q.176-.451.176-1.176q0-.996-.384-1.476q-.38-.484-.952-.484M6.33%2012.85h1.212v-1.722zm9.523%200h1.211v-1.722z%22%20%2F%3E%3C%2Fsvg%3E";
69
+ switch (opt.listStyle) {
70
+ case "icon":
71
+ marker = `<span class='li-marker'>${icon}</span>`;
72
+ break;
73
+ case "image":
74
+ marker = `<img class='li-marker' src="${image}" alt='icon' />`;
75
+ break;
76
+ case "decimal":
77
+ marker = "<span class='li-marker'></span>";
78
+ break;
79
+ default: marker = "";
80
+ }
81
+ const nodeStr = `<li id="li-${contentSlug}" class="${opt.class?.li ?? ""}" style="${opt.style?.li ?? ""}" data-depth=${depth}><div class="li-row">${marker}<a href="#${contentSlug}" class="${opt.class?.a ?? ""}" style="${opt.style?.a ?? ""}">${content}</a></div><span data-depth='${depth}' style="display: none;"></span></li>`;
82
+ ctx.setProperty(node, "id", contentSlug);
83
+ if (ctx.data.nodeStr === void 0) {
84
+ const baseStyle = `
85
+ @keyframes fadeIn {
86
+ from { opacity: 0; }
87
+ to { opacity: 1; }
88
+ }
89
+ summary {
90
+ font-size: 1.25rem;
91
+ margin-bottom: 0.5rem;
92
+ }
93
+ summary:hover {
94
+ color: ${lightThemeHighlightColor};
95
+ }
96
+ html.dark summary:hover {
97
+ color: ${darkThemeHighlightColor};
98
+ }
99
+ ul {
100
+ padding-left: 0;
101
+ list-style-type: ${opt.listStyle === "icon" || opt.listStyle === "image" || opt.listStyle === "decimal" ? "none" : opt.listStyle} ;
102
+ list-style-position: inside;
103
+ }
104
+ li {
105
+ /* 默认缩进为 1rem */
106
+ padding-left: var(--list-indent, 1rem);
107
+ line-height: 1.5rem;
108
+ font-size: 1rem;
109
+ animation: fadeIn 0.1s ease-in; // 防止页面刷新瞬间显示"0 javascript"
110
+ }
111
+ .li-marker {
112
+ display: inline-block;
113
+ margin-right: 0.5rem;
114
+ }
115
+ img.li-marker {
116
+ width: ${opt.markerCssSize ?? "1rem"};
117
+ height: ${opt.markerCssSize ?? "1rem"};
118
+ margin: 0 0.5rem 0 0;
119
+ }
120
+ .li-marker::before {
121
+ font-size: 1rem;
122
+ font-weight: 600;
123
+ width: ${opt.markerCssSize ?? "1rem"};
124
+ height: ${opt.markerCssSize ?? "1rem"};
125
+ }
126
+ /* 嵌套的 ul 内部,让缩进变量自动叠加 1rem */
127
+ ul ul li {
128
+ --list-indent: calc(var(--list-indent, 1rem) + 1rem);
129
+ }
130
+ .li-row {
131
+ display: inline-flex;
132
+ flex-direction: row;
133
+ justify-content: flex-start;
134
+ align-items: center;
135
+ }
136
+ .li-row > img {
137
+ margin: 0 0.5rem 0 0;
138
+ }
139
+ .li-row:hover {
140
+ color: ${lightThemeHighlightColor};
141
+ }
142
+ .li-row:hover > .li-marker::before {
143
+ color: ${lightThemeHighlightColor};
144
+ }
145
+ .li-row:hover > a {
146
+ color: ${lightThemeHighlightColor};
147
+ }
148
+ html.dark .li-row:hover {
149
+ color: ${darkThemeHighlightColor};
150
+ }
151
+ html.dark .li-row:hover > .li-marker::before {
152
+ color: ${darkThemeHighlightColor};
153
+ }
154
+ html.dark .li-row:hover > a {
155
+ color: ${darkThemeHighlightColor};
156
+ }
157
+ `;
158
+ const scriptContent = `<script is:inline data-astro-rerun>
159
+ // 加上{}块级作用域,防止报错
160
+ {
161
+ const currentLocale = window.location.pathname.split('/')[1] || 'en-US';
162
+ const languageMap = ${JSON.stringify(opt.languageMap)};
163
+
164
+ function syncTocTitle(
165
+ locale,
166
+ languageMap
167
+ ) {
168
+ const tocSummary = document.querySelector("[data-satteri-toc-title]");
169
+
170
+ if (tocSummary) {
171
+ const titleKey = tocSummary.getAttribute("data-satteri-toc-title");
172
+ const translation =
173
+ languageMap[locale] || titleKey || defaultTitle;
174
+ tocSummary.textContent = translation;
175
+ }
176
+ }
177
+
178
+ syncTocTitle(currentLocale, languageMap);
179
+
180
+ window.addEventListener("load", () => {
181
+ syncTocTitle(currentLocale, languageMap);
182
+ });
183
+ }
184
+ <\/script>`;
185
+ ctx.data.firstHeading = node;
186
+ ctx.data.firstHeadingDepth = depth;
187
+ ctx.data.firstHeadingId = contentSlug;
188
+ ctx.data.rootNode = ctx.parent(node);
189
+ ctx.data.nodeStr = `<style>${baseStyle + (opt.globalStyle ?? "") + (opt.listStyle === "decimal" ? `ul{
190
+ list-style-type: none;
191
+ counter-reset: toc-counter;
192
+ }
193
+ li {
194
+ counter-increment: toc-counter;
195
+ }
196
+ .li-marker::before {
197
+ content: counters(toc-counter, ".");
198
+ }` : "")}</style><details><summary data-satteri-toc-title="${opt.languageMap?.[opt.locale ?? "en-US"] ?? defaultTitle}" class="${opt.class?.summary ?? ""}" style="${opt.style?.summary ?? ""}">${opt.languageMap?.[opt.locale ?? "en-US"] ?? defaultTitle}</summary><ul class="${opt.class?.ul ?? ""}" style="${opt.style?.ul ?? ""}">${nodeStr}</ul>${scriptContent}</details>`;
199
+ } else {
200
+ const indexA = ctx.data.nodeStr?.lastIndexOf(`<span data-depth='${depth}' style="display: none;"></span>`);
201
+ if (indexA !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexA + 56) + nodeStr + ctx.data.nodeStr?.slice(indexA + 56);
202
+ else if (depth > 1) {
203
+ let indexB = -1;
204
+ for (let i = depth; i > 0; i--) {
205
+ indexB = ctx.data.nodeStr?.lastIndexOf(`<span data-depth='${i}' style="display: none;"></span>`);
206
+ if (indexB !== -1) break;
207
+ }
208
+ if (indexB !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexB) + `<ul class="${opt.class?.ul ?? ""}" style="${opt.style?.ul ?? ""}">` + nodeStr + `</ul>` + ctx.data.nodeStr?.slice(indexB);
209
+ else {
210
+ const indexC = ctx.data.nodeStr?.lastIndexOf(`</ul>`);
211
+ if (indexC !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexC) + nodeStr + ctx.data.nodeStr?.slice(indexC);
212
+ }
213
+ } else {
214
+ const indexB = ctx.data.nodeStr?.lastIndexOf(`</ul>`);
215
+ if (indexB !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexB) + nodeStr + ctx.data.nodeStr?.slice(indexB);
216
+ }
217
+ }
218
+ let parent = ctx.parent(node);
219
+ while (parent.type !== "root" && parent !== void 0) {
220
+ const p = ctx.parent(parent);
221
+ if (p) parent = p;
222
+ else break;
223
+ }
224
+ ctx.replaceNode(parent.children[0], {
225
+ type: "raw",
226
+ value: ctx.data.nodeStr + `<h${ctx.data.firstHeadingDepth} id="${ctx.data.firstHeadingId}">${ctx.data.firstHeading && ctx.textContent(ctx.data.firstHeading)}</h${ctx.data.firstHeadingDepth}>`
227
+ });
228
+ } catch (err) {
229
+ throw err;
230
+ }
231
+ }
232
+ }]
233
+ });
234
+ }
235
+ //#endregion
236
+ export { prettyToc as default };
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "pretty-toc",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "A Table of Contents generation plugin for the Satteri markdown parser library.",
6
+ "keywords": [
7
+ "Table of Contents",
8
+ "TOC",
9
+ "satteri",
10
+ "markdown",
11
+ "plugin"
12
+ ],
13
+ "author": "Virgil",
14
+ "license": "MIT",
15
+ "homepage": "https://github.com/Virgil-N/pretty-toc#readme",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/Virgil-N/pretty-toc.git"
19
+ },
20
+ "bugs": {
21
+ "url": "https://github.com/Virgil-N/pretty-toc/issues"
22
+ },
23
+ "entry": [
24
+ "./src/index.ts"
25
+ ],
26
+ "exports": {
27
+ ".": "./dist/index.mjs",
28
+ "./package.json": "./package.json"
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "scripts": {
34
+ "build": "tsdown",
35
+ "dev": "tsdown --watch",
36
+ "test": "vitest",
37
+ "typecheck": "tsc --noEmit",
38
+ "release": "bumpp",
39
+ "prepublishOnly": "pnpm run build"
40
+ },
41
+ "peerDependencies": {
42
+ "@types/slug": "^5.0.9",
43
+ "satteri": "^0.9.5",
44
+ "slug": "^12.0.1"
45
+ },
46
+ "devDependencies": {
47
+ "@rollup/plugin-url": "^8.0.2",
48
+ "@types/hast": "^3.0.4",
49
+ "@types/node": "^26.1.1",
50
+ "bumpp": "^11.1.0",
51
+ "tsdown": "^0.22.5",
52
+ "typescript": "^7.0.2",
53
+ "vitest": "^4.1.10"
54
+ }
55
+ }