pretty-toc 0.1.6 → 0.1.8

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|-|
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": "目次"}|
81
+ |Property‌|Type|Default Value|Preset Value|Info|
82
+ |:---|:---|:---|:---|:--|
83
+ |title|string|Table of Contents|Table of Contents|-|
84
+ |openedMarker|string or undefined|undefined|▼|titleMarkerType must already be set.|
85
+ |closedMarker|string or undefined|undefined|▶︎|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;
@@ -82,7 +110,9 @@ function prettyToc(option) {
82
110
  break;
83
111
  default: marker = "";
84
112
  }
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>`;
113
+ const tagSignal = `<span data-depth='${depth}' style="display: none;"></span>`;
114
+ const tagSignalLength = tagSignal.length;
115
+ 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>${tagSignal}</li>`;
86
116
  ctx.setProperty(node, "id", contentSlug);
87
117
  let parent = ctx.parent(node);
88
118
  while (parent.type !== "root" && parent !== void 0) {
@@ -92,48 +122,61 @@ function prettyToc(option) {
92
122
  }
93
123
  if (ctx.data.nodeStr === void 0) {
94
124
  const baseStyle = `
95
- @keyframes fadeIn {
96
- from { opacity: 0; }
97
- to { opacity: 1; }
125
+ .toc-wrapper {
126
+ display: grid;
127
+ grid-template-rows: 0fr;
128
+ overflow: hidden;
98
129
  }
99
- summary {
100
- font-size: 1.25rem;
101
- margin-bottom: 0.5rem;
130
+ .toc-wrapper:has(.toc-title.open) {
131
+ grid-template-rows: 1fr;
102
132
  }
103
- 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 {
104
141
  color: ${lightThemeHighlightColor};
105
142
  cursor: pointer;
106
- width: fit-content;
107
143
  }
108
- html.dark summary:hover {
144
+ html.dark .toc-title:hover {
109
145
  color: ${darkThemeHighlightColor};
110
146
  }
111
147
  ul {
112
148
  padding-left: 0;
113
- list-style-type: ${opt.listStyle === "icon" || opt.listStyle === "image" || opt.listStyle === "decimal" ? "none" : opt.listStyle} ;
149
+ list-style-type: ${listStyle} ;
114
150
  list-style-position: inside;
115
151
  }
152
+ .toc-wrapper > ul {
153
+ margin-left: 0.5rem;
154
+ }
155
+ @keyframes fadeIn {
156
+ from { opacity: 0; }
157
+ to { opacity: 1; }
158
+ }
116
159
  li {
117
160
  /* 默认缩进为 1rem */
118
161
  padding-left: var(--list-indent, 1rem);
119
162
  line-height: 1.5rem;
120
163
  font-size: 1rem;
121
- animation: fadeIn 0.1s ease-in; // 防止页面刷新瞬间显示"0 javascript"
164
+ animation: fadeIn 0.01s ease-in; // 防止页面刷新瞬间显示"0 javascript"
122
165
  }
123
166
  .li-marker {
124
167
  display: inline-block;
125
168
  margin-right: 0.5rem;
126
169
  }
127
170
  img.li-marker {
128
- width: ${opt.markerCssSize ?? "1rem"};
129
- height: ${opt.markerCssSize ?? "1rem"};
171
+ width: ${liMarkerCssSize};
172
+ height: ${liMarkerCssSize};
130
173
  margin: 0 0.5rem 0 0;
131
174
  }
132
175
  .li-marker::before {
133
176
  font-size: 1rem;
134
177
  font-weight: 600;
135
- width: ${opt.markerCssSize ?? "1rem"};
136
- height: ${opt.markerCssSize ?? "1rem"};
178
+ width: ${liMarkerCssSize};
179
+ height: ${liMarkerCssSize};
137
180
  }
138
181
  /* 嵌套的 ul 内部,让缩进变量自动叠加 1rem */
139
182
  ul ul li {
@@ -171,45 +214,55 @@ function prettyToc(option) {
171
214
  }
172
215
  `;
173
216
  const iframeContent = `<iframe
174
- style="display:none;"
175
- srcdoc="&lt;!DOCTYPE html&gt;
176
- &lt;html&gt;
177
- &lt;head&gt;&lt;meta charset=&quot;utf-8&quot;&gt;&lt;/head&gt;
178
- &lt;body&gt;
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("\"", "'")};
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("\"", "'")};
184
226
 
185
- function syncTocTitle(
186
- locale,
187
- languageMap
188
- ) {
189
- 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]');
190
232
 
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
- }
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
+ }
198
240
 
199
- 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
+ }
200
249
 
201
- window.parent.addEventListener('load', function() {
202
- syncTocTitle(currentLocale, languageMap);
203
- });
204
- <\/script>
205
- &lt;/body&gt;
206
- &lt;/html&gt;">
207
- </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>`;
208
261
  ctx.data.firstHeading = node;
209
262
  ctx.data.firstHeadingDepth = depth;
210
263
  ctx.data.firstHeadingId = contentSlug;
211
264
  ctx.data.firstHeadingIndex = ctx.indexOf(node) ?? 0;
212
- 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{
213
266
  list-style-type: none;
214
267
  counter-reset: toc-counter;
215
268
  }
@@ -218,10 +271,52 @@ function prettyToc(option) {
218
271
  }
219
272
  .li-marker::before {
220
273
  content: counters(toc-counter, ".");
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>`;
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>`;
222
317
  } else {
223
- const indexA = ctx.data.nodeStr?.lastIndexOf(`<span data-depth='${depth}' style="display: none;"></span>`);
224
- if (indexA !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexA + 56) + nodeStr + ctx.data.nodeStr?.slice(indexA + 56);
318
+ const indexA = ctx.data.nodeStr?.lastIndexOf(tagSignal);
319
+ if (indexA !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexA + tagSignalLength + 5) + nodeStr + ctx.data.nodeStr?.slice(indexA + tagSignalLength + 5);
225
320
  else if (depth > 1) {
226
321
  let indexB = -1;
227
322
  for (let i = depth; i > 0; i--) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pretty-toc",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "type": "module",
5
5
  "description": "A Table of Contents generation plugin for the Satteri markdown parser library.",
6
6
  "keywords": [