@bigmistqke/whiteboard 0.6.0 → 0.7.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 (61) hide show
  1. package/README.md +13 -7
  2. package/dist/bin/whiteboard.js +0 -0
  3. package/dist/client/runtime.js +235 -58
  4. package/dist/plugins/badge/README.md +3 -1
  5. package/dist/plugins/badge/public/style.css +17 -0
  6. package/dist/plugins/chat/public/client.js +2 -2
  7. package/dist/plugins/chat/public/style.css +9 -0
  8. package/dist/plugins/comments/public/client.js +7 -2
  9. package/dist/plugins/comments/public/style.css +4 -0
  10. package/dist/plugins/grid/public/style.css +0 -5
  11. package/dist/plugins/index.js +5 -1
  12. package/dist/plugins/timeline/README.md +40 -0
  13. package/dist/plugins/timeline/index.js +82 -0
  14. package/dist/plugins/timeline/public/style.css +118 -0
  15. package/dist/plugins/toc/README.md +13 -13
  16. package/dist/plugins/toc/index.js +5 -3
  17. package/dist/plugins/toc/public/client.js +1 -1
  18. package/dist/plugins/toc/public/style.css +19 -2
  19. package/dist/solid/index.js +2 -1
  20. package/dist/src/auth.js +7 -3
  21. package/dist/src/client/page/base.css +74 -35
  22. package/dist/src/client/page/main.js +15 -4
  23. package/dist/src/entries.js +9 -3
  24. package/dist/src/events.js +4 -2
  25. package/dist/src/hub.js +43 -18
  26. package/dist/src/serve.js +2 -0
  27. package/dist/src/server.js +49 -11
  28. package/dist/src/ws.js +128 -0
  29. package/package.json +1 -1
  30. package/plugins/badge/README.md +3 -1
  31. package/plugins/badge/public/style.css +17 -0
  32. package/plugins/chat/client.tsx +34 -6
  33. package/plugins/chat/public/style.css +9 -0
  34. package/plugins/comments/client.ts +48 -5
  35. package/plugins/comments/public/style.css +4 -0
  36. package/plugins/grid/public/style.css +0 -5
  37. package/plugins/index.ts +6 -1
  38. package/plugins/timeline/README.md +40 -0
  39. package/plugins/timeline/index.ts +92 -0
  40. package/plugins/timeline/public/style.css +118 -0
  41. package/plugins/toc/README.md +13 -13
  42. package/plugins/toc/client.ts +49 -30
  43. package/plugins/toc/index.ts +12 -12
  44. package/plugins/toc/public/style.css +19 -2
  45. package/skills/whiteboard/SKILL.md +1 -2
  46. package/solid/index.tsx +3 -0
  47. package/src/auth.ts +7 -2
  48. package/src/client/forms.ts +102 -5
  49. package/src/client/page/base.css +74 -35
  50. package/src/client/page/elsewhere.ts +12 -3
  51. package/src/client/page/waiting.ts +10 -5
  52. package/src/client/read.ts +16 -3
  53. package/src/client/runtime.ts +134 -45
  54. package/src/client/toast.ts +35 -5
  55. package/src/define.ts +10 -0
  56. package/src/entries.ts +14 -3
  57. package/src/events.ts +7 -1
  58. package/src/hub.ts +54 -22
  59. package/src/serve.ts +2 -0
  60. package/src/server.ts +73 -17
  61. package/src/ws.ts +155 -0
