@antdv-next/x-markdown 0.0.2 → 0.1.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/LICENSE +21 -0
- package/README.md +15 -1
- package/dist/XMarkdown/components/AnimationText.js +5 -0
- package/dist/XMarkdown/components/AnimationText.vue_vue_type_script_setup_true_lang.js +42 -0
- package/dist/XMarkdown/components/DebugPanel.js +5 -0
- package/dist/XMarkdown/components/DebugPanel.vue_vue_type_script_setup_true_lang.js +128 -0
- package/dist/XMarkdown/components/TailIndicator.js +5 -0
- package/dist/XMarkdown/components/TailIndicator.vue_vue_type_script_setup_true_lang.js +14 -0
- package/dist/XMarkdown/components/index.js +4 -0
- package/dist/XMarkdown/composables/index.d.ts +4 -0
- package/dist/XMarkdown/composables/index.js +5 -0
- package/dist/XMarkdown/composables/useParser.js +24 -0
- package/dist/XMarkdown/composables/useRenderer.js +24 -0
- package/dist/XMarkdown/composables/useStreaming.js +215 -0
- package/dist/XMarkdown/composables/useTail.js +25 -0
- package/dist/XMarkdown/core/Parser.js +162 -0
- package/dist/XMarkdown/core/VueRenderer.js +223 -0
- package/dist/XMarkdown/core/detectUnclosedComponentTags.js +141 -0
- package/dist/XMarkdown/core/index.d.ts +3 -0
- package/dist/XMarkdown/core/index.js +4 -0
- package/dist/XMarkdown/index.d.ts +14 -0
- package/dist/XMarkdown/index.js +13 -0
- package/dist/XMarkdown/index.vue_vue_type_script_setup_true_lang.js +135 -0
- package/dist/XMarkdown/index2.js +5 -0
- package/dist/XMarkdown/interface.js +14 -0
- package/dist/XMarkdown/utils/tail.js +9 -0
- package/dist/env.d.js +0 -0
- package/dist/index.js +12 -1137
- package/package.json +9 -8
- package/{dist → themes}/index.css +0 -45
- /package/{dist/plugins → plugins}/Latex/index.d.ts +0 -0
- /package/{dist/plugins → plugins}/Latex/index.js +0 -0
- /package/{dist/themes → themes}/dark.css +0 -0
- /package/{dist/themes → themes}/light.css +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Antdv Next
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -45,6 +45,8 @@ pnpm add @antdv-next/x-markdown
|
|
|
45
45
|
|
|
46
46
|
```vue
|
|
47
47
|
<script setup>
|
|
48
|
+
import "@antdv-next/x-markdown/themes/index.css";
|
|
49
|
+
import "@antdv-next/x-markdown/themes/light.css";
|
|
48
50
|
import { XMarkdown } from "@antdv-next/x-markdown";
|
|
49
51
|
|
|
50
52
|
const content = `
|
|
@@ -67,6 +69,8 @@ const content = `
|
|
|
67
69
|
|
|
68
70
|
```vue
|
|
69
71
|
<script setup>
|
|
72
|
+
import "@antdv-next/x-markdown/themes/index.css";
|
|
73
|
+
import "@antdv-next/x-markdown/themes/light.css";
|
|
70
74
|
import { ref } from "vue";
|
|
71
75
|
import { XMarkdown } from "@antdv-next/x-markdown";
|
|
72
76
|
|
|
@@ -88,6 +92,15 @@ function onChunk(chunk) {
|
|
|
88
92
|
</template>
|
|
89
93
|
```
|
|
90
94
|
|
|
95
|
+
## Styles
|
|
96
|
+
|
|
97
|
+
Breaking change: `@antdv-next/x-markdown` no longer imports styles automatically. Import the base styles manually, plus one optional theme stylesheet:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import "@antdv-next/x-markdown/themes/index.css";
|
|
101
|
+
import "@antdv-next/x-markdown/themes/light.css";
|
|
102
|
+
```
|
|
103
|
+
|
|
91
104
|
## API
|
|
92
105
|
|
|
93
106
|
### Props
|
|
@@ -132,10 +145,11 @@ function onChunk(chunk) {
|
|
|
132
145
|
|
|
133
146
|
## Themes
|
|
134
147
|
|
|
135
|
-
Import theme styles and set the theme class on the root:
|
|
148
|
+
Import base styles, theme styles, and set the theme class on the root:
|
|
136
149
|
|
|
137
150
|
```vue
|
|
138
151
|
<script setup>
|
|
152
|
+
import "@antdv-next/x-markdown/themes/index.css";
|
|
139
153
|
import "@antdv-next/x-markdown/themes/light.css";
|
|
140
154
|
</script>
|
|
141
155
|
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import AnimationText_vue_vue_type_script_setup_true_lang_default from "./AnimationText.vue_vue_type_script_setup_true_lang.js";
|
|
2
|
+
//#region src/XMarkdown/components/AnimationText.vue
|
|
3
|
+
var AnimationText_default = AnimationText_vue_vue_type_script_setup_true_lang_default;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { AnimationText_default as default };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Fragment, computed, createElementBlock, defineComponent, normalizeStyle, openBlock, ref, renderList, toDisplayString, watch } from "vue";
|
|
2
|
+
//#region src/XMarkdown/components/AnimationText.vue?vue&type=script&setup=true&lang.ts
|
|
3
|
+
var AnimationText_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
|
|
4
|
+
__name: "AnimationText",
|
|
5
|
+
props: {
|
|
6
|
+
text: {},
|
|
7
|
+
fadeDuration: { default: 200 },
|
|
8
|
+
easing: { default: "ease-in-out" }
|
|
9
|
+
},
|
|
10
|
+
setup(__props) {
|
|
11
|
+
const props = __props;
|
|
12
|
+
const chunks = ref([]);
|
|
13
|
+
const previousText = ref("");
|
|
14
|
+
function updateChunks(nextText) {
|
|
15
|
+
if (nextText === previousText.value) return;
|
|
16
|
+
if (!Boolean(previousText.value && nextText.startsWith(previousText.value))) {
|
|
17
|
+
chunks.value = [nextText];
|
|
18
|
+
previousText.value = nextText;
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const delta = nextText.slice(previousText.value.length);
|
|
22
|
+
if (!delta) return;
|
|
23
|
+
chunks.value = [...chunks.value, delta];
|
|
24
|
+
previousText.value = nextText;
|
|
25
|
+
}
|
|
26
|
+
watch(() => props.text, updateChunks, { immediate: true });
|
|
27
|
+
const animationStyle = computed(() => ({
|
|
28
|
+
animation: `x-markdown-fade-in ${props.fadeDuration}ms ${props.easing} forwards`,
|
|
29
|
+
color: "inherit"
|
|
30
|
+
}));
|
|
31
|
+
return (_ctx, _cache) => {
|
|
32
|
+
return openBlock(true), createElementBlock(Fragment, null, renderList(chunks.value, (chunk, index) => {
|
|
33
|
+
return openBlock(), createElementBlock("span", {
|
|
34
|
+
key: `animation-text-${index}`,
|
|
35
|
+
style: normalizeStyle(animationStyle.value)
|
|
36
|
+
}, toDisplayString(chunk), 5);
|
|
37
|
+
}), 128);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
//#endregion
|
|
42
|
+
export { AnimationText_vue_vue_type_script_setup_true_lang_default as default };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import DebugPanel_vue_vue_type_script_setup_true_lang_default from "./DebugPanel.vue_vue_type_script_setup_true_lang.js";
|
|
2
|
+
//#region src/XMarkdown/components/DebugPanel.vue
|
|
3
|
+
var DebugPanel_default = DebugPanel_vue_vue_type_script_setup_true_lang_default;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { DebugPanel_default as default };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { createElementBlock, createElementVNode, defineComponent, normalizeClass, normalizeStyle, onMounted, onUnmounted, openBlock, ref, toDisplayString } from "vue";
|
|
2
|
+
//#region src/XMarkdown/components/DebugPanel.vue?vue&type=script&setup=true&lang.ts
|
|
3
|
+
var _hoisted_1 = { class: "xmd-debug-content" };
|
|
4
|
+
var _hoisted_2 = { class: "xmd-debug-stat" };
|
|
5
|
+
var _hoisted_3 = { class: "xmd-debug-stat" };
|
|
6
|
+
var _hoisted_4 = {
|
|
7
|
+
class: "xmd-debug-chart",
|
|
8
|
+
viewBox: "0 0 200 60"
|
|
9
|
+
};
|
|
10
|
+
var _hoisted_5 = ["d"];
|
|
11
|
+
var maxSnapshots = 60;
|
|
12
|
+
var DebugPanel_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
|
|
13
|
+
__name: "DebugPanel",
|
|
14
|
+
props: { className: {} },
|
|
15
|
+
setup(__props) {
|
|
16
|
+
const isDragging = ref(false);
|
|
17
|
+
const position = ref({
|
|
18
|
+
x: 20,
|
|
19
|
+
y: 20
|
|
20
|
+
});
|
|
21
|
+
const dragOffset = ref({
|
|
22
|
+
x: 0,
|
|
23
|
+
y: 0
|
|
24
|
+
});
|
|
25
|
+
const fps = ref(0);
|
|
26
|
+
const memory = ref(0);
|
|
27
|
+
const snapshots = ref([]);
|
|
28
|
+
let animationFrameId = null;
|
|
29
|
+
let lastFrameTime = 0;
|
|
30
|
+
let frameCount = 0;
|
|
31
|
+
function startMonitoring() {
|
|
32
|
+
lastFrameTime = performance.now();
|
|
33
|
+
frameCount = 0;
|
|
34
|
+
function monitor() {
|
|
35
|
+
frameCount++;
|
|
36
|
+
const now = performance.now();
|
|
37
|
+
const elapsed = now - lastFrameTime;
|
|
38
|
+
if (elapsed >= 1e3) {
|
|
39
|
+
fps.value = Math.round(frameCount * 1e3 / elapsed);
|
|
40
|
+
frameCount = 0;
|
|
41
|
+
lastFrameTime = now;
|
|
42
|
+
if ("memory" in performance) {
|
|
43
|
+
const memInfo = performance.memory;
|
|
44
|
+
memory.value = memInfo ? Math.round(memInfo.usedJSHeapSize / 1024 / 1024) : 0;
|
|
45
|
+
}
|
|
46
|
+
snapshots.value.push({
|
|
47
|
+
fps: fps.value,
|
|
48
|
+
memory: memory.value,
|
|
49
|
+
timestamp: Date.now()
|
|
50
|
+
});
|
|
51
|
+
if (snapshots.value.length > maxSnapshots) snapshots.value.shift();
|
|
52
|
+
}
|
|
53
|
+
animationFrameId = requestAnimationFrame(monitor);
|
|
54
|
+
}
|
|
55
|
+
animationFrameId = requestAnimationFrame(monitor);
|
|
56
|
+
}
|
|
57
|
+
function stopMonitoring() {
|
|
58
|
+
if (animationFrameId !== null) {
|
|
59
|
+
cancelAnimationFrame(animationFrameId);
|
|
60
|
+
animationFrameId = null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function handleMouseDown(e) {
|
|
64
|
+
isDragging.value = true;
|
|
65
|
+
dragOffset.value = {
|
|
66
|
+
x: e.clientX - position.value.x,
|
|
67
|
+
y: e.clientY - position.value.y
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function handleMouseMove(e) {
|
|
71
|
+
if (!isDragging.value) return;
|
|
72
|
+
position.value = {
|
|
73
|
+
x: e.clientX - dragOffset.value.x,
|
|
74
|
+
y: e.clientY - dragOffset.value.y
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function handleMouseUp() {
|
|
78
|
+
isDragging.value = false;
|
|
79
|
+
}
|
|
80
|
+
function getFpsColor(fpsValue) {
|
|
81
|
+
if (fpsValue >= 55) return "#52c41a";
|
|
82
|
+
if (fpsValue >= 40) return "#faad14";
|
|
83
|
+
return "#ff4d4f";
|
|
84
|
+
}
|
|
85
|
+
function getChartPath() {
|
|
86
|
+
if (snapshots.value.length < 2) return "";
|
|
87
|
+
const width = 200;
|
|
88
|
+
const height = 60;
|
|
89
|
+
const maxFps = 60;
|
|
90
|
+
return `M ${snapshots.value.map((s, i) => {
|
|
91
|
+
return `${i / (maxSnapshots - 1) * width},${height - s.fps / maxFps * height}`;
|
|
92
|
+
}).join(" L ")}`;
|
|
93
|
+
}
|
|
94
|
+
onMounted(() => {
|
|
95
|
+
startMonitoring();
|
|
96
|
+
document.addEventListener("mousemove", handleMouseMove);
|
|
97
|
+
document.addEventListener("mouseup", handleMouseUp);
|
|
98
|
+
});
|
|
99
|
+
onUnmounted(() => {
|
|
100
|
+
stopMonitoring();
|
|
101
|
+
document.removeEventListener("mousemove", handleMouseMove);
|
|
102
|
+
document.removeEventListener("mouseup", handleMouseUp);
|
|
103
|
+
});
|
|
104
|
+
return (_ctx, _cache) => {
|
|
105
|
+
return openBlock(), createElementBlock("div", {
|
|
106
|
+
class: normalizeClass(["xmd-debug-panel", __props.className]),
|
|
107
|
+
style: normalizeStyle({
|
|
108
|
+
left: `${position.value.x}px`,
|
|
109
|
+
top: `${position.value.y}px`
|
|
110
|
+
})
|
|
111
|
+
}, [createElementVNode("div", {
|
|
112
|
+
class: "xmd-debug-header",
|
|
113
|
+
onMousedown: handleMouseDown
|
|
114
|
+
}, [..._cache[0] || (_cache[0] = [createElementVNode("span", null, "Debug Panel", -1)])], 32), createElementVNode("div", _hoisted_1, [
|
|
115
|
+
createElementVNode("div", _hoisted_2, [_cache[1] || (_cache[1] = createElementVNode("span", { class: "xmd-debug-label" }, "FPS:", -1)), createElementVNode("span", { style: normalizeStyle({ color: getFpsColor(fps.value) }) }, toDisplayString(fps.value), 5)]),
|
|
116
|
+
createElementVNode("div", _hoisted_3, [_cache[2] || (_cache[2] = createElementVNode("span", { class: "xmd-debug-label" }, "Memory:", -1)), createElementVNode("span", null, toDisplayString(memory.value) + " MB", 1)]),
|
|
117
|
+
(openBlock(), createElementBlock("svg", _hoisted_4, [createElementVNode("path", {
|
|
118
|
+
d: getChartPath(),
|
|
119
|
+
fill: "none",
|
|
120
|
+
stroke: "#1890ff",
|
|
121
|
+
"stroke-width": "2"
|
|
122
|
+
}, null, 8, _hoisted_5)]))
|
|
123
|
+
])], 6);
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
//#endregion
|
|
128
|
+
export { DebugPanel_vue_vue_type_script_setup_true_lang_default as default };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import TailIndicator_vue_vue_type_script_setup_true_lang_default from "./TailIndicator.vue_vue_type_script_setup_true_lang.js";
|
|
2
|
+
//#region src/XMarkdown/components/TailIndicator.vue
|
|
3
|
+
var TailIndicator_default = TailIndicator_vue_vue_type_script_setup_true_lang_default;
|
|
4
|
+
//#endregion
|
|
5
|
+
export { TailIndicator_default as default };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createElementBlock, defineComponent, openBlock, toDisplayString } from "vue";
|
|
2
|
+
//#region src/XMarkdown/components/TailIndicator.vue?vue&type=script&setup=true&lang.ts
|
|
3
|
+
var _hoisted_1 = { class: "xmd-tail" };
|
|
4
|
+
var TailIndicator_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ defineComponent({
|
|
5
|
+
__name: "TailIndicator",
|
|
6
|
+
props: { content: { default: "▋" } },
|
|
7
|
+
setup(__props) {
|
|
8
|
+
return (_ctx, _cache) => {
|
|
9
|
+
return openBlock(), createElementBlock("span", _hoisted_1, toDisplayString(__props.content), 1);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
//#endregion
|
|
14
|
+
export { TailIndicator_vue_vue_type_script_setup_true_lang_default as default };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import AnimationText_default from "./AnimationText.js";
|
|
2
|
+
import DebugPanel_default from "./DebugPanel.js";
|
|
3
|
+
import TailIndicator_default from "./TailIndicator.js";
|
|
4
|
+
export { AnimationText_default as AnimationText, DebugPanel_default as DebugPanel, TailIndicator_default as TailIndicator };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Parser } from "../core/Parser.js";
|
|
2
|
+
import { shallowRef } from "vue";
|
|
3
|
+
//#region src/XMarkdown/composables/useParser.ts
|
|
4
|
+
function useParser(initialOptions) {
|
|
5
|
+
const parser = shallowRef(null);
|
|
6
|
+
function createParser(options) {
|
|
7
|
+
return new Parser(options);
|
|
8
|
+
}
|
|
9
|
+
function parse(markdown, options) {
|
|
10
|
+
if (!parser.value) parser.value = createParser(options || initialOptions);
|
|
11
|
+
if (options) parser.value.setOptions(options);
|
|
12
|
+
return parser.value.parse(markdown);
|
|
13
|
+
}
|
|
14
|
+
function resetParser(options) {
|
|
15
|
+
parser.value = createParser(options);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
parser,
|
|
19
|
+
parse,
|
|
20
|
+
resetParser
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { useParser };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { VueRenderer } from "../core/VueRenderer.js";
|
|
2
|
+
import { shallowRef } from "vue";
|
|
3
|
+
//#region src/XMarkdown/composables/useRenderer.ts
|
|
4
|
+
function useRenderer(initialOptions) {
|
|
5
|
+
const renderer = shallowRef(null);
|
|
6
|
+
function createRenderer(options) {
|
|
7
|
+
return new VueRenderer(options);
|
|
8
|
+
}
|
|
9
|
+
function render(html, options) {
|
|
10
|
+
if (!renderer.value) renderer.value = createRenderer(options || initialOptions);
|
|
11
|
+
if (options) renderer.value.setOptions(options);
|
|
12
|
+
return renderer.value.render(html);
|
|
13
|
+
}
|
|
14
|
+
function resetRenderer(options) {
|
|
15
|
+
renderer.value = createRenderer(options);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
renderer,
|
|
19
|
+
render,
|
|
20
|
+
resetRenderer
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { useRenderer };
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { StreamCacheTokenType } from "../interface.js";
|
|
2
|
+
import { ref, watch } from "vue";
|
|
3
|
+
//#region src/XMarkdown/composables/useStreaming.ts
|
|
4
|
+
var STREAM_INCOMPLETE_REGEX = {
|
|
5
|
+
image: [/^!\[[^\]\r\n]{0,1000}$/, /^!\[[^\r\n]{0,1000}\]\(*[^)\r\n]{0,1000}$/],
|
|
6
|
+
link: [/^\[[^\]\r\n]{0,1000}$/, /^\[[^\r\n]{0,1000}\]\(*[^)\r\n]{0,1000}$/],
|
|
7
|
+
html: [/^<\/$/, /^<\/?[a-zA-Z][a-zA-Z0-9-]{0,100}[^>\r\n]{0,1000}$/],
|
|
8
|
+
commonEmphasis: [/^(\*{1,3}|_{1,3})(?!\s)(?!.*\1$)[^\r\n]{0,1000}$/],
|
|
9
|
+
list: [/^[-+*]\s{0,3}$/, /^[-+*]\s{1,3}(\*{1,3}|_{1,3})(?!\s)(?!.*\1$)[^\r\n]{0,1000}$/],
|
|
10
|
+
"inline-code": [/^`[^`\r\n]{0,300}$/]
|
|
11
|
+
};
|
|
12
|
+
var isTableInComplete = (markdown) => {
|
|
13
|
+
if (markdown.includes("\n\n")) return false;
|
|
14
|
+
const lines = markdown.split("\n");
|
|
15
|
+
if (lines.length <= 1) return true;
|
|
16
|
+
const [header, separator] = lines;
|
|
17
|
+
const trimmedHeader = header.trim();
|
|
18
|
+
if (!/^\|.*\|$/.test(trimmedHeader)) return false;
|
|
19
|
+
const columns = separator.trim().split("|").map((col) => col.trim()).filter(Boolean);
|
|
20
|
+
const separatorRegex = /^:?-+:?$/;
|
|
21
|
+
return columns.every((col, index) => index === columns.length - 1 ? col === ":" || separatorRegex.test(col) : separatorRegex.test(col));
|
|
22
|
+
};
|
|
23
|
+
var tokenRecognizerMap = {
|
|
24
|
+
[StreamCacheTokenType.Link]: {
|
|
25
|
+
tokenType: StreamCacheTokenType.Link,
|
|
26
|
+
isStartOfToken: (markdown) => markdown.startsWith("["),
|
|
27
|
+
isStreamingValid: (markdown) => STREAM_INCOMPLETE_REGEX.link.some((re) => re.test(markdown))
|
|
28
|
+
},
|
|
29
|
+
[StreamCacheTokenType.Image]: {
|
|
30
|
+
tokenType: StreamCacheTokenType.Image,
|
|
31
|
+
isStartOfToken: (markdown) => markdown.startsWith("!"),
|
|
32
|
+
isStreamingValid: (markdown) => STREAM_INCOMPLETE_REGEX.image.some((re) => re.test(markdown))
|
|
33
|
+
},
|
|
34
|
+
[StreamCacheTokenType.Html]: {
|
|
35
|
+
tokenType: StreamCacheTokenType.Html,
|
|
36
|
+
isStartOfToken: (markdown) => markdown.startsWith("<"),
|
|
37
|
+
isStreamingValid: (markdown) => STREAM_INCOMPLETE_REGEX.html.some((re) => re.test(markdown))
|
|
38
|
+
},
|
|
39
|
+
[StreamCacheTokenType.Emphasis]: {
|
|
40
|
+
tokenType: StreamCacheTokenType.Emphasis,
|
|
41
|
+
isStartOfToken: (markdown) => markdown.startsWith("*") || markdown.startsWith("_"),
|
|
42
|
+
isStreamingValid: (markdown) => STREAM_INCOMPLETE_REGEX.commonEmphasis.some((re) => re.test(markdown))
|
|
43
|
+
},
|
|
44
|
+
[StreamCacheTokenType.List]: {
|
|
45
|
+
tokenType: StreamCacheTokenType.List,
|
|
46
|
+
isStartOfToken: (markdown) => /^[-+*]/.test(markdown),
|
|
47
|
+
isStreamingValid: (markdown) => STREAM_INCOMPLETE_REGEX.list.some((re) => re.test(markdown)),
|
|
48
|
+
getCommitPrefix: (pending) => {
|
|
49
|
+
const listPrefix = pending.match(/^([-+*]\s{0,3})/)?.[1];
|
|
50
|
+
const rest = listPrefix ? pending.slice(listPrefix.length) : "";
|
|
51
|
+
return listPrefix && rest.startsWith("`") ? listPrefix : null;
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
[StreamCacheTokenType.Table]: {
|
|
55
|
+
tokenType: StreamCacheTokenType.Table,
|
|
56
|
+
isStartOfToken: (markdown) => markdown.startsWith("|"),
|
|
57
|
+
isStreamingValid: isTableInComplete
|
|
58
|
+
},
|
|
59
|
+
[StreamCacheTokenType.InlineCode]: {
|
|
60
|
+
tokenType: StreamCacheTokenType.InlineCode,
|
|
61
|
+
isStartOfToken: (markdown) => markdown.startsWith("`"),
|
|
62
|
+
isStreamingValid: (markdown) => STREAM_INCOMPLETE_REGEX["inline-code"].some((re) => re.test(markdown))
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var recognize = (cache, tokenType) => {
|
|
66
|
+
const recognizer = tokenRecognizerMap[tokenType];
|
|
67
|
+
if (!recognizer) return;
|
|
68
|
+
const { token, pending } = cache;
|
|
69
|
+
if (token === StreamCacheTokenType.Text && recognizer.isStartOfToken(pending)) {
|
|
70
|
+
cache.token = tokenType;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (token === tokenType && !recognizer.isStreamingValid(pending)) {
|
|
74
|
+
const prefix = recognizer.getCommitPrefix?.(pending);
|
|
75
|
+
if (prefix) {
|
|
76
|
+
cache.completeMarkdown += prefix;
|
|
77
|
+
cache.pending = pending.slice(prefix.length);
|
|
78
|
+
cache.token = StreamCacheTokenType.Text;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
commitCache(cache);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var recognizeHandlers = Object.values(tokenRecognizerMap).filter((recognizer) => Boolean(recognizer));
|
|
85
|
+
var getInitialCache = () => ({
|
|
86
|
+
pending: "",
|
|
87
|
+
token: StreamCacheTokenType.Text,
|
|
88
|
+
processedLength: 0,
|
|
89
|
+
completeMarkdown: ""
|
|
90
|
+
});
|
|
91
|
+
var commitCache = (cache) => {
|
|
92
|
+
if (cache.pending) {
|
|
93
|
+
cache.completeMarkdown += cache.pending;
|
|
94
|
+
cache.pending = "";
|
|
95
|
+
}
|
|
96
|
+
cache.token = StreamCacheTokenType.Text;
|
|
97
|
+
};
|
|
98
|
+
var isInCodeBlock = (text, isFinalChunk = false) => {
|
|
99
|
+
const lines = text.split("\n");
|
|
100
|
+
let inFenced = false;
|
|
101
|
+
let fenceChar = "";
|
|
102
|
+
let fenceLen = 0;
|
|
103
|
+
for (let i = 0; i < lines.length; i++) {
|
|
104
|
+
const rawLine = lines[i];
|
|
105
|
+
const match = (rawLine.endsWith("\r") ? rawLine.slice(0, -1) : rawLine).match(/^(`{3,}|~{3,})(.*)$/);
|
|
106
|
+
if (!match) continue;
|
|
107
|
+
const fence = match[1];
|
|
108
|
+
const after = match[2];
|
|
109
|
+
const char = fence[0];
|
|
110
|
+
const len = fence.length;
|
|
111
|
+
if (!inFenced) {
|
|
112
|
+
inFenced = true;
|
|
113
|
+
fenceChar = char;
|
|
114
|
+
fenceLen = len;
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (char === fenceChar && len >= fenceLen && /^\s*$/.test(after) && (isFinalChunk || i < lines.length - 1)) {
|
|
118
|
+
inFenced = false;
|
|
119
|
+
fenceChar = "";
|
|
120
|
+
fenceLen = 0;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return inFenced;
|
|
124
|
+
};
|
|
125
|
+
var sanitizeForURIComponent = (input) => {
|
|
126
|
+
let result = "";
|
|
127
|
+
for (let i = 0; i < input.length; i++) {
|
|
128
|
+
const charCode = input.charCodeAt(i);
|
|
129
|
+
if (charCode >= 55296 && charCode <= 56319) {
|
|
130
|
+
if (i + 1 < input.length && input.charCodeAt(i + 1) >= 56320 && input.charCodeAt(i + 1) <= 57343) {
|
|
131
|
+
result += input[i] + input[i + 1];
|
|
132
|
+
i++;
|
|
133
|
+
}
|
|
134
|
+
} else if (charCode < 56320 || charCode > 57343) result += input[i];
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
};
|
|
138
|
+
var safeEncodeURIComponent = (str) => {
|
|
139
|
+
try {
|
|
140
|
+
return encodeURIComponent(str);
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (error instanceof URIError) return encodeURIComponent(sanitizeForURIComponent(str));
|
|
143
|
+
return "";
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
function useStreaming(content, streaming, components) {
|
|
147
|
+
const streamCache = ref(getInitialCache());
|
|
148
|
+
const processedContent = ref("");
|
|
149
|
+
function handleIncompleteMarkdown(cache, opts) {
|
|
150
|
+
const { token, pending } = cache;
|
|
151
|
+
if (token === StreamCacheTokenType.Text) return void 0;
|
|
152
|
+
if (token === StreamCacheTokenType.Image && pending === "!") return;
|
|
153
|
+
if (token === StreamCacheTokenType.Table && pending.split("\n").length > 2) return pending;
|
|
154
|
+
const componentName = opts?.incompleteMarkdownComponentMap?.[token] ?? `incomplete-${token}`;
|
|
155
|
+
if (!components?.value?.[componentName]) return;
|
|
156
|
+
return `<${componentName} data-raw="${safeEncodeURIComponent(pending)}" />`;
|
|
157
|
+
}
|
|
158
|
+
function processStreaming(text, opts) {
|
|
159
|
+
if (!text) {
|
|
160
|
+
processedContent.value = "";
|
|
161
|
+
streamCache.value = getInitialCache();
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const expectedPrefix = streamCache.value.completeMarkdown + streamCache.value.pending;
|
|
165
|
+
if (!text.startsWith(expectedPrefix)) streamCache.value = getInitialCache();
|
|
166
|
+
const cache = streamCache.value;
|
|
167
|
+
const chunk = text.slice(cache.processedLength);
|
|
168
|
+
if (!chunk) {
|
|
169
|
+
const incompletePlaceholder = handleIncompleteMarkdown(cache, opts);
|
|
170
|
+
processedContent.value = cache.completeMarkdown + (incompletePlaceholder || "");
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
cache.processedLength += chunk.length;
|
|
174
|
+
for (const char of chunk) {
|
|
175
|
+
cache.pending += char;
|
|
176
|
+
if (isInCodeBlock(cache.completeMarkdown + cache.pending)) {
|
|
177
|
+
commitCache(cache);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (cache.token === StreamCacheTokenType.Text) for (const handler of recognizeHandlers) recognize(cache, handler.tokenType);
|
|
181
|
+
else {
|
|
182
|
+
const currentHandler = recognizeHandlers.find((handler) => handler.tokenType === cache.token);
|
|
183
|
+
if (currentHandler) recognize(cache, currentHandler.tokenType);
|
|
184
|
+
if (cache.token === StreamCacheTokenType.Text) for (const handler of recognizeHandlers) recognize(cache, handler.tokenType);
|
|
185
|
+
}
|
|
186
|
+
if (cache.token === StreamCacheTokenType.Text) commitCache(cache);
|
|
187
|
+
}
|
|
188
|
+
const incompletePlaceholder = handleIncompleteMarkdown(cache, opts);
|
|
189
|
+
processedContent.value = cache.completeMarkdown + (incompletePlaceholder || "");
|
|
190
|
+
}
|
|
191
|
+
function reset() {
|
|
192
|
+
streamCache.value = getInitialCache();
|
|
193
|
+
processedContent.value = "";
|
|
194
|
+
}
|
|
195
|
+
watch([content, () => streaming.value?.hasNextChunk], ([newContent, hasNextChunk]) => {
|
|
196
|
+
const opts = streaming.value;
|
|
197
|
+
if (!Boolean(hasNextChunk)) {
|
|
198
|
+
processedContent.value = newContent;
|
|
199
|
+
streamCache.value = {
|
|
200
|
+
pending: "",
|
|
201
|
+
token: StreamCacheTokenType.Text,
|
|
202
|
+
processedLength: newContent.length,
|
|
203
|
+
completeMarkdown: newContent
|
|
204
|
+
};
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
processStreaming(newContent, opts);
|
|
208
|
+
}, { immediate: true });
|
|
209
|
+
return {
|
|
210
|
+
processedContent,
|
|
211
|
+
reset
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
215
|
+
export { useStreaming };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { computed } from "vue";
|
|
2
|
+
//#region src/XMarkdown/composables/useTail.ts
|
|
3
|
+
var DEFAULT_TAIL_CONTENT = "▋";
|
|
4
|
+
function useTail(streaming) {
|
|
5
|
+
const tailContent = computed(() => {
|
|
6
|
+
const tail = streaming.value?.tail;
|
|
7
|
+
if (!tail) return null;
|
|
8
|
+
if (typeof tail === "boolean") return DEFAULT_TAIL_CONTENT;
|
|
9
|
+
return tail.content || DEFAULT_TAIL_CONTENT;
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
tailContent,
|
|
13
|
+
tailComponent: computed(() => {
|
|
14
|
+
const tail = streaming.value?.tail;
|
|
15
|
+
if (!tail) return null;
|
|
16
|
+
if (typeof tail === "boolean") return null;
|
|
17
|
+
return tail.component || null;
|
|
18
|
+
}),
|
|
19
|
+
showTail: computed(() => {
|
|
20
|
+
return streaming.value?.hasNextChunk && tailContent.value !== null;
|
|
21
|
+
})
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { useTail };
|