@bigmistqke/whiteboard 0.0.0-stage → 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.
Files changed (138) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +609 -2
  3. package/bin/whiteboard.ts +431 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  5. package/dist/bin/whiteboard.js +471 -0
  6. package/dist/client/runtime.js +391 -0
  7. package/dist/define.js +7 -0
  8. package/dist/plugins/callout/README.md +13 -0
  9. package/dist/plugins/callout/index.js +27 -0
  10. package/dist/plugins/callout/style.css +12 -0
  11. package/dist/plugins/chat/README.md +45 -0
  12. package/dist/plugins/chat/client.js +3 -0
  13. package/dist/plugins/chat/index.js +46 -0
  14. package/dist/plugins/chat/style.css +17 -0
  15. package/dist/plugins/claude-code/index.js +120 -0
  16. package/dist/plugins/code/README.md +42 -0
  17. package/dist/plugins/code/diff.js +64 -0
  18. package/dist/plugins/code/index.js +77 -0
  19. package/dist/plugins/code/style.css +29 -0
  20. package/dist/plugins/comments/client.js +10 -0
  21. package/dist/plugins/comments/index.js +51 -0
  22. package/dist/plugins/comments/style.css +32 -0
  23. package/dist/plugins/details/README.md +17 -0
  24. package/dist/plugins/details/index.js +17 -0
  25. package/dist/plugins/details/style.css +3 -0
  26. package/dist/plugins/form/README.md +111 -0
  27. package/dist/plugins/form/index.js +102 -0
  28. package/dist/plugins/form/style.css +41 -0
  29. package/dist/plugins/gallery/README.md +30 -0
  30. package/dist/plugins/gallery/client.js +1 -0
  31. package/dist/plugins/gallery/index.js +74 -0
  32. package/dist/plugins/gallery/style.css +27 -0
  33. package/dist/plugins/grid/README.md +33 -0
  34. package/dist/plugins/grid/index.js +33 -0
  35. package/dist/plugins/grid/style.css +25 -0
  36. package/dist/plugins/index.js +26 -0
  37. package/dist/plugins/mermaid/README.md +21 -0
  38. package/dist/plugins/mermaid/index.js +34 -0
  39. package/dist/plugins/mermaid/style.css +16 -0
  40. package/dist/plugins/quiet/README.md +26 -0
  41. package/dist/plugins/quiet/index.js +17 -0
  42. package/dist/plugins/terminal/client.js +41 -0
  43. package/dist/plugins/terminal/host.js +332 -0
  44. package/dist/plugins/terminal/index.js +171 -0
  45. package/dist/plugins/terminal/modes.js +32 -0
  46. package/dist/plugins/terminal/server.js +256 -0
  47. package/dist/plugins/terminal/sizes.js +57 -0
  48. package/dist/plugins/terminal/style.css +357 -0
  49. package/dist/solid/index.js +35 -0
  50. package/dist/src/auth.js +54 -0
  51. package/dist/src/client/page/base.css +274 -0
  52. package/dist/src/client/page/main.js +1004 -0
  53. package/dist/src/config.js +76 -0
  54. package/dist/src/events.js +162 -0
  55. package/dist/src/html.js +34 -0
  56. package/dist/src/load.js +77 -0
  57. package/dist/src/loader.js +120 -0
  58. package/dist/src/package.js +33 -0
  59. package/dist/src/pages/blocks.js +49 -0
  60. package/dist/src/pages/index.js +14 -0
  61. package/dist/src/pages/markdown.js +307 -0
  62. package/dist/src/pages/render.js +257 -0
  63. package/dist/src/plugins.js +238 -0
  64. package/dist/src/running.js +24 -0
  65. package/dist/src/serve.js +71 -0
  66. package/dist/src/server.js +700 -0
  67. package/dist/src/status.js +43 -0
  68. package/package.json +131 -4
  69. package/plugins/callout/README.md +13 -0
  70. package/plugins/callout/index.ts +25 -0
  71. package/plugins/callout/style.css +12 -0
  72. package/plugins/chat/README.md +45 -0
  73. package/plugins/chat/autosize.ts +101 -0
  74. package/plugins/chat/client.tsx +81 -0
  75. package/plugins/chat/index.ts +61 -0
  76. package/plugins/chat/style.css +17 -0
  77. package/plugins/claude-code/index.ts +136 -0
  78. package/plugins/code/README.md +42 -0
  79. package/plugins/code/diff.ts +58 -0
  80. package/plugins/code/index.ts +94 -0
  81. package/plugins/code/style.css +29 -0
  82. package/plugins/comments/client.ts +448 -0
  83. package/plugins/comments/index.ts +52 -0
  84. package/plugins/comments/style.css +32 -0
  85. package/plugins/details/README.md +17 -0
  86. package/plugins/details/index.ts +21 -0
  87. package/plugins/details/style.css +3 -0
  88. package/plugins/form/README.md +111 -0
  89. package/plugins/form/index.ts +104 -0
  90. package/plugins/form/style.css +41 -0
  91. package/plugins/gallery/README.md +30 -0
  92. package/plugins/gallery/client.tsx +64 -0
  93. package/plugins/gallery/index.ts +67 -0
  94. package/plugins/gallery/style.css +27 -0
  95. package/plugins/grid/README.md +33 -0
  96. package/plugins/grid/index.ts +36 -0
  97. package/plugins/grid/style.css +25 -0
  98. package/plugins/index.ts +19 -0
  99. package/plugins/mermaid/README.md +21 -0
  100. package/plugins/mermaid/index.ts +39 -0
  101. package/plugins/mermaid/style.css +16 -0
  102. package/plugins/quiet/README.md +26 -0
  103. package/plugins/quiet/index.ts +27 -0
  104. package/plugins/terminal/client.ts +317 -0
  105. package/plugins/terminal/host.ts +326 -0
  106. package/plugins/terminal/index.ts +138 -0
  107. package/plugins/terminal/modes.ts +30 -0
  108. package/plugins/terminal/server.ts +215 -0
  109. package/plugins/terminal/sizes.ts +60 -0
  110. package/plugins/terminal/style.css +71 -0
  111. package/skills/whiteboard/SKILL.md +292 -0
  112. package/solid/index.tsx +56 -0
  113. package/src/auth.ts +66 -0
  114. package/src/client/forms.ts +186 -0
  115. package/src/client/page/base.css +274 -0
  116. package/src/client/page/fresh.ts +205 -0
  117. package/src/client/page/main.ts +249 -0
  118. package/src/client/page/orphans.ts +46 -0
  119. package/src/client/page/theme.ts +32 -0
  120. package/src/client/page/view.ts +9 -0
  121. package/src/client/page/waiting.ts +109 -0
  122. package/src/client/runtime.ts +170 -0
  123. package/src/config.ts +96 -0
  124. package/src/define.ts +546 -0
  125. package/src/events.ts +194 -0
  126. package/src/html.ts +40 -0
  127. package/src/load.ts +89 -0
  128. package/src/loader.ts +109 -0
  129. package/src/package.ts +39 -0
  130. package/src/pages/blocks.ts +67 -0
  131. package/src/pages/index.ts +36 -0
  132. package/src/pages/markdown.ts +276 -0
  133. package/src/pages/render.ts +196 -0
  134. package/src/plugins.ts +197 -0
  135. package/src/running.ts +22 -0
  136. package/src/serve.ts +52 -0
  137. package/src/server.ts +644 -0
  138. package/src/status.ts +69 -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,58 @@
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.
6
+
7
+ export type DiffKind = 'add' | 'del' | 'context' | 'hunk' | 'file' | 'meta'
8
+
9
+ export interface DiffLineInfo {
10
+ kind: DiffKind
11
+ /** a line in a hunk: its number, in the new file (a removed line: in the old one) */
12
+ n?: number
13
+ /** `file:line`, for a line in a hunk of a known file */
14
+ at?: string
15
+ /** what to show instead of the line: a file's name, a hunk's start */
16
+ label?: string
17
+ }
18
+
19
+ const unprefix = (p: string) => p.replace(/\t.*$/, '').replace(/^"|"$/g, '').replace(/^[ab]\//, '')
20
+
21
+ export function diffLines(text: string): DiffLineInfo[] {
22
+ let file = '', oldFile = '', oldN = 0, newN = 0, inHunk = false
23
+ let status = '', renamedFrom = ''
24
+ return text.split('\n').map((line): DiffLineInfo => {
25
+ const at = /^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line)
26
+ if (at) {
27
+ oldN = Number(at[1]); newN = Number(at[2]); inHunk = true
28
+ // only a mark that lines were skipped: the numbers are in the gutter, and the text git adds
29
+ // after them is a guess at the enclosing line, often just the one before
30
+ return { kind: 'hunk', label: '' }
31
+ }
32
+ if (inHunk) {
33
+ const sign = line[0]
34
+ const where = (side: 'old' | 'new', n: number): DiffLineInfo | {} => {
35
+ const name = side === 'old' ? oldFile || file : file || oldFile
36
+ return name ? { n, at: side === 'old' ? `${name} (old):${n}` : `${name}:${n}` } : { n }
37
+ }
38
+ if (sign === '+') return { kind: 'add', ...where('new', newN++) }
39
+ if (sign === '-') return { kind: 'del', ...where('old', oldN++) }
40
+ if (sign === ' ') { oldN++; return { kind: 'context', ...where('new', newN++) } }
41
+ // "" belongs to the hunk; anything else ends it
42
+ if (sign === '\\') return { kind: 'meta' }
43
+ inHunk = false
44
+ }
45
+ if (line.startsWith('diff --git ')) { file = ''; oldFile = ''; status = ''; renamedFrom = '' }
46
+ else if (line.startsWith('new file mode')) status = 'new'
47
+ else if (line.startsWith('deleted file mode')) status = 'deleted'
48
+ else if (line.startsWith('rename from ')) renamedFrom = line.slice('rename from '.length)
49
+ else if (line.startsWith('--- ')) oldFile = line === '--- /dev/null' ? '' : unprefix(line.slice(4))
50
+ else if (line.startsWith('+++ ')) {
51
+ // the file's name, once its headers are read: what the reader sees of them
52
+ file = line === '+++ /dev/null' ? '' : unprefix(line.slice(4))
53
+ const name = renamedFrom ? `${renamedFrom} → ${file}` : file || oldFile
54
+ return { kind: 'file', label: status ? `${name} (${status})` : name }
55
+ }
56
+ return { kind: 'meta' }
57
+ })
58
+ }
@@ -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; }