pretty-toc 1.1.5 → 1.2.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/dist/index.d.mts +5 -0
- package/dist/index.mjs +108 -44
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -47,6 +47,11 @@ interface HastOption {
|
|
|
47
47
|
}
|
|
48
48
|
//#endregion
|
|
49
49
|
//#region src/index.d.ts
|
|
50
|
+
/**
|
|
51
|
+
*
|
|
52
|
+
* @param option 用户传入插件的配置
|
|
53
|
+
* @returns 插件实例
|
|
54
|
+
*/
|
|
50
55
|
declare function prettyToc(option?: HastOption): HastPluginDefinition;
|
|
51
56
|
//#endregion
|
|
52
57
|
export { prettyToc as default };
|
package/dist/index.mjs
CHANGED
|
@@ -57,6 +57,11 @@ const DEFAULT_OPTIONS = {
|
|
|
57
57
|
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";
|
|
58
58
|
//#endregion
|
|
59
59
|
//#region src/index.ts
|
|
60
|
+
/**
|
|
61
|
+
*
|
|
62
|
+
* @param option 用户传入插件的配置
|
|
63
|
+
* @returns 插件实例
|
|
64
|
+
*/
|
|
60
65
|
function prettyToc(option) {
|
|
61
66
|
return defineHastPlugin({
|
|
62
67
|
name: "prettyToc",
|
|
@@ -110,22 +115,85 @@ function prettyToc(option) {
|
|
|
110
115
|
break;
|
|
111
116
|
default: marker = "";
|
|
112
117
|
}
|
|
113
|
-
|
|
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>`;
|
|
118
|
+
let 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></li>`;
|
|
116
119
|
ctx.setProperty(node, "id", contentSlug);
|
|
120
|
+
if (ctx.data.nodeTree === void 0) {
|
|
121
|
+
ctx.data.firstHeading = node;
|
|
122
|
+
ctx.data.firstHeadingDepth = depth;
|
|
123
|
+
ctx.data.firstHeadingId = contentSlug;
|
|
124
|
+
ctx.data.firstHeadingIndex = ctx.indexOf(node) ?? 0;
|
|
125
|
+
const rootNode = {
|
|
126
|
+
depth: 0,
|
|
127
|
+
content: "",
|
|
128
|
+
children: [],
|
|
129
|
+
parent: void 0
|
|
130
|
+
};
|
|
131
|
+
ctx.data.nodeTree = {
|
|
132
|
+
rootNode,
|
|
133
|
+
previousNode: rootNode
|
|
134
|
+
};
|
|
135
|
+
if (depth === 1) {
|
|
136
|
+
const currentNode = {
|
|
137
|
+
depth: 1,
|
|
138
|
+
content: nodeStr,
|
|
139
|
+
children: [],
|
|
140
|
+
parent: rootNode
|
|
141
|
+
};
|
|
142
|
+
ctx.data.nodeTree.rootNode.children.push(currentNode);
|
|
143
|
+
ctx.data.nodeTree.previousNode = currentNode;
|
|
144
|
+
} else for (let i = 1; i <= depth; i++) {
|
|
145
|
+
const currentNode = {
|
|
146
|
+
depth: i,
|
|
147
|
+
content: i === depth ? nodeStr : "",
|
|
148
|
+
children: [],
|
|
149
|
+
parent: ctx.data.nodeTree.previousNode
|
|
150
|
+
};
|
|
151
|
+
ctx.data.nodeTree.previousNode.children.push(currentNode);
|
|
152
|
+
ctx.data.nodeTree.previousNode = currentNode;
|
|
153
|
+
}
|
|
154
|
+
} else {
|
|
155
|
+
let hasFound = false;
|
|
156
|
+
let nextSearchNode = ctx.data.nodeTree.rootNode;
|
|
157
|
+
searchNode: while (!hasFound) if (nextSearchNode.depth + 1 === depth) {
|
|
158
|
+
const currentNode = {
|
|
159
|
+
depth,
|
|
160
|
+
content: nodeStr,
|
|
161
|
+
children: [],
|
|
162
|
+
parent: nextSearchNode
|
|
163
|
+
};
|
|
164
|
+
nextSearchNode.children.push(currentNode);
|
|
165
|
+
ctx.data.nodeTree.previousNode = currentNode;
|
|
166
|
+
hasFound = true;
|
|
167
|
+
break searchNode;
|
|
168
|
+
} else if (nextSearchNode.children.length > 0) {
|
|
169
|
+
nextSearchNode = nextSearchNode.children[nextSearchNode.children.length - 1];
|
|
170
|
+
continue searchNode;
|
|
171
|
+
} else {
|
|
172
|
+
const middleNode = {
|
|
173
|
+
depth: nextSearchNode.depth + 1,
|
|
174
|
+
content: "",
|
|
175
|
+
children: [],
|
|
176
|
+
parent: nextSearchNode
|
|
177
|
+
};
|
|
178
|
+
nextSearchNode.children.push(middleNode);
|
|
179
|
+
nextSearchNode = middleNode;
|
|
180
|
+
continue searchNode;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
117
183
|
let parent = ctx.parent(node);
|
|
118
184
|
while (parent.type !== "root" && parent !== void 0) {
|
|
119
185
|
const p = ctx.parent(parent);
|
|
120
186
|
if (p) parent = p;
|
|
121
187
|
else break;
|
|
122
188
|
}
|
|
123
|
-
if (
|
|
189
|
+
if (parent.type === "root") {
|
|
190
|
+
const collectedNodeStr = concatNodeStr(ctx.data.nodeTree.rootNode, opt);
|
|
124
191
|
const baseStyle = `
|
|
125
192
|
.toc-wrapper {
|
|
126
193
|
display: grid;
|
|
127
194
|
grid-template-rows: 0fr;
|
|
128
195
|
overflow: hidden;
|
|
196
|
+
margin-bottom: 2rem;
|
|
129
197
|
}
|
|
130
198
|
.toc-wrapper:has(.toc-title.open) {
|
|
131
199
|
grid-template-rows: 1fr;
|
|
@@ -138,20 +206,18 @@ function prettyToc(option) {
|
|
|
138
206
|
position: relative;
|
|
139
207
|
}
|
|
140
208
|
ul {
|
|
141
|
-
padding-left:
|
|
209
|
+
padding-left: var(--list-indent, 1.5rem);
|
|
142
210
|
list-style-type: ${listStyle} ;
|
|
143
211
|
list-style-position: inside;
|
|
144
212
|
}
|
|
145
|
-
|
|
146
|
-
|
|
213
|
+
ul:first-child {
|
|
214
|
+
|
|
147
215
|
}
|
|
148
216
|
@keyframes fadeIn {
|
|
149
217
|
from { opacity: 0; }
|
|
150
218
|
to { opacity: 1; }
|
|
151
219
|
}
|
|
152
220
|
li {
|
|
153
|
-
/* 默认缩进为 1rem */
|
|
154
|
-
padding-left: var(--list-indent, 1rem);
|
|
155
221
|
line-height: 1.5rem;
|
|
156
222
|
font-size: 1rem;
|
|
157
223
|
animation: fadeIn 0.01s ease-in; // 防止页面刷新瞬间显示"0 javascript"
|
|
@@ -172,7 +238,7 @@ function prettyToc(option) {
|
|
|
172
238
|
height: ${liMarkerCssSize};
|
|
173
239
|
}
|
|
174
240
|
/* 嵌套的 ul 内部,让缩进变量自动叠加 1rem */
|
|
175
|
-
ul ul
|
|
241
|
+
ul ul {
|
|
176
242
|
--list-indent: calc(var(--list-indent, 1rem) + 1rem);
|
|
177
243
|
}
|
|
178
244
|
.li-row {
|
|
@@ -247,7 +313,6 @@ function prettyToc(option) {
|
|
|
247
313
|
<html>
|
|
248
314
|
<head><meta charset="utf-8"></head>
|
|
249
315
|
<body>
|
|
250
|
-
<strong>Hello from iframe!</strong>
|
|
251
316
|
<script is:inline data-astro-rerun>
|
|
252
317
|
const currentLocale = window.parent.location.pathname.split('/')[1] || '${opt.locale || "en-US"}';
|
|
253
318
|
const languageMap = ${JSON.stringify(opt.languageMap).replaceAll("\"", "'")} || ${JSON.stringify(DEFAULT_LANGUAGE_MAP).replaceAll("\"", "'")};
|
|
@@ -275,22 +340,24 @@ function prettyToc(option) {
|
|
|
275
340
|
}
|
|
276
341
|
}
|
|
277
342
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
343
|
+
window.addEventListener('load', function() {
|
|
344
|
+
if (window.parent.document.readyState === 'complete') {
|
|
345
|
+
syncTocTitle(currentLocale, languageMap);
|
|
346
|
+
toggleToc();
|
|
347
|
+
} else {
|
|
348
|
+
window.parent.document.addEventListener('readystatechange', event => {
|
|
349
|
+
if (event.target.readyState === 'complete') {
|
|
350
|
+
syncTocTitle(currentLocale, languageMap);
|
|
351
|
+
toggleToc();
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
}
|
|
284
355
|
});
|
|
285
356
|
<\/script>
|
|
286
357
|
</body>
|
|
287
358
|
</html>">
|
|
288
359
|
</iframe>
|
|
289
360
|
`;
|
|
290
|
-
ctx.data.firstHeading = node;
|
|
291
|
-
ctx.data.firstHeadingDepth = depth;
|
|
292
|
-
ctx.data.firstHeadingId = contentSlug;
|
|
293
|
-
ctx.data.firstHeadingIndex = ctx.indexOf(node) ?? 0;
|
|
294
361
|
ctx.data.nodeStr = `
|
|
295
362
|
<div class="toc-wrapper"><style>${baseStyle + (opt.globalStyle ?? "") + (opt.listStyle === "decimal" ? `ul{
|
|
296
363
|
list-style-type: none;
|
|
@@ -343,31 +410,13 @@ function prettyToc(option) {
|
|
|
343
410
|
top: 50%;
|
|
344
411
|
transform: translateY(-50%);
|
|
345
412
|
}
|
|
346
|
-
` : "")}</style><h2 data-satteri-toc-title="${title}" class="toc-title ${opt.class?.title ?? ""}" style="${opt.style?.title ?? ""}">${title}</h2
|
|
413
|
+
` : "")}</style><h2 data-satteri-toc-title="${title}" class="toc-title ${opt.class?.title ?? ""}" style="${opt.style?.title ?? ""}">${title}</h2>${collectedNodeStr}${iframeContent}</div>
|
|
347
414
|
`;
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
let indexB = -1;
|
|
353
|
-
for (let i = depth; i > 0; i--) {
|
|
354
|
-
indexB = ctx.data.nodeStr?.lastIndexOf(`<span data-depth='${i}' style="display: none;"></span>`);
|
|
355
|
-
if (indexB !== -1) break;
|
|
356
|
-
}
|
|
357
|
-
if (indexB !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexB) + `<ul class="${opt.class?.ul ?? ""}" style="${opt.style?.ul ?? ""}">` + nodeStr + `</ul>` + ctx.data.nodeStr?.slice(indexB);
|
|
358
|
-
else {
|
|
359
|
-
const indexC = ctx.data.nodeStr?.lastIndexOf(`</ul>`);
|
|
360
|
-
if (indexC !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexC) + nodeStr + ctx.data.nodeStr?.slice(indexC);
|
|
361
|
-
}
|
|
362
|
-
} else {
|
|
363
|
-
const indexB = ctx.data.nodeStr?.lastIndexOf(`</ul>`);
|
|
364
|
-
if (indexB !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexB) + nodeStr + ctx.data.nodeStr?.slice(indexB);
|
|
365
|
-
}
|
|
415
|
+
ctx.replaceNode(parent.children[ctx.data.firstHeadingIndex], {
|
|
416
|
+
type: "raw",
|
|
417
|
+
value: ctx.data.nodeStr + `<h${ctx.data.firstHeadingDepth} id="${ctx.data.firstHeadingId}">${ctx.data.firstHeading && ctx.textContent(ctx.data.firstHeading)}</h${ctx.data.firstHeadingDepth}>`
|
|
418
|
+
});
|
|
366
419
|
}
|
|
367
|
-
if (parent.type === "root") ctx.replaceNode(parent.children[ctx.data.firstHeadingIndex], {
|
|
368
|
-
type: "raw",
|
|
369
|
-
value: ctx.data.nodeStr + `<h${ctx.data.firstHeadingDepth} id="${ctx.data.firstHeadingId}">${ctx.data.firstHeading && ctx.textContent(ctx.data.firstHeading)}</h${ctx.data.firstHeadingDepth}>`
|
|
370
|
-
});
|
|
371
420
|
} catch (err) {
|
|
372
421
|
throw err;
|
|
373
422
|
}
|
|
@@ -375,5 +424,20 @@ function prettyToc(option) {
|
|
|
375
424
|
}]
|
|
376
425
|
});
|
|
377
426
|
}
|
|
427
|
+
/**
|
|
428
|
+
*
|
|
429
|
+
* @param node 遍历的当前节点
|
|
430
|
+
* @param opt 传入插件的配置
|
|
431
|
+
* @returns 所有节点的html内容字符串
|
|
432
|
+
*/
|
|
433
|
+
function concatNodeStr(node, opt) {
|
|
434
|
+
let childNodeStr = "";
|
|
435
|
+
if (node.children.length > 0) {
|
|
436
|
+
for (let i = 0; i < node.children.length; i++) childNodeStr += concatNodeStr(node.children[i], opt);
|
|
437
|
+
childNodeStr = `<ul class="${opt.class?.ul ?? ""}" style="${opt.style?.ul ?? ""}">${childNodeStr}</ul>`;
|
|
438
|
+
return node.content + childNodeStr;
|
|
439
|
+
}
|
|
440
|
+
return node.content;
|
|
441
|
+
}
|
|
378
442
|
//#endregion
|
|
379
443
|
export { prettyToc as default };
|