@bigmistqke/whiteboard 0.3.1 → 0.5.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 (174) hide show
  1. package/README.md +119 -18
  2. package/bin/whiteboard.ts +300 -71
  3. package/dist/bin/whiteboard.js +21 -0
  4. package/dist/client/runtime.js +132 -54
  5. package/dist/define.js +21 -2
  6. package/dist/plugins/callout/index.js +3 -1
  7. package/dist/plugins/callout/public/style.css +30 -0
  8. package/dist/plugins/chat/index.js +7 -4
  9. package/dist/plugins/chat/{client.js → public/client.js} +1 -1
  10. package/dist/plugins/chat/public/style.css +98 -0
  11. package/dist/plugins/code/index.js +3 -1
  12. package/dist/plugins/code/public/style.css +68 -0
  13. package/dist/plugins/comments/index.js +4 -5
  14. package/dist/plugins/comments/public/client.js +19 -0
  15. package/dist/plugins/comments/public/style.css +185 -0
  16. package/dist/plugins/details/index.js +7 -2
  17. package/dist/plugins/details/public/style.css +7 -0
  18. package/dist/plugins/draft/README.md +14 -0
  19. package/dist/plugins/draft/index.js +21 -0
  20. package/dist/plugins/draft/public/style.css +21 -0
  21. package/dist/plugins/form/index.js +37 -8
  22. package/dist/plugins/form/public/style.css +126 -0
  23. package/dist/plugins/gallery/index.js +4 -3
  24. package/dist/plugins/gallery/public/client.js +1 -0
  25. package/dist/plugins/gallery/public/style.css +90 -0
  26. package/dist/plugins/grid/index.js +9 -3
  27. package/dist/plugins/grid/public/style.css +71 -0
  28. package/dist/plugins/iframe/index.js +13 -14
  29. package/dist/plugins/iframe/public/style.css +13 -0
  30. package/dist/plugins/index.js +7 -1
  31. package/dist/plugins/mermaid/index.js +3 -1
  32. package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
  33. package/dist/plugins/steps/README.md +42 -0
  34. package/dist/plugins/steps/index.js +99 -0
  35. package/dist/plugins/steps/public/client.js +1 -0
  36. package/dist/plugins/steps/public/style.css +77 -0
  37. package/dist/plugins/terminal/host.js +9 -3
  38. package/dist/plugins/terminal/index.js +10 -9
  39. package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
  40. package/dist/plugins/terminal/sizes.js +2 -1
  41. package/dist/plugins/toc/README.md +21 -3
  42. package/dist/plugins/toc/index.js +12 -5
  43. package/dist/plugins/toc/public/client.js +1 -0
  44. package/dist/plugins/toc/public/style.css +99 -0
  45. package/dist/plugins/window/README.md +29 -0
  46. package/dist/plugins/window/index.js +64 -0
  47. package/dist/solid/index.js +14 -11
  48. package/dist/src/auth.js +2 -1
  49. package/dist/src/client/page/base.css +1078 -199
  50. package/dist/src/client/page/main.js +188 -39
  51. package/dist/src/loader.js +14 -3
  52. package/dist/src/package.js +3 -6
  53. package/dist/src/pages/blocks.js +8 -13
  54. package/dist/src/pages/markdown.js +1 -1
  55. package/dist/src/pages/render.js +106 -20
  56. package/dist/src/pages/window.js +31 -0
  57. package/dist/src/public.js +24 -0
  58. package/dist/src/server.js +251 -23
  59. package/package.json +5 -2
  60. package/plugins/callout/index.ts +3 -1
  61. package/plugins/callout/public/style.css +30 -0
  62. package/plugins/chat/autosize.ts +43 -9
  63. package/plugins/chat/client.tsx +22 -5
  64. package/plugins/chat/index.ts +22 -8
  65. package/plugins/chat/public/style.css +98 -0
  66. package/plugins/claude-code/index.ts +49 -14
  67. package/plugins/code/diff.ts +28 -8
  68. package/plugins/code/index.ts +33 -21
  69. package/plugins/code/public/style.css +68 -0
  70. package/plugins/comments/client.ts +139 -71
  71. package/plugins/comments/index.ts +9 -7
  72. package/plugins/comments/public/style.css +185 -0
  73. package/plugins/details/index.ts +7 -2
  74. package/plugins/details/public/style.css +7 -0
  75. package/plugins/draft/README.md +14 -0
  76. package/plugins/draft/index.ts +23 -0
  77. package/plugins/draft/public/style.css +21 -0
  78. package/plugins/forge/index.ts +16 -3
  79. package/plugins/form/index.ts +75 -27
  80. package/plugins/form/public/style.css +126 -0
  81. package/plugins/gallery/client.tsx +9 -3
  82. package/plugins/gallery/index.ts +31 -9
  83. package/plugins/gallery/public/style.css +90 -0
  84. package/plugins/grid/index.ts +19 -5
  85. package/plugins/grid/public/style.css +71 -0
  86. package/plugins/iframe/index.ts +20 -23
  87. package/plugins/iframe/public/style.css +13 -0
  88. package/plugins/index.ts +20 -1
  89. package/plugins/mermaid/index.ts +7 -3
  90. package/plugins/mermaid/{style.css → public/style.css} +7 -2
  91. package/plugins/steps/README.md +42 -0
  92. package/plugins/steps/client.ts +66 -0
  93. package/plugins/steps/index.ts +127 -0
  94. package/plugins/steps/public/style.css +77 -0
  95. package/plugins/terminal/client.ts +105 -48
  96. package/plugins/terminal/host.ts +143 -35
  97. package/plugins/terminal/index.ts +84 -25
  98. package/plugins/terminal/public/style.css +225 -0
  99. package/plugins/terminal/server.ts +64 -13
  100. package/plugins/terminal/sizes.ts +16 -4
  101. package/plugins/toc/README.md +21 -3
  102. package/plugins/toc/client.ts +90 -30
  103. package/plugins/toc/index.ts +26 -10
  104. package/plugins/toc/public/style.css +99 -0
  105. package/plugins/window/README.md +29 -0
  106. package/plugins/window/index.ts +85 -0
  107. package/skills/whiteboard/SKILL.md +3 -1
  108. package/solid/index.tsx +24 -9
  109. package/src/auth.ts +28 -9
  110. package/src/client/forms.ts +74 -28
  111. package/src/client/markdown.ts +15 -4
  112. package/src/client/page/base.css +1078 -199
  113. package/src/client/page/elsewhere.ts +32 -15
  114. package/src/client/page/fresh.ts +91 -23
  115. package/src/client/page/hold.ts +11 -2
  116. package/src/client/page/live.ts +10 -6
  117. package/src/client/page/main.ts +124 -42
  118. package/src/client/page/orphans.ts +25 -10
  119. package/src/client/page/sort.ts +14 -4
  120. package/src/client/page/sticky.ts +11 -3
  121. package/src/client/page/theme.ts +11 -7
  122. package/src/client/page/waiting.ts +141 -34
  123. package/src/client/page/window.ts +40 -0
  124. package/src/client/reactive.ts +19 -0
  125. package/src/client/read.ts +17 -10
  126. package/src/client/runtime.ts +170 -40
  127. package/src/client/scroll.ts +14 -4
  128. package/src/client/toast.ts +19 -5
  129. package/src/config.ts +20 -5
  130. package/src/define.ts +119 -31
  131. package/src/events.ts +111 -24
  132. package/src/html.ts +6 -2
  133. package/src/icons.ts +4 -2
  134. package/src/load.ts +10 -2
  135. package/src/loader.ts +81 -12
  136. package/src/package.ts +9 -8
  137. package/src/pages/blocks.ts +20 -14
  138. package/src/pages/index.ts +2 -2
  139. package/src/pages/markdown.ts +113 -34
  140. package/src/pages/render.ts +233 -41
  141. package/src/pages/window.ts +78 -0
  142. package/src/plugins.ts +90 -24
  143. package/src/public.ts +39 -0
  144. package/src/running.ts +8 -2
  145. package/src/serve.ts +29 -7
  146. package/src/server.ts +591 -110
  147. package/src/status.ts +15 -9
  148. package/dist/plugins/callout/style.css +0 -12
  149. package/dist/plugins/chat/style.css +0 -24
  150. package/dist/plugins/code/style.css +0 -29
  151. package/dist/plugins/comments/client.js +0 -10
  152. package/dist/plugins/comments/style.css +0 -40
  153. package/dist/plugins/details/style.css +0 -3
  154. package/dist/plugins/form/style.css +0 -44
  155. package/dist/plugins/gallery/client.js +0 -1
  156. package/dist/plugins/gallery/style.css +0 -27
  157. package/dist/plugins/grid/style.css +0 -25
  158. package/dist/plugins/iframe/client.js +0 -1
  159. package/dist/plugins/iframe/style.css +0 -29
  160. package/dist/plugins/toc/client.js +0 -1
  161. package/dist/plugins/toc/style.css +0 -19
  162. package/plugins/callout/style.css +0 -12
  163. package/plugins/chat/style.css +0 -24
  164. package/plugins/code/style.css +0 -29
  165. package/plugins/comments/style.css +0 -40
  166. package/plugins/details/style.css +0 -3
  167. package/plugins/form/style.css +0 -44
  168. package/plugins/gallery/style.css +0 -27
  169. package/plugins/grid/style.css +0 -25
  170. package/plugins/iframe/client.ts +0 -46
  171. package/plugins/iframe/style.css +0 -29
  172. package/plugins/terminal/style.css +0 -71
  173. package/plugins/toc/style.css +0 -19
  174. /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
