@bigmistqke/whiteboard 0.7.0 → 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 (66) hide show
  1. package/README.md +10 -209
  2. package/bin/whiteboard.ts +82 -239
  3. package/dist/bin/whiteboard.js +70 -200
  4. package/dist/client/runtime.js +66 -7
  5. package/dist/plugins/chat/public/style.css +10 -6
  6. package/dist/plugins/code/README.md +10 -0
  7. package/dist/plugins/code/index.js +31 -22
  8. package/dist/plugins/code/public/client.js +1 -0
  9. package/dist/plugins/code/public/style.css +33 -2
  10. package/dist/plugins/comments/public/client.js +1 -1
  11. package/dist/plugins/form/public/style.css +2 -0
  12. package/dist/plugins/iframe/index.js +2 -2
  13. package/dist/plugins/lightbox/public/client.js +1 -1
  14. package/dist/plugins/terminal/index.js +7 -9
  15. package/dist/plugins/terminal/public/client.js +1 -1
  16. package/dist/plugins/terminal/public/style.css +3 -3
  17. package/dist/plugins/toc/index.js +1 -1
  18. package/dist/plugins/toc/public/client.js +1 -1
  19. package/dist/plugins/window/index.js +4 -4
  20. package/dist/src/build.js +159 -0
  21. package/dist/src/client/page/main.js +1 -1
  22. package/dist/src/document.js +93 -0
  23. package/dist/src/events.js +3 -13
  24. package/dist/src/hub.js +47 -35
  25. package/dist/src/listen.js +100 -0
  26. package/dist/src/listeners.js +67 -0
  27. package/dist/src/pages/index.js +1 -1
  28. package/dist/src/pages/markdown.js +23 -11
  29. package/dist/src/pages/render.js +13 -8
  30. package/dist/src/server.js +166 -514
  31. package/dist/src/site.js +173 -0
  32. package/dist/src/status.js +63 -1
  33. package/package.json +1 -1
  34. package/plugins/README.md +258 -0
  35. package/plugins/chat/public/style.css +10 -6
  36. package/plugins/code/README.md +10 -0
  37. package/plugins/code/client.ts +73 -0
  38. package/plugins/code/index.ts +38 -26
  39. package/plugins/code/public/style.css +33 -2
  40. package/plugins/comments/client.ts +8 -1
  41. package/plugins/form/public/style.css +2 -0
  42. package/plugins/iframe/index.ts +2 -2
  43. package/plugins/lightbox/client.ts +13 -3
  44. package/plugins/terminal/client.ts +10 -2
  45. package/plugins/terminal/index.ts +8 -6
  46. package/plugins/terminal/public/style.css +3 -3
  47. package/plugins/toc/client.ts +9 -2
  48. package/plugins/toc/index.ts +1 -1
  49. package/plugins/window/index.ts +4 -4
  50. package/skills/whiteboard/SKILL.md +3 -1
  51. package/src/build.ts +213 -0
  52. package/src/client/page/elsewhere.ts +2 -1
  53. package/src/client/runtime.ts +88 -10
  54. package/src/client/toast.ts +1 -1
  55. package/src/define.ts +34 -18
  56. package/src/document.ts +191 -0
  57. package/src/events.ts +11 -16
  58. package/src/hub.ts +48 -34
  59. package/src/listen.ts +125 -0
  60. package/src/listeners.ts +95 -0
  61. package/src/pages/index.ts +1 -1
  62. package/src/pages/markdown.ts +29 -18
  63. package/src/pages/render.ts +13 -8
  64. package/src/server.ts +165 -609
  65. package/src/site.ts +165 -0
  66. package/src/status.ts +100 -0