package/dist/src/ws.js ADDED
@@ -0,0 +1,128 @@
1
+ import crypto from "node:crypto";
2
+ // src/ws.ts
3
+ const GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
4
+ const PING_EVERY = 25e3;
5
+ /** Is this request asking to become a WebSocket? */
6
+ const isUpgrade = (req) => (req.headers.upgrade ?? "").toLowerCase() === "websocket" && !!req.headers["sec-websocket-key"];
7
+ /** Answer the handshake and give the socket; null (and the connection refused) if it isn't one. */
8
+ function accept(req, conn) {
9
+ const key = req.headers["sec-websocket-key"];
10
+ if (!isUpgrade(req) || typeof key !== "string" || req.headers["sec-websocket-version"] !== "13") {
11
+ refuse(conn, 400, "Bad Request");
12
+ return null;
13
+ }
14
+ const hash = crypto.createHash("sha1").update(key + GUID).digest("base64");
15
+ conn.write(`HTTP/1.1 101 Switching Protocols\r
16
+ Upgrade: websocket\r
17
+ Connection: Upgrade\r
18
+ Sec-WebSocket-Accept: ${hash}\r\n\r\n`);
19
+ if ("setNoDelay" in conn && typeof conn.setNoDelay === "function") conn.setNoDelay(true);
20
+ let open = true;
21
+ const closers = [];
22
+ const ended = () => {
23
+ if (!open) return;
24
+ open = false;
25
+ clearInterval(ping);
26
+ for (const fn of closers.splice(0)) fn();
27
+ };
28
+ const write = (opcode, payload) => {
29
+ if (!open || conn.destroyed) return;
30
+ conn.write(Buffer.concat([header(opcode, payload.length), payload]));
31
+ };
32
+ let alive = true;
33
+ const ping = setInterval(() => {
34
+ if (!alive) return void conn.destroy();
35
+ alive = false;
36
+ write(9, Buffer.alloc(0));
37
+ }, PING_EVERY);
38
+ ping.unref?.();
39
+ let buf = Buffer.alloc(0);
40
+ conn.on("data", (chunk) => {
41
+ buf = Buffer.concat([buf, chunk]);
42
+ for (;;) {
43
+ const frame = parse(buf);
44
+ if (!frame) break;
45
+ buf = buf.subarray(frame.size);
46
+ alive = true;
47
+ if (frame.opcode === 8) {
48
+ write(8, frame.payload.subarray(0, 2));
49
+ ended();
50
+ conn.end();
51
+ return;
52
+ }
53
+ if (frame.opcode === 9) write(10, frame.payload);
54
+ }
55
+ if (buf.length > 1 << 20) conn.destroy();
56
+ });
57
+ conn.on("close", ended);
58
+ conn.on("error", () => conn.destroy());
59
+ return {
60
+ send: (text) => write(1, Buffer.from(text, "utf8")),
61
+ close() {
62
+ if (!open) return;
63
+ write(8, Buffer.from([3, 232]));
64
+ ended();
65
+ conn.end();
66
+ setTimeout(() => conn.destroy(), 1e3).unref?.();
67
+ },
68
+ onClose(fn) {
69
+ if (open) closers.push(fn);
70
+ else fn();
71
+ }
72
+ };
73
+ }
74
+ /** Turn an upgrade down with a plain HTTP response. */
75
+ function refuse(conn, status, text) {
76
+ if (conn.destroyed) return;
77
+ conn.end(`HTTP/1.1 ${status} ${text}\r\nConnection: close\r\nContent-Length: 0\r\n\r\n`);
78
+ }
79
+ /** A frame's header, unmasked as the server sends them, FIN set. */
80
+ function header(opcode, length) {
81
+ if (length < 126) return Buffer.from([128 | opcode, length]);
82
+ if (length < 65536) {
83
+ const h = Buffer.alloc(4);
84
+ h[0] = 128 | opcode;
85
+ h[1] = 126;
86
+ h.writeUInt16BE(length, 2);
87
+ return h;
88
+ }
89
+ const h = Buffer.alloc(10);
90
+ h[0] = 128 | opcode;
91
+ h[1] = 127;
92
+ h.writeBigUInt64BE(BigInt(length), 2);
93
+ return h;
94
+ }
95
+ /** One whole frame at the start of `buf` (unmasked), or null while it hasn't all arrived. */
96
+ function parse(buf) {
97
+ if (buf.length < 2) return null;
98
+ const opcode = buf[0] & 15;
99
+ const masked = (buf[1] & 128) !== 0;
100
+ let length = buf[1] & 127;
101
+ let at = 2;
102
+ if (length === 126) {
103
+ if (buf.length < 4) return null;
104
+ length = buf.readUInt16BE(2);
105
+ at = 4;
106
+ } else if (length === 127) {
107
+ if (buf.length < 10) return null;
108
+ const big = buf.readBigUInt64BE(2);
109
+ if (big > BigInt(1 << 20)) return {
110
+ opcode: 8,
111
+ payload: Buffer.alloc(0),
112
+ size: buf.length
113
+ };
114
+ length = Number(big);
115
+ at = 10;
116
+ }
117
+ const mask = masked ? buf.subarray(at, at + 4) : null;
118
+ if (masked) at += 4;
119
+ if (buf.length < at + length) return null;
120
+ const payload = Buffer.from(buf.subarray(at, at + length));
121
+ if (mask) for (let i = 0; i < payload.length; i++) payload[i] = payload[i] ^ mask[i % 4];
122
+ return {
123
+ opcode,
124
+ payload,
125
+ size: at + length
126
+ };
127
+ }
128
+ export { accept, isUpgrade, refuse };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bigmistqke/whiteboard",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "A whiteboard for you and your agent: it writes markdown pages you read live in the browser, you answer through forms it waits on",
5
5
  "keywords": [
6
6
  "agent",
@@ -15,7 +15,9 @@ green, `active new info` blue, `open todo maybe warning` amber,
15
15
  The release is :badge[blocked] on review.
16
16
  ```
17
17
 
18
- A grid card whose heading ends in a badge shows it at the card's right edge.
18
+ A badge that ends a heading sits at its right edge: a page's heading, a grid
19
+ card's, a timeline step's. A block of your own can have the same for a title
20
+ that isn't a heading, with a `data-wb-title` attribute on it.
19
21
 
20
22
  ### Parser :badge[done]
21
23
 
@@ -28,3 +28,20 @@
28
28
  .wb-badge:is([data-badge='blocked'], [data-badge='no'], [data-badge='bad'], [data-badge='error']) {
29
29
  --c: var(--bad);
30
30
  }
31
+
32
+ /* a badge that ends a title sits at its right edge, centred on its first line: in any heading,
33
+ and in anything a block marks as a title (data-wb-title: a timeline's step). --wb-title-line is
34
+ the title's line height in px (registered, so 1lh resolves on the title, not on the badge) */
35
+ @property --wb-title-line {
36
+ syntax: '<length>';
37
+ inherits: true;
38
+ initial-value: 0px;
39
+ }
40
+ :is(h1, h2, h3, h4, h5, h6, [data-wb-title]):has(> .wb-badge:last-child) {
41
+ --wb-title-line: 1lh;
42
+ }
43
+ :is(h1, h2, h3, h4, h5, h6, [data-wb-title]) > .wb-badge:last-child {
44
+ float: right;
45
+ margin: calc(var(--wb-title-line) / 2 - 0.5em) 0 0 var(--space-lg);
46
+ vertical-align: baseline;
47
+ }
@@ -14,6 +14,8 @@ interface Message {
14
14
  id: string
15
15
  from: 'you' | 'agent'
16
16
  text: string
17
+ /** shown at once, but the server hasn't confirmed it yet */
18
+ sending?: boolean
17
19
  }
18
20
 
19
21
  /**
@@ -49,7 +51,12 @@ function Chat(props: ChatProps) {
49
51
  props.form && e.data.replyTo ? events.find(x => x.id === e.data.replyTo && x.type === props.form) : undefined
50
52
  const asked = to ? written(to, props.choices) : ''
51
53
  if (to && asked && !out.some(m => m.id === to.id)) out.push({ id: to.id, from: 'you', text: asked })
52
- out.push({ id: e.id, from: e.from === 'cli' ? 'agent' : 'you', text: String(e.data.text ?? '') })
54
+ out.push({
55
+ id: e.id,
56
+ from: e.from === 'cli' ? 'agent' : 'you',
57
+ text: String(e.data.text ?? ''),
58
+ sending: !!e.sending,
59
+ })
53
60
  }
54
61
  return out
55
62
  })
@@ -57,10 +64,26 @@ function Chat(props: ChatProps) {
57
64
  const post = async () => {
58
65
  const text = box?.value.trim()
59
66
  if (!text) return
60
- await wb.append(MESSAGE, { chat: props.chat, text })
61
- box!.value = ''
62
- // measure again: a value set from code fires no input event
63
- box!.dispatchEvent(new Event('input'))
67
+ // optimistic: the message is in the chat at once, grey until the server has it, and the box is
68
+ // empty, so a second Send has nothing to send again
69
+ const write = (value: string) => {
70
+ box!.value = value
71
+ // measure again: a value set from code fires no input event
72
+ box!.dispatchEvent(new Event('input'))
73
+ }
74
+ write('')
75
+ try {
76
+ await wb.append(MESSAGE, { chat: props.chat, text })
77
+ } catch (err) {
78
+ console.error(err)
79
+ // it never got there: back into the box, unless something else was written meanwhile
80
+ if (!box!.value.trim()) write(text)
81
+ wb.toast("Couldn't send the message", {
82
+ kind: 'alert',
83
+ name: 'chat-failed',
84
+ details: "It's back in the box: send it again.",
85
+ })
86
+ }
64
87
  }
65
88
  return (
66
89
  // a div, not a form: forms don't nest, and the box has no name, so it is never part of an answer
@@ -69,7 +92,12 @@ function Chat(props: ChatProps) {
69
92
  {m => (
70
93
  // the reader's messages on the right, the agent's on the left
71
94
  // the agent's: read once in view (the core marks it, see EventSpec.forReader)
72
- <div class="wb-chat-message" data-from={m.from} data-wb-message={m.from === 'agent' ? m.id : undefined}>
95
+ <div
96
+ class="wb-chat-message"
97
+ data-from={m.from}
98
+ data-sending={m.sending ? '' : undefined}
99
+ data-wb-message={m.from === 'agent' ? m.id : undefined}
100
+ >
73
101
  <span class="wb-chat-who">{m.from}</span>
74
102
  <Bubble text={m.text} />
75
103
  </div>
@@ -23,6 +23,15 @@
23
23
  align-self: flex-end;
24
24
  align-items: flex-end;
25
25
  }
26
+ /* shown at once, grey until the server has it */
27
+ .wb-chat-message[data-from='you'][data-sending] .wb-chat-bubble {
28
+ background: var(--wb-sending, #9a9fa7);
29
+ border-color: var(--wb-sending, #9a9fa7);
30
+ color: var(--bg);
31
+ transition:
32
+ background-color 0.2s,
33
+ border-color 0.2s;
34
+ }
26
35
  .wb-chat-who {
27
36
  font: var(--weight-strong) var(--ui-tiny)/1.3 var(--sans);
28
37
  letter-spacing: var(--caps-spacing);
@@ -230,7 +230,13 @@ function renderPopover() {
230
230
  const message = (e: WbEvent) => {
231
231
  const text = String(e.data.text ?? '')
232
232
  const md = rendered.get(text)
233
- return html`<div class="wb-thread-message" data-from="${who(e)}" data-wb-message="${e.id}">
233
+ // shown at once, not yet confirmed by the server
234
+ return html`<div
235
+ class="wb-thread-message"
236
+ data-from="${who(e)}"
237
+ data-wb-message="${e.id}"
238
+ ${e.sending ? raw(' data-sending') : ''}
239
+ >
234
240
  <span class="wb-thread-who">${who(e)}</span>
235
241
  <div class="wb-thread-text" ${md != null ? raw(' data-md') : ''}>${md != null ? raw(md) : text}</div>
236
242
  </div>`
@@ -519,13 +525,31 @@ const mount: PluginMount = (el: HTMLElement, handle: PageHandle, core: Api) => {
519
525
  e.preventDefault()
520
526
  const text = composer.querySelector('textarea')!.value.trim()
521
527
  if (!text || !pending) return
522
- const saved = await api.append(NEW, { ...pending, text })
528
+ // optimistic: the thread is on the page at once (append puts the comment in the events), open
529
+ const where = pending
530
+ const sent = api.append(NEW, { ...where, text })
531
+ const shown = api.events().findLast(e => e.type === NEW && e.sending && e.data.text === text)
523
532
  composer.querySelector('textarea')!.value = ''
524
533
  composer.hidden = true
525
534
  pending = null
526
535
  getSelection()?.removeAllRanges()
527
536
  refreshComments()
528
- openPopover(saved.id)
537
+ if (shown) openPopover(shown.id)
538
+ try {
539
+ await sent
540
+ } catch (err) {
541
+ console.error('comments:', err)
542
+ // it never got there: the composer again, with the passage and the text
543
+ if (!composer.hidden) return
544
+ pending = where
545
+ composer.querySelector('textarea')!.value = text
546
+ composer.hidden = false
547
+ api.toast("Couldn't send the comment", {
548
+ kind: 'alert',
549
+ name: 'comment-failed',
550
+ details: "It's back in the box: send it again.",
551
+ })
552
+ }
529
553
  })