@@ -40,11 +40,26 @@ function titleOf(file, fallback) {
40
40
  }
41
41
  function crumbs(id, name) {
42
42
  const parts = id ? id.split("/") : [];
43
- return html`<a href="/">${name}</a>${parts.map((p, i) => html` / <a href="/${parts.slice(0, i + 1).join("/")}">${p}</a>`)}`;
43
+ return html`<a href="/">${name}</a
44
+ >${parts.map((p, i) => html` / <a href="/${parts.slice(0, i + 1).join("/")}">${p}</a>`)}`;
44
45
  }
45
46
  const time = (ts) => html`<time data-ts="${ts}"></time>`;
46
47
  function pageList(pages) {
47
- return html`<ul class="wb-index">${pages.map((p) => html`<li><a href="/${p.id}">${titleOf(p.file, path.basename(p.id))}</a>${p.awaiting > 0 && html` <a class="wb-awaiting" href="/${p.id}#wb-waiting" title="${awaitingLabel(p.awaiting)}" aria-label="${awaitingLabel(p.awaiting)}">${raw("<svg viewBox=\"0 0 16 16\" width=\"13\" height=\"13\" aria-hidden=\"true\"><path d=\"M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\" stroke-linejoin=\"round\"/></svg>")}${p.awaiting}</a>`} <span class="wb-meta"><code>${p.id}</code> · edited ${time(p.mtime)}${p.lastEvent && html` · last submit ${time(Date.parse(p.lastEvent))}`}</span></li>`)}</ul>`;
48
+ return html`<ul class="wb-index">
49
+ ${pages.map((p) => html`<li>
50
+ <a href="/${p.id}">${titleOf(p.file, path.basename(p.id))}</a>${p.awaiting > 0 && html` <a
51
+ class="wb-awaiting"
52
+ href="/${p.id}#wb-waiting"
53
+ title="${awaitingLabel(p.awaiting)}"
54
+ aria-label="${awaitingLabel(p.awaiting)}"
55
+ >${raw("<svg viewBox=\"0 0 16 16\" width=\"13\" height=\"13\" aria-hidden=\"true\"><path d=\"M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\" stroke-linejoin=\"round\"/></svg>")}${p.awaiting}</a
56
+ >`}
57
+ <span class="wb-meta"
58
+ ><code>${p.id}</code> · edited
59
+ ${time(p.mtime)}${p.lastEvent && html` · last submit ${time(Date.parse(p.lastEvent))}`}</span
60
+ >
61
+ </li>`)}
62
+ </ul>`;
48
63
  }
