pretty-toc 0.1.4 → 0.1.6
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 +20 -10
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +86 -69
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -11,20 +11,13 @@ npm install pretty-toc --save-dev
|
|
|
11
11
|
```javascript
|
|
12
12
|
import prettyToc from 'pretty-toc';
|
|
13
13
|
|
|
14
|
+
// example
|
|
14
15
|
satteri({
|
|
15
16
|
hastPlugins: [prettyToc({title: "Table of Contents", listStyle: "decimal"})]
|
|
16
17
|
})
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## options
|
|
20
18
|
|
|
21
|
-
|
|
22
|
-
// config file
|
|
19
|
+
// Full example
|
|
23
20
|
processor: satteri({
|
|
24
|
-
features: {
|
|
25
|
-
directive: true,
|
|
26
|
-
math: true,
|
|
27
|
-
},
|
|
28
21
|
hastPlugins: [
|
|
29
22
|
prettyToc({
|
|
30
23
|
title: "Table of Contents",
|
|
@@ -54,7 +47,7 @@ processor: satteri({
|
|
|
54
47
|
li: "",
|
|
55
48
|
a: "",
|
|
56
49
|
},
|
|
57
|
-
locale: "zh-CN",
|
|
50
|
+
locale: "zh-CN", // Note: The case of the locale variable should match the locale in your page URL.
|
|
58
51
|
languageMap: {
|
|
59
52
|
"de-DE": "Inhaltsverzeichnis",
|
|
60
53
|
"el-GR": "Περιεχόμενα",
|
|
@@ -74,3 +67,20 @@ processor: satteri({
|
|
|
74
67
|
],
|
|
75
68
|
}),
|
|
76
69
|
```
|
|
70
|
+
|
|
71
|
+
## options
|
|
72
|
+
|
|
73
|
+
|Property|Type|Info|
|
|
74
|
+
|:---|:---|:---|
|
|
75
|
+
|title|string|-|
|
|
76
|
+
|listStyle|string|eg. disc, circle, decimal, none... or icon and image|
|
|
77
|
+
|icon|string or string[]|-|
|
|
78
|
+
|image|string or string[]|-|
|
|
79
|
+
|markerCssSize|string|CSS property value|
|
|
80
|
+
|lightThemeHighlightColor|string|eg. "oklch(0.75 0.1229 12.71)"|
|
|
81
|
+
|darkThemeHighlightColor|string|eg. "oklch(0.81 0.1004 305.04)"|
|
|
82
|
+
|class|{summary?: string, ul?: string, li?: string, a?: string}|eg. {summary: "pb-2", li: "py-1"}|
|
|
83
|
+
|globalStyle|string|-|
|
|
84
|
+
|style|Object{summary?: string, ul?: string, li?: string, a?: string}||
|
|
85
|
+
|locale|string|eg. "zh-CN" // **Note: The case of the locale variable should match the locale in your page URL.**|
|
|
86
|
+
|languageMap|Record<string, string>|eg. {"en-US": "Table of Contents", "ja-JP": "目次"}|
|
package/dist/index.d.mts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
import { defineHastPlugin } from "satteri";
|
|
2
2
|
import slug from "slug";
|
|
3
|
+
//#region src/const.ts
|
|
4
|
+
const DEFAULT_TITLE = "Table of Contents";
|
|
5
|
+
const DEFAULT_OPTIONS = {
|
|
6
|
+
title: DEFAULT_TITLE,
|
|
7
|
+
listStyle: "decimal",
|
|
8
|
+
lightThemeHighlightColor: "oklch(0.75 0.1229 12.71)",
|
|
9
|
+
darkThemeHighlightColor: "oklch(0.81 0.1004 305.04)",
|
|
10
|
+
class: {
|
|
11
|
+
summary: "",
|
|
12
|
+
ul: "",
|
|
13
|
+
li: "",
|
|
14
|
+
a: ""
|
|
15
|
+
},
|
|
16
|
+
globalStyle: "",
|
|
17
|
+
style: {
|
|
18
|
+
summary: "",
|
|
19
|
+
ul: "",
|
|
20
|
+
li: "",
|
|
21
|
+
a: ""
|
|
22
|
+
},
|
|
23
|
+
locale: "en-US",
|
|
24
|
+
languageMap: {
|
|
25
|
+
"de-DE": "Inhaltsverzeichnis",
|
|
26
|
+
"el-GR": "Περιεχόμενα",
|
|
27
|
+
"en-US": "Contents",
|
|
28
|
+
"es-ES": "Índice",
|
|
29
|
+
"fr-FR": "Sommaire",
|
|
30
|
+
"it-IT": "Indice",
|
|
31
|
+
"ja-JP": "目次",
|
|
32
|
+
"ko-KR": "목차",
|
|
33
|
+
"ru-RU": "Оглавление",
|
|
34
|
+
"th-TH": "สารบัญ",
|
|
35
|
+
"tr-TR": "İçindekiler",
|
|
36
|
+
"zh-CN": "目录",
|
|
37
|
+
"zh-Hant": "目錄"
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
//#endregion
|
|
3
41
|
//#region src/assets/svg/image-not-found.svg
|
|
4
42
|
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
43
|
//#endregion
|
|
@@ -17,43 +55,9 @@ function prettyToc(option) {
|
|
|
17
55
|
"h6"
|
|
18
56
|
],
|
|
19
57
|
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
|
-
};
|
|
58
|
+
const opt = option ?? DEFAULT_OPTIONS;
|
|
55
59
|
try {
|
|
56
|
-
const defaultTitle =
|
|
60
|
+
const defaultTitle = DEFAULT_TITLE;
|
|
57
61
|
const depth = parseInt(node.tagName.slice(-1), 10);
|
|
58
62
|
const content = ctx.textContent(node);
|
|
59
63
|
const contentSlug = slug(content) + (/* @__PURE__ */ new Date()).getTime();
|
|
@@ -80,6 +84,12 @@ function prettyToc(option) {
|
|
|
80
84
|
}
|
|
81
85
|
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
86
|
ctx.setProperty(node, "id", contentSlug);
|
|
87
|
+
let parent = ctx.parent(node);
|
|
88
|
+
while (parent.type !== "root" && parent !== void 0) {
|
|
89
|
+
const p = ctx.parent(parent);
|
|
90
|
+
if (p) parent = p;
|
|
91
|
+
else break;
|
|
92
|
+
}
|
|
83
93
|
if (ctx.data.nodeStr === void 0) {
|
|
84
94
|
const baseStyle = `
|
|
85
95
|
@keyframes fadeIn {
|
|
@@ -92,6 +102,8 @@ function prettyToc(option) {
|
|
|
92
102
|
}
|
|
93
103
|
summary:hover {
|
|
94
104
|
color: ${lightThemeHighlightColor};
|
|
105
|
+
cursor: pointer;
|
|
106
|
+
width: fit-content;
|
|
95
107
|
}
|
|
96
108
|
html.dark summary:hover {
|
|
97
109
|
color: ${darkThemeHighlightColor};
|
|
@@ -136,6 +148,9 @@ function prettyToc(option) {
|
|
|
136
148
|
.li-row > img {
|
|
137
149
|
margin: 0 0.5rem 0 0;
|
|
138
150
|
}
|
|
151
|
+
.li-row > a {
|
|
152
|
+
text-decoration: none;
|
|
153
|
+
}
|
|
139
154
|
.li-row:hover {
|
|
140
155
|
color: ${lightThemeHighlightColor};
|
|
141
156
|
}
|
|
@@ -155,38 +170,46 @@ function prettyToc(option) {
|
|
|
155
170
|
color: ${darkThemeHighlightColor};
|
|
156
171
|
}
|
|
157
172
|
`;
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
173
|
+
const iframeContent = `<iframe
|
|
174
|
+
style="display:none;"
|
|
175
|
+
srcdoc="<!DOCTYPE html>
|
|
176
|
+
<html>
|
|
177
|
+
<head><meta charset="utf-8"></head>
|
|
178
|
+
<body>
|
|
179
|
+
<strong>Hello from iframe!</strong>
|
|
180
|
+
<script is:inline data-astro-rerun>
|
|
181
|
+
console.log('🚀 pretty-toc running...');
|
|
182
|
+
const currentLocale = window.parent.location.pathname.split('/')[1] || '${opt.locale || "en-US"}';
|
|
183
|
+
const languageMap = ${JSON.stringify(opt.languageMap).replaceAll("\"", "'")};
|
|
163
184
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
185
|
+
function syncTocTitle(
|
|
186
|
+
locale,
|
|
187
|
+
languageMap
|
|
188
|
+
) {
|
|
189
|
+
const tocSummary = window.parent.document.querySelector('[data-satteri-toc-title]');
|
|
169
190
|
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
191
|
+
if (tocSummary) {
|
|
192
|
+
const titleKey = tocSummary.getAttribute('data-satteri-toc-title');
|
|
193
|
+
const translation =
|
|
194
|
+
languageMap[locale] || titleKey || defaultTitle;
|
|
195
|
+
tocSummary.textContent = translation;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
177
198
|
|
|
178
|
-
|
|
199
|
+
syncTocTitle(currentLocale, languageMap);
|
|
179
200
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
201
|
+
window.parent.addEventListener('load', function() {
|
|
202
|
+
syncTocTitle(currentLocale, languageMap);
|
|
203
|
+
});
|
|
204
|
+
<\/script>
|
|
205
|
+
</body>
|
|
206
|
+
</html>">
|
|
207
|
+
</iframe>`;
|
|
185
208
|
ctx.data.firstHeading = node;
|
|
186
209
|
ctx.data.firstHeadingDepth = depth;
|
|
187
210
|
ctx.data.firstHeadingId = contentSlug;
|
|
188
|
-
ctx.data.
|
|
189
|
-
ctx.data.nodeStr = `<style>${baseStyle + (opt.globalStyle ?? "") + (opt.listStyle === "decimal" ? `ul{
|
|
211
|
+
ctx.data.firstHeadingIndex = ctx.indexOf(node) ?? 0;
|
|
212
|
+
ctx.data.nodeStr = `<details><style>${baseStyle + (opt.globalStyle ?? "") + (opt.listStyle === "decimal" ? `ul{
|
|
190
213
|
list-style-type: none;
|
|
191
214
|
counter-reset: toc-counter;
|
|
192
215
|
}
|
|
@@ -195,7 +218,7 @@ function prettyToc(option) {
|
|
|
195
218
|
}
|
|
196
219
|
.li-marker::before {
|
|
197
220
|
content: counters(toc-counter, ".");
|
|
198
|
-
}` : "")}</style><
|
|
221
|
+
}` : "")}</style><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>${iframeContent}</details>`;
|
|
199
222
|
} else {
|
|
200
223
|
const indexA = ctx.data.nodeStr?.lastIndexOf(`<span data-depth='${depth}' style="display: none;"></span>`);
|
|
201
224
|
if (indexA !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexA + 56) + nodeStr + ctx.data.nodeStr?.slice(indexA + 56);
|
|
@@ -215,13 +238,7 @@ function prettyToc(option) {
|
|
|
215
238
|
if (indexB !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexB) + nodeStr + ctx.data.nodeStr?.slice(indexB);
|
|
216
239
|
}
|
|
217
240
|
}
|
|
218
|
-
|
|
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], {
|
|
241
|
+
if (parent.type === "root") ctx.replaceNode(parent.children[ctx.data.firstHeadingIndex], {
|
|
225
242
|
type: "raw",
|
|
226
243
|
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
244
|
});
|