vue-markdown-design 0.3.1 → 0.3.2
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 +5 -1
- package/README.zh-CN.md +4 -0
- package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/asyncToGenerator.mjs +27 -0
- package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/defineProperty.mjs +12 -0
- package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectSpread2.mjs +25 -0
- package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectWithoutProperties.mjs +13 -0
- package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectWithoutPropertiesLoose.mjs +12 -0
- package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/toPrimitive.mjs +14 -0
- package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/toPropertyKey.mjs +9 -0
- package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/typeof.mjs +11 -0
- package/es/hooks/use-element/index.mjs +21 -18
- package/es/hooks/use-scroll-element/index.mjs +44 -45
- package/es/index.mjs +7 -18
- package/es/markdown/Markdown.mjs +221 -316
- package/es/markdown/index.css +3 -0
- package/es/markdown/index2.mjs +6 -0
- package/es/render/Render.mjs +143 -173
- package/es/render/index.css +37 -21
- package/es/render/index2.mjs +6 -0
- package/es/search/Search.mjs +236 -285
- package/es/search/index.css +4 -0
- package/es/search/index2.mjs +6 -0
- package/es/sticky/Sticky.mjs +66 -99
- package/es/sticky/index2.mjs +6 -0
- package/es/toc/Toc.mjs +253 -347
- package/es/toc/index2.mjs +6 -0
- package/es/utils/constant.mjs +4 -4
- package/es/utils/dom.mjs +44 -68
- package/es/utils/format.mjs +33 -39
- package/es/utils/functions.mjs +5 -5
- package/es/utils/vue.mjs +10 -32
- package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/asyncToGenerator.js +27 -0
- package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/defineProperty.js +12 -0
- package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectSpread2.js +25 -0
- package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectWithoutProperties.js +13 -0
- package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectWithoutPropertiesLoose.js +12 -0
- package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/toPrimitive.js +14 -0
- package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/toPropertyKey.js +9 -0
- package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/typeof.js +16 -0
- package/lib/_virtual/_rolldown/runtime.js +23 -0
- package/lib/hooks/use-element/index.js +23 -19
- package/lib/hooks/use-scroll-element/index.js +45 -45
- package/lib/index.js +24 -22
- package/lib/markdown/Markdown.js +224 -317
- package/lib/markdown/index.css +3 -0
- package/lib/markdown/index2.js +6 -0
- package/lib/render/Render.js +152 -176
- package/lib/render/index.css +37 -21
- package/lib/render/index2.js +6 -0
- package/lib/search/Search.js +242 -287
- package/lib/search/index.css +4 -0
- package/lib/search/index2.js +6 -0
- package/lib/sticky/Sticky.js +68 -99
- package/lib/sticky/index2.js +6 -0
- package/lib/toc/Toc.js +259 -349
- package/lib/toc/index2.js +6 -0
- package/lib/utils/constant.js +3 -3
- package/lib/utils/dom.js +46 -69
- package/lib/utils/format.js +33 -33
- package/lib/utils/functions.js +4 -4
- package/lib/utils/vue.js +10 -31
- package/package.json +4 -9
- package/es/markdown/index.mjs +0 -9
- package/es/render/index.mjs +0 -9
- package/es/search/index.mjs +0 -9
- package/es/sticky/index.mjs +0 -7
- package/es/toc/index.mjs +0 -9
- package/lib/markdown/index.js +0 -8
- package/lib/render/index.js +0 -8
- package/lib/search/index.js +0 -8
- package/lib/sticky/index.js +0 -7
- package/lib/toc/index.js +0 -8
package/es/toc/Toc.mjs
CHANGED
|
@@ -1,354 +1,260 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __async = (__this, __arguments, generator) => {
|
|
21
|
-
return new Promise((resolve, reject) => {
|
|
22
|
-
var fulfilled = (value) => {
|
|
23
|
-
try {
|
|
24
|
-
step(generator.next(value));
|
|
25
|
-
} catch (e) {
|
|
26
|
-
reject(e);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
var rejected = (value) => {
|
|
30
|
-
try {
|
|
31
|
-
step(generator.throw(value));
|
|
32
|
-
} catch (e) {
|
|
33
|
-
reject(e);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
import { defineComponent, computed, watchEffect, ref, watch, nextTick, createVNode, mergeProps } from "vue";
|
|
41
|
-
/* empty css */
|
|
42
|
-
import { headers } from "@markdown-design/markdown-it-headers";
|
|
43
|
-
import { isString, isUndefined, isObject, chain, clamp, range, inRange, lte, throttle, isEqual } from "lodash";
|
|
44
|
-
import { createNamespace, computeOffset } from "../utils/format.mjs";
|
|
1
|
+
import { _objectSpread2 } from "../_virtual/_@oxc-project_runtime@0.115.0/helpers/objectSpread2.mjs";
|
|
2
|
+
import { computeOffset, createNamespace } from "../utils/format.mjs";
|
|
3
|
+
import { DATA_ANCHOR } from "../utils/constant.mjs";
|
|
45
4
|
import { useElement } from "../hooks/use-element/index.mjs";
|
|
5
|
+
import { scrollToEl } from "../utils/dom.mjs";
|
|
6
|
+
import { _asyncToGenerator } from "../_virtual/_@oxc-project_runtime@0.115.0/helpers/asyncToGenerator.mjs";
|
|
7
|
+
import './index.css';/* empty css */
|
|
8
|
+
import { useScrollParent } from "../hooks/use-scroll-element/index.mjs";
|
|
9
|
+
import { chain, clamp, inRange, isEqual, isObject, isString, isUndefined, lte, range, throttle } from "lodash";
|
|
10
|
+
import { computed, createVNode, defineComponent, mergeProps, nextTick, ref, watch, watchEffect } from "vue";
|
|
46
11
|
import MarkdownIt from "markdown-it";
|
|
47
12
|
import { toValue, useEventListener } from "@vueuse/core";
|
|
48
13
|
import { compute } from "compute-scroll-into-view";
|
|
49
|
-
import {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
14
|
+
import { headers } from "@markdown-design/markdown-it-headers";
|
|
15
|
+
//#region src/toc/Toc.tsx
|
|
16
|
+
var START_LEVEL = 1;
|
|
17
|
+
var END_LEVEL = 6;
|
|
18
|
+
var { name, addPrefix } = createNamespace("toc");
|
|
19
|
+
var tocProps = {
|
|
20
|
+
startLevel: {
|
|
21
|
+
type: [Number, String],
|
|
22
|
+
default: START_LEVEL
|
|
23
|
+
},
|
|
24
|
+
endLevel: {
|
|
25
|
+
type: [Number, String],
|
|
26
|
+
default: END_LEVEL
|
|
27
|
+
},
|
|
28
|
+
orderedList: Boolean,
|
|
29
|
+
plainText: Boolean,
|
|
30
|
+
changeHash: {
|
|
31
|
+
type: Boolean,
|
|
32
|
+
default: true
|
|
33
|
+
},
|
|
34
|
+
target: [
|
|
35
|
+
String,
|
|
36
|
+
Object,
|
|
37
|
+
Function
|
|
38
|
+
],
|
|
39
|
+
offset: {
|
|
40
|
+
type: [
|
|
41
|
+
String,
|
|
42
|
+
Number,
|
|
43
|
+
Function
|
|
44
|
+
],
|
|
45
|
+
default: "start"
|
|
46
|
+
},
|
|
47
|
+
bound: {
|
|
48
|
+
type: Number,
|
|
49
|
+
default: 0
|
|
50
|
+
},
|
|
51
|
+
ignore: {
|
|
52
|
+
type: Array,
|
|
53
|
+
default: []
|
|
54
|
+
},
|
|
55
|
+
emptyText: {
|
|
56
|
+
type: String,
|
|
57
|
+
default: "No Data"
|
|
58
|
+
},
|
|
59
|
+
markdown: {
|
|
60
|
+
type: String,
|
|
61
|
+
default: ""
|
|
62
|
+
},
|
|
63
|
+
smooth: Boolean
|
|
95
64
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
65
|
+
var tocEmits = {
|
|
66
|
+
click: (payload) => isObject(payload),
|
|
67
|
+
change: (payload) => isString(payload) || isUndefined(payload)
|
|
99
68
|
};
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
const renderItem = () => {
|
|
290
|
-
const {
|
|
291
|
-
orderedList
|
|
292
|
-
} = props;
|
|
293
|
-
const [startLevel, endLevel] = runtimeLevel.value;
|
|
294
|
-
const orderCounter = new Array(endLevel - startLevel + 1).fill(0);
|
|
295
|
-
return listData.value.map((item) => {
|
|
296
|
-
const {
|
|
297
|
-
level,
|
|
298
|
-
text,
|
|
299
|
-
id,
|
|
300
|
-
top
|
|
301
|
-
} = item;
|
|
302
|
-
const relativeLevel = level - startLevel;
|
|
303
|
-
orderedList && orderCounter.fill(0, relativeLevel + 1);
|
|
304
|
-
const attrs = {
|
|
305
|
-
class: addPrefix("__text"),
|
|
306
|
-
style: {
|
|
307
|
-
paddingLeft: `${relativeLevel}em`
|
|
308
|
-
}
|
|
309
|
-
};
|
|
310
|
-
const plainText = orderedList ? `${++orderCounter[relativeLevel]}. ${text}` : text;
|
|
311
|
-
const isActive = activeId.value === id;
|
|
312
|
-
const tocItem = {
|
|
313
|
-
id,
|
|
314
|
-
top,
|
|
315
|
-
level,
|
|
316
|
-
relativeLevel,
|
|
317
|
-
isActive,
|
|
318
|
-
text: plainText
|
|
319
|
-
};
|
|
320
|
-
const itemContent = slots.item ? slots.item(tocItem) : plainText;
|
|
321
|
-
const className = addPrefix("__item");
|
|
322
|
-
if (isPlainText.value || !id) {
|
|
323
|
-
return createVNode("li", {
|
|
324
|
-
"class": className
|
|
325
|
-
}, [createVNode("span", attrs, [itemContent])]);
|
|
326
|
-
}
|
|
327
|
-
return createVNode("li", {
|
|
328
|
-
"class": [className, {
|
|
329
|
-
[addPrefix("__item--active")]: isActive
|
|
330
|
-
}]
|
|
331
|
-
}, [createVNode("a", mergeProps(attrs, {
|
|
332
|
-
"href": `#${id}`,
|
|
333
|
-
"onClick": (e) => onClick(tocItem, e)
|
|
334
|
-
}), [itemContent])]);
|
|
335
|
-
});
|
|
336
|
-
};
|
|
337
|
-
expose({
|
|
338
|
-
refresh,
|
|
339
|
-
scrollTo
|
|
340
|
-
});
|
|
341
|
-
return () => createVNode("nav", {
|
|
342
|
-
"class": name
|
|
343
|
-
}, [listData.value.length ? createVNode("ul", {
|
|
344
|
-
"class": [addPrefix("__list"), addPrefix("--padding")]
|
|
345
|
-
}, [renderItem()]) : createVNode("div", {
|
|
346
|
-
"class": [addPrefix("__empty"), addPrefix("--padding")]
|
|
347
|
-
}, [props.emptyText])]);
|
|
348
|
-
}
|
|
69
|
+
var Toc_default = /* @__PURE__ */ defineComponent({
|
|
70
|
+
name,
|
|
71
|
+
props: tocProps,
|
|
72
|
+
emits: tocEmits,
|
|
73
|
+
setup(props, { emit, expose, slots }) {
|
|
74
|
+
const target = computed(() => toValue(props.target));
|
|
75
|
+
const { targetEl } = useElement(() => target.value || document.documentElement);
|
|
76
|
+
const levelWithNum = computed(() => {
|
|
77
|
+
const { startLevel, endLevel, ignore } = props;
|
|
78
|
+
return chain([startLevel, endLevel]).map((level) => clamp(Number(level), START_LEVEL, END_LEVEL)).thru(([start, end]) => range(start, end + 1)).difference(ignore.map(Number)).value();
|
|
79
|
+
});
|
|
80
|
+
watchEffect(() => {
|
|
81
|
+
const { startLevel, endLevel } = props;
|
|
82
|
+
const levels = [startLevel, endLevel].map(Number);
|
|
83
|
+
if (!levels.every((level) => inRange(level, START_LEVEL, END_LEVEL + 1))) console.warn("[vue-markdown-design] The start-level or end-level is outside the valid range.");
|
|
84
|
+
if (!lte(...levels)) console.warn("[vue-markdown-design] The start-level must be less than the end-level.");
|
|
85
|
+
});
|
|
86
|
+
const md = new MarkdownIt({ html: true });
|
|
87
|
+
md.use(headers);
|
|
88
|
+
const mdToc = computed(() => {
|
|
89
|
+
const env = {};
|
|
90
|
+
md.render(props.markdown, env);
|
|
91
|
+
const { headings = [] } = env;
|
|
92
|
+
return headings.filter((item) => levelWithNum.value.includes(item.level));
|
|
93
|
+
});
|
|
94
|
+
const isMd = computed(() => !target.value && !!props.markdown);
|
|
95
|
+
const isPlainText = computed(() => isMd.value || props.plainText);
|
|
96
|
+
const headings = ref([]);
|
|
97
|
+
const setHeading = () => {
|
|
98
|
+
var _targetEl$value;
|
|
99
|
+
if (!levelWithNum.value.length) {
|
|
100
|
+
headings.value = [];
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const selectors = levelWithNum.value.map((level) => `h${level}`).join(",");
|
|
104
|
+
const headingEl = (_targetEl$value = targetEl.value) === null || _targetEl$value === void 0 ? void 0 : _targetEl$value.querySelectorAll(selectors);
|
|
105
|
+
const newHeadings = Array.from(headingEl || []);
|
|
106
|
+
if (isEqual(headings.value, newHeadings)) return;
|
|
107
|
+
headings.value = newHeadings;
|
|
108
|
+
};
|
|
109
|
+
const offsetResult = computed(() => computeOffset(props.offset));
|
|
110
|
+
const topMap = ref(/* @__PURE__ */ new Map());
|
|
111
|
+
const setTop = () => {
|
|
112
|
+
const { block, getOffset } = offsetResult.value;
|
|
113
|
+
headings.value.forEach((heading) => {
|
|
114
|
+
const [scrollAction] = compute(heading, { block });
|
|
115
|
+
const curOffset = getOffset(scrollAction, true);
|
|
116
|
+
const val = scrollAction.top - curOffset - props.bound;
|
|
117
|
+
topMap.value.set(heading.id, Math.floor(val));
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
const setToc = () => {
|
|
121
|
+
if (isMd.value || !targetEl.value) return;
|
|
122
|
+
setHeading();
|
|
123
|
+
setTop();
|
|
124
|
+
};
|
|
125
|
+
watchEffect(setToc);
|
|
126
|
+
const refresh = function() {
|
|
127
|
+
var _ref = _asyncToGenerator(function* () {
|
|
128
|
+
headings.value = [];
|
|
129
|
+
yield nextTick();
|
|
130
|
+
setToc();
|
|
131
|
+
});
|
|
132
|
+
return function refresh() {
|
|
133
|
+
return _ref.apply(this, arguments);
|
|
134
|
+
};
|
|
135
|
+
}();
|
|
136
|
+
watch(() => {
|
|
137
|
+
var _target$value;
|
|
138
|
+
return (_target$value = target.value) === null || _target$value === void 0 ? void 0 : _target$value.htmlStr;
|
|
139
|
+
}, refresh);
|
|
140
|
+
const getText = (el) => {
|
|
141
|
+
const node = el.cloneNode(true);
|
|
142
|
+
node.querySelectorAll(`[${DATA_ANCHOR}]`).forEach((item) => node.removeChild(item));
|
|
143
|
+
return node.innerText.trim();
|
|
144
|
+
};
|
|
145
|
+
const listData = computed(() => {
|
|
146
|
+
if (isMd.value) return mdToc.value;
|
|
147
|
+
return headings.value.map((heading) => {
|
|
148
|
+
const { id, tagName } = heading;
|
|
149
|
+
return {
|
|
150
|
+
id,
|
|
151
|
+
text: getText(heading),
|
|
152
|
+
level: Number(tagName[1]),
|
|
153
|
+
top: topMap.value.get(id)
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
const runtimeLevel = computed(() => {
|
|
158
|
+
if (!listData.value.length) return [0, 0];
|
|
159
|
+
const level = listData.value.map((item) => item.level);
|
|
160
|
+
return [Math.min(...level), Math.max(...level)];
|
|
161
|
+
});
|
|
162
|
+
const wait = 16;
|
|
163
|
+
let ScrollStatus = /* @__PURE__ */ function(ScrollStatus) {
|
|
164
|
+
ScrollStatus[ScrollStatus["Start"] = 0] = "Start";
|
|
165
|
+
ScrollStatus[ScrollStatus["Update"] = 1] = "Update";
|
|
166
|
+
ScrollStatus[ScrollStatus["Done"] = 2] = "Done";
|
|
167
|
+
return ScrollStatus;
|
|
168
|
+
}({});
|
|
169
|
+
let scrollStatus = ScrollStatus.Done;
|
|
170
|
+
const setScrollStatus = (key) => {
|
|
171
|
+
const status = ScrollStatus[key];
|
|
172
|
+
if (scrollStatus !== status) scrollStatus = status;
|
|
173
|
+
};
|
|
174
|
+
const { scrollEl } = useScrollParent(targetEl);
|
|
175
|
+
const resolvedScrollEl = computed(() => {
|
|
176
|
+
var _targetEl$value2;
|
|
177
|
+
return scrollEl.value || ((_targetEl$value2 = targetEl.value) === null || _targetEl$value2 === void 0 ? void 0 : _targetEl$value2.ownerDocument.documentElement);
|
|
178
|
+
});
|
|
179
|
+
const activeId = ref();
|
|
180
|
+
const onChange = () => {
|
|
181
|
+
if (scrollStatus === ScrollStatus.Done) emit("change", activeId.value);
|
|
182
|
+
};
|
|
183
|
+
const onScroll = throttle(() => {
|
|
184
|
+
var _resolvedScrollEl$val;
|
|
185
|
+
if (scrollStatus === ScrollStatus.Start || isPlainText.value) return;
|
|
186
|
+
setToc();
|
|
187
|
+
const scrollTop = Math.floor(((_resolvedScrollEl$val = resolvedScrollEl.value) === null || _resolvedScrollEl$val === void 0 ? void 0 : _resolvedScrollEl$val.scrollTop) || 0);
|
|
188
|
+
const current = listData.value.map((item) => _objectSpread2(_objectSpread2({}, item), {}, { top: item.top || 0 })).sort((a, b) => a.top - b.top).find((item, index, arr) => {
|
|
189
|
+
const next = arr[index + 1];
|
|
190
|
+
return item.top <= scrollTop && (!next || next.top > scrollTop) && !(index === 0 && scrollTop === 0);
|
|
191
|
+
});
|
|
192
|
+
const id = (current === null || current === void 0 ? void 0 : current.id) || void 0;
|
|
193
|
+
if (activeId.value === id) return;
|
|
194
|
+
activeId.value = id;
|
|
195
|
+
onChange();
|
|
196
|
+
}, wait);
|
|
197
|
+
watchEffect(() => nextTick().then(onScroll));
|
|
198
|
+
useEventListener(() => scrollEl.value || window, "scroll", onScroll);
|
|
199
|
+
const scrollTo = (href) => {
|
|
200
|
+
var _targetEl$value3;
|
|
201
|
+
if (!href) return console.warn("[vue-markdown-design] Href has no value.");
|
|
202
|
+
const headingEl = (_targetEl$value3 = targetEl.value) === null || _targetEl$value3 === void 0 ? void 0 : _targetEl$value3.querySelector(`[id='${href}']`);
|
|
203
|
+
if (!headingEl) return console.warn("[vue-markdown-design] The provided href failed to query the DOM.");
|
|
204
|
+
const { offset, smooth } = props;
|
|
205
|
+
scrollToEl(headingEl, {
|
|
206
|
+
offset,
|
|
207
|
+
smooth,
|
|
208
|
+
onPlay: () => setScrollStatus("Start"),
|
|
209
|
+
onUpdate: () => setScrollStatus("Update"),
|
|
210
|
+
onComplete: () => setTimeout(() => {
|
|
211
|
+
setScrollStatus("Done");
|
|
212
|
+
onChange();
|
|
213
|
+
}, wait + 4)
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
const onClick = (tocItem, e) => {
|
|
217
|
+
if (!props.changeHash) e.preventDefault();
|
|
218
|
+
scrollTo(tocItem.id);
|
|
219
|
+
emit("click", tocItem);
|
|
220
|
+
};
|
|
221
|
+
const renderItem = () => {
|
|
222
|
+
const { orderedList } = props;
|
|
223
|
+
const [startLevel, endLevel] = runtimeLevel.value;
|
|
224
|
+
const orderCounter = new Array(endLevel - startLevel + 1).fill(0);
|
|
225
|
+
return listData.value.map((item) => {
|
|
226
|
+
const { level, text, id, top } = item;
|
|
227
|
+
const relativeLevel = level - startLevel;
|
|
228
|
+
orderedList && orderCounter.fill(0, relativeLevel + 1);
|
|
229
|
+
const attrs = {
|
|
230
|
+
class: addPrefix("__text"),
|
|
231
|
+
style: { paddingLeft: `${relativeLevel}em` }
|
|
232
|
+
};
|
|
233
|
+
const plainText = orderedList ? `${++orderCounter[relativeLevel]}. ${text}` : text;
|
|
234
|
+
const isActive = activeId.value === id;
|
|
235
|
+
const tocItem = {
|
|
236
|
+
id,
|
|
237
|
+
top,
|
|
238
|
+
level,
|
|
239
|
+
relativeLevel,
|
|
240
|
+
isActive,
|
|
241
|
+
text: plainText
|
|
242
|
+
};
|
|
243
|
+
const itemContent = slots.item ? slots.item(tocItem) : plainText;
|
|
244
|
+
const className = addPrefix("__item");
|
|
245
|
+
if (isPlainText.value || !id) return createVNode("li", { "class": className }, [createVNode("span", attrs, [itemContent])]);
|
|
246
|
+
return createVNode("li", { "class": [className, { [addPrefix("__item--active")]: isActive }] }, [createVNode("a", mergeProps(attrs, {
|
|
247
|
+
"href": `#${id}`,
|
|
248
|
+
"onClick": (e) => onClick(tocItem, e)
|
|
249
|
+
}), [itemContent])]);
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
expose({
|
|
253
|
+
refresh,
|
|
254
|
+
scrollTo
|
|
255
|
+
});
|
|
256
|
+
return () => createVNode("nav", { "class": name }, [listData.value.length ? createVNode("ul", { "class": [addPrefix("__list"), addPrefix("--padding")] }, [renderItem()]) : createVNode("div", { "class": [addPrefix("__empty"), addPrefix("--padding")] }, [props.emptyText])]);
|
|
257
|
+
}
|
|
349
258
|
});
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
tocEmits,
|
|
353
|
-
tocProps
|
|
354
|
-
};
|
|
259
|
+
//#endregion
|
|
260
|
+
export { Toc_default as default, tocEmits, tocProps };
|
package/es/utils/constant.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
1
|
+
//#region src/utils/constant.ts
|
|
2
|
+
var DATA_ANCHOR = "data-vmd-anchor";
|
|
3
|
+
//#endregion
|
|
4
|
+
export { DATA_ANCHOR };
|