@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.
- package/README.md +23 -216
- package/bin/whiteboard.ts +82 -239
- package/dist/bin/whiteboard.js +70 -200
- package/dist/client/runtime.js +297 -61
- package/dist/plugins/badge/README.md +3 -1
- package/dist/plugins/badge/public/style.css +17 -0
- package/dist/plugins/chat/public/client.js +2 -2
- package/dist/plugins/chat/public/style.css +19 -6
- package/dist/plugins/code/README.md +10 -0
- package/dist/plugins/code/index.js +31 -22
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +33 -2
- package/dist/plugins/comments/public/client.js +7 -2
- package/dist/plugins/comments/public/style.css +4 -0
- package/dist/plugins/form/public/style.css +2 -0
- package/dist/plugins/grid/public/style.css +0 -5
- package/dist/plugins/iframe/index.js +2 -2
- package/dist/plugins/index.js +5 -1
- package/dist/plugins/lightbox/public/client.js +1 -1
- package/dist/plugins/terminal/index.js +7 -9
- package/dist/plugins/terminal/public/client.js +1 -1
- package/dist/plugins/terminal/public/style.css +3 -3
- package/dist/plugins/timeline/README.md +40 -0
- package/dist/plugins/timeline/index.js +82 -0
- package/dist/plugins/timeline/public/style.css +118 -0
- package/dist/plugins/toc/README.md +13 -13
- package/dist/plugins/toc/index.js +6 -4
- package/dist/plugins/toc/public/client.js +1 -1
- package/dist/plugins/toc/public/style.css +19 -2
- package/dist/plugins/window/index.js +4 -4
- package/dist/solid/index.js +2 -1
- package/dist/src/auth.js +7 -3
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +74 -35
- package/dist/src/client/page/main.js +16 -5
- package/dist/src/document.js +93 -0
- package/dist/src/events.js +7 -15
- package/dist/src/hub.js +90 -53
- package/dist/src/listen.js +100 -0
- package/dist/src/listeners.js +67 -0
- package/dist/src/pages/index.js +1 -1
- package/dist/src/pages/markdown.js +23 -11
- package/dist/src/pages/render.js +13 -8
- package/dist/src/serve.js +2 -0
- package/dist/src/server.js +204 -514
- package/dist/src/site.js +173 -0
- package/dist/src/status.js +63 -1
- package/dist/src/ws.js +128 -0
- package/package.json +1 -1
- package/plugins/README.md +258 -0
- package/plugins/badge/README.md +3 -1
- package/plugins/badge/public/style.css +17 -0
- package/plugins/chat/client.tsx +34 -6
- package/plugins/chat/public/style.css +19 -6
- package/plugins/code/README.md +10 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/index.ts +38 -26
- package/plugins/code/public/style.css +33 -2
- package/plugins/comments/client.ts +56 -6
- package/plugins/comments/public/style.css +4 -0
- package/plugins/form/public/style.css +2 -0
- package/plugins/grid/public/style.css +0 -5
- package/plugins/iframe/index.ts +2 -2
- package/plugins/index.ts +6 -1
- package/plugins/lightbox/client.ts +13 -3
- package/plugins/terminal/client.ts +10 -2
- package/plugins/terminal/index.ts +8 -6
- package/plugins/terminal/public/style.css +3 -3
- package/plugins/timeline/README.md +40 -0
- package/plugins/timeline/index.ts +92 -0
- package/plugins/timeline/public/style.css +118 -0
- package/plugins/toc/README.md +13 -13
- package/plugins/toc/client.ts +58 -32
- package/plugins/toc/index.ts +13 -13
- package/plugins/toc/public/style.css +19 -2
- package/plugins/window/index.ts +4 -4
- package/skills/whiteboard/SKILL.md +3 -1
- package/solid/index.tsx +3 -0
- package/src/auth.ts +7 -2
- package/src/build.ts +213 -0
- package/src/client/forms.ts +102 -5
- package/src/client/page/base.css +74 -35
- package/src/client/page/elsewhere.ts +14 -4
- package/src/client/page/waiting.ts +10 -5
- package/src/client/read.ts +16 -3
- package/src/client/runtime.ts +219 -52
- package/src/client/toast.ts +36 -6
- package/src/define.ts +44 -18
- package/src/document.ts +191 -0
- package/src/events.ts +18 -17
- package/src/hub.ts +102 -56
- package/src/listen.ts +125 -0
- package/src/listeners.ts +95 -0
- package/src/pages/index.ts +1 -1
- package/src/pages/markdown.ts +29 -18
- package/src/pages/render.ts +13 -8
- package/src/serve.ts +2 -0
- package/src/server.ts +223 -611
- package/src/site.ts +165 -0
- package/src/status.ts +100 -0
- package/src/ws.ts +155 -0
package/dist/src/hub.js
CHANGED
|
@@ -2,16 +2,30 @@ import { project } from "./config.js";
|
|
|
2
2
|
import { followEntries, machineDir, readMachineConfig, stateDir } from "./entries.js";
|
|
3
3
|
import { VERSION, compareVersions } from "./package.js";
|
|
4
4
|
import { createServer } from "./server.js";
|
|
5
|
+
import { holdStream, listenerOf } from "./listeners.js";
|
|
5
6
|
import fs from "node:fs";
|
|
6
7
|
import path from "node:path";
|
|
7
8
|
// src/hub.ts
|
|
8
9
|
function createHub({ dir = machineDir(), state = stateDir(), home, interval, onShutdown, version = VERSION } = {}) {
|
|
9
10
|
const served = /* @__PURE__ */ new Map();
|
|
10
11
|
const fallbacks = /* @__PURE__ */ new Map();
|
|
12
|
+
/** An entry's fallback held on one of its projects, followed there; false when the project refuses it. */
|
|
13
|
+
function hold(s, fb) {
|
|
14
|
+
const off = s.server.addListener(fb.listener);
|
|
15
|
+
if (!off) return false;
|
|
16
|
+
const unfollow = s.server.onListeners(() => fb.tell());
|
|
17
|
+
s.fallback = () => {
|
|
18
|
+
unfollow();
|
|
19
|
+
off();
|
|
20
|
+
delete s.fallback;
|
|
21
|
+
};
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
11
24
|
function sync(projects) {
|
|
12
25
|
setImmediate(() => touch());
|
|
13
26
|
const wanted = new Map(projects.map((p) => [`${p.base}\n${p.dir}`, p]));
|
|
14
27
|
for (const [key, s] of served) if (!wanted.has(key)) {
|
|
28
|
+
s.fallback?.();
|
|
15
29
|
s.server.close();
|
|
16
30
|
served.delete(key);
|
|
17
31
|
console.log(`whiteboard: ${s.base} is gone (${s.dir})`);
|
|
@@ -24,17 +38,19 @@ function createHub({ dir = machineDir(), state = stateDir(), home, interval, onS
|
|
|
24
38
|
base: p.base,
|
|
25
39
|
entry: p.entry,
|
|
26
40
|
label: [p.group, p.name].filter(Boolean).join(" › "),
|
|
27
|
-
onChange: () =>
|
|
41
|
+
onChange: () => changed(p.base)
|
|
28
42
|
});
|
|
29
|
-
const
|
|
30
|
-
served.set(key, {
|
|
43
|
+
const s = {
|
|
31
44
|
...p,
|
|
32
45
|
project: project$1,
|
|
33
|
-
server
|
|
34
|
-
|
|
35
|
-
|
|
46
|
+
server
|
|
47
|
+
};
|
|
48
|
+
served.set(key, s);
|
|
49
|
+
const fb = fallbacks.get(p.entry);
|
|
50
|
+
if (fb) hold(s, fb);
|
|
36
51
|
console.log(`whiteboard: ${p.base} → ${p.dir}`);
|
|
37
52
|
}
|
|
53
|
+
for (const fb of fallbacks.values()) fb.tell();
|
|
38
54
|
}
|
|
39
55
|
const overviewDir = path.join(state, "overview");
|
|
40
56
|
fs.mkdirSync(path.join(overviewDir, ".whiteboard", "pages"), { recursive: true });
|
|
@@ -63,9 +79,19 @@ function createHub({ dir = machineDir(), state = stateDir(), home, interval, onS
|
|
|
63
79
|
}
|
|
64
80
|
});
|
|
65
81
|
let touching;
|
|
82
|
+
const changes = /* @__PURE__ */ new Set();
|
|
66
83
|
const touch = () => {
|
|
67
84
|
clearTimeout(touching);
|
|
68
|
-
touching = setTimeout(() =>
|
|
85
|
+
touching = setTimeout(() => {
|
|
86
|
+
root.touch();
|
|
87
|
+
const from = [...changes];
|
|
88
|
+
changes.clear();
|
|
89
|
+
for (const s of served.values()) if (from.some((b) => b !== s.base)) s.server.elsewhere();
|
|
90
|
+
}, 150);
|
|
91
|
+
};
|
|
92
|
+
const changed = (base) => {
|
|
93
|
+
changes.add(base);
|
|
94
|
+
touch();
|
|
69
95
|
};
|
|
70
96
|
const entries = followEntries(() => readMachineConfig(dir), sync, {
|
|
71
97
|
interval,
|
|
@@ -180,73 +206,84 @@ function createHub({ dir = machineDir(), state = stateDir(), home, interval, onS
|
|
|
180
206
|
res.end();
|
|
181
207
|
return;
|
|
182
208
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
res.end();
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
rest = p.slice(s.base.length).split("/").map(encodeURIComponent).join("/") + url.search;
|
|
192
|
-
} else {
|
|
193
|
-
const from = req.headers.referer ? new URL(req.headers.referer).pathname : null;
|
|
194
|
-
s = /^\/(api|_)\//.test(p) && from ? owner(decodeURIComponent(from)) : null;
|
|
195
|
-
if (!s) return root.handle(req, res);
|
|
196
|
-
rest = (req.url ?? "/").replace(/^\/+/, "/");
|
|
209
|
+
const s = owner(p);
|
|
210
|
+
if (s && p === s.base) {
|
|
211
|
+
res.writeHead(302, { location: `${s.base}/${url.search}` });
|
|
212
|
+
res.end();
|
|
213
|
+
return;
|
|
197
214
|
}
|
|
198
|
-
req.
|
|
199
|
-
return s.server.handle(req, res);
|
|
215
|
+
return route(req, p, url).handle(req, res);
|
|
200
216
|
} catch (err) {
|
|
201
217
|
console.error(err);
|
|
202
218
|
send(res, 500, String(err?.stack || err), "text/plain");
|
|
203
219
|
}
|
|
204
220
|
}
|
|
205
221
|
/**
|
|
222
|
+
* Whose request this is: a project's (`req.url` made the path within it), or else the overview's.
|
|
223
|
+
*/
|
|
224
|
+
function route(req, p, url) {
|
|
225
|
+
let s = owner(p);
|
|
226
|
+
if (s) {
|
|
227
|
+
req.url = p.slice(s.base.length).split("/").map(encodeURIComponent).join("/") + url.search;
|
|
228
|
+
return s.server;
|
|
229
|
+
}
|
|
230
|
+
const from = req.headers.referer ? new URL(req.headers.referer).pathname : null;
|
|
231
|
+
s = /^\/(api|_)\//.test(p) && from ? owner(decodeURIComponent(from)) : null;
|
|
232
|
+
req.url = s ? (req.url ?? "/").replace(/^\/+/, "/") : req.url;
|
|
233
|
+
return s ? s.server : root;
|
|
234
|
+
}
|
|
235
|
+
/** An upgrade (a page's live channel), routed as a request is. */
|
|
236
|
+
function upgrade(req, conn, head) {
|
|
237
|
+
try {
|
|
238
|
+
const url = new URL((req.url ?? "/").replace(/^\/+/, "/"), "http://localhost");
|
|
239
|
+
route(req, decodeURIComponent(url.pathname), url).upgrade(req, conn, head);
|
|
240
|
+
} catch (err) {
|
|
241
|
+
console.error(err);
|
|
242
|
+
conn.destroy();
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
206
246
|
* The fallback of a whole entry (a `wait` or `watch` without --page in one of its projects): one
|
|
207
247
|
* per entry, refused (409) while another runs, or while a project of it has a fallback of its own
|
|
208
248
|
* (from a whiteboard before entries). Each project holds it as a listener, so its pages say an
|
|
209
|
-
* agent listens;
|
|
249
|
+
* agent listens; its stream is told each project of the entry with who listens there, at once and
|
|
250
|
+
* whenever that or the projects change. The command itself reads the projects' logs.
|
|
210
251
|
*/
|
|
211
252
|
function listenEntry(req, res, q) {
|
|
212
253
|
const entry = q.get("entry");
|
|
213
|
-
const projects = [...served.values()].filter((s) => s.entry === entry);
|
|
214
254
|
if (fallbacks.has(entry)) return sendJson(res, 409, { error: `another agent is the fallback of ${entry} already` });
|
|
215
|
-
const
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
type: q.get("type") || null,
|
|
220
|
-
command: q.get("command") === "wait" ? "wait" : "watch",
|
|
221
|
-
fallback: true
|
|
255
|
+
const mine = () => [...served.values()].filter((s) => s.entry === entry);
|
|
256
|
+
const fb = {
|
|
257
|
+
listener: listenerOf(q, true),
|
|
258
|
+
tell: () => {}
|
|
222
259
|
};
|
|
223
|
-
|
|
224
|
-
for (const s of
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
connection: "keep-alive"
|
|
229
|
-
});
|
|
230
|
-
res.write(": listening\n\n");
|
|
231
|
-
const ping = setInterval(() => res.write(": ping\n\n"), 25e3);
|
|
232
|
-
const end = () => {
|
|
233
|
-
clearInterval(ping);
|
|
234
|
-
if (fallbacks.get(entry) !== l) return;
|
|
235
|
-
fallbacks.delete(entry);
|
|
236
|
-
for (const s of served.values()) {
|
|
237
|
-
s.fallback?.();
|
|
238
|
-
delete s.fallback;
|
|
260
|
+
const held = [];
|
|
261
|
+
for (const s of mine()) {
|
|
262
|
+
if (!hold(s, fb)) {
|
|
263
|
+
for (const h of held) h.fallback?.();
|
|
264
|
+
return sendJson(res, 409, { error: `${s.base} has a fallback of its own already (a wait or watch without --page)` });
|
|
239
265
|
}
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
266
|
+
held.push(s);
|
|
267
|
+
}
|
|
268
|
+
fallbacks.set(entry, fb);
|
|
269
|
+
const stream = holdStream(req, res, () => {
|
|
270
|
+
closers.delete(stream.end);
|
|
271
|
+
if (fallbacks.get(entry) !== fb) return;
|
|
272
|
+
fallbacks.delete(entry);
|
|
273
|
+
for (const s of mine()) s.fallback?.();
|
|
244
274
|
});
|
|
245
|
-
|
|
275
|
+
fb.tell = () => stream.send("listeners", { projects: mine().map((s) => ({
|
|
276
|
+
dir: s.dir,
|
|
277
|
+
base: s.base,
|
|
278
|
+
listeners: s.server.listeners()
|
|
279
|
+
})) });
|
|
280
|
+
closers.add(stream.end);
|
|
281
|
+
fb.tell();
|
|
246
282
|
}
|
|
247
283
|
const closers = /* @__PURE__ */ new Set();
|
|
248
284
|
return {
|
|
249
285
|
handle,
|
|
286
|
+
upgrade,
|
|
250
287
|
projects: list,
|
|
251
288
|
rescan() {
|
|
252
289
|
entries.rescan();
|
|
@@ -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 };
|
package/dist/src/pages/index.js
CHANGED
|
@@ -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.
|
|
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
|
|
155
|
-
|
|
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 ?? "")
|
|
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);
|
package/dist/src/pages/render.js
CHANGED
|
@@ -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(
|
|
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
|
-
|
|
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(
|
|
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.
|
|
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>`}<
|
|
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"
|
package/dist/src/serve.js
CHANGED
|
@@ -21,6 +21,7 @@ async function start(project) {
|
|
|
21
21
|
const server = http.createServer((req, res) => {
|
|
22
22
|
wb.handle(req, res);
|
|
23
23
|
});
|
|
24
|
+
server.on("upgrade", (req, conn, head) => wb.upgrade(req, conn, head));
|
|
24
25
|
const candidates = [...new Set([
|
|
25
26
|
recordedPort(project),
|
|
26
27
|
DEFAULT_PORT,
|
|
@@ -93,6 +94,7 @@ async function startShared(dir = machineDir(), state = stateDir()) {
|
|
|
93
94
|
const server = http.createServer((req, res) => {
|
|
94
95
|
hub.handle(req, res);
|
|
95
96
|
});
|
|
97
|
+
server.on("upgrade", (req, conn, head) => hub.upgrade(req, conn, head));
|
|
96
98
|
const first = config.port ?? DEFAULT_PORT;
|
|
97
99
|
const candidates = [...new Set([
|
|
98
100
|
other ? Number(new URL(other.origin).port) : null,
|