@@ -0,0 +1,100 @@
1
+ import { takenByOther } from "./status.js";
2
+ import http from "node:http";
3
+ // src/listen.ts
4
+ /**
5
+ * Hold a listener at a server for as long as the process runs, and again after the server restarts.
6
+ * Resolves once the server holds it; with `orNone`, also after a first try that found no server
7
+ * running (a wait reads the log all the same, and registers once one appears).
8
+ */
9
+ function listen({ at, query, onSnapshot, onRefused }, { orNone = false } = {}) {
10
+ return new Promise((settled) => {
11
+ const connect = async () => {
12
+ let base = null;
13
+ try {
14
+ base = await at();
15
+ } catch {}
16
+ if (!base) {
17
+ if (orNone) settled();
18
+ return setTimeout(connect, 2e3).unref();
19
+ }
20
+ let again = false;
21
+ const retry = (ms) => {
22
+ if (!again) {
23
+ again = true;
24
+ setTimeout(connect, ms).unref();
25
+ }
26
+ };
27
+ const req = http.get(`${base}/api/listen?${new URLSearchParams(query)}`, (res) => {
28
+ res.setEncoding("utf8");
29
+ if (res.statusCode === 409) {
30
+ let body = "";
31
+ res.on("data", (d) => body += d);
32
+ res.on("end", () => {
33
+ let reason = "";
34
+ try {
35
+ reason = JSON.parse(body).error ?? "";
36
+ } catch {}
37
+ onRefused(reason || "another agent is the fallback already");
38
+ });
39
+ return;
40
+ }
41
+ settled();
42
+ let buf = "";
43
+ res.on("data", (chunk) => {
44
+ buf += chunk;
45
+ let end;
46
+ while ((end = buf.indexOf("\n\n")) >= 0) {
47
+ const block = buf.slice(0, end);
48
+ buf = buf.slice(end + 2);
49
+ const event = /^event: (.*)$/m.exec(block)?.[1];
50
+ const data = /^data: (.*)$/m.exec(block)?.[1];
51
+ if (event === "listeners" && data && onSnapshot) onSnapshot(JSON.parse(data));
52
+ }
53
+ });
54
+ res.on("close", () => retry(1e3));
55
+ });
56
+ req.on("error", () => retry(2e3));
57
+ req.on("socket", (socket) => socket.unref());
58
+ };
59
+ connect();
60
+ });
61
+ }
62
+ /**
63
+ * The fallback over the projects a server names: `start` is called once for each project as it
64
+ * appears, to hand out from its log what no other listener there takes (`skip`) and nobody picked
65
+ * up (`unpicked`), looked at again (`recheck`) whenever who listens there changes; a project no
66
+ * longer named is stopped (`signal`).
67
+ * @returns what takes each snapshot the server sends
68
+ */
69
+ function fallback(start) {
70
+ const followed = /* @__PURE__ */ new Map();
71
+ return ({ projects }) => {
72
+ for (const p of projects) {
73
+ const known = followed.get(p.dir);
74
+ if (known) {
75
+ known.listeners = p.listeners;
76
+ known.recheck();
77
+ continue;
78
+ }
79
+ const f = {
80
+ listeners: p.listeners,
81
+ stop: new AbortController(),
82
+ recheck: () => {}
83
+ };
84
+ followed.set(p.dir, f);
85
+ start(p, {
86
+ skip: (e) => takenByOther(f.listeners, e),
87
+ recheck: (again) => {
88
+ f.recheck = again;
89
+ },
90
+ signal: f.stop.signal,
91
+ unpicked: true
92
+ });
93
+ }
94
+ for (const [dir, f] of followed) if (!projects.some((p) => p.dir === dir)) {
95
+ f.stop.abort();
96
+ followed.delete(dir);
97
+ }
98
+ };
99
+ }
100
+ export { fallback, listen };
@@ -0,0 +1,67 @@
1
+ import { covers } from "./status.js";
2
+ // src/listeners.ts
3
+ function createRegistry() {
4
+ const listeners = /* @__PURE__ */ new Set();
5
+ const watchers = /* @__PURE__ */ new Set();
6
+ const changed = () => {
7
+ for (const fn of watchers) fn();
8
+ };
9
+ return {
10
+ list: (page = null) => [...listeners].filter((l) => page == null || covers(l, page)).map(({ page, type, command, fallback }) => ({
11
+ page,
12
+ type,
13
+ command,
14
+ ...fallback && { fallback }
15
+ })),
16
+ add(l) {
17
+ if (l.fallback && [...listeners].some((x) => x.fallback)) return null;
18
+ listeners.add(l);
19
+ changed();
20
+ return () => {
21
+ if (listeners.delete(l)) changed();
22
+ };
23
+ },
24
+ onChange(fn) {
25
+ watchers.add(fn);
26
+ return () => watchers.delete(fn);
27
+ }
28
+ };
29
+ }
30
+ /** The listener a /api/listen request asks for. */
31
+ const listenerOf = (q, fallback = q.has("fallback")) => ({
32
+ page: q.get("page") || "",
33
+ type: q.get("type") || null,
34
+ command: q.get("command") === "wait" ? "wait" : "watch",
35
+ ...fallback && { fallback }
36
+ });
37
+ /**
38
+ * Hold a /api/listen connection open as an event stream (with a ping, so nothing between closes it
39
+ * as idle). `send` writes an event on it; `onEnd` runs once, when the agent goes or `end` is called.
40
+ */
41
+ function holdStream(req, res, onEnd) {
42
+ res.writeHead(200, {
43
+ "content-type": "text/event-stream",
44
+ "cache-control": "no-store",
45
+ connection: "keep-alive"
46
+ });
47
+ res.write(": listening\n\n");
48
+ const ping = setInterval(() => res.write(": ping\n\n"), 25e3);
49
+ let ended = false;
50
+ const finish = () => {
51
+ if (ended) return;
52
+ ended = true;
53
+ clearInterval(ping);
54
+ onEnd();
55
+ };
56
+ req.on("close", finish);
57
+ return {
58
+ send(event, data) {
59
+ if (!ended) res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
60
+ },
61
+ end() {
62
+ finish();
63
+ res.end();
64
+ }
65
+ };
66
+ }
67
+ export { createRegistry, holdStream, listenerOf };
@@ -9,7 +9,7 @@ function createPages(options) {
9
9
  render: createRender(blocks, options.style, options.styles ?? [], options.links ?? [], options.composers),
10
10
  message: createMessageRender(blocks, options.links ?? []),
11
11
  docs: createDocs(blocks),
12
- questions: (page) => formNames(page.source)
12
+ questions: (page) => formNames(page.body)
13
13
  };
