comark 0.3.1 → 0.4.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/dist/internal/frontmatter.d.ts +1 -0
- package/dist/internal/frontmatter.js +4 -2
- package/dist/internal/parse/auto-close/index.js +69 -31
- package/dist/internal/parse/auto-close/table.js +12 -9
- package/dist/internal/parse/auto-unwrap.js +6 -10
- package/dist/internal/parse/html/html_block_rule.js +10 -16
- package/dist/internal/parse/html/html_inline_rule.js +3 -7
- package/dist/internal/parse/html/html_re.js +1 -1
- package/dist/internal/parse/html/index.d.ts +1 -0
- package/dist/internal/parse/html/index.js +15 -3
- package/dist/internal/parse/syntax/block-params.d.ts +9 -0
- package/dist/internal/parse/syntax/block-params.js +48 -0
- package/dist/internal/parse/syntax/brackets.d.ts +8 -0
- package/dist/internal/parse/syntax/brackets.js +20 -0
- package/dist/internal/parse/syntax/props.d.ts +5 -0
- package/dist/internal/parse/syntax/props.js +119 -0
- package/dist/internal/parse/token-processor.js +89 -50
- package/dist/internal/props-validation.js +4 -9
- package/dist/internal/stringify/attributes.d.ts +7 -0
- package/dist/internal/stringify/attributes.js +56 -1
- package/dist/internal/stringify/handlers/a.js +1 -3
- package/dist/internal/stringify/handlers/blockquote.js +19 -4
- package/dist/internal/stringify/handlers/code.js +1 -3
- package/dist/internal/stringify/handlers/emphesis.js +1 -3
- package/dist/internal/stringify/handlers/heading.js +6 -1
- package/dist/internal/stringify/handlers/html.js +34 -18
- package/dist/internal/stringify/handlers/img.js +1 -3
- package/dist/internal/stringify/handlers/li.js +18 -9
- package/dist/internal/stringify/handlers/mdc.js +3 -4
- package/dist/internal/stringify/handlers/ol.js +12 -2
- package/dist/internal/stringify/handlers/p.d.ts +1 -1
- package/dist/internal/stringify/handlers/p.js +8 -1
- package/dist/internal/stringify/handlers/pre.js +20 -14
- package/dist/internal/stringify/handlers/strong.js +1 -3
- package/dist/internal/stringify/handlers/table.js +14 -5
- package/dist/internal/stringify/handlers/template.js +5 -2
- package/dist/internal/stringify/handlers/ul.js +12 -2
- package/dist/internal/stringify/state.js +1 -1
- package/dist/internal/yaml.js +1 -1
- package/dist/parse.d.ts +4 -4
- package/dist/parse.js +20 -10
- package/dist/plugins/alert.d.ts +1 -1
- package/dist/plugins/alert.js +1 -1
- package/dist/plugins/binding.d.ts +1 -1
- package/dist/plugins/binding.js +1 -3
- package/dist/plugins/breaks.d.ts +1 -1
- package/dist/plugins/breaks.js +1 -1
- package/dist/plugins/emoji.d.ts +1 -1
- package/dist/plugins/emoji.js +8 -8
- package/dist/plugins/footnotes.d.ts +1 -1
- package/dist/plugins/footnotes.js +19 -13
- package/dist/plugins/headings.d.ts +19 -8
- package/dist/plugins/headings.js +27 -19
- package/dist/plugins/highlight.d.ts +2 -12
- package/dist/plugins/highlight.js +201 -103
- package/dist/plugins/json-render.d.ts +1 -1
- package/dist/plugins/json-render.js +5 -9
- package/dist/plugins/math.d.ts +1 -1
- package/dist/plugins/math.js +4 -6
- package/dist/plugins/mermaid.d.ts +1 -1
- package/dist/plugins/mermaid.js +6 -20
- package/dist/plugins/punctuation.d.ts +1 -1
- package/dist/plugins/punctuation.js +5 -6
- package/dist/plugins/security.d.ts +1 -1
- package/dist/plugins/security.js +2 -2
- package/dist/plugins/summary.d.ts +4 -1
- package/dist/plugins/syntax.d.ts +49 -0
- package/dist/plugins/syntax.js +558 -0
- package/dist/plugins/task-list.d.ts +2 -2
- package/dist/plugins/task-list.js +11 -8
- package/dist/plugins/toc.d.ts +3 -1
- package/dist/plugins/toc.js +1 -1
- package/dist/types.d.ts +57 -12
- package/dist/utils/comark.tmLanguage.d.ts +335 -0
- package/dist/utils/comark.tmLanguage.js +597 -0
- package/dist/utils/helpers.d.ts +16 -4
- package/dist/utils/helpers.js +16 -6
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +25 -3
- package/package.json +40 -40
- package/skills/comark/references/rendering-svelte.md +51 -7
- package/dist/internal/stringify/indent.d.ts +0 -5
- package/dist/internal/stringify/indent.js +0 -9
- package/dist/vite.d.ts +0 -1
- package/dist/vite.js +0 -1
- package/skills/skills/comark/AGENTS.md +0 -261
- package/skills/skills/comark/SKILL.md +0 -489
- package/skills/skills/comark/references/markdown-syntax.md +0 -599
- package/skills/skills/comark/references/parsing-ast.md +0 -378
- package/skills/skills/comark/references/rendering-react.md +0 -445
- package/skills/skills/comark/references/rendering-svelte.md +0 -453
- package/skills/skills/comark/references/rendering-vue.md +0 -462
- /package/skills/{skills/migrate-mdc-to-comark → migrate-mdc-to-comark}/SKILL.md +0 -0
package/dist/utils/index.d.ts
CHANGED
|
@@ -18,6 +18,11 @@ export declare function textContent(node: ComarkNode, options?: {
|
|
|
18
18
|
* @param visitor - A function that visits a node
|
|
19
19
|
*/
|
|
20
20
|
export declare function visit(tree: ComarkTree, checker: (node: ComarkNode) => boolean, visitor: (node: ComarkNode) => ComarkNode | false | undefined | void): void;
|
|
21
|
+
export declare function indent(text: string, { ignoreFirstLine, level, width }?: {
|
|
22
|
+
ignoreFirstLine?: boolean;
|
|
23
|
+
level?: number;
|
|
24
|
+
width?: number;
|
|
25
|
+
}): string;
|
|
21
26
|
/**
|
|
22
27
|
* Convert a string to pascal case
|
|
23
28
|
* @param str - The string to convert
|
package/dist/utils/index.js
CHANGED
|
@@ -38,10 +38,12 @@ visitor) {
|
|
|
38
38
|
const res = visitor(node);
|
|
39
39
|
if (res === false) {
|
|
40
40
|
// remove the node from the parent
|
|
41
|
+
;
|
|
41
42
|
parent.splice(index, 1);
|
|
42
43
|
return true; // signal that node was removed
|
|
43
44
|
}
|
|
44
45
|
if (res !== undefined) {
|
|
46
|
+
;
|
|
45
47
|
parent[index] = res;
|
|
46
48
|
currentNode = res;
|
|
47
49
|
}
|
|
@@ -72,13 +74,29 @@ visitor) {
|
|
|
72
74
|
}
|
|
73
75
|
}
|
|
74
76
|
// #region String Utils
|
|
77
|
+
export function indent(text, { ignoreFirstLine = false, level = 1, width } = {}) {
|
|
78
|
+
const pad = width ? ' '.repeat(width) : ' '.repeat(level);
|
|
79
|
+
return text
|
|
80
|
+
.split('\n')
|
|
81
|
+
.map((line, index) => {
|
|
82
|
+
if (ignoreFirstLine && index === 0) {
|
|
83
|
+
return line;
|
|
84
|
+
}
|
|
85
|
+
return line ? pad + line : line;
|
|
86
|
+
})
|
|
87
|
+
.join('\n');
|
|
88
|
+
}
|
|
75
89
|
/**
|
|
76
90
|
* Convert a string to pascal case
|
|
77
91
|
* @param str - The string to convert
|
|
78
92
|
* @returns The pascal case string
|
|
79
93
|
*/
|
|
80
94
|
export function pascalCase(str) {
|
|
81
|
-
return str
|
|
95
|
+
return str
|
|
96
|
+
? splitByCase(str)
|
|
97
|
+
.map((p) => (p ? p[0].toUpperCase() + p.slice(1) : ''))
|
|
98
|
+
.join('')
|
|
99
|
+
: '';
|
|
82
100
|
}
|
|
83
101
|
/**
|
|
84
102
|
* Convert a string to kebab case
|
|
@@ -86,7 +104,11 @@ export function pascalCase(str) {
|
|
|
86
104
|
* @returns The kebab case string
|
|
87
105
|
*/
|
|
88
106
|
export function kebabCase(str) {
|
|
89
|
-
return str
|
|
107
|
+
return str
|
|
108
|
+
? splitByCase(str)
|
|
109
|
+
.map((p) => p.toLowerCase())
|
|
110
|
+
.join('-')
|
|
111
|
+
: '';
|
|
90
112
|
}
|
|
91
113
|
/**
|
|
92
114
|
* Convert a string to camel case
|
|
@@ -123,7 +145,7 @@ function splitByCase(str) {
|
|
|
123
145
|
const charCode = char.charCodeAt(0);
|
|
124
146
|
const isNumber = charCode >= 48 && charCode <= 57; // '0' to '9'
|
|
125
147
|
// Fast uppercase check using character codes
|
|
126
|
-
const isUpper = isNumber ? void 0 :
|
|
148
|
+
const isUpper = isNumber ? void 0 : charCode >= 65 && charCode <= 90; // 'A' to 'Z'
|
|
127
149
|
if (previousSplitter === false) {
|
|
128
150
|
if (previousUpper === false && isUpper === true) {
|
|
129
151
|
parts.push(buff);
|
package/package.json
CHANGED
|
@@ -1,26 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "comark",
|
|
3
|
-
"
|
|
4
|
-
"version": "0.3.1",
|
|
3
|
+
"version": "0.4.0",
|
|
5
4
|
"description": "Components in Markdown (Comark) parser with streaming support for Vue, React, Svelte and HTML",
|
|
6
|
-
"author": "",
|
|
7
|
-
"license": "MIT",
|
|
8
|
-
"repository": {
|
|
9
|
-
"type": "git",
|
|
10
|
-
"url": "git+https://github.com/comarkdown/comark.git"
|
|
11
|
-
},
|
|
12
|
-
"bugs": {
|
|
13
|
-
"url": "https://github.com/comarkdown/comark/issues"
|
|
14
|
-
},
|
|
15
|
-
"homepage": "https://comark.dev",
|
|
16
5
|
"keywords": [
|
|
17
6
|
"markdown",
|
|
18
7
|
"mdc",
|
|
19
8
|
"parser",
|
|
9
|
+
"react",
|
|
20
10
|
"streaming",
|
|
21
|
-
"vue"
|
|
22
|
-
"react"
|
|
11
|
+
"vue"
|
|
23
12
|
],
|
|
13
|
+
"homepage": "https://comark.dev",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/comarkdown/comark/issues"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"author": "",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/comarkdown/comark.git"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist",
|
|
25
|
+
"skills"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"main": "./dist/index.js",
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
24
32
|
"exports": {
|
|
25
33
|
".": "./dist/index.js",
|
|
26
34
|
"./plugins/*": "./dist/plugins/*.js",
|
|
@@ -28,16 +36,27 @@
|
|
|
28
36
|
"./parse": "./dist/parse.js",
|
|
29
37
|
"./render": "./dist/render.js"
|
|
30
38
|
},
|
|
31
|
-
"main": "./dist/index.js",
|
|
32
|
-
"module": "./dist/index.js",
|
|
33
|
-
"types": "./dist/index.d.ts",
|
|
34
|
-
"files": [
|
|
35
|
-
"dist",
|
|
36
|
-
"skills"
|
|
37
|
-
],
|
|
38
39
|
"publishConfig": {
|
|
39
40
|
"access": "public"
|
|
40
41
|
},
|
|
42
|
+
"dependencies": {
|
|
43
|
+
"entities": "^8.0.0",
|
|
44
|
+
"htmlparser2": "^12.0.0",
|
|
45
|
+
"js-yaml": "^4.1.1",
|
|
46
|
+
"markdown-exit": "1.0.0-beta.9"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@json-render/core": "^0.16.0",
|
|
50
|
+
"@shikijs/twoslash": "^4.0.2",
|
|
51
|
+
"@types/js-yaml": "^4.0.9",
|
|
52
|
+
"@types/markdown-it": "^14.1.2",
|
|
53
|
+
"github-slugger": "^2.0.0",
|
|
54
|
+
"hast-util-to-string": "^3.0.1",
|
|
55
|
+
"minimark": "0.2.0",
|
|
56
|
+
"tsx": "^4.22.4",
|
|
57
|
+
"twoslash": "^0.3.6",
|
|
58
|
+
"vitest": "^4.1.4"
|
|
59
|
+
},
|
|
41
60
|
"peerDependencies": {
|
|
42
61
|
"beautiful-mermaid": "^1.1.3",
|
|
43
62
|
"katex": "^0.16.45",
|
|
@@ -54,25 +73,6 @@
|
|
|
54
73
|
"optional": true
|
|
55
74
|
}
|
|
56
75
|
},
|
|
57
|
-
"devDependencies": {
|
|
58
|
-
"@json-render/core": "^0.16.0",
|
|
59
|
-
"@shikijs/twoslash": "^4.0.2",
|
|
60
|
-
"@types/js-yaml": "^4.0.9",
|
|
61
|
-
"@types/markdown-it": "^14.1.2",
|
|
62
|
-
"github-slugger": "^2.0.0",
|
|
63
|
-
"hast-util-to-string": "^3.0.1",
|
|
64
|
-
"minimark": "0.2.0",
|
|
65
|
-
"tsx": "^4.21.0",
|
|
66
|
-
"twoslash": "^0.3.6",
|
|
67
|
-
"vitest": "^4.1.4"
|
|
68
|
-
},
|
|
69
|
-
"dependencies": {
|
|
70
|
-
"@comark/markdown-it": "^0.3.4",
|
|
71
|
-
"entities": "^8.0.0",
|
|
72
|
-
"htmlparser2": "^12.0.0",
|
|
73
|
-
"js-yaml": "^4.1.1",
|
|
74
|
-
"markdown-exit": "1.0.0-beta.9"
|
|
75
|
-
},
|
|
76
76
|
"scripts": {
|
|
77
77
|
"stub": "node ../../scripts/stub.mjs",
|
|
78
78
|
"build": "tsc",
|
|
@@ -48,9 +48,9 @@ Map custom Svelte components to Comark elements:
|
|
|
48
48
|
```svelte
|
|
49
49
|
<script lang="ts">
|
|
50
50
|
import { Comark } from '@comark/svelte'
|
|
51
|
-
import CustomHeading from './CustomHeading.svelte'
|
|
52
|
-
import CustomAlert from './CustomAlert.svelte'
|
|
53
|
-
import CustomCard from './CustomCard.svelte'
|
|
51
|
+
import CustomHeading from './components/comark/CustomHeading.svelte'
|
|
52
|
+
import CustomAlert from './components/comark/CustomAlert.svelte'
|
|
53
|
+
import CustomCard from './components/comark/CustomCard.svelte'
|
|
54
54
|
|
|
55
55
|
const customComponents = {
|
|
56
56
|
h1: CustomHeading,
|
|
@@ -127,9 +127,9 @@ Load components dynamically using `componentsManifest`:
|
|
|
127
127
|
import { Comark } from '@comark/svelte'
|
|
128
128
|
|
|
129
129
|
const componentMap: Record<string, () => Promise<any>> = {
|
|
130
|
-
'alert': () => import('./Alert.svelte'),
|
|
131
|
-
'card': () => import('./Card.svelte'),
|
|
132
|
-
'button': () => import('./Button.svelte'),
|
|
130
|
+
'alert': () => import('./components/comark/Alert.svelte'),
|
|
131
|
+
'card': () => import('./components/comark/Card.svelte'),
|
|
132
|
+
'button': () => import('./components/comark/Button.svelte'),
|
|
133
133
|
}
|
|
134
134
|
|
|
135
135
|
async function loadComponent(name: string) {
|
|
@@ -144,6 +144,50 @@ Load components dynamically using `componentsManifest`:
|
|
|
144
144
|
<Comark markdown={content} componentsManifest={loadComponent} />
|
|
145
145
|
```
|
|
146
146
|
|
|
147
|
+
In SvelteKit projects, keep components rendered from Markdown in a dedicated folder such as `$lib/components/comark/`. This keeps Comark-rendered components separate from normal app UI components and makes `componentsManifest` globs easier to audit.
|
|
148
|
+
|
|
149
|
+
For SvelteKit SSR with non-eager lazy components, use `ComarkAsync` and a manifest that returns dynamic imports. An explicit map is the easiest option to audit:
|
|
150
|
+
|
|
151
|
+
```svelte
|
|
152
|
+
<script lang="ts">
|
|
153
|
+
import { ComarkAsync } from '@comark/svelte/async'
|
|
154
|
+
|
|
155
|
+
const componentMap: Record<string, () => Promise<any>> = {
|
|
156
|
+
'alert': () => import('$lib/components/comark/Alert.svelte'),
|
|
157
|
+
'lazy-card': () => import('$lib/components/comark/LazyCard.svelte'),
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const componentsManifest = (name: string) => componentMap[name]?.()
|
|
161
|
+
</script>
|
|
162
|
+
|
|
163
|
+
<svelte:boundary>
|
|
164
|
+
<ComarkAsync markdown={content} {componentsManifest} />
|
|
165
|
+
</svelte:boundary>
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Use `import.meta.glob` when you want the manifest to cover every Svelte component in a folder:
|
|
169
|
+
|
|
170
|
+
```svelte
|
|
171
|
+
<script lang="ts">
|
|
172
|
+
import { ComarkAsync } from '@comark/svelte/async'
|
|
173
|
+
import { pascalCase } from '@comark/svelte/utils'
|
|
174
|
+
|
|
175
|
+
const modules = import.meta.glob('../lib/components/comark/*.svelte')
|
|
176
|
+
|
|
177
|
+
const componentsManifest = (name: string) => {
|
|
178
|
+
return modules[`../lib/components/comark/${pascalCase(name)}.svelte`]?.()
|
|
179
|
+
}
|
|
180
|
+
</script>
|
|
181
|
+
|
|
182
|
+
<svelte:boundary>
|
|
183
|
+
<ComarkAsync markdown={content} {componentsManifest} />
|
|
184
|
+
</svelte:boundary>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Omit the boundary `pending` snippet when you want SvelteKit SSR to wait and include the resolved lazy components in the initial HTML.
|
|
188
|
+
|
|
189
|
+
Use eager/static components with `ComarkRenderer` when you need stable SSR without Svelte's experimental async support.
|
|
190
|
+
|
|
147
191
|
---
|
|
148
192
|
|
|
149
193
|
## Slots Support
|
|
@@ -319,7 +363,7 @@ Override native HTML elements using the `Prose` prefix:
|
|
|
319
363
|
|
|
320
364
|
## Experimental Async
|
|
321
365
|
|
|
322
|
-
The `ComarkAsync` component uses Svelte's experimental `await` in `$derived` for a declarative approach. Requires `experimental.async` in your Svelte config:
|
|
366
|
+
The `ComarkAsync` component uses Svelte's experimental `await` in `$derived` for a declarative approach. It can also await async `componentsManifest` entries during SSR, so lazy dynamic imports render into SvelteKit server HTML. Requires `experimental.async` in your Svelte config:
|
|
323
367
|
|
|
324
368
|
```js
|
|
325
369
|
// svelte.config.js
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export function indent(text, { ignoreFirstLine = false, level = 1, width } = {}) {
|
|
2
|
-
const pad = width ? ' '.repeat(width) : ' '.repeat(level);
|
|
3
|
-
return text.split('\n').map((line, index) => {
|
|
4
|
-
if (ignoreFirstLine && index === 0) {
|
|
5
|
-
return line;
|
|
6
|
-
}
|
|
7
|
-
return line ? pad + line : line;
|
|
8
|
-
}).join('\n');
|
|
9
|
-
}
|
package/dist/vite.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '../src/vite'
|
package/dist/vite.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '../src/vite.ts'
|
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
# Comark — AI Agents & LLM Streaming
|
|
2
|
-
|
|
3
|
-
A guide for using Comark in AI agent and LLM-powered applications where markdown is generated incrementally by a language model.
|
|
4
|
-
|
|
5
|
-
## Why Comark for AI?
|
|
6
|
-
|
|
7
|
-
LLMs stream markdown token-by-token. Standard markdown parsers expect complete input — they fail or produce broken output on partial streams. Comark was built to handle exactly this:
|
|
8
|
-
|
|
9
|
-
- **`autoClose`** (default: `true`) — incomplete syntax like `**bold text` is automatically closed on every parse, so partial tokens always render correctly
|
|
10
|
-
- **Streaming mode** — re-renders efficiently as content arrives
|
|
11
|
-
- **Caret indicator** — shows a live cursor during generation
|
|
12
|
-
- **ANSI rendering** — styled terminal output for CLI agents
|
|
13
|
-
|
|
14
|
-
---
|
|
15
|
-
|
|
16
|
-
## Vue
|
|
17
|
-
|
|
18
|
-
```vue
|
|
19
|
-
<script setup lang="ts">
|
|
20
|
-
import { ref } from 'vue'
|
|
21
|
-
import { Comark } from '@comark/vue'
|
|
22
|
-
|
|
23
|
-
const content = ref('')
|
|
24
|
-
const streaming = ref(false)
|
|
25
|
-
|
|
26
|
-
async function generate(prompt: string) {
|
|
27
|
-
content.value = ''
|
|
28
|
-
streaming.value = true
|
|
29
|
-
|
|
30
|
-
const res = await fetch('/api/chat', {
|
|
31
|
-
method: 'POST',
|
|
32
|
-
body: JSON.stringify({ prompt }),
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
const reader = res.body!.getReader()
|
|
36
|
-
const decoder = new TextDecoder()
|
|
37
|
-
|
|
38
|
-
while (true) {
|
|
39
|
-
const { done, value } = await reader.read()
|
|
40
|
-
if (done) break
|
|
41
|
-
content.value += decoder.decode(value, { stream: true })
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
streaming.value = false
|
|
45
|
-
}
|
|
46
|
-
</script>
|
|
47
|
-
|
|
48
|
-
<template>
|
|
49
|
-
<Comark :streaming="streaming" caret>{{ content }}</Comark>
|
|
50
|
-
</template>
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
---
|
|
54
|
-
|
|
55
|
-
## React
|
|
56
|
-
|
|
57
|
-
```tsx
|
|
58
|
-
import { useState } from 'react'
|
|
59
|
-
import { Comark } from '@comark/react'
|
|
60
|
-
|
|
61
|
-
export default function Chat() {
|
|
62
|
-
const [content, setContent] = useState('')
|
|
63
|
-
const [streaming, setStreaming] = useState(false)
|
|
64
|
-
|
|
65
|
-
async function generate(prompt: string) {
|
|
66
|
-
setContent('')
|
|
67
|
-
setStreaming(true)
|
|
68
|
-
|
|
69
|
-
const res = await fetch('/api/chat', {
|
|
70
|
-
method: 'POST',
|
|
71
|
-
body: JSON.stringify({ prompt }),
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
const reader = res.body!.getReader()
|
|
75
|
-
const decoder = new TextDecoder()
|
|
76
|
-
|
|
77
|
-
while (true) {
|
|
78
|
-
const { done, value } = await reader.read()
|
|
79
|
-
if (done) break
|
|
80
|
-
setContent(prev => prev + decoder.decode(value, { stream: true }))
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
setStreaming(false)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return <Comark streaming={streaming} caret>{content}</Comark>
|
|
87
|
-
}
|
|
88
|
-
```
|
|
89
|
-
|
|
90
|
-
---
|
|
91
|
-
|
|
92
|
-
## Svelte
|
|
93
|
-
|
|
94
|
-
```svelte
|
|
95
|
-
<script lang="ts">
|
|
96
|
-
import { Comark } from '@comark/svelte'
|
|
97
|
-
|
|
98
|
-
let content = $state('')
|
|
99
|
-
let streaming = $state(false)
|
|
100
|
-
|
|
101
|
-
async function generate(prompt: string) {
|
|
102
|
-
content = ''
|
|
103
|
-
streaming = true
|
|
104
|
-
|
|
105
|
-
const res = await fetch('/api/chat', {
|
|
106
|
-
method: 'POST',
|
|
107
|
-
body: JSON.stringify({ prompt }),
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
const reader = res.body!.getReader()
|
|
111
|
-
const decoder = new TextDecoder()
|
|
112
|
-
|
|
113
|
-
while (true) {
|
|
114
|
-
const { done, value } = await reader.read()
|
|
115
|
-
if (done) break
|
|
116
|
-
content += decoder.decode(value, { stream: true })
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
streaming = false
|
|
120
|
-
}
|
|
121
|
-
</script>
|
|
122
|
-
|
|
123
|
-
<Comark markdown={content} {streaming} caret />
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
---
|
|
127
|
-
|
|
128
|
-
## Terminal / CLI Agents
|
|
129
|
-
|
|
130
|
-
Use `@comark/ansi` to render LLM markdown output in terminal-based agents:
|
|
131
|
-
|
|
132
|
-
```typescript
|
|
133
|
-
import { log } from '@comark/ansi'
|
|
134
|
-
|
|
135
|
-
// Print a complete LLM response to stdout with ANSI styling
|
|
136
|
-
await log(llmResponse)
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
For streaming terminal output, use `createLog` with a custom `write` function:
|
|
140
|
-
|
|
141
|
-
```typescript
|
|
142
|
-
import { createLog } from '@comark/ansi'
|
|
143
|
-
|
|
144
|
-
const logStream = createLog({
|
|
145
|
-
write: (s) => process.stdout.write(s),
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
// Call after each chunk to show partial output
|
|
149
|
-
await logStream(partialMarkdown)
|
|
150
|
-
```
|
|
151
|
-
|
|
152
|
-
---
|
|
153
|
-
|
|
154
|
-
## Caret Customization
|
|
155
|
-
|
|
156
|
-
The `caret` prop appends a blinking cursor to the last text node while `streaming` is `true`. Customize it with a CSS class:
|
|
157
|
-
|
|
158
|
-
```vue
|
|
159
|
-
<Comark :streaming="streaming" :caret="{ class: 'animate-blink' }">{{ content }}</Comark>
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
```tsx
|
|
163
|
-
<Comark streaming={streaming} caret={{ class: 'animate-blink' }}>{content}</Comark>
|
|
164
|
-
```
|
|
165
|
-
|
|
166
|
-
```svelte
|
|
167
|
-
<Comark markdown={content} {streaming} caret={{ class: 'animate-blink' }} />
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
---
|
|
171
|
-
|
|
172
|
-
## With Custom Components
|
|
173
|
-
|
|
174
|
-
If your LLM produces Comark component syntax (e.g., `::alert`), register components before streaming begins:
|
|
175
|
-
|
|
176
|
-
```vue
|
|
177
|
-
<script setup lang="ts">
|
|
178
|
-
import { Comark } from '@comark/vue'
|
|
179
|
-
import Alert from './Alert.vue'
|
|
180
|
-
import CodeBlock from './CodeBlock.vue'
|
|
181
|
-
</script>
|
|
182
|
-
|
|
183
|
-
<template>
|
|
184
|
-
<Comark :components="{ alert: Alert, pre: CodeBlock }" :streaming="streaming" caret>
|
|
185
|
-
{{ content }}
|
|
186
|
-
</Comark>
|
|
187
|
-
</template>
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
---
|
|
191
|
-
|
|
192
|
-
## With Syntax Highlighting
|
|
193
|
-
|
|
194
|
-
Syntax highlighting works during streaming — each re-parse will highlight newly completed code blocks:
|
|
195
|
-
|
|
196
|
-
```vue
|
|
197
|
-
<script setup lang="ts">
|
|
198
|
-
import { Comark } from '@comark/vue'
|
|
199
|
-
import highlight from '@comark/vue/plugins/highlight'
|
|
200
|
-
import githubDark from '@shikijs/themes/github-dark'
|
|
201
|
-
|
|
202
|
-
const plugins = [highlight({ themes: { light: githubDark, dark: githubDark } })]
|
|
203
|
-
</script>
|
|
204
|
-
|
|
205
|
-
<template>
|
|
206
|
-
<Suspense>
|
|
207
|
-
<Comark :plugins="plugins" :streaming="streaming" caret>{{ content }}</Comark>
|
|
208
|
-
</Suspense>
|
|
209
|
-
</template>
|
|
210
|
-
```
|
|
211
|
-
|
|
212
|
-
```tsx
|
|
213
|
-
import { Comark } from '@comark/react'
|
|
214
|
-
import highlight from '@comark/react/plugins/highlight'
|
|
215
|
-
import githubDark from '@shikijs/themes/github-dark'
|
|
216
|
-
|
|
217
|
-
const plugins = [highlight({ themes: { light: githubDark, dark: githubDark } })]
|
|
218
|
-
|
|
219
|
-
export default function Chat({ content, streaming }) {
|
|
220
|
-
return (
|
|
221
|
-
<Comark plugins={plugins} streaming={streaming} caret>
|
|
222
|
-
{content}
|
|
223
|
-
</Comark>
|
|
224
|
-
)
|
|
225
|
-
}
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
---
|
|
229
|
-
|
|
230
|
-
## defineComarkComponent for AI Chat
|
|
231
|
-
|
|
232
|
-
Pre-configure a Comark component for your AI chat UI once, then reuse it everywhere:
|
|
233
|
-
|
|
234
|
-
```typescript
|
|
235
|
-
// comark.ts
|
|
236
|
-
import { defineComarkComponent } from '@comark/vue'
|
|
237
|
-
import highlight from '@comark/vue/plugins/highlight'
|
|
238
|
-
import math, { Math } from '@comark/vue/plugins/math'
|
|
239
|
-
import githubDark from '@shikijs/themes/github-dark'
|
|
240
|
-
import Alert from './components/Alert.vue'
|
|
241
|
-
|
|
242
|
-
export const ChatComark = defineComarkComponent({
|
|
243
|
-
name: 'ChatComark',
|
|
244
|
-
plugins: [
|
|
245
|
-
math(),
|
|
246
|
-
highlight({ themes: { light: githubDark, dark: githubDark } }),
|
|
247
|
-
],
|
|
248
|
-
components: { Math, alert: Alert },
|
|
249
|
-
autoClose: true,
|
|
250
|
-
})
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
```vue
|
|
254
|
-
<template>
|
|
255
|
-
<ChatComark :streaming="streaming" caret>{{ content }}</ChatComark>
|
|
256
|
-
</template>
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
---
|
|
260
|
-
|
|
261
|
-
[← Back to Skills Guide](./SKILL.md)
|