@bigmistqke/whiteboard 0.6.1 → 0.8.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 (101) hide show
  1. package/README.md +23 -216
  2. package/bin/whiteboard.ts +82 -239
  3. package/dist/bin/whiteboard.js +70 -200
  4. package/dist/client/runtime.js +297 -61
  5. package/dist/plugins/badge/README.md +3 -1
  6. package/dist/plugins/badge/public/style.css +17 -0
  7. package/dist/plugins/chat/public/client.js +2 -2
  8. package/dist/plugins/chat/public/style.css +19 -6
  9. package/dist/plugins/code/README.md +10 -0
  10. package/dist/plugins/code/index.js +31 -22
  11. package/dist/plugins/code/public/client.js +1 -0
  12. package/dist/plugins/code/public/style.css +33 -2
  13. package/dist/plugins/comments/public/client.js +7 -2
  14. package/dist/plugins/comments/public/style.css +4 -0
  15. package/dist/plugins/form/public/style.css +2 -0
  16. package/dist/plugins/grid/public/style.css +0 -5
  17. package/dist/plugins/iframe/index.js +2 -2
  18. package/dist/plugins/index.js +5 -1
  19. package/dist/plugins/lightbox/public/client.js +1 -1
  20. package/dist/plugins/terminal/index.js +7 -9
  21. package/dist/plugins/terminal/public/client.js +1 -1
  22. package/dist/plugins/terminal/public/style.css +3 -3
  23. package/dist/plugins/timeline/README.md +40 -0
  24. package/dist/plugins/timeline/index.js +82 -0
  25. package/dist/plugins/timeline/public/style.css +118 -0
  26. package/dist/plugins/toc/README.md +13 -13
  27. package/dist/plugins/toc/index.js +6 -4
  28. package/dist/plugins/toc/public/client.js +1 -1
  29. package/dist/plugins/toc/public/style.css +19 -2
  30. package/dist/plugins/window/index.js +4 -4
  31. package/dist/solid/index.js +2 -1
  32. package/dist/src/auth.js +7 -3
  33. package/dist/src/build.js +159 -0
  34. package/dist/src/client/page/base.css +74 -35
  35. package/dist/src/client/page/main.js +16 -5
  36. package/dist/src/document.js +93 -0
  37. package/dist/src/events.js +7 -15
  38. package/dist/src/hub.js +90 -53
  39. package/dist/src/listen.js +100 -0
  40. package/dist/src/listeners.js +67 -0
  41. package/dist/src/pages/index.js +1 -1
  42. package/dist/src/pages/markdown.js +23 -11
  43. package/dist/src/pages/render.js +13 -8
  44. package/dist/src/serve.js +2 -0
  45. package/dist/src/server.js +204 -514
  46. package/dist/src/site.js +173 -0
  47. package/dist/src/status.js +63 -1
  48. package/dist/src/ws.js +128 -0
  49. package/package.json +1 -1
  50. package/plugins/README.md +258 -0
  51. package/plugins/badge/README.md +3 -1
  52. package/plugins/badge/public/style.css +17 -0
  53. package/plugins/chat/client.tsx +34 -6
  54. package/plugins/chat/public/style.css +19 -6
  55. package/plugins/code/README.md +10 -0
  56. package/plugins/code/client.ts +73 -0
  57. package/plugins/code/index.ts +38 -26
  58. package/plugins/code/public/style.css +33 -2
  59. package/plugins/comments/client.ts +56 -6
  60. package/plugins/comments/public/style.css +4 -0
  61. package/plugins/form/public/style.css +2 -0
  62. package/plugins/grid/public/style.css +0 -5
  63. package/plugins/iframe/index.ts +2 -2
  64. package/plugins/index.ts +6 -1
  65. package/plugins/lightbox/client.ts +13 -3
  66. package/plugins/terminal/client.ts +10 -2
  67. package/plugins/terminal/index.ts +8 -6
  68. package/plugins/terminal/public/style.css +3 -3
  69. package/plugins/timeline/README.md +40 -0
  70. package/plugins/timeline/index.ts +92 -0
  71. package/plugins/timeline/public/style.css +118 -0
  72. package/plugins/toc/README.md +13 -13
  73. package/plugins/toc/client.ts +58 -32
  74. package/plugins/toc/index.ts +13 -13
  75. package/plugins/toc/public/style.css +19 -2
  76. package/plugins/window/index.ts +4 -4
  77. package/skills/whiteboard/SKILL.md +3 -1
  78. package/solid/index.tsx +3 -0
  79. package/src/auth.ts +7 -2
  80. package/src/build.ts +213 -0
  81. package/src/client/forms.ts +102 -5
  82. package/src/client/page/base.css +74 -35
  83. package/src/client/page/elsewhere.ts +14 -4
  84. package/src/client/page/waiting.ts +10 -5
  85. package/src/client/read.ts +16 -3
  86. package/src/client/runtime.ts +219 -52
  87. package/src/client/toast.ts +36 -6
  88. package/src/define.ts +44 -18
  89. package/src/document.ts +191 -0
  90. package/src/events.ts +18 -17
  91. package/src/hub.ts +102 -56
  92. package/src/listen.ts +125 -0
  93. package/src/listeners.ts +95 -0
  94. package/src/pages/index.ts +1 -1
  95. package/src/pages/markdown.ts +29 -18
  96. package/src/pages/render.ts +13 -8
  97. package/src/serve.ts +2 -0
  98. package/src/server.ts +223 -611
  99. package/src/site.ts +165 -0
  100. package/src/status.ts +100 -0
  101. package/src/ws.ts +155 -0