49
64
  /** The folder's README, when it has one. */
50
65
  function readmeOf(page) {
@@ -118,7 +133,7 @@ function documentsIn(dir) {
118
133
  walk("");
119
134
  }
120
135
  const depth = (f) => f.split("/").length;
121
- return [...new Set(files)].sort((a, b) => depth(a) - depth(b) || a.localeCompare(b));
136
+ return [...new Set(files)].toSorted((a, b) => depth(a) - depth(b) || a.localeCompare(b));
122
137
  }
123
138
  /**
124
139
  * The overview's list of the project's documents: each by its title, linked to its rendered page,
@@ -137,9 +152,29 @@ function documentList(ctx) {
137
152
  try {
138
153
  mtime = fs.statSync(abs).mtimeMs;
139
154
  } catch {}
140
- return html`<li data-order="${i}" data-updated="${mtime}" data-title="${title.toLowerCase()}"><a href="${ctx.urlFor(abs) ?? `/-/${f}`}">${title}</a> <span class="wb-meta">${where !== "." && html`<code>${where === f ? f : `${where}/`}</code> · `}edited ${time(mtime)}</span></li>`;
155
+ return html`<li data-order="${i}" data-updated="${mtime}" data-title="${title.toLowerCase()}">
156
+ <a href="${ctx.urlFor(abs) ?? `/-/${f}`}">${title}</a>
157
+ <span class="wb-meta"
158
+ >${where !== "." && html`<code>${where === f ? f : `${where}/`}</code> · `}edited ${time(mtime)}</span
159
+ >
160
+ </li>`;
141
161
  });
142
- return html`<section class="wb-documents"><div class="wb-list-head"><h2>Documents</h2><label class="wb-sort">sort <select data-wb-sort="documents" aria-label="Sort the documents"><option value="order">by folder</option><option value="updated">last updated</option><option value="title">by title</option></select></label></div><ul class="wb-index">${rows}</ul></section>`;
162
+ return html`<section class="wb-documents">
163
+ <div class="wb-list-head">
164
+ <h2>Documents</h2>
165
+ <label class="wb-sort"
166
+ >sort
167
+ <select data-wb-sort="documents" aria-label="Sort the documents">
168
+ <option value="order">by folder</option>
169
+ <option value="updated">last updated</option>
170
+ <option value="title">by title</option>
171
+ </select></label
172
+ >
173
+ </div>
174
+ <ul class="wb-index">
175
+ ${rows}
176
+ </ul>
177
+ </section>`;
143
178
  }
144
179
  /** The plugins' text links for a project folder, asked once per folder. */
145
180
  function textLinksOf(links) {
@@ -181,16 +216,35 @@ function createMessageRender(blocks, links = []) {
181
216
  };
182
217
  };
183
218
  }