530
554
  composer.querySelector('[data-cancel]')!.addEventListener('click', () => {
531
555
  composer.hidden = true
@@ -541,9 +565,23 @@ const mount: PluginMount = (el: HTMLElement, handle: PageHandle, core: Api) => {
541
565
  const box = popover.querySelector('textarea')!
542
566
  const text = box.value.trim()
543
567
  if (!text || !openThread) return
544
- await api.append(REPLY, { replyTo: openThread, text })
568
+ // optimistic: the reply is in the thread at once, marked sending, and the box is free
569
+ const thread = openThread
545
570
  box.value = ''
571
+ const sent = api.append(REPLY, { replyTo: thread, text })
546
572
  refreshComments()
573
+ try {
574
+ await sent
575
+ } catch (err) {
576
+ console.error('comments:', err)
577
+ const again = popover.querySelector('textarea')
578
+ if (openThread === thread && again && !again.value.trim()) again.value = text
579
+ api.toast("Couldn't send the reply", {
580
+ kind: 'alert',
581
+ name: 'comment-failed',
582
+ details: "It's back in the box: send it again.",
583
+ })
584
+ }
547
585
  })
548
586
  popover.addEventListener('click', async e => {
549
587
  if (!(e.target as HTMLElement).closest('[data-close]') || !openThread) return
@@ -551,8 +589,13 @@ const mount: PluginMount = (el: HTMLElement, handle: PageHandle, core: Api) => {
551
589
  closePopover()
552
590
  // closed, the thread's replies ask nothing of the reader any more
553
591
  api.markRead(threads.find(t => t.id === id)?.replies.map(r => r.id) ?? [])
554
- await api.append(RESOLVE, { replyTo: id })
592
+ // optimistic: the thread leaves the page at once; should that not get through, it comes back
593
+ const sent = api.append(RESOLVE, { replyTo: id })
555
594
  refreshComments()
595
+ sent.catch(err => {
596
+ console.error('comments:', err)
597
+ api.toast("Couldn't close the thread", { kind: 'alert', name: 'comment-failed', details: "It's open again." })
598
+ })
556
599
  })
557
600
  // the page goes to an unread reply here: open its thread
558
601
  layer.addEventListener('wb-reveal', e => {
@@ -183,3 +183,7 @@
183
183
  font: var(--ui-small) var(--mono, monospace);
184
184
  color: var(--muted, #696e76);
185
185
  }
186
+ /* shown at once, grey until the server has it */
187
+ .wb-thread-message[data-sending] .wb-thread-text {
188
+ color: var(--wb-sending, #9a9fa7);
189
+ }
@@ -35,8 +35,3 @@
35
35
  margin-bottom: 0;
36
36
  }
37
37
 
38
- /* a badge that ends the heading (`### Parser :badge[done]`) sits at the card's right edge */
39
- .wb-card h3 > .wb-badge:last-child {
40
- float: right;
41
- margin: 0.45em 0 0 var(--space-lg);
42
- }
package/plugins/index.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  // The built-in plugins: what every project has, before its config's `plugins` (which add to them,
2
2
  // and replace one of the same name: `code({ themes })` for the default `code`). The terminal and the
3
3
  // Claude Code plugin are not among them: they run code, or are about one agent, so a project adds
4
- // them itself.
4
+ // them itself. The table of contents is, but shows only on a page that asks for it, unless the
5
+ // config replaces it with `toc({ always: true })`.
5
6
  import type { Plugin } from '../src/define.ts'
6
7
  import callout from './callout/index.ts'
7
8
  import chat from './chat/index.ts'
@@ -20,6 +21,8 @@ import draft from './draft/index.ts'
20
21
  import lightbox from './lightbox/index.ts'
21
22
  import badge from './badge/index.ts'
22
23
  import ansi from './ansi/index.ts'
24
+ import timeline from './timeline/index.ts'
25
+ import toc from './toc/index.ts'
23
26
 
24
27
  export function builtins(): Plugin[] {
25
28
  return [
@@ -40,5 +43,7 @@ export function builtins(): Plugin[] {
40
43
  lightbox(),
41
44
  badge(),
42
45
  ansi(),
46
+ timeline(),
47
+ toc(),
43
48
  ]
44
49
  }
@@ -0,0 +1,40 @@
1
+ Steps in order down a line, a dot per step: what was done and what it showed.
2
+ Each step has room for full sentences, unlike a diagram's boxes.
3
+
4
+ A heading starts a step, titled by it: the shallowest headings in the block are
5
+ its steps, whatever their level. Deeper headings are the step's own and stay
6
+ headings in its body ("What it does", "Result"). A step's body is any markdown,
7
+ blocks too: a takeaway that should stand out can be a `:::callout` (the
8
+ timeline around it then takes four colons, `::::timeline`). A `---` starts an
9
+ untitled step. A badge ending a step's heading sits at its right. A
10
+ `:when[2026-11-21 10:43]` in a step's heading is when it happened: shown above
11
+ the title, as written.
12
+
13
+ **Sub steps.** A timeline inside a step is that step's sub steps: drawn a level
14
+ in, on a thinner line, and a timeline inside one of those again a level
15
+ further, to any depth. The outer timeline then needs more colons than the inner
16
+ one (`::::timeline` around `:::timeline`). That is also how steps are grouped:
17
+ a step for the group, its steps in a timeline inside it.
18
+
19
+ ````markdown
20
+ ::::timeline[Steps so far]
21
+ ### :when[2026-11-21 10:43] Made a minimal repro :badge[done]
22
+ A chain of 6,400 closures overflows the stack: the crash is the chain, not
23
+ Solid.
24
+
25
+ ### Counted the visits
26
+ 5,150 visits for 100 links: each link walks the whole chain again.
27
+
28
+ :::timeline
29
+ ### With the probe off
30
+ #### What it does
31
+ Runs the same chain with the probe switched off.
32
+
33
+ #### Result
34
+ The same 5,150: the probe isn't what walks it.
35
+
36
+ ### With the cache on
37
+ 100 visits: one per link.
38
+ :::
39
+ ::::
40
+ ````
@@ -0,0 +1,92 @@
1
+ // Timeline: steps in order down a line, a dot per step, each with room for full sentences: what was
2
+ // done and what it showed. The shallowest headings in it are its steps; deeper headings are a
3
+ // step's own. A `:when[…]` in a step's heading is when it happened. A timeline inside a step is
4
+ // that step's sub steps, drawn a level in (style.css). Rendered on the server; no script.
5
+ import fs from 'node:fs'
6
+ import { definePlugin, html, raw, type DirectiveContext, type Plugin } from '@bigmistqke/whiteboard'
7
+ import { builtinPublic } from '../../src/package.ts'
8
+
9
+ type Node = {
10
+ type: string
11
+ name?: string
12
+ value?: string
13
+ depth?: number
14
+ children?: Node[]
15
+ }
16
+
17
+ interface Step {
18
+ title: Node[] | null
19
+ body: Node[]
20
+ }
21
+
22
+ const text = (node: Node): string => node.value ?? (node.children ?? []).map(text).join('')
23
+
24
+ /** A when's text. Its `:59` in `19:59` parses as a directive named `59`: put it back as written. */
25
+ const whenText = (node: Node): string =>
26
+ (node.children ?? [])
27
+ .map(n => (n.type === 'textDirective' ? `:${n.name}${n.children?.length ? `[${text(n)}]` : ''}` : text(n)))
28
+ .join('')
29
+
30
+ /** Takes a `:when[…]` out of a heading: its text, and the heading without it. */
31
+ export function when(title: Node[]): { when: string | null; title: Node[] } {
32
+ const found = title.find(n => n.type === 'textDirective' && n.name === 'when')
33
+ if (!found) return { when: null, title }
34
+ const rest = title.filter(n => n !== found)
35
+ // the space that separated it from the title
36
+ const last = rest[rest.length - 1]
37
+ if (last?.type === 'text' && last.value) last.value = last.value.trimEnd()
38
+ const first = rest[0]
39
+ if (first?.type === 'text' && first.value) first.value = first.value.trimStart()
40
+ return { when: whenText(found).trim(), title: rest }
41
+ }
42
+
43
+ /**
44
+ * Splits the block's content into steps: a heading at the shallowest level in it starts one, titled
45
+ * by it, and so does a `---`, untitled. Deeper headings are part of a step's body. Content before
46
+ * the first heading is an untitled step.
47
+ */
48
+ export function parse(children: Node[]): Step[] {
49
+ const depth = Math.min(...children.filter(n => n.type === 'heading').map(n => n.depth!))
50
+ const steps: Step[] = []
51
+ for (const node of children) {
52
+ if (node.type === 'heading' && node.depth === depth) steps.push({ title: node.children ?? [], body: [] })
53
+ else if (node.type === 'thematicBreak') steps.push({ title: null, body: [] })
54
+ else {
55
+ if (!steps.length) steps.push({ title: null, body: [] })
56
+ steps[steps.length - 1].body.push(node)
57
+ }
58
+ }
59
+ return steps
60
+ }
61
+
62
+ /** A step: when, title and body; a timeline in its body (its sub steps) renders as its own block. */
63
+ async function renderStep(ctx: DirectiveContext, step: Step) {
64
+ const split = step.title ? when(step.title) : { when: null, title: null }
65
+ const title = split.title && (await ctx.toHtml(split.title))
66
+ const body = await ctx.toHtml(step.body)
67
+ return html`<li class="wb-tl-step">
68
+ ${split.when && html`<time class="wb-tl-when">${split.when}</time>`}
69
+ ${title && html`<div class="wb-tl-title" data-wb-title>${raw(title)}</div>`}
70
+ ${body.trim() && html`<div class="wb-tl-body">${raw(body)}</div>`}
71
+ </li>`
72
+ }
73
+
74
+ async function render(ctx: DirectiveContext) {
75
+ const steps = await Promise.all(parse(ctx.children as Node[]).map(step => renderStep(ctx, step)))
76
+ return html`<div class="wb-timeline">
77
+ ${ctx.label && html`<div class="wb-tl-caption">${raw(ctx.label)}</div>`}
78
+ <ol class="wb-tl-steps">
79
+ ${steps}
80
+ </ol>
81
+ </div>`
82
+ }
83
+
84
+ export default function timeline(): Plugin {
85
+ return definePlugin({
86
+ name: 'timeline',
87
+ public: builtinPublic('timeline'),
88
+ style: 'style.css',
89
+ blocks: { timeline: { html: render } },
90
+ docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
91
+ })
92
+ }
@@ -0,0 +1,118 @@
1
+ .wb-timeline {
2
+ margin: 3em 0;
3
+ text-align: left;
4
+ }
5
+ .wb-tl-caption {
6
+ font-family: var(--sans);
7
+ font-size: var(--ui-small);
8
+ color: var(--muted);
9
+ text-transform: uppercase;
10
+ letter-spacing: 0.06em;
11
+ margin-bottom: var(--space-2xl);
12
+ }
13
+ .wb-tl-steps {
14
+ list-style: none;
15
+ margin: 0;
16
+ padding: 0;
17
+ }
18
+
19
+ /* a line down the left, a dot per step */
20
+ .wb-tl-steps {
21
+ position: relative;
22
+ padding-left: 28px;
23
+ }
24
+ .wb-tl-steps::before {
25
+ content: '';
26
+ position: absolute;
27
+ left: 7px;
28
+ top: 6px;
29
+ bottom: 6px;
30
+ width: 2px;
31
+ background: var(--border);
32
+ }
33
+ .wb-tl-step {
34
+ position: relative;
35
+ padding-bottom: var(--space-2xl);
36
+ }
37
+ .wb-tl-step:last-child {
38
+ padding-bottom: 0;
39
+ }
40
+ /* the dot sits on the title's first line; a step without a title has it at its top */
41
+ .wb-tl-step:has(> .wb-tl-title)::before {
42
+ display: none;
43
+ }
44
+ .wb-tl-title {
45
+ position: relative;
46
+ }
47
+ .wb-tl-step::before,
48
+ .wb-tl-title::before {
49
+ content: '';
50
+ position: absolute;
51
+ left: -27px;
52
+ top: 0.45em;
53
+ width: 12px;
54
+ height: 12px;
55
+ border-radius: 50%;
56
+ background: var(--bg);
57
+ border: 2px solid var(--accent);
58
+ box-sizing: border-box;
59
+ }
60
+ .wb-tl-when {
61
+ display: block;
62
+ font-family: var(--mono);
63
+ font-size: var(--ui-tiny);
64
+ color: var(--muted);
65
+ line-height: 1.4;
66
+ }
67
+ /* centred on the title's first line (line-height 1.4: its middle is 0.7em down) */
68
+ .wb-tl-title::before {
69
+ top: calc(0.7em - 6px);
70
+ }
71
+ .wb-tl-title {
72
+ font-weight: 600;
73
+ line-height: 1.4;
74
+ }
75
+ .wb-tl-body {
76
+ font-size: var(--ui-text);
77
+ line-height: 1.6;
78
+ margin-top: var(--space-xs);
79
+ }
80
+ .wb-timeline .wb-tl-body > * {
81
+ margin-top: var(--space-md);
82
+ margin-bottom: 0;
83
+ }
84
+ .wb-timeline .wb-tl-body > :first-child {
85
+ margin-top: 0;
86
+ }
87
+
88
+ /* a timeline inside a step is its sub steps: a level in, a thinner line and smaller rings, the
89
+ same at every depth */
90
+ .wb-tl-body .wb-timeline {
91
+ margin: var(--space-md) 0 0;
92
+ }
93
+ .wb-tl-body .wb-tl-steps {
94
+ padding-left: 20px;
95
+ }
96
+ .wb-tl-body .wb-tl-steps::before {
97
+ left: 4px;
98
+ width: 1px;
99
+ }
100
+ .wb-tl-body .wb-tl-step {
101
+ padding-bottom: var(--space-md);
102
+ }
103
+ .wb-tl-body .wb-tl-step:last-child {
104
+ padding-bottom: 0;
105
+ }
106
+ .wb-tl-body .wb-tl-step::before,
107
+ .wb-tl-body .wb-tl-title::before {
108
+ left: -20px;
109
+ width: 9px;
110
+ height: 9px;
111
+ border-width: 1.5px;
112
+ }
113
+ .wb-tl-body .wb-tl-title::before {
114
+ top: calc(0.7em - 4.5px);
115
+ }
116
+ .wb-tl-body .wb-tl-title {
117
+ font-size: var(--ui-text);
118
+ }
@@ -1,16 +1,11 @@
1
- A table of contents beside the page, from its `##` and `###` headings,
2
- numbered as the page numbers them. The section being read is marked, a click
1
+ A table of contents beside the page, from its headings (`##` down to
2
+ `######`), numbered as the page numbers them, each section's subsections on a
3
+ line under it. The section being read is marked, a click
3
4
  goes to a section, and a section with an unanswered question or an unread
4
5
  message has a dot. It sits in the margin left of the text when the window has
5
- room; else a button in the header opens it. Opt in, in the config:
6
-
7
- ```ts
8
- import toc from '@bigmistqke/whiteboard/toc'
9
- export default defineConfig({ plugins: [toc()] })
10
- ```
11
-
12
- A page has it when its frontmatter asks, under `plugins:` (where every
13
- plugin's settings for a page go, by the plugin's name):
6
+ room; else a button in the header opens it. It is built in: a page has it
7
+ when its frontmatter asks, under `plugins:` (where every plugin's settings for
8
+ a page go, by the plugin's name):
14
9
 
15
10
  ```markdown
16
11
  ---
@@ -20,8 +15,13 @@ plugins:
20
15
  # A long page
21
16
  ```
22
17
 
23
- To have it on every page, `toc({ always: true })`; a page then goes without
24
- with `toc: false` there.
18
+ To have it on every page, replace the built-in one in the config; a page then
19
+ goes without with `toc: false` there:
20
+
21
+ ```ts
22
+ import toc from '@bigmistqke/whiteboard/toc'
23
+ export default defineConfig({ plugins: [toc({ always: true })] })
24
+ ```
25
25
 
26
26
  The panel fits its items, between a minimum and a maximum width (160px and
27
27
  320px by default). A project sets its own in the options, and a page in its