pretty-toc 0.1.7 → 0.1.9

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 CHANGED
@@ -21,6 +21,10 @@ processor: satteri({
21
21
  hastPlugins: [
22
22
  prettyToc({
23
23
  title: "Table of Contents",
24
+ openedMarker: '🦋',
25
+ closedMarker: '🐢',
26
+ titleMarkerType: 'icon',
27
+ titleMarkerCssSize: '1.5rem',
24
28
  listStyle: "image", // disc, circle, decimal, none... icon, image...
25
29
  icon: ["🐶", "🦁", "🐷", "🐸", "🐥", "🐞"], // or just a string, eg. "🐥"
26
30
  image: [
@@ -31,9 +35,13 @@ processor: satteri({
31
35
  "/svg/unjs--ofetch.svg",
32
36
  "/svg/unjs--uncrypto.svg",
33
37
  ], // or just a string, eg. "/svg/unjs--automd.svg"
34
- markerCssSize: "1rem",
38
+ liMarkerCssSize: "1rem",
35
39
  lightThemeHighlightColor: "oklch(0.75 0.1229 12.71)",
36
40
  darkThemeHighlightColor: "oklch(0.81 0.1004 305.04)",
41
+ animation: {
42
+ duration: '0.8s',
43
+ timingFunction: 'ease-in-out'
44
+ },
37
45
  class: {
38
46
  summary: "",
39
47
  ul: "",
@@ -70,17 +78,22 @@ processor: satteri({
70
78
 
71
79
  ## options
72
80
 
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|eg. "summary {background-color: yellow;}"|
84
- |style|Object{summary?: string, ul?: string, li?: string, a?: string}|eg. {a: "font-style: italic;"}|
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": "目次"}|
81
+ |Property‌|Type|Default Value|Preset Value|Info|
82
+ |:---|:---|:---|:---|:--|
83
+ |title|string|Table of Contents|Table of Contents|-|
84
+ |openedMarker|string or undefined|undefined|▼|***Note: titleMarkerType must already be set.***|
85
+ |closedMarker|string or undefined|undefined|▶︎|***Note: titleMarkerType must already be set.***|
86
+ |titleMarkerType|string or undefined|undefined|icon|Value can only be "icon" or "image".|
87
+ |titleMarkerCssSize|string or undefined|undefined|fit-content|-|
88
+ |listStyle|string or undefined|undefined|decimal|eg. disc, circle, decimal, none... or icon and image|
89
+ |icon|string or string[] or undefined|undefined|decimal|listStyle must already be set.|
90
+ |image|string or string[] or undefined|undefined|404 image|listStyle must already be set.|
91
+ |liMarkerCssSize|string or undefined|undefined|1.5rem|CSS property value|
92
+ |lightThemeHighlightColor|string or undefined|undefined|oklch(0.75 0.1229 12.71)|-|
93
+ |darkThemeHighlightColor|string or undefined|undefined|oklch(0.81 0.1004 305.04)|-|
94
+ |animation|boolean or { duration: string, timingFunction: string } or undefined|undefined|{ duration: "0.8s", timingFunction: 'ease-in-out' }|-|
95
+ |class|{summary?: string, ul?: string, li?: string, a?: string} or or undefined|undefined|undefined|eg. {summary: "pb-2", li: "py-1"}|
96
+ |globalStyle|string or or undefined|undefined|undefined|eg. "summary {background-color: yellow;}"|
97
+ |style|Object{summary?: string, ul?: string, li?: string, a?: string} or or undefined|undefined|undefined|eg. {a: "font-style: italic;"}|
98
+ |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.**|
99
+ |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": "目次"}|
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { HastPluginDefinition } from "satteri";
2
2
  //#region basic.d.ts
3
3
  interface tagHasProperty {
4
- summary?: string;
4
+ title?: string;
5
5
  ul?: string;
6
6
  li?: string;
7
7
  a?: string;
@@ -9,18 +9,31 @@ interface tagHasProperty {
9
9
  interface HastOption {
10
10
  /** TOC标题 */
11
11
  title?: string;
12
+ /** 展开时的标记 */
13
+ openedMarker?: string;
14
+ /** 关闭时的标记 */
15
+ closedMarker?: string;
16
+ /** 标题标记类型 "icon" | "image" */
17
+ titleMarkerType?: string;
18
+ /** TOC标题标记大小 */
19
+ titleMarkerCssSize?: string;
12
20
  /** 列表形式 */
13
21
  listStyle?: string;
14
22
  /** 图标 */
15
23
  icon?: string | string[];
16
24
  /** 图片路径 */
17
25
  image?: string | string[];
18
- /** 标记大小 */
19
- markerCssSize?: string;
26
+ /** 列表项标记大小 */
27
+ liMarkerCssSize?: string;
20
28
  /** 浅色主题高亮颜色 */
21
29
  lightThemeHighlightColor?: string;
22
30
  /** 深色主题高亮颜色 */
23
31
  darkThemeHighlightColor?: string;
32
+ /** 动画 */
33
+ animation?: boolean | {
34
+ duration: string;
35
+ timingFunction: string;
36
+ };
24
37
  /** 类名 */
25
38
  class?: tagHasProperty;
26
39
  /** 全局样式 */
package/dist/index.mjs CHANGED
@@ -2,40 +2,55 @@ import { defineHastPlugin } from "satteri";
2
2
  import slug from "slug";
3
3
  //#region src/const.ts
4
4
  const DEFAULT_TITLE = "Table of Contents";
5
+ const DEFAULT_TITLE_MARKER_TYPE = "icon";
6
+ const DEFAULT_TITLE_MARKER_CSS_SIZE = "fit-content";
7
+ const DEFALUT_LOCALE = "en-US";
8
+ const DEFAULT_LIST_STYLE = "decimal";
9
+ const DEFAULT_LI_MARKER_CSS_SIZE = "1.5rem";
10
+ const DEFAULT_ANIMATION = {
11
+ duration: "0.8s",
12
+ timingFunction: "ease-in-out"
13
+ };
14
+ const DEFAULT_LIGHT_THEME_HIGHLIGHT_COLOR = "oklch(0.75 0.1229 12.71)";
15
+ const DEFAULT_DARK_THEME_HIGHLIGHT_COLOR = "oklch(0.81 0.1004 305.04)";
16
+ const DEFAULT_LANGUAGE_MAP = {
17
+ "de-DE": "Inhaltsverzeichnis",
18
+ "el-GR": "Περιεχόμενα",
19
+ "en-US": "Contents",
20
+ "es-ES": "Índice",
21
+ "fr-FR": "Sommaire",
22
+ "it-IT": "Indice",
23
+ "ja-JP": "目次",
24
+ "ko-KR": "목차",
25
+ "ru-RU": "Оглавление",
26
+ "th-TH": "สารบัญ",
27
+ "tr-TR": "İçindekiler",
28
+ "zh-CN": "目录",
29
+ "zh-Hant": "目錄"
30
+ };
5
31
  const DEFAULT_OPTIONS = {
6
32
  title: DEFAULT_TITLE,
7
- listStyle: "decimal",
8
- lightThemeHighlightColor: "oklch(0.75 0.1229 12.71)",
9
- darkThemeHighlightColor: "oklch(0.81 0.1004 305.04)",
33
+ listStyle: DEFAULT_LIST_STYLE,
34
+ titleMarkerType: DEFAULT_TITLE_MARKER_TYPE,
35
+ titleMarkerCssSize: DEFAULT_TITLE_MARKER_CSS_SIZE,
36
+ lightThemeHighlightColor: DEFAULT_LIGHT_THEME_HIGHLIGHT_COLOR,
37
+ darkThemeHighlightColor: DEFAULT_DARK_THEME_HIGHLIGHT_COLOR,
38
+ liMarkerCssSize: DEFAULT_LI_MARKER_CSS_SIZE,
10
39
  class: {
11
- summary: "",
40
+ title: "",
12
41
  ul: "",
13
42
  li: "",
14
43
  a: ""
15
44
  },
16
45
  globalStyle: "",
17
46
  style: {
18
- summary: "",
47
+ title: "",
19
48
  ul: "",
20
49
  li: "",
21
50
  a: ""
22
51
  },
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
- }
52
+ locale: DEFALUT_LOCALE,
53
+ languageMap: DEFAULT_LANGUAGE_MAP
39
54
  };
40
55
  //#endregion
41
56
  //#region src/assets/svg/image-not-found.svg
@@ -57,12 +72,25 @@ function prettyToc(option) {
57
72
  visit(node, ctx) {
58
73
  const opt = option ?? DEFAULT_OPTIONS;
59
74
  try {
60
- const defaultTitle = DEFAULT_TITLE;
61
75
  const depth = parseInt(node.tagName.slice(-1), 10);
62
76
  const content = ctx.textContent(node);
63
77
  const contentSlug = slug(content) + (/* @__PURE__ */ new Date()).getTime();
64
- const lightThemeHighlightColor = opt.lightThemeHighlightColor ?? "oklch(0.75 0.1229 12.71)";
65
- const darkThemeHighlightColor = opt.darkThemeHighlightColor ?? "oklch(0.81 0.1004 305.04)";
78
+ let title = opt.title || "Table of Contents";
79
+ if (opt.languageMap) title = opt.languageMap[opt.locale ?? "en-US"] || "Table of Contents";
80
+ else title = DEFAULT_LANGUAGE_MAP[opt.locale ?? "en-US"] || "Table of Contents";
81
+ let listStyle = DEFAULT_LIST_STYLE;
82
+ if (opt.listStyle === "icon" || opt.listStyle === "image" || opt.listStyle === "decimal") listStyle = "none";
83
+ else if (opt.listStyle !== void 0) listStyle = opt.listStyle;
84
+ const lightThemeHighlightColor = opt.lightThemeHighlightColor || "oklch(0.75 0.1229 12.71)";
85
+ const darkThemeHighlightColor = opt.darkThemeHighlightColor || "oklch(0.81 0.1004 305.04)";
86
+ const titleMarkerCssSize = opt.titleMarkerCssSize || "fit-content";
87
+ const openedMarker = opt.openedMarker || "▼";
88
+ const closedMarker = opt.closedMarker || "▶︎";
89
+ const liMarkerCssSize = opt.liMarkerCssSize || "1.5rem";
90
+ let animation = void 0;
91
+ if (typeof opt.animation === "object" && opt.animation !== void 0) animation = opt.animation ?? DEFAULT_ANIMATION;
92
+ else if (opt.animation === true) animation = DEFAULT_ANIMATION;
93
+ else animation = void 0;
66
94
  let marker = "";
67
95
  let icon = "🍎";
68
96
  let image = image_not_found_default;
@@ -94,48 +122,61 @@ function prettyToc(option) {
94
122
  }
95
123
  if (ctx.data.nodeStr === void 0) {
96
124
  const baseStyle = `
97
- @keyframes fadeIn {
98
- from { opacity: 0; }
99
- to { opacity: 1; }
125
+ .toc-wrapper {
126
+ display: grid;
127
+ grid-template-rows: 0fr;
128
+ overflow: hidden;
100
129
  }
101
- summary {
102
- font-size: 1.25rem;
103
- margin-bottom: 0.5rem;
130
+ .toc-wrapper:has(.toc-title.open) {
131
+ grid-template-rows: 1fr;
104
132
  }
105
- summary:hover {
133
+ .toc-title {
134
+ font-size: 1.2rem;
135
+ font-weight: 600;
136
+ margin: 0 0 0.5rem 0;
137
+ width: fit-content;
138
+ position: relative;
139
+ }
140
+ .toc-title:hover {
106
141
  color: ${lightThemeHighlightColor};
107
142
  cursor: pointer;
108
- width: fit-content;
109
143
  }
110
- html.dark summary:hover {
144
+ html.dark .toc-title:hover {
111
145
  color: ${darkThemeHighlightColor};
112
146
  }
113
147
  ul {
114
148
  padding-left: 0;
115
- list-style-type: ${opt.listStyle === "icon" || opt.listStyle === "image" || opt.listStyle === "decimal" ? "none" : opt.listStyle} ;
149
+ list-style-type: ${listStyle} ;
116
150
  list-style-position: inside;
117
151
  }
152
+ .toc-wrapper > ul {
153
+ margin-left: 0.5rem;
154
+ }
155
+ @keyframes fadeIn {
156
+ from { opacity: 0; }
157
+ to { opacity: 1; }
158
+ }
118
159
  li {
119
160
  /* 默认缩进为 1rem */
120
161
  padding-left: var(--list-indent, 1rem);
121
162
  line-height: 1.5rem;
122
163
  font-size: 1rem;
123
- animation: fadeIn 0.1s ease-in; // 防止页面刷新瞬间显示"0 javascript"
164
+ animation: fadeIn 0.01s ease-in; // 防止页面刷新瞬间显示"0 javascript"
124
165
  }
125
166
  .li-marker {
126
167
  display: inline-block;
127
168
  margin-right: 0.5rem;
128
169
  }
129
170
  img.li-marker {
130
- width: ${opt.markerCssSize ?? "1rem"};
131
- height: ${opt.markerCssSize ?? "1rem"};
171
+ width: ${liMarkerCssSize};
172
+ height: ${liMarkerCssSize};
132
173
  margin: 0 0.5rem 0 0;
133
174
  }
134
175
  .li-marker::before {
135
176
  font-size: 1rem;
136
177
  font-weight: 600;
137
- width: ${opt.markerCssSize ?? "1rem"};
138
- height: ${opt.markerCssSize ?? "1rem"};
178
+ width: ${liMarkerCssSize};
179
+ height: ${liMarkerCssSize};
139
180
  }
140
181
  /* 嵌套的 ul 内部,让缩进变量自动叠加 1rem */
141
182
  ul ul li {
@@ -173,45 +214,55 @@ function prettyToc(option) {
173
214
  }
174
215
  `;
175
216
  const iframeContent = `<iframe
176
- style="display:none;"
177
- srcdoc="&lt;!DOCTYPE html&gt;
178
- &lt;html&gt;
179
- &lt;head&gt;&lt;meta charset=&quot;utf-8&quot;&gt;&lt;/head&gt;
180
- &lt;body&gt;
181
- <strong>Hello from iframe!</strong>
182
- <script is:inline data-astro-rerun>
183
- console.log('🚀 pretty-toc running...');
184
- const currentLocale = window.parent.location.pathname.split('/')[1] || '${opt.locale || "en-US"}';
185
- const languageMap = ${JSON.stringify(opt.languageMap).replaceAll("\"", "'")};
217
+ style="display:none;"
218
+ srcdoc="&lt;!DOCTYPE html&gt;
219
+ &lt;html&gt;
220
+ &lt;head&gt;&lt;meta charset=&quot;utf-8&quot;&gt;&lt;/head&gt;
221
+ &lt;body&gt;
222
+ <strong>Hello from iframe!</strong>
223
+ <script is:inline data-astro-rerun>
224
+ const currentLocale = window.parent.location.pathname.split('/')[1] || '${opt.locale || "en-US"}';
225
+ const languageMap = ${JSON.stringify(opt.languageMap).replaceAll("\"", "'")} || ${JSON.stringify(DEFAULT_LANGUAGE_MAP).replaceAll("\"", "'")};
186
226
 
187
- function syncTocTitle(
188
- locale,
189
- languageMap
190
- ) {
191
- const tocSummary = window.parent.document.querySelector('[data-satteri-toc-title]');
227
+ function syncTocTitle(
228
+ locale,
229
+ languageMap
230
+ ) {
231
+ const tocSummary = window.parent.document.querySelector('[data-satteri-toc-title]');
192
232
 
193
- if (tocSummary) {
194
- const titleKey = tocSummary.getAttribute('data-satteri-toc-title');
195
- const translation =
196
- languageMap[locale] || titleKey || defaultTitle;
197
- tocSummary.textContent = translation;
198
- }
199
- }
233
+ if (tocSummary) {
234
+ const titleKey = tocSummary.getAttribute('data-satteri-toc-title');
235
+ const translation =
236
+ languageMap[locale] || '${title}' || titleKey;
237
+ tocSummary.textContent = translation;
238
+ }
239
+ }
200
240
 
201
- syncTocTitle(currentLocale, languageMap);
241
+ function toggleToc() {
242
+ const tocTitle = window.parent.document.querySelector('.toc-title');
243
+ if (tocTitle) {
244
+ tocTitle.addEventListener('click', function () {
245
+ tocTitle.classList.toggle('open');
246
+ })
247
+ }
248
+ }
202
249
 
203
- window.parent.addEventListener('load', function() {
204
- syncTocTitle(currentLocale, languageMap);
205
- });
206
- <\/script>
207
- &lt;/body&gt;
208
- &lt;/html&gt;">
209
- </iframe>`;
250
+ syncTocTitle(currentLocale, languageMap);
251
+ toggleToc();
252
+
253
+ window.parent.addEventListener('load', function() {
254
+ syncTocTitle(currentLocale, languageMap);
255
+ toggleToc();
256
+ });
257
+ <\/script>
258
+ &lt;/body&gt;
259
+ &lt;/html&gt;">
260
+ </iframe>`;
210
261
  ctx.data.firstHeading = node;
211
262
  ctx.data.firstHeadingDepth = depth;
212
263
  ctx.data.firstHeadingId = contentSlug;
213
264
  ctx.data.firstHeadingIndex = ctx.indexOf(node) ?? 0;
214
- ctx.data.nodeStr = `<details><style>${baseStyle + (opt.globalStyle ?? "") + (opt.listStyle === "decimal" ? `ul{
265
+ ctx.data.nodeStr = `<div class="toc-wrapper"><style>${baseStyle + (opt.globalStyle ?? "") + (opt.listStyle === "decimal" ? `ul{
215
266
  list-style-type: none;
216
267
  counter-reset: toc-counter;
217
268
  }
@@ -220,7 +271,49 @@ function prettyToc(option) {
220
271
  }
221
272
  .li-marker::before {
222
273
  content: counters(toc-counter, ".");
223
- }` : "")}</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>`;
274
+ }` : "") + (opt.animation && animation !== void 0 ? `.toc-wrapper {
275
+ max-height: 2rem;
276
+ transition: max-height ${animation.duration} ${animation.timingFunction};
277
+ }
278
+ .toc-wrapper:has(.toc-title.open) {
279
+ max-height: 100vh;
280
+ }
281
+ ` : "") + (opt.titleMarkerType === "icon" ? `
282
+ .toc-title::before {
283
+ content: '${closedMarker} ';
284
+ display: contents;
285
+ font-size: 1rem;
286
+ }
287
+ .toc-title.open::before {
288
+ content: '${openedMarker} ';
289
+ display: contents;
290
+ font-size: 1rem;
291
+ }
292
+ ` : "") + (opt.titleMarkerType === "image" ? `
293
+ .toc-wrapper > .toc-title, .toc-wrapper > ul {
294
+ margin-left: 2rem;
295
+ }
296
+ .toc-title::before {
297
+ content: "";
298
+ background: url('${closedMarker}') center / contain no-repeat;
299
+ width: ${titleMarkerCssSize};
300
+ height: ${titleMarkerCssSize};
301
+ position: absolute;
302
+ left: -2rem;
303
+ top: 50%;
304
+ transform: translateY(-50%);
305
+ }
306
+ .toc-title.open::before {
307
+ content: "";
308
+ background: url('${openedMarker}') center / contain no-repeat;
309
+ width: ${titleMarkerCssSize};
310
+ height: ${titleMarkerCssSize};
311
+ position: absolute;
312
+ left: -2rem;
313
+ top: 50%;
314
+ transform: translateY(-50%);
315
+ }
316
+ ` : "")}</style><h2 data-satteri-toc-title="${title}" class="toc-title ${opt.class?.title ?? ""}" style="${opt.style?.title ?? ""}">${title}</h2><ul class="${opt.class?.ul ?? ""}" style="${opt.style?.ul ?? ""}">${nodeStr}</ul>${iframeContent}</div>`;
224
317
  } else {
225
318
  const indexA = ctx.data.nodeStr?.lastIndexOf(tagSignal);
226
319
  if (indexA !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexA + tagSignalLength + 5) + nodeStr + ctx.data.nodeStr?.slice(indexA + tagSignalLength + 5);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pretty-toc",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "description": "A Table of Contents generation plugin for the Satteri markdown parser library.",
6
6
  "keywords": [