219
+ /** What a block left out of a build (RenderContext.skip) is: nothing, for its directives and fences alike. */
220
+ const skipped = (name) => ({
221
+ name,
222
+ code: false,
223
+ fences: true,
224
+ props: null,
225
+ html: () => "",
226
+ client: null,
227
+ style: null,
228
+ source: "build"
229
+ });
184
230
  function createRender(blocks, style, pluginStyles = [], links = []) {
185
231
  const textLinks = textLinksOf(links);
186
232
  const STYLE = stylePath(style);
187
- const PLUGIN_STYLES = pluginStyles.map(stylePath).filter((f) => !!f);
233
+ const PLUGIN_STYLES = pluginStyles.map((s) => ({
234
+ plugin: s.plugin,
235
+ file: stylePath(s.file)
236
+ })).filter((s) => !!s.file);
188
237
  async function markdown(source, page, file, ctx) {
189
238
  const { blocks: set, code } = blocks();
239
+ const skip = ctx.skip;
190
240
  return renderMarkdown(body(source), {
191
241
  page,
192
242
  file,
193
- resolve: (name) => set.get(name) ?? null,
243
+ resolve: (name) => {
244
+ const block = set.get(name) ?? null;
245
+ if (skip && (skip.has(name) || block && skip.has(block.source))) return skipped(name);
246
+ return block;
247
+ },
194
248
  code,
195
249
  linkFor: ctx.urlFor,
196
250
  textLinks: textLinks(ctx.project.dir)
@@ -203,7 +257,14 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
203
257
  case "pages": {
204
258
  const list = page.pages.length ? pageList(page.pages) : html`<p class="wb-meta">No pages yet.</p>`;
205
259
  return {
206
- html: page.id ? html`<h1>${page.id}</h1>${list}<p class="wb-meta">${page.file}</p>` : html`<h1>${project.name}</h1>${list}${project.files && documentList(ctx)}<p class="wb-meta"><code>${project.dir}</code>${project.files && html` · <a href="/-/">files</a>`} · <a href="/_/docs">how to write pages</a></p>`,
260
+ html: page.id ? html`<h1>${page.id}</h1>
261
+ ${list}
262
+ <p class="wb-meta">${page.file}</p>` : html`<h1>${project.name}</h1>
263
+ ${list}${project.files && documentList(ctx)}
264
+ <p class="wb-meta">
265
+ <code>${project.dir}</code>${project.files && html` · <a href="/-/">files</a>`} ·
266
+ <a href="/_/docs">how to write pages</a>
267
+ </p>`,
207
268
  title: page.id || project.name,
208
269
  islands: {},
209
270
  styles: []
@@ -211,8 +272,14 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
211
272
  }
212
273
  case "folder": {
213
274
  const rel = page.id.slice(2);
214
- const rows = page.entries.map((e) => html`<li><a href="${encodeURIComponent(e.name) + (e.dir ? "/" : "")}">${e.name + (e.dir ? "/" : "")}</a></li>`);
215
- const listing = html`<h1>${rel || project.name}</h1><ul class="wb-files">${rel && html`<li><a href="../">../</a></li>`}${rows}</ul><p class="wb-meta">${page.file} · <code>${page.id}</code></p>`;
275
+ const rows = page.entries.map((e) => html`<li>
276
+ <a href="${encodeURIComponent(e.name) + (e.dir ? "/" : "")}">${e.name + (e.dir ? "/" : "")}</a>
277
+ </li>`);
278
+ const listing = html`<h1>${rel || project.name}</h1>
279
+ <ul class="wb-files">
280
+ ${rel && html`<li><a href="../">../</a></li>`}${rows}
281
+ </ul>
282
+ <p class="wb-meta">${page.file} · <code>${page.id}</code></p>`;
216
283
  const readme = readmeOf(page);
217
284
  if (!readme) return {
218
285
  html: listing,
@@ -223,7 +290,9 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
223
290
  const r = await markdown(readme.source, page.id, readme.file, ctx);
224
291
  return {
225
292
  ...r,
226
- html: html`${raw(r.html)}<hr>${listing}`
293
+ html: html`${raw(r.html)}
294
+ <hr />
295
+ ${listing}`
227
296
  };
228
297
  }
229
298
  case "file": {
@@ -246,7 +315,8 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
246
315
  linkFor: ctx.urlFor
247
316
  });
248
317
  return {
249
- html: html`<h1 class="wb-code-title wb-wide">${page.id.slice(2)}</h1><div class="wb-code wb-wide">${raw(out.html)}</div>`,
318
+ html: html`<h1 class="wb-code-title wb-wide">${page.id.slice(2)}</h1>
319
+ <div class="wb-code wb-wide">${raw(out.html)}</div>`,
250
320
  title: path.basename(page.file),
251
321
  islands: out.islands,
252
322
  styles: out.styles
@@ -270,23 +340,39 @@ function createRender(blocks, style, pluginStyles = [], links = []) {
270
340
  client: ctx.asset(island.client)
271
341
  }]));
272
342
  const styles = out.styles.map((s) => ctx.asset(s));
273
- const nav = html`<nav class="wb-nav"><span>${crumbs(page.id, ctx.project.name)}</span>${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<button type="button" class="wb-theme" title="Theme: follow the system, light or dark" aria-label="Theme"></button><span class="wb-live" role="img" title="Connecting…" aria-label="Connecting…"></span></nav>`;
343
+ const nav = ctx.live === false ? html`<nav class="wb-nav">
344
+ <span>${ctx.project.name}</span
345
+ ><button
346
+ type="button"
347
+ class="wb-theme"
348
+ title="Theme: follow the system, light or dark"
349
+ aria-label="Theme"
350
+ ></button>
351
+ </nav>` : html`<nav class="wb-nav">
352
+ <span>${crumbs(page.id, ctx.project.name)}</span
353
+ >${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<button
354
+ type="button"
355
+ class="wb-theme"
356
+ title="Theme: follow the system, light or dark"
357
+ aria-label="Theme"
358
+ ></button
359
+ ><span class="wb-live" role="img" title="Connecting…" aria-label="Connecting…"></span>
360
+ </nav>`;
274
361
  const links = [
275
362
  ctx.asset(BASE_CSS),
276
- ...PLUGIN_STYLES.map((f) => ctx.asset(f)),
363
+ ...PLUGIN_STYLES.filter((s) => !ctx.skip?.has(s.plugin)).map((s) => ctx.asset(s.file)),
277
364
  ...styles,
278
365
  ...STYLE ? [ctx.asset(STYLE)] : []
279
366
  ];
280
367
  return {
281
368
  title: out.title || page.id || ctx.project.name,
282
- head: html`${THEME_SCRIPT}
283
- ${links.map((href) => html`<link rel="stylesheet" href="${href}">`)}`,
284
- html: html`${nav}<main id="wb-main">${raw(out.html)}</main>`,
369
+ head: html`${THEME_SCRIPT} ${links.map((href) => html`<link rel="stylesheet" href="${href}" />`)}`,
370
+ html: html`${nav}
371
+ <main id="wb-main">${raw(out.html)}</main>`,
285
372
  data: {
286
373
  islands,
287
374
  styles,
288
- mtime: page.mtime,
289
- frontmatter: page.kind === "page" ? page.frontmatter : {}
375
+ mtime: page.mtime
290
376
  }
291
377
  };
292
378
  };
@@ -305,7 +391,7 @@ A page is markdown: GFM tables, \`<details>\`, raw HTML and a hand-written \`<fo
305
391
 
306
392
  The blocks here, by the plugin that brings them (each plugin's section below says how to write them):
307
393
 
308
- ${[...byPlugin].sort(([a], [b]) => a.localeCompare(b)).map(([plugin, blocks]) => `- **${plugin}**: ${blocks.map((b) => `\`${b}\``).join(", ")}`).join("\n")}
394
+ ${[...byPlugin].toSorted(([a], [b]) => a.localeCompare(b)).map(([plugin, blocks]) => `- **${plugin}**: ${blocks.map((b) => `\`${b}\``).join(", ")}`).join("\n")}
309
395
  `;
310
396
  };
311
397
  }
