@bigmistqke/whiteboard 0.0.0-stage → 0.2.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 +628 -2
- package/bin/whiteboard.ts +488 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +542 -0
- package/dist/client/runtime.js +544 -0
- package/dist/define.js +7 -0
- package/dist/plugins/callout/README.md +13 -0
- package/dist/plugins/callout/index.js +27 -0
- package/dist/plugins/callout/style.css +12 -0
- package/dist/plugins/chat/README.md +46 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +47 -0
- package/dist/plugins/chat/style.css +24 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +65 -0
- package/dist/plugins/code/index.js +77 -0
- package/dist/plugins/code/style.css +29 -0
- package/dist/plugins/comments/client.js +10 -0
- package/dist/plugins/comments/index.js +54 -0
- package/dist/plugins/comments/style.css +40 -0
- package/dist/plugins/details/README.md +17 -0
- package/dist/plugins/details/index.js +17 -0
- package/dist/plugins/details/style.css +3 -0
- package/dist/plugins/forge/README.md +12 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +111 -0
- package/dist/plugins/form/index.js +102 -0
- package/dist/plugins/form/style.css +41 -0
- package/dist/plugins/gallery/README.md +30 -0
- package/dist/plugins/gallery/client.js +1 -0
- package/dist/plugins/gallery/index.js +74 -0
- package/dist/plugins/gallery/style.css +27 -0
- package/dist/plugins/grid/README.md +33 -0
- package/dist/plugins/grid/index.js +33 -0
- package/dist/plugins/grid/style.css +25 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/client.js +1 -0
- package/dist/plugins/iframe/index.js +40 -0
- package/dist/plugins/iframe/style.css +29 -0
- package/dist/plugins/index.js +30 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +35 -0
- package/dist/plugins/mermaid/style.css +16 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +17 -0
- package/dist/plugins/terminal/client.js +41 -0
- package/dist/plugins/terminal/host.js +332 -0
- package/dist/plugins/terminal/index.js +171 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +57 -0
- package/dist/plugins/terminal/style.css +357 -0
- package/dist/solid/index.js +36 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +295 -0
- package/dist/src/client/page/main.js +1228 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +183 -0
- package/dist/src/html.js +34 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +126 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +50 -0
- package/dist/src/pages/index.js +15 -0
- package/dist/src/pages/markdown.js +410 -0
- package/dist/src/pages/render.js +311 -0
- package/dist/src/plugins.js +238 -0
- package/dist/src/running.js +24 -0
- package/dist/src/serve.js +71 -0
- package/dist/src/server.js +725 -0
- package/dist/src/status.js +71 -0
- package/package.json +131 -4
- package/plugins/callout/README.md +13 -0
- package/plugins/callout/index.ts +25 -0
- package/plugins/callout/style.css +12 -0
- package/plugins/chat/README.md +46 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +93 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +24 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +64 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +492 -0
- package/plugins/comments/index.ts +54 -0
- package/plugins/comments/style.css +40 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/forge/README.md +12 -0
- package/plugins/forge/index.ts +71 -0
- package/plugins/form/README.md +111 -0
- package/plugins/form/index.ts +104 -0
- package/plugins/form/style.css +41 -0
- package/plugins/gallery/README.md +30 -0
- package/plugins/gallery/client.tsx +64 -0
- package/plugins/gallery/index.ts +67 -0
- package/plugins/gallery/style.css +27 -0
- package/plugins/grid/README.md +33 -0
- package/plugins/grid/index.ts +36 -0
- package/plugins/grid/style.css +25 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/client.ts +46 -0
- package/plugins/iframe/index.ts +54 -0
- package/plugins/iframe/style.css +29 -0
- package/plugins/index.ts +21 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +40 -0
- package/plugins/mermaid/style.css +16 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +27 -0
- package/plugins/terminal/client.ts +317 -0
- package/plugins/terminal/host.ts +326 -0
- package/plugins/terminal/index.ts +138 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/server.ts +215 -0
- package/plugins/terminal/sizes.ts +60 -0
- package/plugins/terminal/style.css +71 -0
- package/skills/whiteboard/SKILL.md +302 -0
- package/solid/index.tsx +59 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +185 -0
- package/src/client/markdown.ts +36 -0
- package/src/client/page/base.css +295 -0
- package/src/client/page/elsewhere.ts +70 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/live.ts +30 -0
- package/src/client/page/main.ts +272 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/sort.ts +38 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +182 -0
- package/src/client/read.ts +73 -0
- package/src/client/runtime.ts +183 -0
- package/src/client/toast.ts +25 -0
- package/src/config.ts +96 -0
- package/src/define.ts +612 -0
- package/src/events.ts +218 -0
- package/src/html.ts +40 -0
- package/src/icons.ts +8 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +112 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +70 -0
- package/src/pages/index.ts +42 -0
- package/src/pages/markdown.ts +364 -0
- package/src/pages/render.ts +233 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +667 -0
- package/src/status.ts +113 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// Claude Code, as a whiteboard plugin: the pages an agent session makes are claimed for that
|
|
2
|
+
// session, and Claude Code hooks report to it what is open on them. In a config:
|
|
3
|
+
//
|
|
4
|
+
// import claudeCode from '@bigmistqke/whiteboard/claude-code'
|
|
5
|
+
// export default defineConfig({ plugins: [claudeCode()] })
|
|
6
|
+
//
|
|
7
|
+
// `whiteboard claude-code install` adds the hooks to the project's .claude/settings.json. Each runs
|
|
8
|
+
// `whiteboard claude-code hook <stop|prompt|session>`, and none of them ever blocks:
|
|
9
|
+
// - stop (a turn ends): the reader sees what on the session's pages nobody listens for;
|
|
10
|
+
// - prompt (the reader sends a message): the agent gets, as context, the answers on its pages that
|
|
11
|
+
// no wait picked up, which count as picked up from then on;
|
|
12
|
+
// - session (a session starts): the same, plus what is open on other sessions' pages, without
|
|
13
|
+
// handing it over.
|
|
14
|
+
//
|
|
15
|
+
// Claude Code gives each conversation an id, in the environment of the commands it runs
|
|
16
|
+
// (`CLAUDE_CODE_SESSION_ID`) and on the standard input of a hook (`session_id`). That id is the
|
|
17
|
+
// session the core records when `whiteboard new` claims a page.
|
|
18
|
+
import fs from 'node:fs'
|
|
19
|
+
import path from 'node:path'
|
|
20
|
+
import { spawnSync } from 'node:child_process'
|
|
21
|
+
import type { CommandContext, PageStatus, Plugin, WbEvent } from '../../src/define.ts'
|
|
22
|
+
import { statusLines, whose } from '../../src/status.ts'
|
|
23
|
+
|
|
24
|
+
/** What is open on a page, in short phrases; the command that picks it up comes last, once. */
|
|
25
|
+
const openOn = (p: PageStatus, options?: { waiting?: boolean }) => statusLines(p, relFile(p.file), options)
|
|
26
|
+
|
|
27
|
+
type Mode = 'stop' | 'prompt' | 'session'
|
|
28
|
+
|
|
29
|
+
/** Claude Code's settings file, as far as hooks go. */
|
|
30
|
+
interface Settings {
|
|
31
|
+
hooks?: Record<string, { hooks?: { type?: string; command?: string }[] }[]>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const MODES: Record<Mode, string> = { stop: 'Stop', prompt: 'UserPromptSubmit', session: 'SessionStart' }
|
|
35
|
+
|
|
36
|
+
/** A hook command this plugin installs. */
|
|
37
|
+
const OURS = /\bwhiteboard claude-code hook\b/
|
|
38
|
+
|
|
39
|
+
export default function claudeCode(): Plugin {
|
|
40
|
+
return {
|
|
41
|
+
name: 'claude-code',
|
|
42
|
+
session: () => process.env.CLAUDE_CODE_SESSION_ID || null,
|
|
43
|
+
commands: {
|
|
44
|
+
hook: {
|
|
45
|
+
description: 'what Claude Code runs on an event (stop, prompt or session): report what is open on the session\'s pages; never blocks',
|
|
46
|
+
args: '<stop|prompt|session>',
|
|
47
|
+
async run(ctx) {
|
|
48
|
+
const mode = ctx.positionals[0] as Mode
|
|
49
|
+
if (!mode || !(mode in MODES)) throw new Error(`usage: whiteboard claude-code hook <${Object.keys(MODES).join('|')}>`)
|
|
50
|
+
// a hook that fails must not get in the way of the session it runs in: say why, and exit cleanly
|
|
51
|
+
try { await hook(ctx, mode) } catch (err) { console.error(`whiteboard claude-code hook ${mode}: ${(err as Error).message}`) }
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
install: {
|
|
55
|
+
description: 'add the hooks to this project\'s .claude/settings.json',
|
|
56
|
+
run: ctx => install(ctx.project.dir),
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The JSON Claude Code passes a hook on its standard input, or nothing. */
|
|
63
|
+
function hookInput(): { session_id?: string } {
|
|
64
|
+
if (process.stdin.isTTY) return {}
|
|
65
|
+
try { return JSON.parse(fs.readFileSync(0, 'utf8') || '{}') } catch { return {} }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const relFile = (file: string) => path.relative(process.cwd(), file) || file
|
|
69
|
+
async function hook(ctx: CommandContext, mode: Mode) {
|
|
70
|
+
const session = hookInput().session_id ?? process.env.CLAUDE_CODE_SESSION_ID ?? null
|
|
71
|
+
const pages = await ctx.status()
|
|
72
|
+
const busy = pages.filter(p => p.unpicked.length || p.open.length)
|
|
73
|
+
const mine = busy.filter(p => session && p.owner === session)
|
|
74
|
+
|
|
75
|
+
if (mode === 'stop') {
|
|
76
|
+
// shown to the reader when a turn ends, never blocking it
|
|
77
|
+
const loose = mine.filter(p => p.unheard.length || p.unpicked.length)
|
|
78
|
+
if (!loose.length) return
|
|
79
|
+
const said = (p: PageStatus) => { const l = openOn(p, { waiting: false }); return `${l.slice(0, -1).join('; ')} ${l.at(-1)}` }
|
|
80
|
+
console.log(JSON.stringify({ systemMessage: `whiteboard: ${loose.map(p => `${p.page}: ${said(p)}`).join(' · ')}` }))
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// prompt and session: what is printed becomes context for the agent. Answers on its own pages
|
|
85
|
+
// that no wait picked up are handed over here, as a wait would, and count as picked up from then
|
|
86
|
+
// on. Only the page itself: a page below it may be another session's.
|
|
87
|
+
const out: string[] = []
|
|
88
|
+
for (const p of mine) {
|
|
89
|
+
const handed: WbEvent[] = []
|
|
90
|
+
await ctx.handOut({ page: p.page, exact: true }, e => handed.push(e), { follow: false })
|
|
91
|
+
if (handed.length) {
|
|
92
|
+
out.push(`Answers on your page ${p.page} (${relFile(p.file)}) that no wait picked up; they count as picked up now:`)
|
|
93
|
+
for (const e of handed) out.push(JSON.stringify(e))
|
|
94
|
+
}
|
|
95
|
+
if (p.unheard.length) out.push(`Nobody listens for ${p.unheard.join(', ')} on your page ${p.page}: run \`whiteboard wait --page ${relFile(p.file)}\` in the background.`)
|
|
96
|
+
}
|
|
97
|
+
if (mode === 'session') {
|
|
98
|
+
// a new session may carry on work another one started: say what is open elsewhere, without handing it over
|
|
99
|
+
const others = busy.filter(p => !mine.includes(p))
|
|
100
|
+
if (others.length) {
|
|
101
|
+
out.push('Other pages in this whiteboard project with something open (not this session\'s; their answers stay for whoever waits on them):')
|
|
102
|
+
for (const p of others) out.push(`- ${p.page} (${whose(p, session)}): ${openOn(p).join(' ')}`)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (out.length) console.log(out.join('\n'))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Add the hooks to the project's .claude/settings.json, replacing ours that run another command. */
|
|
109
|
+
function install(dir: string) {
|
|
110
|
+
const file = path.join(dir, '.claude', 'settings.json')
|
|
111
|
+
let settings: Settings = {}
|
|
112
|
+
if (fs.existsSync(file)) {
|
|
113
|
+
try { settings = JSON.parse(fs.readFileSync(file, 'utf8')) } catch { throw new Error(`${file} is not valid JSON; fix it first`) }
|
|
114
|
+
}
|
|
115
|
+
// the hooks run outside any project folder's node_modules/.bin, so name the command that works here
|
|
116
|
+
const onPath = spawnSync('sh', ['-c', 'command -v whiteboard'], { encoding: 'utf8' }).status === 0
|
|
117
|
+
const bin = onPath ? 'whiteboard' : 'npx whiteboard'
|
|
118
|
+
const changed: string[] = []
|
|
119
|
+
const hooks = settings.hooks ??= {}
|
|
120
|
+
for (const [mode, event] of Object.entries(MODES)) {
|
|
121
|
+
const command = `${bin} claude-code hook ${mode}`
|
|
122
|
+
const groups = hooks[event] ?? []
|
|
123
|
+
if (groups.some(g => g.hooks?.some(h => h.command === command))) continue
|
|
124
|
+
// drop the hooks of ours that run another command, and the groups that leaves empty
|
|
125
|
+
const kept = groups
|
|
126
|
+
.map(g => ({ ...g, hooks: (g.hooks ?? []).filter(h => !OURS.test(h.command ?? '')) }))
|
|
127
|
+
.filter(g => g.hooks.length)
|
|
128
|
+
hooks[event] = [...kept, { hooks: [{ type: 'command', command }] }]
|
|
129
|
+
changed.push(event)
|
|
130
|
+
}
|
|
131
|
+
if (!changed.length) { console.log(`hooks already installed: ${file}`); return }
|
|
132
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
133
|
+
fs.writeFileSync(`${file}.tmp`, JSON.stringify(settings, null, 2) + '\n')
|
|
134
|
+
fs.renameSync(`${file}.tmp`, file)
|
|
135
|
+
console.log(`hooks added to ${file}: ${changed.join(', ')}`)
|
|
136
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
Syntax highlighting for code blocks. [Shiki](https://shiki.style) runs on the server, so no script
|
|
2
|
+
runs on the page, and the colours follow the page theme. It applies to every
|
|
3
|
+
fence that no other block claims by name, and to the code view of
|
|
4
|
+
repository files. Write an ordinary fence with a language:
|
|
5
|
+
|
|
6
|
+
````markdown
|
|
7
|
+
```ts
|
|
8
|
+
const page = await whiteboard.show('plan')
|
|
9
|
+
```
|
|
10
|
+
````
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
const page = await whiteboard.show('plan')
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
A `diff` fence (what `git diff` or `diff -u` prints) is shown for reading:
|
|
17
|
+
each file's headers become its name, a hunk's `@@ … @@` line becomes a `⋯`
|
|
18
|
+
where lines are skipped, lines have their numbers in a gutter and are tinted
|
|
19
|
+
when added or removed. Each line in a hunk also says where it is from
|
|
20
|
+
(`src/a.ts:12`), and a comment on it records that. Use four
|
|
21
|
+
backticks around it, since a diff can hold three.
|
|
22
|
+
|
|
23
|
+
A missing or unknown language renders as plain text. Fences named after
|
|
24
|
+
another block, such as `mermaid`, still go to that block.
|
|
25
|
+
|
|
26
|
+
It's a built-in plugin. For options, replace it in the config with your own
|
|
27
|
+
`code(…)`:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import code from '@bigmistqke/whiteboard/code'
|
|
31
|
+
|
|
32
|
+
export default defineConfig({
|
|
33
|
+
plugins: [code({
|
|
34
|
+
themes: { light: 'github-light', dark: 'github-dark' }, // any Shiki theme names
|
|
35
|
+
colorReplacements: { 'github-light': { '#e36209': '#0f766e' } }, // per theme
|
|
36
|
+
})],
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Without options it uses the GitHub themes, with their oranges swapped for a
|
|
41
|
+
teal that fits the default palette. If you pass `themes`, nothing is swapped
|
|
42
|
+
unless you also pass `colorReplacements`.
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// What each line of a diff fence is, for showing it to a reader rather than a tool: added, removed,
|
|
2
|
+
// unchanged, a gap between hunks, a file's name, or a header line nobody needs to read (`diff --git`,
|
|
3
|
+
// `index`, `---`, modes), which the block leaves out. A line in a hunk has its number (`n`) and
|
|
4
|
+
// where it is (`at`: `file:line` in the new file; a removed line counts in the old one,
|
|
5
|
+
// `file (old):line`). Reads unified diffs, as `git diff` and `diff -u` print them, and snippets of
|
|
6
|
+
// only +, - and unchanged lines.
|
|
7
|
+
|
|
8
|
+
export type DiffKind = 'add' | 'del' | 'context' | 'hunk' | 'file' | 'meta'
|
|
9
|
+
|
|
10
|
+
export interface DiffLineInfo {
|
|
11
|
+
kind: DiffKind
|
|
12
|
+
/** a line in a hunk: its number, in the new file (a removed line: in the old one) */
|
|
13
|
+
n?: number
|
|
14
|
+
/** `file:line`, for a line in a hunk of a known file */
|
|
15
|
+
at?: string
|
|
16
|
+
/** what to show instead of the line: a file's name, a hunk's start */
|
|
17
|
+
label?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const unprefix = (p: string) => p.replace(/\t.*$/, '').replace(/^"|"$/g, '').replace(/^[ab]\//, '')
|
|
21
|
+
|
|
22
|
+
export function diffLines(text: string): DiffLineInfo[] {
|
|
23
|
+
// a snippet without hunks or file headers (only +, - and unchanged lines, as people paste them):
|
|
24
|
+
// every line is one of those, without numbers, as no header says where it is
|
|
25
|
+
if (!/^(@@ -\d|diff --git )/m.test(text)) {
|
|
26
|
+
return text.split('\n').map((line): DiffLineInfo => ({ kind: line[0] === '+' ? 'add' : line[0] === '-' ? 'del' : 'context' }))
|
|
27
|
+
}
|
|
28
|
+
let file = '', oldFile = '', oldN = 0, newN = 0, inHunk = false
|
|
29
|
+
let status = '', renamedFrom = ''
|
|
30
|
+
return text.split('\n').map((line): DiffLineInfo => {
|
|
31
|
+
const at = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line)
|
|
32
|
+
if (at) {
|
|
33
|
+
oldN = Number(at[1]); newN = Number(at[2]); inHunk = true
|
|
34
|
+
// only a mark that lines were skipped: the numbers are in the gutter, and the text git adds
|
|
35
|
+
// after them is a guess at the enclosing line, often just the one before
|
|
36
|
+
return { kind: 'hunk', label: '' }
|
|
37
|
+
}
|
|
38
|
+
if (inHunk) {
|
|
39
|
+
const sign = line[0]
|
|
40
|
+
const where = (side: 'old' | 'new', n: number): DiffLineInfo | {} => {
|
|
41
|
+
const name = side === 'old' ? oldFile || file : file || oldFile
|
|
42
|
+
return name ? { n, at: side === 'old' ? `${name} (old):${n}` : `${name}:${n}` } : { n }
|
|
43
|
+
}
|
|
44
|
+
if (sign === '+') return { kind: 'add', ...where('new', newN++) }
|
|
45
|
+
if (sign === '-') return { kind: 'del', ...where('old', oldN++) }
|
|
46
|
+
if (sign === ' ') { oldN++; return { kind: 'context', ...where('new', newN++) } }
|
|
47
|
+
// "" belongs to the hunk; anything else ends it
|
|
48
|
+
if (sign === '\\') return { kind: 'meta' }
|
|
49
|
+
inHunk = false
|
|
50
|
+
}
|
|
51
|
+
if (line.startsWith('diff --git ')) { file = ''; oldFile = ''; status = ''; renamedFrom = '' }
|
|
52
|
+
else if (line.startsWith('new file mode')) status = 'new'
|
|
53
|
+
else if (line.startsWith('deleted file mode')) status = 'deleted'
|
|
54
|
+
else if (line.startsWith('rename from ')) renamedFrom = line.slice('rename from '.length)
|
|
55
|
+
else if (line.startsWith('--- ')) oldFile = line === '--- /dev/null' ? '' : unprefix(line.slice(4))
|
|
56
|
+
else if (line.startsWith('+++ ')) {
|
|
57
|
+
// the file's name, once its headers are read: what the reader sees of them
|
|
58
|
+
file = line === '+++ /dev/null' ? '' : unprefix(line.slice(4))
|
|
59
|
+
const name = renamedFrom ? `${renamedFrom} → ${file}` : file || oldFile
|
|
60
|
+
return { kind: 'file', label: status ? `${name} (${status})` : name }
|
|
61
|
+
}
|
|
62
|
+
return { kind: 'meta' }
|
|
63
|
+
})
|
|
64
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import { definePlugin, type Plugin } from '@bigmistqke/whiteboard'
|
|
3
|
+
// Syntax highlighting for code blocks, on the server with Shiki: the page gets plain HTML, no script.
|
|
4
|
+
//
|
|
5
|
+
// Built in; for options, replace it in the config: plugins: [code({ themes: { light, dark } })].
|
|
6
|
+
//
|
|
7
|
+
// Its block takes `fence: 'any'`, so it renders every fence no other block claims by name, and the
|
|
8
|
+
// code view of repository files.
|
|
9
|
+
//
|
|
10
|
+
// Every token carries both a light and a dark colour as CSS variables (--shiki-light,
|
|
11
|
+
// --shiki-dark); style.css picks one per theme, so highlighting follows the theme toggle without
|
|
12
|
+
// re-rendering. The block's background stays the page's own code background.
|
|
13
|
+
//
|
|
14
|
+
// A `diff` fence is shown for reading (see diff.ts): its header lines go, except the file's name;
|
|
15
|
+
// a hunk's start is a `⋯` where lines are skipped; each line says what it is (`data-diff`: add, del,
|
|
16
|
+
// context, hunk, file), its number (`data-n`, drawn in a gutter by style.css, so it is never part
|
|
17
|
+
// of the text) and where it is (`data-wb-at="file:line"`), which comments on the line record.
|
|
18
|
+
// shiki loads the first time a page has code, not with the plugin: the command line loads the
|
|
19
|
+
// built-in plugins too, for their declarations
|
|
20
|
+
import type { BundledTheme, Highlighter } from 'shiki'
|
|
21
|
+
import type { FenceContext } from '@bigmistqke/whiteboard'
|
|
22
|
+
import { diffLines } from './diff.ts'
|
|
23
|
+
|
|
24
|
+
export interface Options {
|
|
25
|
+
/** Shiki theme names for each page theme (default: GitHub light and dark) */
|
|
26
|
+
themes?: { light: BundledTheme; dark: BundledTheme }
|
|
27
|
+
/** per theme, colours to swap: { 'github-light': { '#e36209': '#0f766e' } } */
|
|
28
|
+
colorReplacements?: Record<string, Record<string, string>>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const DEFAULT_THEMES = { light: 'github-light', dark: 'github-dark' } as const
|
|
32
|
+
|
|
33
|
+
// the default palette has no orange or brown: the GitHub themes' oranges become a teal
|
|
34
|
+
const DEFAULT_REPLACEMENTS = {
|
|
35
|
+
'github-light': { '#e36209': '#0f766e', '#b08800': '#0f766e' },
|
|
36
|
+
'github-dark': { '#ffab70': '#5eead4', '#c24e00': '#2dd4bf' },
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default function code(options: Options = {}): Plugin {
|
|
40
|
+
const themes = options.themes ?? DEFAULT_THEMES
|
|
41
|
+
const colorReplacements = options.colorReplacements ?? (options.themes ? {} : DEFAULT_REPLACEMENTS)
|
|
42
|
+
let highlighter: Promise<Highlighter> | undefined
|
|
43
|
+
let shiki: typeof import('shiki') | undefined
|
|
44
|
+
const loaded = new Set<string>()
|
|
45
|
+
|
|
46
|
+
/** Highlight `code` as `lang`; an unknown or missing language renders as plain text. */
|
|
47
|
+
async function highlight(code: string, lang: string | null) {
|
|
48
|
+
shiki ??= await import('shiki')
|
|
49
|
+
highlighter ??= shiki.createHighlighter({ themes: Object.values(themes), langs: [] })
|
|
50
|
+
const h = await highlighter
|
|
51
|
+
let language = String(lang ?? '').toLowerCase()
|
|
52
|
+
if (language in shiki.bundledLanguages) {
|
|
53
|
+
if (!loaded.has(language)) {
|
|
54
|
+
await h.loadLanguage(language as keyof typeof shiki.bundledLanguages)
|
|
55
|
+
loaded.add(language)
|
|
56
|
+
}
|
|
57
|
+
} else language = 'text'
|
|
58
|
+
const diff = language === 'diff' ? diffLines(code) : null
|
|
59
|
+
return h.codeToHtml(code, {
|
|
60
|
+
lang: language, themes, defaultColor: false, colorReplacements,
|
|
61
|
+
transformers: diff ? [{
|
|
62
|
+
line(node, n) {
|
|
63
|
+
const info = diff[n - 1]
|
|
64
|
+
if (!info) return
|
|
65
|
+
node.properties['data-diff'] = info.kind
|
|
66
|
+
if (info.n != null) node.properties['data-n'] = String(info.n)
|
|
67
|
+
if (info.at) node.properties['data-wb-at'] = info.at
|
|
68
|
+
// a file's headers read as its name; a hunk's header as nothing but the gutter's `⋯`
|
|
69
|
+
if (info.label != null) node.children = [{ type: 'text', value: info.label }]
|
|
70
|
+
},
|
|
71
|
+
// the headers nobody reads go, with the line break after each
|
|
72
|
+
code(node) {
|
|
73
|
+
node.children = node.children.filter((child, i, all) => {
|
|
74
|
+
const isMeta = (c: typeof child | undefined) => c?.type === 'element' && c.properties['data-diff'] === 'meta'
|
|
75
|
+
return !isMeta(child) && !(child.type === 'text' && isMeta(all[i - 1]))
|
|
76
|
+
})
|
|
77
|
+
},
|
|
78
|
+
}] : [],
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return definePlugin({
|
|
83
|
+
name: 'code',
|
|
84
|
+
blocks: {
|
|
85
|
+
code: {
|
|
86
|
+
// every code fence no other block claims by name, and the code view of repository files
|
|
87
|
+
fence: 'any',
|
|
88
|
+
html: ctx => highlight((ctx as FenceContext).text, (ctx as FenceContext).lang),
|
|
89
|
+
style: new URL('./style.css', import.meta.url),
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
|
|
93
|
+
})
|
|
94
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* Code blocks highlighted by Shiki: tokens carry both colours; pick the one for the current theme.
|
|
2
|
+
The block keeps the page's own code background. */
|
|
3
|
+
pre.shiki { background: var(--code-bg); }
|
|
4
|
+
.shiki span {
|
|
5
|
+
color: var(--shiki-light); font-style: var(--shiki-light-font-style);
|
|
6
|
+
font-weight: var(--shiki-light-font-weight); text-decoration: var(--shiki-light-text-decoration);
|
|
7
|
+
}
|
|
8
|
+
@media (prefers-color-scheme: dark) {
|
|
9
|
+
:root:not([data-theme="light"]) .shiki span {
|
|
10
|
+
color: var(--shiki-dark); font-style: var(--shiki-dark-font-style);
|
|
11
|
+
font-weight: var(--shiki-dark-font-weight); text-decoration: var(--shiki-dark-text-decoration);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
:root[data-theme="dark"] .shiki span {
|
|
15
|
+
color: var(--shiki-dark); font-style: var(--shiki-dark-font-style);
|
|
16
|
+
font-weight: var(--shiki-dark-font-weight); text-decoration: var(--shiki-dark-text-decoration);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/* a diff: one row per line, as wide as the longest (a grid leaves out the line breaks between them,
|
|
20
|
+
which stay in the text), tinted by what it is, with its number in a gutter */
|
|
21
|
+
.shiki code:has(> .line[data-diff]) { display: grid; width: max-content; min-width: 100%; }
|
|
22
|
+
.shiki .line[data-diff=add] { background: color-mix(in srgb, #16a34a 14%, transparent); }
|
|
23
|
+
.shiki .line[data-diff=del] { background: color-mix(in srgb, #dc2626 14%, transparent); }
|
|
24
|
+
.shiki .line[data-diff]::before { content: attr(data-n); display: inline-block; width: 4ch; margin-right: 1.5ch; text-align: right; color: var(--muted, #696e76); opacity: .7; user-select: none; }
|
|
25
|
+
:root .shiki .line[data-diff=hunk] { color: var(--muted, #696e76); line-height: 1.2; margin: 2px 0; }
|
|
26
|
+
.shiki .line[data-diff=hunk]::before { content: '⋯'; text-align: center; }
|
|
27
|
+
.shiki .line[data-diff=file] { font-weight: 600; margin-bottom: 8px; }
|
|
28
|
+
.shiki .line[data-diff=file]::before { content: none; }
|
|
29
|
+
.shiki .line[data-diff=file] + .line[data-diff=hunk] { margin-top: 0; }
|