viveworker 0.8.0 → 0.8.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/.agents/plugins/marketplace.json +20 -0
- package/README.md +82 -0
- package/package.json +7 -1
- package/plugins/viveworker-control-plane/.codex-plugin/plugin.json +49 -0
- package/plugins/viveworker-control-plane/.mcp.json +11 -0
- package/plugins/viveworker-control-plane/assets/viveworker-logo-v2.png +0 -0
- package/plugins/viveworker-control-plane/assets/viveworker-logo-v2.svg +39 -0
- package/plugins/viveworker-control-plane/skills/viveworker-control-plane/SKILL.md +83 -0
- package/scripts/lib/markdown-render.mjs +128 -1
- package/scripts/mcp-server.mjs +891 -0
- package/scripts/stats-cli.mjs +683 -0
- package/scripts/viveworker-bridge.mjs +350 -22
- package/scripts/viveworker.mjs +262 -1
- package/skills/viveworker-control-plane/SKILL.md +104 -0
- package/skills/viveworker-control-plane/agents/openai.yaml +12 -0
- package/templates/CLAUDE.viveworker.md +67 -0
- package/web/app.css +49 -0
- package/web/app.js +51 -2
- package/web/build-id.js +1 -0
- package/web/icons/apple-touch-icon.png +0 -0
- package/web/icons/viveworker-icon-192.png +0 -0
- package/web/icons/viveworker-icon-512.png +0 -0
- package/web/icons/viveworker-v-pulse.svg +16 -1
- package/web/index.html +1 -1
- package/web/sw.js +2 -2
- package/web/icons/viveworker-beacon-v.svg +0 -19
- package/web/icons/viveworker-icon-1024.png +0 -0
- package/web/icons/viveworker-v-check.svg +0 -19
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "viveworker",
|
|
3
|
+
"interface": {
|
|
4
|
+
"displayName": "viveworker"
|
|
5
|
+
},
|
|
6
|
+
"plugins": [
|
|
7
|
+
{
|
|
8
|
+
"name": "viveworker-control-plane",
|
|
9
|
+
"source": {
|
|
10
|
+
"source": "local",
|
|
11
|
+
"path": "./plugins/viveworker-control-plane"
|
|
12
|
+
},
|
|
13
|
+
"policy": {
|
|
14
|
+
"installation": "AVAILABLE",
|
|
15
|
+
"authentication": "ON_INSTALL"
|
|
16
|
+
},
|
|
17
|
+
"category": "Productivity"
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|
package/README.md
CHANGED
|
@@ -151,6 +151,14 @@ Use these commands most often:
|
|
|
151
151
|
install the Moltbook watcher and auto-scout after base setup
|
|
152
152
|
- `npx viveworker enable scout`
|
|
153
153
|
tune, reinstall, or uninstall the Moltbook auto-scout job
|
|
154
|
+
- `npx viveworker stats`
|
|
155
|
+
show server-side adoption signals for npm, A2A, File Share, Remote connection, and local Moltbook state
|
|
156
|
+
- `npx viveworker mcp`
|
|
157
|
+
start the stdio MCP server for MCP-compatible AI tools
|
|
158
|
+
- `npx viveworker mcp config`
|
|
159
|
+
print a Claude Desktop / Cursor / Codex MCP config snippet
|
|
160
|
+
- `npx viveworker enable mcp --target claude|cursor|codex|all`
|
|
161
|
+
install the MCP config entry after showing the exact change
|
|
154
162
|
- `npx viveworker start`
|
|
155
163
|
start `viveworker` again using the saved config
|
|
156
164
|
- `npx viveworker stop`
|
|
@@ -173,6 +181,78 @@ Useful options:
|
|
|
173
181
|
It does not change the main app URL, port, session secret, TLS, or Web Push settings.
|
|
174
182
|
Use it only when you want to add another trusted device or browser.
|
|
175
183
|
|
|
184
|
+
## MCP Integration
|
|
185
|
+
|
|
186
|
+
`viveworker` can run as a local stdio MCP server, so MCP-compatible tools can use the same mobile control plane.
|
|
187
|
+
|
|
188
|
+
Install it into a supported client:
|
|
189
|
+
|
|
190
|
+
```bash
|
|
191
|
+
npx viveworker enable mcp --target claude
|
|
192
|
+
npx viveworker enable mcp --target cursor
|
|
193
|
+
npx viveworker enable mcp --target codex
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Use `--target all` to update every detected client, `--dry-run` to preview without writing, and `--yes` for non-interactive installs.
|
|
197
|
+
`--target claude` configures Claude Desktop. Claude Code keeps a separate MCP config; add it with:
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
claude mcp add --scope user viveworker -- npx viveworker mcp
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
If you prefer manual setup, add this to an MCP client:
|
|
204
|
+
|
|
205
|
+
```json
|
|
206
|
+
{
|
|
207
|
+
"mcpServers": {
|
|
208
|
+
"viveworker": {
|
|
209
|
+
"command": "npx",
|
|
210
|
+
"args": ["viveworker", "mcp"]
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
Available tools:
|
|
217
|
+
|
|
218
|
+
- `viveworker_status` checks bridge, pairing, Remote connection, A2A, File Share, and Moltbook status
|
|
219
|
+
- `viveworker_notify` sends an informational phone notification and timeline entry
|
|
220
|
+
- `viveworker_ask` asks a question on the paired phone and waits for the answer
|
|
221
|
+
- `viveworker_request_approval` asks the phone to approve or reject a proposed action
|
|
222
|
+
- `viveworker_share_file` uploads a workspace file to File Share after phone approval
|
|
223
|
+
- `viveworker_thread_share` shares context into another Codex / Claude / inbox thread
|
|
224
|
+
- `viveworker_send_a2a_task` sends a task to a registered A2A target after phone approval
|
|
225
|
+
|
|
226
|
+
Available prompts include setup guidance, control-plane usage, risky-action approval, File Share deliverables, and A2A delegation.
|
|
227
|
+
|
|
228
|
+
Security defaults:
|
|
229
|
+
|
|
230
|
+
- MCP is stdio-only in this release; there is no HTTP MCP server
|
|
231
|
+
- file sharing is limited to the current workspace and refuses `.env`, credential directories, private keys, and secret-looking paths
|
|
232
|
+
- File Share and A2A task sending require phone approval
|
|
233
|
+
- MCP tool calls are recorded locally with `provider: "mcp"`
|
|
234
|
+
- prompts, message bodies, file contents, file paths, command text, tokens, public keys, and IP addresses are not sent to central analytics
|
|
235
|
+
|
|
236
|
+
Bundled agent guidance:
|
|
237
|
+
|
|
238
|
+
- `skills/viveworker-control-plane/SKILL.md` gives Codex-style agents a compact policy for when to use each viveworker MCP tool
|
|
239
|
+
- `templates/CLAUDE.viveworker.md` can be copied into a repo `CLAUDE.md` so Claude knows when to ask, approve, share, hand off, or delegate through viveworker
|
|
240
|
+
- `.agents/plugins/marketplace.json` and `plugins/viveworker-control-plane/` package the MCP config and Codex skill as a local Codex plugin for plugin-based installs
|
|
241
|
+
- use these guides when you want agents to guide first-run setup, ask on mobile, request approval, share deliverables, hand off context, or delegate through A2A instead of guessing
|
|
242
|
+
|
|
243
|
+
For local Codex plugin testing from a checkout, register this repo as a marketplace in `~/.codex/config.toml`:
|
|
244
|
+
|
|
245
|
+
```toml
|
|
246
|
+
[marketplaces.viveworker]
|
|
247
|
+
source_type = "local"
|
|
248
|
+
source = "/path/to/viveworker"
|
|
249
|
+
|
|
250
|
+
[plugins."viveworker-control-plane@viveworker"]
|
|
251
|
+
enabled = true
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
Restart Codex after changing the config. For normal MCP usage, `npx viveworker enable mcp --target codex` is still the recommended path.
|
|
255
|
+
|
|
176
256
|
## File Share
|
|
177
257
|
|
|
178
258
|
`viveworker` includes **File Share**, a private file-hosting surface for agent outputs. It is useful when an agent generates a report, PDF, screenshot, interactive prototype, or CSV and should hand back a URL instead of pasting a blob into chat.
|
|
@@ -500,6 +580,8 @@ If you can map your tool onto one of those paths, you probably do not need a bra
|
|
|
500
580
|
- Remote connection uses a relay only for rendezvous/transport; paired-device traffic is end-to-end encrypted to your bridge
|
|
501
581
|
- relay tokens can be rotated manually and are refreshed automatically during LAN enrollment after the rotation window
|
|
502
582
|
- remote connection activity is recorded locally in `~/.viveworker/remote-pairing-audit.jsonl`
|
|
583
|
+
- public relay analytics expose only coarse, delayed adoption counters; detailed relay health and abuse counters require an operator admin token
|
|
584
|
+
- relay analytics never store prompts, replies, file contents, file paths, command text, relay tokens, public keys, or IP addresses
|
|
503
585
|
- if you lose a paired device, revoke it from `Settings > Devices`
|
|
504
586
|
- use `pair` only when you want to add another trusted device
|
|
505
587
|
- A2A relay authentication: external agents must provide a valid API key (`X-A2A-Key` header), and registration requires GitHub OAuth
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "viveworker",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Open mobile control surface for Codex, Claude, Thread Sharing, File Share, Moltbook, and A2A tasks on your trusted LAN.",
|
|
5
5
|
"author": "Yuta Hoshino <hoshino.lireneo@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,7 +52,13 @@
|
|
|
52
52
|
"scripts/a2a-executor.mjs",
|
|
53
53
|
"scripts/a2a-relay-client.mjs",
|
|
54
54
|
"scripts/share-cli.mjs",
|
|
55
|
+
"scripts/stats-cli.mjs",
|
|
56
|
+
"scripts/mcp-server.mjs",
|
|
55
57
|
"scripts/lib",
|
|
58
|
+
".agents",
|
|
59
|
+
"skills",
|
|
60
|
+
"templates",
|
|
61
|
+
"plugins",
|
|
56
62
|
"web",
|
|
57
63
|
"README.md",
|
|
58
64
|
"viveworker.env.example",
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "viveworker-control-plane",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "Run AI agent work from your phone.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "viveworker team",
|
|
7
|
+
"email": "hello@viveworker.com",
|
|
8
|
+
"url": "https://viveworker.com/"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://viveworker.com/",
|
|
11
|
+
"repository": "https://github.com/viveworker-dev/viveworker",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"viveworker",
|
|
15
|
+
"mcp",
|
|
16
|
+
"codex",
|
|
17
|
+
"mobile",
|
|
18
|
+
"approvals",
|
|
19
|
+
"thread-sharing",
|
|
20
|
+
"file-share",
|
|
21
|
+
"a2a"
|
|
22
|
+
],
|
|
23
|
+
"skills": "./skills/",
|
|
24
|
+
"mcpServers": "./.mcp.json",
|
|
25
|
+
"interface": {
|
|
26
|
+
"displayName": "viveworker",
|
|
27
|
+
"shortDescription": "Control Codex, Claude, and agent tasks from your phone.",
|
|
28
|
+
"longDescription": "Pair your phone with viveworker to control Codex, Claude, File Share, Thread Share, A2A tasks, and social posting workflows like Moltbook. Approve actions, answer questions, receive updates, share deliverables, delegate work, and keep agent work moving without being tied to your computer.",
|
|
29
|
+
"developerName": "viveworker team",
|
|
30
|
+
"category": "Productivity",
|
|
31
|
+
"capabilities": [
|
|
32
|
+
"Interactive",
|
|
33
|
+
"MCP",
|
|
34
|
+
"Mobile approvals",
|
|
35
|
+
"File sharing",
|
|
36
|
+
"Thread sharing",
|
|
37
|
+
"A2A delegation"
|
|
38
|
+
],
|
|
39
|
+
"websiteURL": "https://viveworker.com/",
|
|
40
|
+
"defaultPrompt": [
|
|
41
|
+
"Set up viveworker and pair my phone.",
|
|
42
|
+
"Ask me on my phone before risky actions.",
|
|
43
|
+
"Share this result with another agent."
|
|
44
|
+
],
|
|
45
|
+
"brandColor": "#64D99A",
|
|
46
|
+
"composerIcon": "./assets/viveworker-logo-v2.png",
|
|
47
|
+
"logo": "./assets/viveworker-logo-v2.png"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
<svg width="1024" height="1024" viewBox="0 0 1024 1024" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
2
|
+
<defs>
|
|
3
|
+
<radialGradient id="bg" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(238 164) rotate(43.8309) scale(925.664)">
|
|
4
|
+
<stop stop-color="#173448"/>
|
|
5
|
+
<stop offset="0.38" stop-color="#0F1F2B"/>
|
|
6
|
+
<stop offset="1" stop-color="#091117"/>
|
|
7
|
+
</radialGradient>
|
|
8
|
+
<radialGradient id="glow" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(760 248) rotate(130.1) scale(256.971)">
|
|
9
|
+
<stop stop-color="#5ED6FF" stop-opacity="0.34"/>
|
|
10
|
+
<stop offset="1" stop-color="#5ED6FF" stop-opacity="0"/>
|
|
11
|
+
</radialGradient>
|
|
12
|
+
<linearGradient id="vFill" x1="264" y1="286" x2="778" y2="776" gradientUnits="userSpaceOnUse">
|
|
13
|
+
<stop stop-color="#FFFFFF"/>
|
|
14
|
+
<stop offset="0.5" stop-color="#F5FBFF"/>
|
|
15
|
+
<stop offset="1" stop-color="#E7F4F8"/>
|
|
16
|
+
</linearGradient>
|
|
17
|
+
<radialGradient id="vShade" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(512 674) rotate(90) scale(196 292)">
|
|
18
|
+
<stop stop-color="#CFEAF5" stop-opacity="0.46"/>
|
|
19
|
+
<stop offset="1" stop-color="#CFEAF5" stop-opacity="0"/>
|
|
20
|
+
</radialGradient>
|
|
21
|
+
<filter id="soft" x="557" y="90" width="320" height="320" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
|
|
22
|
+
<feGaussianBlur stdDeviation="24"/>
|
|
23
|
+
</filter>
|
|
24
|
+
<filter id="vShadow" x="190" y="240" width="650" height="630" filterUnits="userSpaceOnUse" color-interpolation-filters="sRGB">
|
|
25
|
+
<feDropShadow dx="0" dy="24" stdDeviation="30" flood-color="#5ED6FF" flood-opacity="0.14"/>
|
|
26
|
+
</filter>
|
|
27
|
+
</defs>
|
|
28
|
+
<rect width="1024" height="1024" rx="224" fill="url(#bg)"/>
|
|
29
|
+
<circle cx="717" cy="250" r="112" fill="url(#glow)"/>
|
|
30
|
+
<g filter="url(#soft)">
|
|
31
|
+
<circle cx="717" cy="250" r="58" fill="#7DD3FC" fill-opacity="0.36"/>
|
|
32
|
+
</g>
|
|
33
|
+
<g filter="url(#vShadow)">
|
|
34
|
+
<path d="M270 292C235 304 220 342 237 376L421 729C458 800 566 800 603 729L787 376C804 342 789 304 754 292C719 280 684 301 670 335L534 657C525 679 499 679 490 657L354 335C340 301 305 280 270 292Z" fill="url(#vFill)"/>
|
|
35
|
+
<path d="M270 292C235 304 220 342 237 376L421 729C458 800 566 800 603 729L787 376C804 342 789 304 754 292C719 280 684 301 670 335L534 657C525 679 499 679 490 657L354 335C340 301 305 280 270 292Z" fill="url(#vShade)"/>
|
|
36
|
+
</g>
|
|
37
|
+
<path d="M697 180C748 180 789 221 789 272" stroke="#7DD3FC" stroke-width="40" stroke-linecap="round"/>
|
|
38
|
+
<path d="M667 120C752 120 821 189 821 274" stroke="#7DD3FC" stroke-width="28" stroke-linecap="round" opacity="0.92"/>
|
|
39
|
+
</svg>
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: viveworker-control-plane
|
|
3
|
+
description: Use viveworker MCP as the mobile control plane for Codex when work needs phone-based questions, approvals, File Share uploads, Thread Share handoffs, or A2A delegation. Use when the user asks to ask on mobile, approve before acting, share a deliverable, send context to another agent/session, or delegate work to an A2A target.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# viveworker Control Plane
|
|
7
|
+
|
|
8
|
+
Use viveworker when the user wants the agent workflow to leave the chat and enter the mobile control plane: ask, approve, notify, share, hand off, or delegate.
|
|
9
|
+
|
|
10
|
+
## First check
|
|
11
|
+
|
|
12
|
+
If anything looks stale or disconnected, call `viveworker_status` before troubleshooting. It summarizes bridge, pairing, Remote connection, A2A, File Share, and Moltbook state.
|
|
13
|
+
|
|
14
|
+
If viveworker MCP tools are unavailable, tell the user to run:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npx viveworker enable mcp --target codex
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Then they should restart Codex.
|
|
21
|
+
|
|
22
|
+
## First-run onboarding
|
|
23
|
+
|
|
24
|
+
Use this flow when the user says "set up viveworker", "pair my phone", "viveworker を初期設定して", or "viveworker を使えるようにして".
|
|
25
|
+
|
|
26
|
+
1. Try `viveworker_status` if MCP tools are available.
|
|
27
|
+
2. If the bridge is missing or setup is incomplete, ask for confirmation before running or recommending `npx viveworker setup`.
|
|
28
|
+
3. Do not silently run setup. It can install local certificates, write config, register launchd services, and update agent hooks.
|
|
29
|
+
4. If setup exists but no phone is trusted, guide the user to run `npx viveworker pair` and open the pairing URL on the phone.
|
|
30
|
+
5. Ask the user to allow notifications from the Home Screen app.
|
|
31
|
+
6. Re-check with `viveworker_status`.
|
|
32
|
+
7. Send a smoke notification with `viveworker_notify`.
|
|
33
|
+
8. Summarize what is ready and what remains optional, such as Remote connection, File Share, A2A, or Moltbook.
|
|
34
|
+
|
|
35
|
+
## Tool policy
|
|
36
|
+
|
|
37
|
+
- Use `viveworker_ask` for a short user decision that blocks progress.
|
|
38
|
+
- Use `viveworker_request_approval` before risky, external, irreversible, payment-related, or user-visible actions.
|
|
39
|
+
- Use `viveworker_notify` for informational updates that should appear on the phone and timeline.
|
|
40
|
+
- Use `viveworker_share_file` when a local deliverable should become a limited File Share URL.
|
|
41
|
+
- Use `viveworker_thread_share` when context should move to another Codex / Claude / inbox thread.
|
|
42
|
+
- Use `viveworker_send_a2a_task` when the user asks to delegate to a registered A2A target.
|
|
43
|
+
|
|
44
|
+
## Safety rules
|
|
45
|
+
|
|
46
|
+
- Keep phone prompts concise: action, reason, expected result, and choices.
|
|
47
|
+
- Treat timeout, rejection, or missing response as not approved.
|
|
48
|
+
- Do not send secrets, private keys, `.env` content, credentials, relay tokens, public keys, or unnecessary file contents through MCP.
|
|
49
|
+
- Do not use MCP as a shell executor. It creates control-plane events; it should not run arbitrary commands.
|
|
50
|
+
- Do not upload files through File Share without phone approval.
|
|
51
|
+
- Use registered A2A target aliases only. Do not inline API keys.
|
|
52
|
+
|
|
53
|
+
## Natural language triggers
|
|
54
|
+
|
|
55
|
+
Use `viveworker_ask` for:
|
|
56
|
+
|
|
57
|
+
- "ask me on my phone"
|
|
58
|
+
- "スマホに聞いて"
|
|
59
|
+
- "確認してから進めて"
|
|
60
|
+
|
|
61
|
+
Use `viveworker_request_approval` for:
|
|
62
|
+
|
|
63
|
+
- "approve this on my phone"
|
|
64
|
+
- "承認してから実行して"
|
|
65
|
+
- "外部に送る前に確認して"
|
|
66
|
+
|
|
67
|
+
Use `viveworker_share_file` for:
|
|
68
|
+
|
|
69
|
+
- "share this report"
|
|
70
|
+
- "共有リンクにして"
|
|
71
|
+
- "File Share にアップして"
|
|
72
|
+
|
|
73
|
+
Use `viveworker_thread_share` for:
|
|
74
|
+
|
|
75
|
+
- "share this with Claude"
|
|
76
|
+
- "tell Codex what we decided"
|
|
77
|
+
- "Aの内容をBに共有して"
|
|
78
|
+
|
|
79
|
+
Use `viveworker_send_a2a_task` for:
|
|
80
|
+
|
|
81
|
+
- "delegate this to another agent"
|
|
82
|
+
- "A2Aで依頼して"
|
|
83
|
+
- "registered target に送って"
|
|
@@ -170,15 +170,125 @@ function renderBlockquote(lines) {
|
|
|
170
170
|
|
|
171
171
|
function renderCodeBlock(lines) {
|
|
172
172
|
const [fence, ...rest] = lines;
|
|
173
|
+
// parseBlocks pushes the closing fence onto the block before breaking out
|
|
174
|
+
// of the loop, so `rest` ends with that closing line whenever the fence is
|
|
175
|
+
// properly closed. Strip it so the rendered <pre><code>...</code></pre>
|
|
176
|
+
// doesn't show the literal "```" as the last line of the code body.
|
|
177
|
+
const body = [...rest];
|
|
178
|
+
if (body.length > 0 && /^```\s*$/u.test(body[body.length - 1])) {
|
|
179
|
+
body.pop();
|
|
180
|
+
}
|
|
173
181
|
const language = fence.replace(/^```/u, "").trim();
|
|
174
182
|
const className = language ? ` class="language-${escapeHtml(language)}"` : "";
|
|
175
|
-
return `<pre><code${className}>${escapeHtml(
|
|
183
|
+
return `<pre><code${className}>${escapeHtml(body.join("\n"))}</code></pre>`;
|
|
176
184
|
}
|
|
177
185
|
|
|
178
186
|
function renderParagraph(lines) {
|
|
179
187
|
return `<p>${lines.map((line) => renderInline(line.trim())).join("<br>")}</p>`;
|
|
180
188
|
}
|
|
181
189
|
|
|
190
|
+
// ---------------------------------------------------------------------------
|
|
191
|
+
// Pipe-style markdown tables
|
|
192
|
+
// ---------------------------------------------------------------------------
|
|
193
|
+
//
|
|
194
|
+
// | header 1 | header 2 |
|
|
195
|
+
// |----------|---------:|
|
|
196
|
+
// | cell | 42 |
|
|
197
|
+
//
|
|
198
|
+
// Detection requires both a header row (single line starting AND ending with
|
|
199
|
+
// `|`) and a separator row immediately below it whose cells are
|
|
200
|
+
// `:?-{2,}:?` (with optional alignment markers). Anything else falls through
|
|
201
|
+
// to the paragraph fallback so a stray `| not a table |` line still
|
|
202
|
+
// renders as plain text.
|
|
203
|
+
//
|
|
204
|
+
// Cells go through `renderInline` so they pick up the existing escape /
|
|
205
|
+
// inline-formatting pipeline; we never inject raw HTML.
|
|
206
|
+
|
|
207
|
+
function isTableRowLine(line) {
|
|
208
|
+
if (typeof line !== "string") return false;
|
|
209
|
+
const trimmed = line.trim();
|
|
210
|
+
return trimmed.length >= 2 && trimmed.startsWith("|") && trimmed.endsWith("|");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function splitTableCells(line) {
|
|
214
|
+
// Strip the leading + trailing pipes; honour `\|` as an escaped literal.
|
|
215
|
+
const trimmed = line.trim().replace(/^\|/u, "").replace(/\|$/u, "");
|
|
216
|
+
const cells = [];
|
|
217
|
+
let current = "";
|
|
218
|
+
let i = 0;
|
|
219
|
+
while (i < trimmed.length) {
|
|
220
|
+
if (trimmed[i] === "\\" && trimmed[i + 1] === "|") {
|
|
221
|
+
current += "|";
|
|
222
|
+
i += 2;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (trimmed[i] === "|") {
|
|
226
|
+
cells.push(current);
|
|
227
|
+
current = "";
|
|
228
|
+
i += 1;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
current += trimmed[i];
|
|
232
|
+
i += 1;
|
|
233
|
+
}
|
|
234
|
+
cells.push(current);
|
|
235
|
+
return cells.map((cell) => cell.trim());
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function isTableSeparatorLine(line) {
|
|
239
|
+
if (!isTableRowLine(line)) return false;
|
|
240
|
+
const cells = splitTableCells(line);
|
|
241
|
+
if (cells.length === 0) return false;
|
|
242
|
+
return cells.every((cell) => /^:?-{2,}:?$/u.test(cell));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function parseTableAlignments(separatorLine) {
|
|
246
|
+
return splitTableCells(separatorLine).map((cell) => {
|
|
247
|
+
const left = cell.startsWith(":");
|
|
248
|
+
const right = cell.endsWith(":");
|
|
249
|
+
if (left && right) return "center";
|
|
250
|
+
if (right) return "right";
|
|
251
|
+
if (left) return "left";
|
|
252
|
+
return "";
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function renderTableCell(cell, align, tag) {
|
|
257
|
+
const styleAttr = align ? ` style="text-align:${align}"` : "";
|
|
258
|
+
return `<${tag}${styleAttr}>${renderInline(cell)}</${tag}>`;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function renderTable(headerLine, separatorLine, dataLines) {
|
|
262
|
+
const alignments = parseTableAlignments(separatorLine);
|
|
263
|
+
const headerCells = splitTableCells(headerLine);
|
|
264
|
+
// Pad alignments out to header width so any data row that's wider still
|
|
265
|
+
// gets a sensible alignment default.
|
|
266
|
+
while (alignments.length < headerCells.length) alignments.push("");
|
|
267
|
+
|
|
268
|
+
const headerHtml = headerCells
|
|
269
|
+
.map((cell, i) => renderTableCell(cell, alignments[i] || "", "th"))
|
|
270
|
+
.join("");
|
|
271
|
+
const bodyHtml = dataLines
|
|
272
|
+
.map((row) => {
|
|
273
|
+
const cells = splitTableCells(row);
|
|
274
|
+
const cellsHtml = cells
|
|
275
|
+
.map((cell, i) => renderTableCell(cell, alignments[i] || "", "td"))
|
|
276
|
+
.join("");
|
|
277
|
+
return `<tr>${cellsHtml}</tr>`;
|
|
278
|
+
})
|
|
279
|
+
.join("");
|
|
280
|
+
|
|
281
|
+
return `<table><thead><tr>${headerHtml}</tr></thead><tbody>${bodyHtml}</tbody></table>`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
function looksLikeTableStart(lines, index) {
|
|
285
|
+
return (
|
|
286
|
+
isTableRowLine(lines[index]) &&
|
|
287
|
+
index + 1 < lines.length &&
|
|
288
|
+
isTableSeparatorLine(lines[index + 1])
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
182
292
|
function parseBlocks(markdown) {
|
|
183
293
|
const lines = markdown.replace(/\r\n/gu, "\n").trim().split("\n");
|
|
184
294
|
const blocks = [];
|
|
@@ -241,6 +351,19 @@ function parseBlocks(markdown) {
|
|
|
241
351
|
continue;
|
|
242
352
|
}
|
|
243
353
|
|
|
354
|
+
if (looksLikeTableStart(lines, index)) {
|
|
355
|
+
const headerLine = lines[index];
|
|
356
|
+
const separatorLine = lines[index + 1];
|
|
357
|
+
const dataLines = [];
|
|
358
|
+
index += 2;
|
|
359
|
+
while (index < lines.length && isTableRowLine(lines[index])) {
|
|
360
|
+
dataLines.push(lines[index]);
|
|
361
|
+
index += 1;
|
|
362
|
+
}
|
|
363
|
+
blocks.push(renderTable(headerLine, separatorLine, dataLines));
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
|
|
244
367
|
const block = [];
|
|
245
368
|
while (index < lines.length) {
|
|
246
369
|
const current = lines[index];
|
|
@@ -250,6 +373,10 @@ function parseBlocks(markdown) {
|
|
|
250
373
|
if (isUnorderedList(current) || isOrderedList(current) || /^(?:---|\*\*\*|___)\s*$/u.test(current)) {
|
|
251
374
|
break;
|
|
252
375
|
}
|
|
376
|
+
if (looksLikeTableStart(lines, index)) {
|
|
377
|
+
// Don't swallow a table-start row into the previous paragraph.
|
|
378
|
+
break;
|
|
379
|
+
}
|
|
253
380
|
block.push(current);
|
|
254
381
|
index += 1;
|
|
255
382
|
}
|