@@ -0,0 +1,31 @@
1
+ import { html, raw } from "../html.js";
2
+ // src/pages/window.ts
3
+ const svg = (d) => raw(`<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round">${d}</svg>`);
4
+ const OPEN = svg("<path d=\"M9 2.5h4.5V7M13.5 2.5 7.5 8.5M11.5 9.5v3a1 1 0 0 1-1 1h-7a1 1 0 0 1-1-1v-7a1 1 0 0 1 1-1h3\"/>");
5
+ const MAXIMIZE = svg("<path d=\"M2.5 6V2.5H6M10 2.5h3.5V6M13.5 10v3.5H10M6 13.5H2.5V10\"/>");
6
+ const RESTORE = svg("<path d=\"M6 2.5V6H2.5M13.5 6H10V2.5M10 13.5V10h3.5M2.5 10H6v3.5\"/>");
7
+ /** A window around a block's HTML: see the top of this file. */
8
+ function windowFrame({ body, label = null, open = null, wide = false, bare = false, bodyClass = "", height, className = "", controls = "" }) {
9
+ const name = label != null && String(label) !== "" ? html`<span class="wb-window-title">${raw(String(label))}</span>` : open ? html`<span class="wb-window-address">${open}</span>` : html``;
10
+ const header = !bare && html`<div class="wb-window-bar">
11
+ <span class="wb-window-name" ${open ? html` title="${open}"` : ""}>${name}</span>
12
+ ${raw(String(controls))}
13
+ ${open && html`<a class="wb-window-button" href="${open}" target="_blank" rel="noopener" title="Open on its own" aria-label="Open on its own">${OPEN}</a>`}
14
+ <button
15
+ type="button"
16
+ class="wb-window-button"
17
+ data-wb-maximize
18
+ title="Fill the window"
19
+ aria-label="Fill the window"
20
+ >
21
+ <span class="wb-window-max">${MAXIMIZE}</span><span class="wb-window-restore">${RESTORE}</span>
22
+ </button>
23
+ </div>`;
24
+ return html`<div class="wb-window${wide && raw(" wb-wide")}${className && raw(" " + className)}">
25
+ ${header}
26
+ <div class="wb-window-body${bodyClass && raw(" " + bodyClass)}" ${height ? html` style="height: ${height}"` : ""}>
27
+ ${raw(String(body))}
28
+ </div>
29
+ </div>`;
30
+ }
31
+ export { windowFrame };
@@ -0,0 +1,24 @@
1
+ import path from "node:path";
2
+ import { fileURLToPath } from "node:url";
3
+ // src/public.ts
4
+ const asPath = (ref) => ref instanceof URL || ref.startsWith("file:") ? fileURLToPath(ref) : ref;
5
+ /** The folder a plugin's browser files are served from. */
6
+ function publicDirOf(plugin, wb) {
7
+ return path.resolve(plugin.public != null ? asPath(plugin.public) : path.join(wb, "public"));
8
+ }
9
+ /** Is `file` inside `dir`? */
10
+ const within = (file, dir) => {
11
+ const rel = path.relative(dir, file);
12
+ return !!rel && !rel.startsWith("..") && !path.isAbsolute(rel);
13
+ };
14
+ /**
15
+ * A file a plugin names, as a path: a name within its public folder (`style.css`,
16
+ * `client/main.js`), or a URL or absolute path, which must be inside it. Anything else is an
17
+ * error, said when the config loads, rather than a file that quietly isn't served.
18
+ */
19
+ function inPublic(dir, ref, what) {
20
+ const file = typeof ref === "string" && !ref.startsWith("file:") && !path.isAbsolute(ref) ? path.resolve(dir, ref) : path.resolve(asPath(ref));
21
+ if (!within(file, dir)) throw new Error(`${what}: ${file} is not in its public folder (${dir}); a plugin's browser files live there`);
22
+ return file;
23
+ }
24
+ export { inPublic, publicDirOf, within };
@@ -6,9 +6,12 @@ import { createAuth } from "./auth.js";
6
6
  import { FROM_SOURCE, PACKAGE_DIR } from "./package.js";
7
7
  import { awaiting, covers, fromReader, heard, questionState, unreadMessages } from "./status.js";
8
8
  import { titleOf } from "./pages/render.js";
9
+ import { inPublic } from "./public.js";
9
10
  import _taggedTemplateLiteral from "../_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js";
11
+ import { stripTypeScriptTypes } from "node:module";
10
12
  import fs from "node:fs";
11
13
  import path from "node:path";
14
+ import { parse } from "yaml";
12
15
  // src/server.ts
13
16
  var _templateObject;
14
17
  const ENGINE_DIR = PACKAGE_DIR;
