pretty-toc 0.1.4 → 0.1.5

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
@@ -54,7 +54,7 @@ processor: satteri({
54
54
  li: "",
55
55
  a: "",
56
56
  },
57
- locale: "zh-CN",
57
+ locale: "zh-CN", // Note: The case of the locale variable should match the locale in your page URL.
58
58
  languageMap: {
59
59
  "de-DE": "Inhaltsverzeichnis",
60
60
  "el-GR": "Περιεχόμενα",
package/dist/index.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { HastPluginDefinition } from "satteri";
2
- //#region app.d.ts
2
+ //#region basic.d.ts
3
3
  interface tagHasProperty {
4
4
  summary?: string;
5
5
  ul?: string;
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 = "Table of Contents";
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 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)};
173
+ 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("\"", "'")};
163
184
 
164
- function syncTocTitle(
165
- locale,
166
- languageMap
167
- ) {
168
- const tocSummary = document.querySelector("[data-satteri-toc-title]");
185
+ function syncTocTitle(
186
+ locale,
187
+ languageMap
188
+ ) {
189
+ const tocSummary = window.parent.document.querySelector('[data-satteri-toc-title]');
169
190
 
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
- }
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
- syncTocTitle(currentLocale, languageMap);
199
+ syncTocTitle(currentLocale, languageMap);
179
200
 
180
- window.addEventListener("load", () => {
181
- syncTocTitle(currentLocale, languageMap);
182
- });
183
- }
184
- <\/script>`;
201
+ window.parent.addEventListener('load', function() {
202
+ syncTocTitle(currentLocale, languageMap);
203
+ });
204
+ <\/script>
205
+ &lt;/body&gt;
206
+ &lt;/html&gt;">
207
+ </iframe>`;
185
208
  ctx.data.firstHeading = node;
186
209
  ctx.data.firstHeadingDepth = depth;
187
210
  ctx.data.firstHeadingId = contentSlug;
188
- ctx.data.rootNode = ctx.parent(node);
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><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>`;
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
- 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], {
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
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pretty-toc",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "type": "module",
5
5
  "description": "A Table of Contents generation plugin for the Satteri markdown parser library.",
6
6
  "keywords": [