@@ -0,0 +1,95 @@
1
+ // Who listens, as a server holds it. An agent waiting for answers (`whiteboard wait` or `watch`)
2
+ // holds a connection to /api/listen for as long as it runs: that connection is the listener, gone
3
+ // when the command is. A project's server keeps its listeners in a registry; the shared server adds
4
+ // an entry's fallback to the registry of each project of the entry. The fallback (a wait or watch
5
+ // without --page) takes only what no other listener would, so its stream is told who else listens
6
+ // (`listeners`, a Snapshot), at once and on every change.
7
+ import type { IncomingMessage, ServerResponse } from 'node:http'
8
+ import type { Listener } from './define.ts'
9
+ import { covers } from './status.ts'
10
+
11
+ /** The listeners of one project, and the rule that one fallback holds it at a time. */
12
+ export interface Registry {
13
+ /** the listeners, those covering `page` when given; each a copy */
14
+ list(page?: string | null): Listener[]
15
+ /** hold `l` until the returned function is called; null when refused: a second fallback */
16
+ add(l: Listener): (() => void) | null
17
+ /** call `fn` whenever a listener comes or goes; returns what stops it */
18
+ onChange(fn: () => void): () => void
19
+ }
20
+
21
+ export function createRegistry(): Registry {
22
+ const listeners = new Set<Listener>()
23
+ const watchers = new Set<() => void>()
24
+ const changed = () => {
25
+ for (const fn of watchers) fn()
26
+ }
27
+ return {
28
+ list: (page = null) =>
29
+ [...listeners]
30
+ .filter(l => page == null || covers(l, page))
31
+ .map(({ page, type, command, fallback }) => ({ page, type, command, ...(fallback && { fallback }) })),
32
+ add(l) {
33
+ // one fallback per project: two would split what nobody else takes between them
34
+ if (l.fallback && [...listeners].some(x => x.fallback)) return null
35
+ listeners.add(l)
36
+ changed()
37
+ return () => {
38
+ if (listeners.delete(l)) changed()
39
+ }
40
+ },
41
+ onChange(fn) {
42
+ watchers.add(fn)
43
+ return () => watchers.delete(fn)
44
+ },
45
+ }
46
+ }
47
+
48
+ /** A project as a fallback's stream names it, with who listens there. */
49
+ export interface Followed {
50
+ /** the project's folder */
51
+ dir: string
52
+ /** where the server serves it: '' for a project's own server, `/rg/develop` under the shared one */
53
+ base: string
54
+ listeners: Listener[]
55
+ }
56
+
57
+ /** What a fallback's stream is told (`event: listeners`): every project it covers, now. */
58
+ export interface Snapshot {
59
+ projects: Followed[]
60
+ }
61
+
62
+ /** The listener a /api/listen request asks for. */
63
+ export const listenerOf = (q: URLSearchParams, fallback = q.has('fallback')): Listener => ({
64
+ page: q.get('page') || '',
65
+ type: q.get('type') || null,
66
+ command: q.get('command') === 'wait' ? 'wait' : 'watch',
67
+ ...(fallback && { fallback }),
68
+ })
69
+
70
+ /**
71
+ * Hold a /api/listen connection open as an event stream (with a ping, so nothing between closes it
72
+ * as idle). `send` writes an event on it; `onEnd` runs once, when the agent goes or `end` is called.
73
+ */
74
+ export function holdStream(req: IncomingMessage, res: ServerResponse, onEnd: () => void) {
75
+ res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' })
76
+ res.write(': listening\n\n')
77
+ const ping = setInterval(() => res.write(': ping\n\n'), 25000)
78
+ let ended = false
79
+ const finish = () => {
80
+ if (ended) return
81
+ ended = true
82
+ clearInterval(ping)
83
+ onEnd()
84
+ }
85
+ req.on('close', finish)
86
+ return {
87
+ send(event: string, data: unknown) {
88
+ if (!ended) res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`)
89
+ },
90
+ end() {
91
+ finish()
92
+ res.end()
93
+ },
94
+ }
95
+ }
@@ -39,6 +39,6 @@ export function createPages(options: PagesOptions): Pages {
39
39
  render: createRender(blocks, options.style, options.styles ?? [], options.links ?? [], options.composers),
40
40
  message: createMessageRender(blocks, options.links ?? []),
41
41
  docs: createDocs(blocks),
42
- questions: page => formNames(page.source),
42
+ questions: page => formNames(page.body),
43
43
  }
44
44
  }
@@ -177,25 +177,35 @@ function linkText(tree: MdNode, links: TextLink[]) {
177
177
  walk(tree)
178
178
  }
179
179
 
180
+ /**
181
+ * The address of something named relative to the page (a link, an image, a block's `src`): resolved
182
+ * from the page's folder and handed to `linkFor`. A URL, an address from the root, an anchor, or
183
+ * something nothing serves stays as written.
184
+ */
185
+ function addressOf(ref: string, file: string, linkFor?: LinkFor): string {
186
+ if (!linkFor || !file || !ref || SCHEME.test(ref) || /^[/#]/.test(ref)) return ref
187
+ const [, target, rest] = /^([^?#]*)(.*)$/s.exec(ref)!
188
+ let decoded = target
189
+ try {
190
+ decoded = decodeURIComponent(target)
191
+ } catch {
192
+ /* keep as written */
193
+ }
194
+ const url = decoded && linkFor(path.resolve(path.dirname(file), decoded))
195
+ return url ? url + rest : ref
196
+ }
197
+
198
+ /** Raw HTML's references to files beside the page (`<img src>`, `<video poster>`, `<a href>`). */
199
+ const HTML_REFS = /(\b(?:src|href|poster)=")([^"]*)(")/g
200
+
180
201
  function rewriteLinks(tree: MdNode, file: string, linkFor?: LinkFor) {
181
202
  if (!linkFor || !file) return
182
203
  const walk = (node: MdNode) => {
183
- if (
184
- (node.type === 'link' || node.type === 'image' || node.type === 'definition') &&
185
- node.url &&
186
- !SCHEME.test(node.url) &&
187
- !/^[/#]/.test(node.url)
188
- ) {
189
- const [, target, rest] = /^([^?#]*)(.*)$/s.exec(node.url)!
190
- let decoded = target
191
- try {
192
- decoded = decodeURIComponent(target)
193
- } catch {
194
- /* keep as written */
195
- }
196
- const url = decoded && linkFor(path.resolve(path.dirname(file), decoded))
197
- if (url) node.url = url + rest
198
- }
204
+ if ((node.type === 'link' || node.type === 'image' || node.type === 'definition') && node.url)
205
+ node.url = addressOf(node.url as string, file, linkFor)
206
+ // raw HTML the page writes itself, as the markdown it is in: before it is a string
207
+ if (node.type === 'html' && typeof node.value === 'string')
208
+ node.value = node.value.replace(HTML_REFS, (_, open, ref, close) => open + addressOf(ref, file, linkFor) + close)
199
209
  node.children?.forEach(walk)
200
210
  }
201
211
  walk(tree)
@@ -226,7 +236,7 @@ function literalDirective(node: MdNode, label: MdNode[] | null) {
226
236
  * code block doesn't count.
227
237
  */
228
238
  export function formNames(source: string) {
229
- const tree = parseMarkdown(String(source ?? '').replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, ''))
239
+ const tree = parseMarkdown(String(source ?? ''))
230
240
  const names: string[] = []
231
241
  const walk = (node: MdNode) => {
232
242
  if (node.type === 'containerDirective' && node.name === 'form') {
@@ -415,7 +425,8 @@ plugin "${name}" failed to compose around block "${block.name}": ${(err as Error
415
425
  }
416
426
  const markdown = async (src: string) => toHtml(parse(src).children ?? [])
417
427
  const inline = async (src: string) => (await markdown(src)).trim().replace(/^<p>([\s\S]*)<\/p>$/, '$1')
418
- const base = { page, file, dir: path.dirname(file), parseAttrs, toHtml, toString, markdown, inline }
428
+ const fileUrl = (src: string) => addressOf(src, file, linkFor)
429
+ const base = { page, file, dir: path.dirname(file), parseAttrs, toHtml, toString, markdown, inline, fileUrl }
419
430
 
420
431
  async function directive(node: MdNode): Promise<MdNode> {
421
432
  const { label, children } = splitLabel(node)
@@ -2,6 +2,7 @@
2
2
  // directives and fences handed to the plugins' blocks (markdown.ts); repository files show as highlighted
3
3
  // code, folders as listings with their README on top; around all of it goes the page's frame.
4
4
  // What the page draws itself is built with `html` (../html.ts), which escapes every value.
5
+ import { FRONTMATTER } from '../site.ts'
5
6
  import fs from 'node:fs'
6
7
  import os from 'node:os'
7
8
  import path from 'node:path'
@@ -51,8 +52,8 @@ const CODE_LANG: Record<string, string> = {
51
52
  yaml: 'yaml',
52
53
  }
53
54
 
54
- /** Frontmatter is the core's; the page starts after it. */
55
- const body = (source: string) => source.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, '')
55
+ /** Frontmatter is the core's; the page starts after it (a page's own is cut off by the site: PageSource.body). */
56
+ const body = (source: string) => source.replace(FRONTMATTER, '')
56
57
 
57
58
  /** A page's title: its first heading, else its name. */
58
59
  export function titleOf(file: string, fallback: string) {
@@ -342,10 +343,11 @@ export function createRender(
342
343
  const PLUGIN_STYLES = pluginStyles
343
344
  .map(s => ({ plugin: s.plugin, file: stylePath(s.file) }))
344
345
  .filter((s): s is { plugin: string; file: string } => !!s.file)
345
- async function markdown(source: string, page: string, file: string, ctx: RenderContext) {
346
+ /** `content`: the markdown without its frontmatter. */
347
+ async function markdown(content: string, page: string, file: string, ctx: RenderContext) {
346
348
  const { blocks: set, code } = blocks()
347
349
  const skip = ctx.skip
348
- return renderMarkdown(body(source), {
350
+ return renderMarkdown(content, {
349
351
  page,
350
352
  file,
351
353
  resolve: name => {
@@ -371,7 +373,7 @@ export function createRender(
371
373
  const { project } = ctx
372
374
  switch (page.kind) {
373
375
  case 'page':
374
- return markdown(page.source, page.id, page.file, ctx)
376
+ return markdown(page.body, page.id, page.file, ctx)
375
377
  case 'pages': {
376
378
  // the shared server's overview: every project's pages, by project, the projects with
377
379
  // something waiting first, and in each the pages waiting first
@@ -436,7 +438,7 @@ export function createRender(
436
438
  const readme = readmeOf(page)
437
439
  if (!readme) return { html: listing, title: rel || project.name, islands: {}, styles: [] }
438
440
  // markdown's output is HTML already
439
- const r = await markdown(readme.source, page.id, readme.file, ctx)
441
+ const r = await markdown(body(readme.source), page.id, readme.file, ctx)
440
442
  return {
441
443
  ...r,
442
444
  html: html`${raw(r.html)}
@@ -483,7 +485,7 @@ export function createRender(
483
485
  const nav =
484
486
  ctx.live === false
485
487
  ? html`<nav class="wb-nav">
486
- <span>${ctx.project.name}</span
488
+ <span>${ctx.project.name}</span><span data-wb-slot="header"></span
487
489
  ><button
488
490
  type="button"
489
491
  class="wb-theme"
@@ -493,7 +495,10 @@ export function createRender(
493
495
  </nav>`
494
496
  : html`<nav class="wb-nav">
495
497
  <span>${crumbs(page.id, ctx.entry?.label || ctx.project.name, ctx.base ?? '', ctx.entry)}</span
496
- >${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<button
498
+ >${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<span
499
+ data-wb-slot="header"
500
+ ></span
501
+ ><button
497
502
  type="button"
498
503
  class="wb-theme"
499
504
  title="Theme: follow the system, light or dark"
package/src/serve.ts CHANGED
@@ -25,6 +25,7 @@ export async function start(project: Project): Promise<number> {
25
25
  const server = http.createServer((req, res) => {
26
26
  void wb.handle(req, res)
27
27
  })
28
+ server.on('upgrade', (req, conn, head) => wb.upgrade(req, conn, head))
28
29
  const candidates = [
29
30
  ...new Set(
30
31
  [recordedPort(project), DEFAULT_PORT, ...Array.from({ length: 50 }, (_, i) => DEFAULT_PORT + 1 + i)].filter(
@@ -100,6 +101,7 @@ export async function startShared(dir = machineDir(), state = stateDir()): Promi
100
101
  const server = http.createServer((req, res) => {
101
102
  void hub.handle(req, res)
102
103
  })
104
+ server.on('upgrade', (req, conn, head) => hub.upgrade(req, conn, head))
103
105
  const first = config.port ?? DEFAULT_PORT
104
106
  const candidates = [
105
107
  ...new Set(