@@ -66,6 +69,8 @@ function createServer(project) {
66
69
  try {
67
70
  pluginDirs.add(e.name);
68
71
  const dataDir = pluginDir(project.wb, e.name);
72
+ const dir = c.publicDirs.get(e.name);
73
+ const own = (ref) => asset(inPublic(dir, ref, `plugin ${e.name}`));
69
74
  const hooks = await e.setup?.({
70
75
  project: {
71
76
  name: project.name,
@@ -74,11 +79,13 @@ function createServer(project) {
74
79
  },
75
80
  dataDir,
76
81
  authorized: auth.authorized,
77
- asset
82
+ asset: own
78
83
  }) ?? {};
84
+ const client = hooks.client != null ? inPublic(dir, hooks.client, `plugin ${e.name}: client`) : void 0;
79
85
  return {
80
86
  name: e.name,
81
87
  ...hooks,
88
+ client,
82
89
  table: mountRoutes(e.name, hooks.routes),
83
90
  plugin: e
84
91
  };
@@ -131,12 +138,15 @@ function createServer(project) {
131
138
  async function config() {
132
139
  const c = await loader.config();
133
140
  setFiles(c.files);
141
+ publicDirs = [...c.publicDirs.values()];
134
142
  return c;
135
143
  }
136
144
  const assets = /* @__PURE__ */ new Set();
137
145
  const assetDirs = /* @__PURE__ */ new Set();
138
- const BROWSER_ASSET = /\.(m?js|css|map|svg|png|jpe?g|gif|webp|avif|ico|woff2?|ttf|otf|wasm)$/i;
139
- const servable = (file) => assets.has(file) || assetDirs.has(path.dirname(file)) && BROWSER_ASSET.test(file);
146
+ const TYPESCRIPT = /\.m?ts$/i;
147
+ const typeScriptModule = (file) => stripTypeScriptTypes(fs.readFileSync(file, "utf8"));
148
+ let publicDirs = [];
149
+ const servable = (file) => assets.has(file) || publicDirs.some((dir) => within(file, dir) && file !== dir);
140
150
  function asset(ref) {
141
151
  const file = toPath(ref);
142
152
  if (!assets.has(file)) {
@@ -156,14 +166,34 @@ function createServer(project) {
156
166
  return within(p, base) ? p : null;
157
167
  }
158
168
  const isRepoId = (id) => id === "-" || id.startsWith("-/");
169
+ /** A page's frontmatter, parsed as YAML; empty, with the reason, when it isn't fields. */
159
170
  function frontmatter(source) {
160
171
  const m = FRONTMATTER.exec(source);
161
- const out = {};
162
- if (m) for (const line of m[1].split(/\r?\n/)) {
163
- const kv = /^([\w-]+)\s*:\s*(.*)$/.exec(line);
164
- if (kv) out[kv[1]] = kv[2].trim().replace(/^["']|["']$/g, "");
172
+ if (!m) return {
173
+ data: {},
174
+ error: null
175
+ };
176
+ let data;
177
+ try {
178
+ data = parse(m[1]);
179
+ } catch (err) {
180
+ return {
181
+ data: {},
182
+ error: err.message
183
+ };
165
184
  }
166
- return out;
185
+ if (data == null) return {
186
+ data: {},
187
+ error: null
188
+ };
189
+ if (typeof data !== "object" || Array.isArray(data)) return {
190
+ data: {},
191
+ error: "it must be fields (`key: value` lines), not a single value or a list"
192
+ };
193
+ return {
194
+ data,
195
+ error: null
196
+ };
167
197
  }
168
198
  function pageSource(id, file) {
169
199
  const source = fs.readFileSync(file, "utf8");
@@ -174,7 +204,10 @@ function createServer(project) {
174
204
  mtime: fs.statSync(file).mtimeMs,
175
205
  source,
176
206
  body: source.replace(FRONTMATTER, ""),
177
- frontmatter: frontmatter(source)
207
+ ...(({ data, error }) => ({
208
+ frontmatter: data,
209
+ frontmatterError: error
210
+ }))(frontmatter(source))
178
211
  };
179
212
  }
180
213
  /** The pages in a folder of the pages folder, newest first, with when each was last answered. */
@@ -288,7 +321,9 @@ function createServer(project) {
288
321
  }
289
322
  /** A config that failed to load: the page as stored, as text, under the error. */
290
323
  function rawRender(page) {
291
- const list = (items) => html`<ul>${items.map(([href, text]) => html`<li><a href="${href}">${text}</a></li>`)}</ul>`;
324
+ const list = (items) => html`<ul>
325
+ ${items.map(([href, text]) => html`<li><a href="${href}">${text}</a></li>`)}
326
+ </ul>`;
292
327
  if (page.kind === "pages") return { html: list(page.pages.map((p) => [`/${p.id}`, p.id])) };
293
328
  if (page.kind === "folder") return { html: list(page.entries.map((e) => [encodeURIComponent(e.name) + (e.dir ? "/" : ""), e.name + (e.dir ? "/" : "")])) };
294
329
  return { html: html`<pre>${page.source}</pre>` };
@@ -302,10 +337,13 @@ function createServer(project) {
302
337
  asset,
303
338
  urlFor
304
339
  });
340
+ /** A stored page's frontmatter, for the browser (PageHandle.frontmatter): a markdown page's, else none. */
341
+ const frontmatterOf = (page) => page.kind === "page" ? page.frontmatter : {};
305
342
  /** Render a stored page as markdown, with the plugins' blocks, and anything the reader should be told. */
306
343
  async function render(page) {
307
344
  const { pages, errors } = await config();
308
345
  const notices = errors.map((e) => `config: ${e.message}`);
346
+ if (page.kind === "page" && page.frontmatterError) notices.push(`frontmatter: ${page.frontmatterError}`);
309
347
  let out;
310
348
  let rendered = !!pages;
311
349
  try {
@@ -404,28 +442,31 @@ function createServer(project) {
404
442
  head: [],
405
443
  body: [],
406
444
  clients: []
407
- }) {
445
+ }, built) {
408
446
  const handoff = {
409
447
  id: page.id,
410
448
  kind: page.kind,
411
- client: rendered ? asset(PAGE_CLIENT) : null,
449
+ live: !built,
450
+ client: rendered ? (built ?? { asset }).asset(PAGE_CLIENT) : null,
412
451
  data: out.data ?? null,
452
+ frontmatter: frontmatterOf(page),
413
453
  plugins: extras.clients,
414
454
  messages: extras.messages ?? []
415
455
  };
416
456
  return String(html(_templateObject || (_templateObject = _taggedTemplateLiteral([
417
- "<!doctype html><html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n<title>",
418
- "</title>\n<style>",
419
- "</style>\n",
457
+ "<!doctype html>\n <html lang=\"en\">\n <head>\n <meta charset=\"utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\" />\n <title>",
458
+ "</title>\n <style>\n ",
459
+ "\n </style>\n ",
420
460
  "",
421
- "</head>\n<body data-page=\"",
461
+ "\n </head>\n <body data-page=\"",
422
462
  "\" data-kind=\"",
423
- "\">\n",
424
- "\n<div id=\"wb-root\">",
425
- "</div>\n<div id=\"wb-dock\"></div>\n<script type=\"application/json\" id=\"wb-page\">",
426
- "<\/script>\n<script type=\"module\" src=\"/_/runtime.js\"><\/script>",
427
- "</body></html>"
428
- ])), out.title || page.id || project.name, DOCK_STYLE, raw(out.head ?? ""), extras.head, page.id, page.kind, notices.map((n) => html`<pre class="wb-error" role="alert">${n}</pre>`), raw(String(out.html)), raw(json(handoff)), extras.body));
463
+ "\">\n ",
464
+ "\n <div id=\"wb-root\">",
465
+ "</div>\n <div id=\"wb-dock\"></div>\n <script type=\"application/json\" id=\"wb-page\">\n ",
466
+ "\n <\/script>\n <script type=\"module\" src=\"",
467
+ "\"><\/script>\n ",
468
+ "\n </body>\n </html>"
469
+ ])), out.title || page.id || project.name, DOCK_STYLE, raw(out.head ?? ""), extras.head, page.id, page.kind, notices.map((n) => html`<pre class="wb-error" role="alert">${n}</pre>`), raw(String(out.html)), raw(json(handoff)), built ? built.asset(RUNTIME) : "/_/runtime.js", extras.body));
429
470
  }
430
471
  /** A listener as the server holds it. */
431
472
  const listeners = /* @__PURE__ */ new Set();
@@ -571,8 +612,15 @@ function createServer(project) {
571
612
  if (owner) return sendJson(res, 404, { error: `plugin ${owner.name} has no route ${req.method} ${p}` });
572
613
  }
573
614
  if (p.startsWith("/_/file/")) {
615
+ await config();
574
616
  const file = path.resolve(p.slice(7));
575
617
  if (!servable(file) || !fs.existsSync(file) || !fs.statSync(file).isFile()) return send(res, 404, "not found", "text/plain");
618
+ asset(file);
619
+ if (TYPESCRIPT.test(file)) try {
620
+ return send(res, 200, typeScriptModule(file), "text/javascript; charset=utf-8");
621
+ } catch (err) {
622
+ return send(res, 200, `throw new SyntaxError(${JSON.stringify(`${path.basename(file)}: ${err.message}`)})`, "text/javascript; charset=utf-8");
623
+ }
576
624
  return sendFile(res, file);
577
625
  }
578
626
  if (p === "/api/health") return sendJson(res, 200, {
@@ -661,7 +709,8 @@ function createServer(project) {
661
709
  return sendJson(res, 200, {
662
710
  ...out,
663
711
  notices,
664
- mtime: r.page.mtime
712
+ mtime: r.page.mtime,
713
+ frontmatter: frontmatterOf(r.page)
665
714
  });
666
715
  }
667
716
  if (p === "/api/watch") {
@@ -718,10 +767,189 @@ function createServer(project) {
718
767
  send(res, 500, String(err?.stack || err), "text/plain");
719
768
  }
720
769
  }
770
+ /** What a build leaves out unless told otherwise: what is for writing a page, not for reading it. */
771
+ const BUILD_SKIP = [
772
+ "form",
773
+ "chat",
774
+ "comments",
775
+ "draft"
776
+ ];
777
+ /** In the folder a build writes, so that a next build knows it may replace it. */
778
+ const BUILD_MARK = ".whiteboard-build";
779
+ async function build(options = {}) {
780
+ const cfg = await config();
781
+ if (cfg.errors.length) throw new Error(`the config failed to load: ${cfg.errors[0].message}`);
782
+ if (!cfg.pages) throw new Error("the config has no pages renderer");
783
+ const pages = cfg.pages;
784
+ const defined = (...xs) => xs.find((x) => x !== void 0);
785
+ const patterns = defined(options.pages, cfg.build.pages) ?? [];
786
+ const skip = new Set(defined(options.skip, cfg.build.skip) ?? BUILD_SKIP);
787
+ const outDir = path.resolve(project.dir, defined(options.out, cfg.build.out) ?? "site");
788
+ if (!patterns.length) throw new Error("nothing to build: name the pages, as globs from the project's folder (whiteboard build 'docs/**/*.md'), or set build.pages in the config");
789
+ const warnings = [];
790
+ const include = patterns.filter((p) => !p.startsWith("!"));
791
+ const exclude = patterns.filter((p) => p.startsWith("!")).map((p) => p.slice(1));
792
+ const files = [...new Set(include.flatMap((p) => fs.globSync(p, { cwd: project.dir }).map((f) => path.resolve(project.dir, f))))].filter((f) => /\.(md|markdown)$/i.test(f) && fs.statSync(f).isFile()).filter((f) => !exclude.some((g) => path.matchesGlob(path.relative(project.dir, f), g))).sort();
793
+ if (!files.length) throw new Error(`no markdown files match ${patterns.join(" ")}`);
794
+ const base = files.map(path.dirname).reduce((a, b) => {
795
+ while (!within(b, a)) a = path.dirname(a);
796
+ return a;
797
+ });
798
+ const outOf = (file) => {
799
+ const rel = path.relative(base, file);
800
+ return path.join(outDir, /^(readme|index)\.(md|markdown)$/i.test(path.basename(rel)) ? path.join(path.dirname(rel), "index.html") : rel.replace(/\.(md|markdown)$/i, ".html"));
801
+ };
802
+ const built = new Map(files.map((f) => [f, outOf(f)]));
803
+ if (fs.existsSync(outDir)) {
804
+ if (fs.readdirSync(outDir).length && !fs.existsSync(path.join(outDir, BUILD_MARK))) throw new Error(`${outDir} has files a build didn't write: give another folder (--out), or empty it`);
805
+ fs.rmSync(outDir, { recursive: true });
806
+ }
807
+ fs.mkdirSync(outDir, { recursive: true });
808
+ fs.writeFileSync(path.join(outDir, BUILD_MARK), "written by whiteboard build; the next build replaces this folder\n");
809
+ const copied = /* @__PURE__ */ new Map();
810
+ function copyAsset(file) {
811
+ const known = copied.get(file);
812
+ if (known) return known;
813
+ const rel = within(file, ENGINE_DIR) ? path.join("_", "whiteboard", path.relative(ENGINE_DIR, file)) : within(file, project.dir) ? path.join("_", "project", path.relative(project.dir, file)) : path.join("_", "files", path.basename(path.dirname(file)), path.basename(file));
814
+ const dest = path.join(outDir, TYPESCRIPT.test(rel) ? rel.replace(/\.m?ts$/i, ".js") : rel);
815
+ copied.set(file, dest);
816
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
817
+ if (TYPESCRIPT.test(file)) fs.writeFileSync(dest, typeScriptModule(file));
818
+ else fs.copyFileSync(file, dest);
819
+ if (/\.css$/i.test(file)) for (const m of fs.readFileSync(file, "utf8").matchAll(/url\(\s*['"]?([^'")]+)['"]?\s*\)/g)) {
820
+ const ref = m[1];
821
+ if (/^(data:|[a-z][a-z0-9+.-]*:|\/|#)/i.test(ref)) continue;
822
+ const target = path.resolve(path.dirname(file), ref.replace(/[?#].*$/, ""));
823
+ if (fs.existsSync(target) && fs.statSync(target).isFile()) copyAsset(target);
824
+ }
825
+ return dest;
826
+ }
827
+ /** a file beside the pages (an image), copied to its place below the site */
828
+ function copyBeside(file) {
829
+ if (!within(file, base)) return null;
830
+ const dest = path.join(outDir, path.relative(base, file));
831
+ if (!fs.existsSync(dest)) {
832
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
833
+ fs.copyFileSync(file, dest);
834
+ }
835
+ return dest;
836
+ }
837
+ const hooks = await pluginHooks();
838
+ const result = {
839
+ out: outDir,
840
+ pages: [],
841
+ warnings
842
+ };
843
+ for (const [file, dest] of built) {
844
+ const here = path.dirname(dest);
845
+ const relTo = (target) => path.relative(here, target).split(path.sep).map(encodeURIComponent).join("/") || ".";
846
+ const assetHere = (ref) => relTo(copyAsset(toPath(ref)));
847
+ const urlFor = (abs) => {
848
+ for (const candidate of [abs, abs + ".md"]) {
849
+ if (!fs.existsSync(candidate)) continue;
850
+ if (fs.statSync(candidate).isDirectory()) {
851
+ const index = [...built.keys()].find((f) => path.dirname(f) === candidate && /^(readme|index)\.(md|markdown)$/i.test(path.basename(f)));
852
+ if (index) return relTo(built.get(index));
853
+ continue;
854
+ }
855
+ if (built.has(candidate)) return relTo(built.get(candidate));
856
+ if (/\.(md|markdown)$/i.test(candidate)) {
857
+ warnings.push(`${path.relative(project.dir, file)} links to ${path.relative(project.dir, candidate)}, which is not in the build`);
858
+ return null;
859
+ }
860
+ const copy = copyBeside(candidate);
861
+ if (copy) return relTo(copy);
862
+ warnings.push(`${path.relative(project.dir, file)} links to ${path.relative(project.dir, candidate)}, outside the built pages' folder`);
863
+ return null;
864
+ }
865
+ return null;
866
+ };
867
+ const id = within(file, project.pagesDir) ? path.relative(project.pagesDir, file).split(path.sep).join("/").replace(/\.(md|markdown)$/i, "") : "-/" + path.relative(project.dir, file).split(path.sep).join("/");
868
+ const stored = pageSource(id, file);
869
+ if (stored.frontmatterError) warnings.push(`${path.relative(project.dir, file)}: frontmatter: ${stored.frontmatterError}`);
870
+ const context = {
871
+ project: {
872
+ name: project.name,
873
+ dir: project.dir,
874
+ files: !!FILES
875
+ },
876
+ asset: assetHere,
877
+ urlFor,
878
+ live: false,
879
+ skip
880
+ };
881
+ const rendered = await pages.render(stored, context);
882
+ const named = /^(readme|index)\.(md|markdown)$/i.test(path.basename(file)) ? path.basename(path.dirname(file)) : path.basename(file).replace(/\.(md|markdown)$/i, "");
883
+ const title = !rendered.title || rendered.title === id ? named : rendered.title;
884
+ const out = {
885
+ ...rendered,
886
+ title,
887
+ html: String(rendered.html ?? ""),
888
+ head: String(rendered.head ?? "")
889
+ };
890
+ const included = hooks.filter((h) => !h.error && !skip.has(h.name));
891
+ const clients = included.filter((h) => h.client).map((h) => ({
892
+ name: h.name,
893
+ client: assetHere(h.client)
894
+ }));
895
+ const parts = await Promise.all(included.map(async (h) => {
896
+ try {
897
+ return await h.shell?.(null) ?? null;
898
+ } catch (err) {
899
+ warnings.push(`plugin ${h.name} failed on ${path.relative(project.dir, file)}: ${err.message}`);
900
+ return null;
901
+ }
902
+ }));
903
+ const extras = {
904
+ head: parts.map((x) => raw(String(x?.head ?? ""))),
905
+ body: parts.map((x) => raw(String(x?.body ?? ""))),
906
+ clients
907
+ };
908
+ let doc = shell(stored, {
909
+ page: true,
910
+ out,
911
+ notices: []
912
+ }, extras, { asset: assetHere });
913
+ doc = doc.replace(/\/_\/file(\/[^"'\s)<>]+)/g, (all, served) => {
914
+ let file;
915
+ try {
916
+ file = path.resolve(served.split("/").map(decodeURIComponent).join(path.sep));
917
+ } catch {
918
+ return all;
919
+ }
920
+ return fs.existsSync(file) && fs.statSync(file).isFile() ? assetHere(file) : all;
921
+ });
922
+ doc = doc.replace(/\b(src|href|poster)="([^"#?:]+)([^"]*)"/g, (all, _attr, ref) => {
923
+ if (ref.startsWith("/") || ref.startsWith("_/") || ref.startsWith("../")) return all;
924
+ let decoded = ref;
925
+ try {
926
+ decoded = decodeURIComponent(ref);
927
+ } catch {}
928
+ const source = path.resolve(path.dirname(file), decoded);
929
+ if (fs.existsSync(source) && fs.statSync(source).isFile() && !/\.(md|markdown)$/i.test(source)) {
930
+ const target = path.resolve(here, decoded);
931
+ if (within(target, outDir) && !fs.existsSync(target)) {
932
+ fs.mkdirSync(path.dirname(target), { recursive: true });
933
+ fs.copyFileSync(source, target);
934
+ }
935
+ }
936
+ return all;
937
+ });
938
+ fs.mkdirSync(here, { recursive: true });
939
+ fs.writeFileSync(dest, doc);
940
+ result.pages.push({
941
+ file,
942
+ to: dest
943
+ });
944
+ }
945
+ if (![...built.values()].includes(path.join(outDir, "index.html"))) warnings.push("no README.md or index.md in the top folder: the site has no index.html");
946
+ return result;
947
+ }
721
948
  return {
722
949
  handle,
723
950
  status: (page = null) => status(page),
724
951
  config,
952
+ build,
725
953
  close() {
726
954
  for (const w of watchers) w.close();
727
955
  watchers.clear();