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.
Files changed (72) hide show
  1. package/README.md +5 -1
  2. package/README.zh-CN.md +4 -0
  3. package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/asyncToGenerator.mjs +27 -0
  4. package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/defineProperty.mjs +12 -0
  5. package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectSpread2.mjs +25 -0
  6. package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectWithoutProperties.mjs +13 -0
  7. package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectWithoutPropertiesLoose.mjs +12 -0
  8. package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/toPrimitive.mjs +14 -0
  9. package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/toPropertyKey.mjs +9 -0
  10. package/es/_virtual/_@oxc-project_runtime@0.115.0/helpers/typeof.mjs +11 -0
  11. package/es/hooks/use-element/index.mjs +21 -18
  12. package/es/hooks/use-scroll-element/index.mjs +44 -45
  13. package/es/index.mjs +7 -18
  14. package/es/markdown/Markdown.mjs +221 -316
  15. package/es/markdown/index.css +3 -0
  16. package/es/markdown/index2.mjs +6 -0
  17. package/es/render/Render.mjs +143 -173
  18. package/es/render/index.css +37 -21
  19. package/es/render/index2.mjs +6 -0
  20. package/es/search/Search.mjs +236 -285
  21. package/es/search/index.css +4 -0
  22. package/es/search/index2.mjs +6 -0
  23. package/es/sticky/Sticky.mjs +66 -99
  24. package/es/sticky/index2.mjs +6 -0
  25. package/es/toc/Toc.mjs +253 -347
  26. package/es/toc/index2.mjs +6 -0
  27. package/es/utils/constant.mjs +4 -4
  28. package/es/utils/dom.mjs +44 -68
  29. package/es/utils/format.mjs +33 -39
  30. package/es/utils/functions.mjs +5 -5
  31. package/es/utils/vue.mjs +10 -32
  32. package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/asyncToGenerator.js +27 -0
  33. package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/defineProperty.js +12 -0
  34. package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectSpread2.js +25 -0
  35. package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectWithoutProperties.js +13 -0
  36. package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/objectWithoutPropertiesLoose.js +12 -0
  37. package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/toPrimitive.js +14 -0
  38. package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/toPropertyKey.js +9 -0
  39. package/lib/_virtual/_@oxc-project_runtime@0.115.0/helpers/typeof.js +16 -0
  40. package/lib/_virtual/_rolldown/runtime.js +23 -0
  41. package/lib/hooks/use-element/index.js +23 -19
  42. package/lib/hooks/use-scroll-element/index.js +45 -45
  43. package/lib/index.js +24 -22
  44. package/lib/markdown/Markdown.js +224 -317
  45. package/lib/markdown/index.css +3 -0
  46. package/lib/markdown/index2.js +6 -0
  47. package/lib/render/Render.js +152 -176
  48. package/lib/render/index.css +37 -21
  49. package/lib/render/index2.js +6 -0
  50. package/lib/search/Search.js +242 -287
  51. package/lib/search/index.css +4 -0
  52. package/lib/search/index2.js +6 -0
  53. package/lib/sticky/Sticky.js +68 -99
  54. package/lib/sticky/index2.js +6 -0
  55. package/lib/toc/Toc.js +259 -349
  56. package/lib/toc/index2.js +6 -0
  57. package/lib/utils/constant.js +3 -3
  58. package/lib/utils/dom.js +46 -69
  59. package/lib/utils/format.js +33 -33
  60. package/lib/utils/functions.js +4 -4
  61. package/lib/utils/vue.js +10 -31
  62. package/package.json +4 -9
  63. package/es/markdown/index.mjs +0 -9
  64. package/es/render/index.mjs +0 -9
  65. package/es/search/index.mjs +0 -9
  66. package/es/sticky/index.mjs +0 -7
  67. package/es/toc/index.mjs +0 -9
  68. package/lib/markdown/index.js +0 -8
  69. package/lib/render/index.js +0 -8
  70. package/lib/search/index.js +0 -8
  71. package/lib/sticky/index.js +0 -7
  72. package/lib/toc/index.js +0 -8