14
14
  }
15
15
  export { createPages };
@@ -148,18 +148,28 @@ function linkText(tree, links) {
148
148
  };
149
149
  walk(tree);
150
150
  }
151
+ /**
152
+ * The address of something named relative to the page (a link, an image, a block's `src`): resolved
153
+ * from the page's folder and handed to `linkFor`. A URL, an address from the root, an anchor, or
154
+ * something nothing serves stays as written.
155
+ */
156
+ function addressOf(ref, file, linkFor) {
157
+ if (!linkFor || !file || !ref || SCHEME.test(ref) || /^[/#]/.test(ref)) return ref;
158
+ const [, target, rest] = /^([^?#]*)(.*)$/s.exec(ref);
159
+ let decoded = target;
160
+ try {
161
+ decoded = decodeURIComponent(target);
162
+ } catch {}
163
+ const url = decoded && linkFor(path.resolve(path.dirname(file), decoded));
164
+ return url ? url + rest : ref;
165
+ }
166
+ /** Raw HTML's references to files beside the page (`<img src>`, `<video poster>`, `<a href>`). */
167
+ const HTML_REFS = /(\b(?:src|href|poster)=")([^"]*)(")/g;
151
168
  function rewriteLinks(tree, file, linkFor) {
152
169
  if (!linkFor || !file) return;
153
170
  const walk = (node) => {
154
- if ((node.type === "link" || node.type === "image" || node.type === "definition") && node.url && !SCHEME.test(node.url) && !/^[/#]/.test(node.url)) {
155
- const [, target, rest] = /^([^?#]*)(.*)$/s.exec(node.url);
156
- let decoded = target;
157
- try {
158
- decoded = decodeURIComponent(target);
159
- } catch {}
160
- const url = decoded && linkFor(path.resolve(path.dirname(file), decoded));
161
- if (url) node.url = url + rest;
162
- }
171
+ if ((node.type === "link" || node.type === "image" || node.type === "definition") && node.url) node.url = addressOf(node.url, file, linkFor);
172
+ if (node.type === "html" && typeof node.value === "string") node.value = node.value.replace(HTML_REFS, (_, open, ref, close) => open + addressOf(ref, file, linkFor) + close);
163
173
  node.children?.forEach(walk);
164
174
  };
165
175
  walk(tree);
@@ -199,7 +209,7 @@ function literalDirective(node, label) {
199
209
  * code block doesn't count.
200
210
  */
201
211
  function formNames(source) {
202
- const tree = parseMarkdown(String(source ?? "").replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, ""));
212
+ const tree = parseMarkdown(String(source ?? ""));
203
213
  const names = [];
204
214
  const walk = (node) => {
205
215
  if (node.type === "containerDirective" && node.name === "form") {
@@ -367,6 +377,7 @@ plugin "${name}" failed to compose around block "${block.name}": ${err.message}<
367
377
  };
368
378
  const markdown = async (src) => toHtml(parse(src).children ?? []);
369
379
  const inline = async (src) => (await markdown(src)).trim().replace(/^<p>([\s\S]*)<\/p>$/, "$1");
380
+ const fileUrl = (src) => addressOf(src, file, linkFor);
370
381
  const base = {
371
382
  page,
372
383
  file,
@@ -375,7 +386,8 @@ plugin "${name}" failed to compose around block "${block.name}": ${err.message}<
375
386
  toHtml,
376
387
  toString,
377
388
  markdown,
378
- inline
389
+ inline,
390
+ fileUrl
379
391
  };
380
392
  async function directive(node) {
381
393
  const { label, children } = splitLabel(node);
@@ -1,3 +1,4 @@
1
+ import { FRONTMATTER } from "../site.js";
1
2
  import { renderMarkdown } from "./markdown.js";
2
3
  import { html, raw } from "../html.js";
3
4
  import { awaitingLabel } from "../icons.js";
@@ -28,8 +29,8 @@ const CODE_LANG = {
28
29
  yml: "yaml",
29
30
  yaml: "yaml"
30
31
  };
31
- /** Frontmatter is the core's; the page starts after it. */
32
- const body = (source) => source.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, "");
32
+ /** Frontmatter is the core's; the page starts after it (a page's own is cut off by the site: PageSource.body). */
33
+ const body = (source) => source.replace(FRONTMATTER, "");
33
34
  /** A page's title: its first heading, else its name. */
34
35
  function titleOf(file, fallback) {
35
36
  try {
@@ -309,10 +310,11 @@ function createRender(blocks, style, pluginStyles = [], links = [], composers =
309
310
  plugin: s.plugin,
310
311
  file: stylePath(s.file)
311
312
  })).filter((s) => !!s.file);
312
- async function markdown(source, page, file, ctx) {
313
+ /** `content`: the markdown without its frontmatter. */
314
+ async function markdown(content, page, file, ctx) {
313
315
  const { blocks: set, code } = blocks();
314
316
  const skip = ctx.skip;
315
- return renderMarkdown(body(source), {
317
+ return renderMarkdown(content, {
316
318
  page,
317
319
  file,
318
320
  resolve: (name) => {
@@ -329,7 +331,7 @@ function createRender(blocks, style, pluginStyles = [], links = [], composers =
329
331
  async function content(page, ctx) {
330
332
  const { project } = ctx;
331
333
  switch (page.kind) {
332
- case "page": return markdown(page.source, page.id, page.file, ctx);
334
+ case "page": return markdown(page.body, page.id, page.file, ctx);
333
335
  case "pages": {
334
336
  if (page.groups) {
335
337
  const waiting = (ps) => ps.reduce((n, p) => n + p.awaiting, 0);
@@ -389,7 +391,7 @@ function createRender(blocks, style, pluginStyles = [], links = [], composers =
389
391
  islands: {},
390
392
  styles: []
391
393
  };
392
- const r = await markdown(readme.source, page.id, readme.file, ctx);
394
+ const r = await markdown(body(readme.source), page.id, readme.file, ctx);
393
395
  return {
394
396
  ...r,
395
397
  html: html`${raw(r.html)}
@@ -443,7 +445,7 @@ function createRender(blocks, style, pluginStyles = [], links = [], composers =
443
445
  }]));
444
446
  const styles = out.styles.map((s) => ctx.asset(s));
445
447
  const nav = ctx.live === false ? html`<nav class="wb-nav">
446
- <span>${ctx.project.name}</span
448
+ <span>${ctx.project.name}</span><span data-wb-slot="header"></span
447
449
  ><button
448
450
  type="button"
449
451
  class="wb-theme"
@@ -452,7 +454,10 @@ function createRender(blocks, style, pluginStyles = [], links = [], composers =
452
454
  ></button>
453
455
  </nav>` : html`<nav class="wb-nav">
454
456
  <span>${crumbs(page.id, ctx.entry?.label || ctx.project.name, ctx.base ?? "", ctx.entry)}</span
455
- >${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<button
457
+ >${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<span
458
+ data-wb-slot="header"
459
+ ></span
460
+ ><button
456
461
  type="button"
457
462
  class="wb-theme"
458
463
  title="Theme: follow the system, light or dark"