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