package/es/toc/Toc.mjs CHANGED
@@ -1,354 +1,260 @@
1
- import './index.css';var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
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 { useScrollParent } from "../hooks/use-scroll-element/index.mjs";
50
- import { scrollToEl } from "../utils/dom.mjs";
51
- import { DATA_ANCHOR } from "../utils/constant.mjs";
52
- const START_LEVEL = 1;
53
- const END_LEVEL = 6;
54
- const {
55
- name,
56
- addPrefix
57
- } = createNamespace("toc");
58
- const tocProps = {
59
- startLevel: {
60
- type: [Number, String],
61
- default: START_LEVEL
62
- },
63
- endLevel: {
64
- type: [Number, String],
65
- default: END_LEVEL
66
- },
67
- orderedList: Boolean,
68
- plainText: Boolean,
69
- changeHash: {
70
- type: Boolean,
71
- default: true
72
- },
73
- target: [String, Object, Function],
74
- offset: {
75
- type: [String, Number, Function],
76
- default: "start"
77
- },
78
- bound: {
79
- type: Number,
80
- default: 0
81
- },
82
- ignore: {
83
- type: Array,
84
- default: []
85
- },
86
- emptyText: {
87
- type: String,
88
- default: "No Data"
89
- },
90
- markdown: {
91
- type: String,
92
- default: ""
93
- },
94
- smooth: Boolean
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
- const tocEmits = {
97
- click: (payload) => isObject(payload),
98
- change: (payload) => isString(payload) || isUndefined(payload)
65
+ var tocEmits = {
66
+ click: (payload) => isObject(payload),
67
+ change: (payload) => isString(payload) || isUndefined(payload)
99
68
  };
100
- const _Toc = /* @__PURE__ */ defineComponent({
101
- name,
102
- props: tocProps,
103
- emits: tocEmits,
104
- setup(props, {
105
- emit,
106
- expose,
107
- slots
108
- }) {
109
- const target = computed(() => toValue(props.target));
110
- const {
111
- targetEl
112
- } = useElement(() => target.value || document.documentElement);
113
- const levelWithNum = computed(() => {
114
- const {
115
- startLevel,
116
- endLevel,
117
- ignore
118
- } = props;
119
- 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();
120
- });
121
- watchEffect(() => {
122
- const {
123
- startLevel,
124
- endLevel
125
- } = props;
126
- const levels = [startLevel, endLevel].map(Number);
127
- if (!levels.every((level) => inRange(level, START_LEVEL, END_LEVEL + 1))) {
128
- console.warn("[vue-markdown-design] The start-level or end-level is outside the valid range.");
129
- }
130
- if (!lte(...levels)) {
131
- console.warn("[vue-markdown-design] The start-level must be less than the end-level.");
132
- }
133
- });
134
- const md = new MarkdownIt({
135
- html: true
136
- });
137
- md.use(headers);
138
- const mdToc = computed(() => {
139
- const env = {};
140
- md.render(props.markdown, env);
141
- const {
142
- headings: headings2 = []
143
- } = env;
144
- return headings2.filter((item) => levelWithNum.value.includes(item.level));
145
- });
146
- const isMd = computed(() => !target.value && !!props.markdown);
147
- const isPlainText = computed(() => isMd.value || props.plainText);
148
- const headings = ref([]);
149
- const setHeading = () => {
150
- var _a;
151
- if (!levelWithNum.value.length) {
152
- headings.value = [];
153
- return;
154
- }
155
- const selectors = levelWithNum.value.map((level) => `h${level}`).join(",");
156
- const headingEl = (_a = targetEl.value) == null ? void 0 : _a.querySelectorAll(selectors);
157
- const newHeadings = Array.from(headingEl || []);
158
- if (isEqual(headings.value, newHeadings)) return;
159
- headings.value = newHeadings;
160
- };
161
- const offsetResult = computed(() => computeOffset(props.offset));
162
- const topMap = ref(/* @__PURE__ */ new Map());
163
- const setTop = () => {
164
- const {
165
- block,
166
- getOffset
167
- } = offsetResult.value;
168
- headings.value.forEach((heading) => {
169
- const [scrollAction] = compute(heading, {
170
- block
171
- });
172
- const curOffset = getOffset(scrollAction, true);
173
- const val = scrollAction.top - curOffset - props.bound;
174
- topMap.value.set(heading.id, Math.floor(val));
175
- });
176
- };
177
- const setToc = () => {
178
- if (isMd.value || !targetEl.value) return;
179
- setHeading();
180
- setTop();
181
- };
182
- watchEffect(setToc);
183
- const refresh = () => __async(this, null, function* () {
184
- headings.value = [];
185
- yield nextTick();
186
- setToc();
187
- });
188
- watch(() => {
189
- var _a;
190
- return (_a = target.value) == null ? void 0 : _a.htmlStr;
191
- }, refresh);
192
- const getText = (el) => {
193
- const node = el.cloneNode(true);
194
- const anchors = node.querySelectorAll(`[${DATA_ANCHOR}]`);
195
- anchors.forEach((item) => node.removeChild(item));
196
- return node.innerText.trim();
197
- };
198
- const listData = computed(() => {
199
- if (isMd.value) return mdToc.value;
200
- return headings.value.map((heading) => {
201
- const {
202
- id,
203
- tagName
204
- } = heading;
205
- return {
206
- id,
207
- text: getText(heading),
208
- level: Number(tagName[1]),
209
- top: topMap.value.get(id)
210
- };
211
- });
212
- });
213
- const runtimeLevel = computed(() => {
214
- if (!listData.value.length) return [0, 0];
215
- const level = listData.value.map((item) => item.level);
216
- return [Math.min(...level), Math.max(...level)];
217
- });
218
- const wait = 16;
219
- let ScrollStatus = /* @__PURE__ */ function(ScrollStatus2) {
220
- ScrollStatus2[ScrollStatus2["Start"] = 0] = "Start";
221
- ScrollStatus2[ScrollStatus2["Update"] = 1] = "Update";
222
- ScrollStatus2[ScrollStatus2["Done"] = 2] = "Done";
223
- return ScrollStatus2;
224
- }({});
225
- let scrollStatus = ScrollStatus.Done;
226
- const setScrollStatus = (key) => {
227
- const status = ScrollStatus[key];
228
- if (scrollStatus !== status) {
229
- scrollStatus = status;
230
- }
231
- };
232
- const {
233
- scrollEl
234
- } = useScrollParent(targetEl);
235
- const resolvedScrollEl = computed(() => {
236
- var _a;
237
- return scrollEl.value || ((_a = targetEl.value) == null ? void 0 : _a.ownerDocument.documentElement);
238
- });
239
- const activeId = ref();
240
- const onChange = () => {
241
- if (scrollStatus === ScrollStatus.Done) {
242
- emit("change", activeId.value);
243
- }
244
- };
245
- const onScroll = throttle(() => {
246
- var _a;
247
- if (scrollStatus === ScrollStatus.Start || isPlainText.value) return;
248
- setToc();
249
- const scrollTop = Math.floor(((_a = resolvedScrollEl.value) == null ? void 0 : _a.scrollTop) || 0);
250
- const current = listData.value.map((item) => __spreadProps(__spreadValues({}, item), {
251
- top: item.top || 0
252
- })).sort((a, b) => a.top - b.top).find((item, index, arr) => {
253
- const next = arr[index + 1];
254
- return item.top <= scrollTop && (!next || next.top > scrollTop) && !(index === 0 && scrollTop === 0);
255
- });
256
- const id = (current == null ? void 0 : current.id) || void 0;
257
- if (activeId.value === id) return;
258
- activeId.value = id;
259
- onChange();
260
- }, wait);
261
- watchEffect(() => nextTick().then(onScroll));
262
- useEventListener(() => scrollEl.value || window, "scroll", onScroll);
263
- const scrollTo = (href) => {
264
- var _a;
265
- if (!href) return console.warn("[vue-markdown-design] Href has no value.");
266
- const headingEl = (_a = targetEl.value) == null ? void 0 : _a.querySelector(`[id='${href}']`);
267
- if (!headingEl) return console.warn("[vue-markdown-design] The provided href failed to query the DOM.");
268
- const {
269
- offset,
270
- smooth
271
- } = props;
272
- scrollToEl(headingEl, {
273
- offset,
274
- smooth,
275
- onPlay: () => setScrollStatus("Start"),
276
- onUpdate: () => setScrollStatus("Update"),
277
- onComplete: () => setTimeout(() => {
278
- setScrollStatus("Done");
279
- onChange();
280
- }, wait + 4)
281
- // 设置延迟可确保在 onScroll 后面再执行
282
- });
283
- };
284
- const onClick = (tocItem, e) => {
285
- if (!props.changeHash) e.preventDefault();
286
- scrollTo(tocItem.id);
287
- emit("click", tocItem);
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
- export {
351
- _Toc as default,
352
- tocEmits,
353
- tocProps
354
- };
259
+ //#endregion
260
+ export { Toc_default as default, tocEmits, tocProps };
@@ -0,0 +1,6 @@
1
+ import { withInstall } from "../utils/vue.mjs";
2
+ import Toc_default from "./Toc.mjs";
3
+ //#region src/toc/index.ts
4
+ var VmdToc = withInstall(Toc_default);
5
+ //#endregion
6
+ export { VmdToc as default };
@@ -1,4 +1,4 @@
1
- const DATA_ANCHOR = "data-vmd-anchor";
2
- export {
3
- DATA_ANCHOR
4
- };
1
+ //#region src/utils/constant.ts
2
+ var DATA_ANCHOR = "data-vmd-anchor";
3
+ //#endregion
4
+ export { DATA_ANCHOR };