pretty-toc 1.1.6 → 1.2.1
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 +96 -36
- 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,19 @@ 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
213
|
.toc-wrapper > ul {
|
|
146
|
-
|
|
214
|
+
padding-left: 0;
|
|
147
215
|
}
|
|
148
216
|
@keyframes fadeIn {
|
|
149
217
|
from { opacity: 0; }
|
|
150
218
|
to { opacity: 1; }
|
|
151
219
|
}
|
|
152
220
|
li {
|
|
153
|
-
|
|
154
|
-
padding-left: var(--list-indent, 1rem);
|
|
221
|
+
padding: 0;
|
|
155
222
|
line-height: 1.5rem;
|
|
156
223
|
font-size: 1rem;
|
|
157
224
|
animation: fadeIn 0.01s ease-in; // 防止页面刷新瞬间显示"0 javascript"
|
|
@@ -172,7 +239,7 @@ function prettyToc(option) {
|
|
|
172
239
|
height: ${liMarkerCssSize};
|
|
173
240
|
}
|
|
174
241
|
/* 嵌套的 ul 内部,让缩进变量自动叠加 1rem */
|
|
175
|
-
ul ul
|
|
242
|
+
ul ul {
|
|
176
243
|
--list-indent: calc(var(--list-indent, 1rem) + 1rem);
|
|
177
244
|
}
|
|
178
245
|
.li-row {
|
|
@@ -292,10 +359,6 @@ function prettyToc(option) {
|
|
|
292
359
|
</html>">
|
|
293
360
|
</iframe>
|
|
294
361
|
`;
|
|
295
|
-
ctx.data.firstHeading = node;
|
|
296
|
-
ctx.data.firstHeadingDepth = depth;
|
|
297
|
-
ctx.data.firstHeadingId = contentSlug;
|
|
298
|
-
ctx.data.firstHeadingIndex = ctx.indexOf(node) ?? 0;
|
|
299
362
|
ctx.data.nodeStr = `
|
|
300
363
|
<div class="toc-wrapper"><style>${baseStyle + (opt.globalStyle ?? "") + (opt.listStyle === "decimal" ? `ul{
|
|
301
364
|
list-style-type: none;
|
|
@@ -348,31 +411,13 @@ function prettyToc(option) {
|
|
|
348
411
|
top: 50%;
|
|
349
412
|
transform: translateY(-50%);
|
|
350
413
|
}
|
|
351
|
-
` : "")}</style><h2 data-satteri-toc-title="${title}" class="toc-title ${opt.class?.title ?? ""}" style="${opt.style?.title ?? ""}">${title}</h2
|
|
414
|
+
` : "")}</style><h2 data-satteri-toc-title="${title}" class="toc-title ${opt.class?.title ?? ""}" style="${opt.style?.title ?? ""}">${title}</h2>${collectedNodeStr}${iframeContent}</div>
|
|
352
415
|
`;
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
let indexB = -1;
|
|
358
|
-
for (let i = depth; i > 0; i--) {
|
|
359
|
-
indexB = ctx.data.nodeStr?.lastIndexOf(`<span data-depth='${i}' style="display: none;"></span>`);
|
|
360
|
-
if (indexB !== -1) break;
|
|
361
|
-
}
|
|
362
|
-
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);
|
|
363
|
-
else {
|
|
364
|
-
const indexC = ctx.data.nodeStr?.lastIndexOf(`</ul>`);
|
|
365
|
-
if (indexC !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexC) + nodeStr + ctx.data.nodeStr?.slice(indexC);
|
|
366
|
-
}
|
|
367
|
-
} else {
|
|
368
|
-
const indexB = ctx.data.nodeStr?.lastIndexOf(`</ul>`);
|
|
369
|
-
if (indexB !== -1) ctx.data.nodeStr = ctx.data.nodeStr?.slice(0, indexB) + nodeStr + ctx.data.nodeStr?.slice(indexB);
|
|
370
|
-
}
|
|
416
|
+
ctx.replaceNode(parent.children[ctx.data.firstHeadingIndex], {
|
|
417
|
+
type: "raw",
|
|
418
|
+
value: ctx.data.nodeStr + `<h${ctx.data.firstHeadingDepth} id="${ctx.data.firstHeadingId}">${ctx.data.firstHeading && ctx.textContent(ctx.data.firstHeading)}</h${ctx.data.firstHeadingDepth}>`
|
|
419
|
+
});
|
|
371
420
|
}
|
|
372
|
-
if (parent.type === "root") ctx.replaceNode(parent.children[ctx.data.firstHeadingIndex], {
|
|
373
|
-
type: "raw",
|
|
374
|
-
value: ctx.data.nodeStr + `<h${ctx.data.firstHeadingDepth} id="${ctx.data.firstHeadingId}">${ctx.data.firstHeading && ctx.textContent(ctx.data.firstHeading)}</h${ctx.data.firstHeadingDepth}>`
|
|
375
|
-
});
|
|
376
421
|
} catch (err) {
|
|
377
422
|
throw err;
|
|
378
423
|
}
|
|
@@ -380,5 +425,20 @@ function prettyToc(option) {
|
|
|
380
425
|
}]
|
|
381
426
|
});
|
|
382
427
|
}
|
|
428
|
+
/**
|
|
429
|
+
*
|
|
430
|
+
* @param node 遍历的当前节点
|
|
431
|
+
* @param opt 传入插件的配置
|
|
432
|
+
* @returns 所有节点的html内容字符串
|
|
433
|
+
*/
|
|
434
|
+
function concatNodeStr(node, opt) {
|
|
435
|
+
let childNodeStr = "";
|
|
436
|
+
if (node.children.length > 0) {
|
|
437
|
+
for (let i = 0; i < node.children.length; i++) childNodeStr += concatNodeStr(node.children[i], opt);
|
|
438
|
+
childNodeStr = `<ul class="${opt.class?.ul ?? ""}" style="${opt.style?.ul ?? ""}">${childNodeStr}</ul>`;
|
|
439
|
+
return node.content + childNodeStr;
|
|
440
|
+
}
|
|
441
|
+
return node.content;
|
|
442
|
+
}
|
|
383
443
|
//#endregion
|
|
384
444
|
export { prettyToc as default };
|