comark 0.2.1 → 0.3.1
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 +12 -2
- package/dist/internal/frontmatter.d.ts +2 -1
- package/dist/internal/frontmatter.js +2 -2
- package/dist/internal/parse/auto-close/index.js +58 -23
- package/dist/internal/parse/token-processor.js +18 -3
- package/dist/internal/stringify/attributes.d.ts +37 -1
- package/dist/internal/stringify/attributes.js +96 -12
- package/dist/internal/stringify/handlers/a.js +3 -0
- package/dist/internal/stringify/handlers/code.js +1 -1
- package/dist/internal/stringify/handlers/del.js +1 -1
- package/dist/internal/stringify/handlers/html.js +12 -1
- package/dist/internal/stringify/handlers/img.js +1 -1
- package/dist/internal/stringify/handlers/li.js +14 -1
- package/dist/internal/stringify/handlers/math.js +1 -1
- package/dist/internal/stringify/handlers/mdc.js +1 -1
- package/dist/internal/stringify/handlers/ol.js +2 -2
- package/dist/internal/stringify/handlers/pre.js +1 -1
- package/dist/internal/stringify/handlers/template.js +1 -1
- package/dist/internal/stringify/handlers/ul.js +2 -2
- package/dist/internal/stringify/indent.d.ts +2 -1
- package/dist/internal/stringify/indent.js +3 -2
- package/dist/internal/stringify/state.d.ts +3 -3
- package/dist/internal/stringify/state.js +71 -15
- package/dist/internal/yaml.d.ts +2 -1
- package/dist/internal/yaml.js +3 -1
- package/dist/parse.js +13 -2
- package/dist/plugins/alert.js +1 -1
- package/dist/plugins/binding.d.ts +20 -0
- package/dist/plugins/binding.js +61 -0
- package/dist/plugins/breaks.d.ts +2 -0
- package/dist/plugins/breaks.js +34 -0
- package/dist/plugins/footnotes.d.ts +61 -0
- package/dist/plugins/footnotes.js +187 -0
- package/dist/plugins/highlight.js +6 -4
- package/dist/plugins/json-render.d.ts +1 -1
- package/dist/plugins/json-render.js +3 -3
- package/dist/plugins/punctuation.d.ts +67 -0
- package/dist/plugins/punctuation.js +236 -0
- package/dist/plugins/security.js +1 -1
- package/dist/render.d.ts +2 -1
- package/dist/render.js +3 -1
- package/dist/types.d.ts +71 -16
- package/dist/utils/index.d.ts +9 -0
- package/dist/utils/index.js +24 -0
- package/dist/vite.d.ts +1 -0
- package/dist/vite.js +1 -0
- package/package.json +22 -18
- package/skills/comark/AGENTS.md +261 -0
- package/skills/comark/SKILL.md +489 -0
- package/skills/comark/references/markdown-syntax.md +599 -0
- package/skills/comark/references/parsing-ast.md +378 -0
- package/skills/comark/references/rendering-react.md +445 -0
- package/skills/comark/references/rendering-svelte.md +453 -0
- package/skills/comark/references/rendering-vue.md +462 -0
- package/skills/skills/comark/AGENTS.md +261 -0
- package/skills/skills/comark/SKILL.md +489 -0
- package/skills/skills/comark/references/markdown-syntax.md +599 -0
- package/skills/skills/comark/references/parsing-ast.md +378 -0
- package/skills/skills/comark/references/rendering-react.md +445 -0
- package/skills/skills/comark/references/rendering-svelte.md +453 -0
- package/skills/skills/comark/references/rendering-vue.md +462 -0
- package/skills/skills/migrate-mdc-to-comark/SKILL.md +191 -0
- package/dist/utils/serialized-task.d.ts +0 -1
- package/dist/utils/serialized-task.js +0 -1
|
@@ -0,0 +1,261 @@
|
|
|
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)
|