pretty-toc 1.3.7 → 1.4.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 +23 -23
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +62 -49
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -43,11 +43,11 @@ import prettyToc from 'pretty-toc';
|
|
|
43
43
|
|
|
44
44
|
// Basic example
|
|
45
45
|
satteri({
|
|
46
|
-
hastPlugins: [prettyToc({title: "Table of Contents",
|
|
46
|
+
hastPlugins: [prettyToc({title: "Table of Contents", listMarkerType: "decimal"})]
|
|
47
47
|
})
|
|
48
48
|
|
|
49
49
|
// Full example
|
|
50
|
-
|
|
50
|
+
satteri({
|
|
51
51
|
hastPlugins: [
|
|
52
52
|
prettyToc({
|
|
53
53
|
title: "Table of Contents",
|
|
@@ -55,7 +55,7 @@ processor: satteri({
|
|
|
55
55
|
closedMarker: '🐢',
|
|
56
56
|
titleMarkerType: 'icon',
|
|
57
57
|
titleMarkerCssSize: '1rem',
|
|
58
|
-
|
|
58
|
+
listMarkerType: "image", // disc, circle, decimal, none... icon, image...
|
|
59
59
|
icon: ["🐶", "🦁", "🐷", "🐸", "🐥", "🐞"], // or just a string, eg. "🐥"
|
|
60
60
|
image: [
|
|
61
61
|
"/svg/unjs--automd.svg",
|
|
@@ -65,7 +65,7 @@ processor: satteri({
|
|
|
65
65
|
"/svg/unjs--ofetch.svg",
|
|
66
66
|
"/svg/unjs--uncrypto.svg",
|
|
67
67
|
], // or just a string, eg. "/svg/unjs--automd.svg"
|
|
68
|
-
|
|
68
|
+
listMarkerCssSize: "1rem",
|
|
69
69
|
lightThemeHighlightColor: "oklch(0.75 0.1229 12.71)",
|
|
70
70
|
darkThemeHighlightColor: "oklch(0.81 0.1004 305.04)",
|
|
71
71
|
animation: {
|
|
@@ -109,7 +109,7 @@ processor: satteri({
|
|
|
109
109
|
## Screenshots
|
|
110
110
|
|
|
111
111
|
<details>
|
|
112
|
-
<summary
|
|
112
|
+
<summary>Click to open</summary>
|
|
113
113
|
<ol>
|
|
114
114
|
<li>
|
|
115
115
|
<p>icon</p>
|
|
@@ -136,24 +136,24 @@ processor: satteri({
|
|
|
136
136
|
|
|
137
137
|
## Options
|
|
138
138
|
|
|
139
|
-
| Property | Type | Default Value | Preset Value
|
|
140
|
-
| :----------------------- | :--------------------------------------------------------------------------- | :---------------- |
|
|
141
|
-
| title | string | Table of Contents | Table of Contents
|
|
142
|
-
| openedMarker | string or undefined | undefined | ▼
|
|
143
|
-
| closedMarker | string or undefined | undefined | ▶︎
|
|
144
|
-
| titleMarkerType | string or undefined | undefined | icon
|
|
145
|
-
| titleMarkerCssSize | string or undefined | undefined | fit-content
|
|
146
|
-
|
|
|
147
|
-
| icon | string or string[] or undefined | undefined | decimal
|
|
148
|
-
| image | string or string[] or undefined | undefined | 404 image
|
|
149
|
-
|
|
|
150
|
-
| lightThemeHighlightColor | string or undefined | undefined | oklch(0.75 0.1229 12.71)
|
|
151
|
-
| darkThemeHighlightColor | string or undefined | undefined | oklch(0.81 0.1004 305.04)
|
|
152
|
-
| animation | boolean or { duration: string, timingFunction: string } or undefined | undefined | { duration: "0.8s", timingFunction: 'ease-in-out' }
|
|
153
|
-
| class | {title?: string, ul?: string, li?: string, a?: string} or or undefined | undefined | undefined
|
|
154
|
-
| globalStyle | string or or undefined | undefined | undefined
|
|
155
|
-
| style | Object{title?: string, ul?: string, li?: string, a?: string} or or undefined | undefined | undefined
|
|
156
|
-
| locale | string or undefined | undefined | en-US
|
|
139
|
+
| Property | Type | Default Value | Preset Value | Description |
|
|
140
|
+
| :----------------------- | :--------------------------------------------------------------------------- | :---------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------- |
|
|
141
|
+
| title | string | Table of Contents | Table of Contents | - |
|
|
142
|
+
| openedMarker | string or undefined | undefined | ▼ | _**Note: titleMarkerType must already be set.**_ |
|
|
143
|
+
| closedMarker | string or undefined | undefined | ▶︎ | _**Note: titleMarkerType must already be set.**_ |
|
|
144
|
+
| titleMarkerType | string or undefined | undefined | icon | Value can only be "icon" or "image". |
|
|
145
|
+
| titleMarkerCssSize | string or undefined | undefined | fit-content | - |
|
|
146
|
+
| listMarkerType | string or undefined | undefined | decimal | eg. disc, circle, decimal, none... or icon and image |
|
|
147
|
+
| icon | string or string[] or undefined | undefined | decimal | _**Note: listMarkerType must already be set.**_ |
|
|
148
|
+
| image | string or string[] or undefined | undefined | 404 image | _**Note: listMarkerType must already be set.**_ |
|
|
149
|
+
| listMarkerCssSize | string or undefined | undefined | 1rem | CSS property value |
|
|
150
|
+
| lightThemeHighlightColor | string or undefined | undefined | oklch(0.75 0.1229 12.71) | - |
|
|
151
|
+
| darkThemeHighlightColor | string or undefined | undefined | oklch(0.81 0.1004 305.04) | - |
|
|
152
|
+
| animation | boolean or { duration: string, timingFunction: string } or undefined | undefined | { duration: "0.8s", timingFunction: 'ease-in-out' } | - |
|
|
153
|
+
| class | {title?: string, ul?: string, li?: string, a?: string} or or undefined | undefined | undefined | eg. {li: "py-1"} |
|
|
154
|
+
| globalStyle | string or or undefined | undefined | undefined | eg. "ul {background-color: yellow;}" |
|
|
155
|
+
| style | Object{title?: string, ul?: string, li?: string, a?: string} or or undefined | undefined | undefined | eg. {a: "font-style: italic;"} |
|
|
156
|
+
| locale | string or undefined | undefined | en-US | eg. "zh-CN" // **Note: The case of the locale variable should match the locale in your page URL.** |
|
|
157
157
|
| languageMap | Record<string, string> or undefined | undefined | {"de-DE": "Inhaltsverzeichnis","el-GR": "Περιεχόμενα","en-US": "Contents","es-ES": "Índice","fr-FR": "Sommaire","it-IT": "Indice","ja-JP": "目次","ko-KR": "목차","ru-RU": "Оглавление","th-TH": "สารบัญ","tr-TR": "İçindekiler","zh-CN": "目录","zh-Hant": "目錄"} | eg. {"en-US": "Table of Contents", "ja-JP": "目次"} |
|
|
158
158
|
|
|
159
159
|
## Contribution
|
package/dist/index.d.mts
CHANGED
|
@@ -17,13 +17,13 @@ interface HastOption {
|
|
|
17
17
|
/** TOC标题标记大小 */
|
|
18
18
|
titleMarkerCssSize?: string;
|
|
19
19
|
/** 列表形式 */
|
|
20
|
-
|
|
20
|
+
listMarkerType?: string;
|
|
21
21
|
/** 图标 */
|
|
22
22
|
icon?: string | string[];
|
|
23
23
|
/** 图片路径 */
|
|
24
24
|
image?: string | string[];
|
|
25
25
|
/** 列表项标记大小 */
|
|
26
|
-
|
|
26
|
+
listMarkerCssSize?: string;
|
|
27
27
|
/** 浅色主题高亮颜色 */
|
|
28
28
|
lightThemeHighlightColor?: string;
|
|
29
29
|
/** 深色主题高亮颜色 */
|
package/dist/index.mjs
CHANGED
|
@@ -3,10 +3,10 @@ import slug from "slug";
|
|
|
3
3
|
// src/const.ts
|
|
4
4
|
const DEFAULT_TITLE = "Table of Contents";
|
|
5
5
|
const DEFAULT_TITLE_MARKER_TYPE = "icon";
|
|
6
|
-
const DEFAULT_TITLE_MARKER_CSS_SIZE = "fit-content";
|
|
7
6
|
const DEFALUT_LOCALE = "en-US";
|
|
8
|
-
const
|
|
9
|
-
const
|
|
7
|
+
const DEFAULT_LIST_MARKER_TYPE = "decimal";
|
|
8
|
+
const DEFAULT_TITLE_FONT_SIZE = "1.2rem";
|
|
9
|
+
const DEFAULT_LIST_FONT_SIZE = "1rem";
|
|
10
10
|
const DEFAULT_ANIMATION = {
|
|
11
11
|
duration: "0.8s",
|
|
12
12
|
timingFunction: "ease-in-out"
|
|
@@ -30,12 +30,12 @@ const DEFAULT_LANGUAGE_MAP = {
|
|
|
30
30
|
};
|
|
31
31
|
const DEFAULT_OPTIONS = {
|
|
32
32
|
title: DEFAULT_TITLE,
|
|
33
|
-
|
|
33
|
+
listMarkerType: DEFAULT_LIST_MARKER_TYPE,
|
|
34
34
|
titleMarkerType: DEFAULT_TITLE_MARKER_TYPE,
|
|
35
|
-
titleMarkerCssSize:
|
|
35
|
+
titleMarkerCssSize: DEFAULT_TITLE_FONT_SIZE,
|
|
36
36
|
lightThemeHighlightColor: DEFAULT_LIGHT_THEME_HIGHLIGHT_COLOR,
|
|
37
37
|
darkThemeHighlightColor: DEFAULT_DARK_THEME_HIGHLIGHT_COLOR,
|
|
38
|
-
|
|
38
|
+
listMarkerCssSize: DEFAULT_LIST_FONT_SIZE,
|
|
39
39
|
class: {
|
|
40
40
|
title: "",
|
|
41
41
|
ul: "",
|
|
@@ -64,12 +64,12 @@ function prettyToc(option) {
|
|
|
64
64
|
const opt = option ?? DEFAULT_OPTIONS;
|
|
65
65
|
const lightThemeHighlightColor = opt.lightThemeHighlightColor || "oklch(0.75 0.1229 12.71)";
|
|
66
66
|
const darkThemeHighlightColor = opt.darkThemeHighlightColor || "oklch(0.81 0.1004 305.04)";
|
|
67
|
-
const titleMarkerCssSize = opt.titleMarkerCssSize || "
|
|
67
|
+
const titleMarkerCssSize = opt.titleMarkerCssSize || "1.2rem";
|
|
68
68
|
const openedMarker = opt.openedMarker || "▼";
|
|
69
69
|
const closedMarker = opt.closedMarker || "▶︎";
|
|
70
|
-
const
|
|
70
|
+
const listMarkerCssSize = opt.listMarkerCssSize || "1rem";
|
|
71
71
|
let title = opt.title || "Table of Contents";
|
|
72
|
-
let
|
|
72
|
+
let listMarkerType = DEFAULT_LIST_MARKER_TYPE;
|
|
73
73
|
let animation = void 0;
|
|
74
74
|
let headingTree = void 0;
|
|
75
75
|
let firstHeading = {};
|
|
@@ -88,11 +88,11 @@ function prettyToc(option) {
|
|
|
88
88
|
try {
|
|
89
89
|
const depth = parseInt(node.tagName.slice(-1), 10);
|
|
90
90
|
const content = ctx.textContent(node);
|
|
91
|
-
const contentSlug = slug(content) +
|
|
91
|
+
const contentSlug = slug(content) + "-" + crypto.randomUUID();
|
|
92
92
|
if (opt.languageMap) title = opt.languageMap[opt.locale ?? "en-US"] || "Table of Contents";
|
|
93
93
|
else title = DEFAULT_LANGUAGE_MAP[opt.locale ?? "en-US"] || "Table of Contents";
|
|
94
|
-
if (opt.
|
|
95
|
-
else if (opt.
|
|
94
|
+
if (opt.listMarkerType === "icon" || opt.listMarkerType === "image" || opt.listMarkerType === "decimal") listMarkerType = "none";
|
|
95
|
+
else if (opt.listMarkerType !== void 0) listMarkerType = opt.listMarkerType;
|
|
96
96
|
if (typeof opt.animation === "object" && opt.animation !== void 0) animation = opt.animation ?? DEFAULT_ANIMATION;
|
|
97
97
|
else if (opt.animation === true) animation = DEFAULT_ANIMATION;
|
|
98
98
|
else animation = void 0;
|
|
@@ -103,7 +103,7 @@ function prettyToc(option) {
|
|
|
103
103
|
else if (Array.isArray(opt.icon)) icon = opt.icon[depth - 1] ?? "🍎";
|
|
104
104
|
if (opt.image && typeof opt.image === "string") image = opt.image;
|
|
105
105
|
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";
|
|
106
|
-
switch (opt.
|
|
106
|
+
switch (opt.listMarkerType) {
|
|
107
107
|
case "icon":
|
|
108
108
|
marker = `<span class='li-marker'>${icon}</span>`;
|
|
109
109
|
break;
|
|
@@ -202,15 +202,24 @@ function prettyToc(option) {
|
|
|
202
202
|
grid-template-rows: 1fr;
|
|
203
203
|
}
|
|
204
204
|
.toc-title {
|
|
205
|
-
font-size:
|
|
205
|
+
font-size: ${titleMarkerCssSize};
|
|
206
206
|
font-weight: 600;
|
|
207
|
-
margin: 0
|
|
207
|
+
margin: 0;
|
|
208
208
|
width: fit-content;
|
|
209
209
|
position: relative;
|
|
210
|
+
display: inline-flex;
|
|
211
|
+
flex-direction: row;
|
|
212
|
+
justify-content: flex-start;
|
|
213
|
+
align-items: flex-end;
|
|
214
|
+
}
|
|
215
|
+
.title-marker {
|
|
216
|
+
display: inline-flex;
|
|
217
|
+
height: 100%;
|
|
218
|
+
align-items: center;
|
|
210
219
|
}
|
|
211
220
|
ul {
|
|
212
221
|
padding-left: var(--list-indent, 1.5rem);
|
|
213
|
-
list-style-type: ${
|
|
222
|
+
list-style-type: ${listMarkerType} ;
|
|
214
223
|
list-style-position: inside;
|
|
215
224
|
}
|
|
216
225
|
ul:not(:has(> li)) {
|
|
@@ -223,7 +232,7 @@ function prettyToc(option) {
|
|
|
223
232
|
li {
|
|
224
233
|
padding: 0;
|
|
225
234
|
line-height: 1.5rem;
|
|
226
|
-
font-size:
|
|
235
|
+
font-size: ${listMarkerCssSize};
|
|
227
236
|
animation: fadeIn 0.01s ease-in; // 防止页面刷新瞬间显示"0 javascript"
|
|
228
237
|
}
|
|
229
238
|
.li-marker {
|
|
@@ -231,15 +240,15 @@ function prettyToc(option) {
|
|
|
231
240
|
margin-right: 0.5rem;
|
|
232
241
|
}
|
|
233
242
|
img.li-marker {
|
|
234
|
-
width: ${
|
|
235
|
-
height: ${
|
|
243
|
+
width: ${listMarkerCssSize};
|
|
244
|
+
height: ${listMarkerCssSize};
|
|
236
245
|
margin: 0 0.5rem 0 0;
|
|
237
246
|
}
|
|
238
247
|
.li-marker::before {
|
|
239
|
-
font-size:
|
|
248
|
+
font-size: ${listMarkerCssSize};
|
|
240
249
|
font-weight: 600;
|
|
241
|
-
width: ${
|
|
242
|
-
height: ${
|
|
250
|
+
width: ${listMarkerCssSize};
|
|
251
|
+
height: ${listMarkerCssSize};
|
|
243
252
|
}
|
|
244
253
|
/* 嵌套的 ul 内部,让缩进变量自动叠加 1rem */
|
|
245
254
|
ul ul {
|
|
@@ -249,7 +258,7 @@ function prettyToc(option) {
|
|
|
249
258
|
display: inline-flex;
|
|
250
259
|
flex-direction: row;
|
|
251
260
|
justify-content: flex-start;
|
|
252
|
-
align-items:
|
|
261
|
+
align-items: baseline;
|
|
253
262
|
}
|
|
254
263
|
.li-row > img {
|
|
255
264
|
margin: 0 0.5rem 0 0;
|
|
@@ -320,7 +329,7 @@ function prettyToc(option) {
|
|
|
320
329
|
<body>
|
|
321
330
|
<script is:inline data-astro-rerun>
|
|
322
331
|
const currentLocale = window.parent.location.pathname.split('/')[1] || '${opt.locale || "en-US"}';
|
|
323
|
-
const languageMap = ${JSON.stringify(opt.languageMap).replaceAll("\"", "'")
|
|
332
|
+
const languageMap = ${opt.languageMap ? JSON.stringify(opt.languageMap).replaceAll("\"", "'") : JSON.stringify(DEFAULT_LANGUAGE_MAP).replaceAll("\"", "'")};
|
|
324
333
|
|
|
325
334
|
function syncTocTitle(
|
|
326
335
|
locale,
|
|
@@ -337,10 +346,10 @@ function prettyToc(option) {
|
|
|
337
346
|
}
|
|
338
347
|
|
|
339
348
|
function toggleToc() {
|
|
340
|
-
const tocTitle = window.parent.document.querySelector('
|
|
349
|
+
const tocTitle = window.parent.document.querySelector('[data-satteri-toc-title]');
|
|
341
350
|
if (tocTitle) {
|
|
342
351
|
tocTitle.addEventListener('click', function () {
|
|
343
|
-
tocTitle.classList.toggle('open');
|
|
352
|
+
tocTitle.parentElement.classList.toggle('open');
|
|
344
353
|
})
|
|
345
354
|
}
|
|
346
355
|
}
|
|
@@ -364,7 +373,7 @@ function prettyToc(option) {
|
|
|
364
373
|
</iframe>
|
|
365
374
|
`;
|
|
366
375
|
const totalNodeStr = `
|
|
367
|
-
<div class="toc-wrapper"><style>${baseStyle + (opt.globalStyle ?? "") + (opt.
|
|
376
|
+
<div class="toc-wrapper"><style>${baseStyle + (opt.globalStyle ?? "") + (opt.listMarkerType === "decimal" ? `.toc-wrapper ul:has(> li) {
|
|
368
377
|
list-style-type: none;
|
|
369
378
|
counter-reset: toc-counter;
|
|
370
379
|
}
|
|
@@ -380,42 +389,46 @@ function prettyToc(option) {
|
|
|
380
389
|
.toc-wrapper:has(.toc-title.open) {
|
|
381
390
|
max-height: 100vh;
|
|
382
391
|
}
|
|
383
|
-
` : "") + (opt.titleMarkerType
|
|
384
|
-
.toc-wrapper .toc-
|
|
392
|
+
` : "") + (opt.titleMarkerType !== "image" ? `
|
|
393
|
+
.toc-wrapper .title-marker + span, .toc-wrapper > ul {
|
|
394
|
+
margin-left: 0.5rem;
|
|
395
|
+
}
|
|
396
|
+
.toc-wrapper .toc-title .title-marker::before {
|
|
385
397
|
content: '${closedMarker} ';
|
|
386
|
-
display:
|
|
387
|
-
font-size:
|
|
398
|
+
display: inline-block;
|
|
399
|
+
font-size: ${titleMarkerCssSize};
|
|
400
|
+
vertical-align: bottom;
|
|
401
|
+
|
|
388
402
|
}
|
|
389
|
-
.toc-wrapper .toc-title.open::before {
|
|
403
|
+
.toc-wrapper .toc-title.open .title-marker::before {
|
|
390
404
|
content: '${openedMarker} ';
|
|
391
|
-
display:
|
|
392
|
-
font-size:
|
|
405
|
+
display: inline-block;
|
|
406
|
+
font-size: ${titleMarkerCssSize};
|
|
407
|
+
vertical-align: bottom;
|
|
393
408
|
}
|
|
394
409
|
` : "") + (opt.titleMarkerType === "image" ? `
|
|
395
|
-
.toc-wrapper
|
|
396
|
-
|
|
410
|
+
.toc-wrapper .title-marker {
|
|
411
|
+
display: inline-block;
|
|
397
412
|
}
|
|
398
|
-
.toc-wrapper .
|
|
413
|
+
.toc-wrapper .title-marker::before {
|
|
399
414
|
content: "";
|
|
400
415
|
background: url('${closedMarker}') center / contain no-repeat;
|
|
401
416
|
width: ${titleMarkerCssSize};
|
|
402
|
-
height:
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
transform: translateY(-50%);
|
|
417
|
+
height: 100%;
|
|
418
|
+
display: inline-block;
|
|
419
|
+
margin-right: 0.3rem;
|
|
420
|
+
vertical-align: text-bottom;
|
|
407
421
|
}
|
|
408
|
-
.toc-wrapper .toc-title.open::before {
|
|
422
|
+
.toc-wrapper .toc-title.open .title-marker::before {
|
|
409
423
|
content: "";
|
|
410
424
|
background: url('${openedMarker}') center / contain no-repeat;
|
|
411
425
|
width: ${titleMarkerCssSize};
|
|
412
|
-
height:
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
transform: translateY(-50%);
|
|
426
|
+
height: 100%;
|
|
427
|
+
display: inline-block;
|
|
428
|
+
margin-right: 0.3rem;
|
|
429
|
+
vertical-align: text-bottom;
|
|
417
430
|
}
|
|
418
|
-
` : "")}</style><h2
|
|
431
|
+
` : "")}</style><h2 class="toc-title ${opt.class?.title ?? ""}" style="${opt.style?.title ?? ""}"><span class="title-marker"></span><span data-satteri-toc-title="${title}">${title}</span></h2>${collectedNodeStr}${iframeContent}</div>
|
|
419
432
|
`;
|
|
420
433
|
if (firstHeading.headingIndex !== void 0) ctx.replaceNode(root.children[firstHeading.headingIndex], {
|
|
421
434
|
type: "raw",
|