@bigmistqke/whiteboard 0.5.0 → 0.6.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 +125 -25
- package/bin/whiteboard.ts +299 -16
- package/dist/bin/whiteboard.js +247 -17
- package/dist/client/runtime.js +22 -10
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +64 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +28 -0
- package/dist/plugins/badge/index.js +24 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/public/style.css +7 -2
- package/dist/plugins/code/README.md +4 -2
- package/dist/plugins/code/diff.js +12 -2
- package/dist/plugins/grid/README.md +12 -12
- package/dist/plugins/grid/index.js +7 -19
- package/dist/plugins/grid/public/style.css +4 -33
- package/dist/plugins/iframe/README.md +7 -7
- package/dist/plugins/iframe/index.js +11 -11
- package/dist/plugins/iframe/public/style.css +15 -0
- package/dist/plugins/index.js +7 -3
- package/dist/plugins/lightbox/README.md +22 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/steps/README.md +3 -3
- package/dist/plugins/steps/index.js +3 -42
- package/dist/plugins/steps/public/client.js +1 -1
- package/dist/plugins/terminal/host.js +1 -1
- package/dist/plugins/terminal/public/client.js +1 -1
- package/dist/plugins/window/README.md +34 -14
- package/dist/plugins/window/index.js +21 -13
- package/dist/src/auth.js +4 -3
- package/dist/src/client/page/base.css +61 -1
- package/dist/src/client/page/main.js +60 -14
- package/dist/src/entries.js +211 -0
- package/dist/src/events.js +1 -1
- package/dist/src/hub.js +272 -0
- package/dist/src/loader.js +4 -1
- package/dist/src/package.js +23 -1
- package/dist/src/pages/blocks.js +1 -0
- package/dist/src/pages/index.js +1 -1
- package/dist/src/pages/markdown.js +87 -33
- package/dist/src/pages/render.js +116 -14
- package/dist/src/pages/window.js +16 -18
- package/dist/src/running.js +37 -2
- package/dist/src/serve.js +111 -2
- package/dist/src/server.js +49 -14
- package/package.json +1 -1
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +28 -0
- package/plugins/badge/index.ts +35 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/public/style.css +7 -2
- package/plugins/code/README.md +4 -2
- package/plugins/code/diff.ts +14 -2
- package/plugins/grid/README.md +12 -12
- package/plugins/grid/index.ts +11 -26
- package/plugins/grid/public/style.css +4 -33
- package/plugins/iframe/README.md +7 -7
- package/plugins/iframe/index.ts +15 -15
- package/plugins/iframe/public/style.css +15 -0
- package/plugins/index.ts +6 -2
- package/plugins/lightbox/README.md +22 -0
- package/plugins/lightbox/client.ts +99 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/steps/README.md +3 -3
- package/plugins/steps/client.ts +15 -6
- package/plugins/steps/index.ts +3 -46
- package/plugins/terminal/client.ts +16 -7
- package/plugins/window/README.md +34 -14
- package/plugins/window/index.ts +20 -13
- package/skills/whiteboard/SKILL.md +18 -10
- package/src/auth.ts +5 -3
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +2 -1
- package/src/client/markdown.ts +2 -1
- package/src/client/page/base.css +61 -1
- package/src/client/page/elsewhere.ts +72 -16
- package/src/client/page/fresh.ts +2 -1
- package/src/client/page/sort.ts +2 -1
- package/src/client/runtime.ts +11 -9
- package/src/define.ts +66 -7
- package/src/entries.ts +258 -0
- package/src/events.ts +2 -1
- package/src/hub.ts +355 -0
- package/src/loader.ts +7 -1
- package/src/package.ts +22 -0
- package/src/pages/blocks.ts +3 -0
- package/src/pages/index.ts +4 -2
- package/src/pages/markdown.ts +89 -32
- package/src/pages/render.ts +134 -13
- package/src/pages/window.ts +16 -25
- package/src/running.ts +47 -1
- package/src/serve.ts +114 -1
- package/src/server.ts +80 -14
- package/dist/plugins/gallery/README.md +0 -30
- package/dist/plugins/gallery/index.js +0 -75
- package/dist/plugins/gallery/public/client.js +0 -1
- package/dist/plugins/gallery/public/style.css +0 -90
- package/plugins/gallery/README.md +0 -30
- package/plugins/gallery/client.tsx +0 -70
- package/plugins/gallery/index.ts +0 -89
- package/plugins/gallery/public/style.css +0 -90
package/dist/src/hub.js
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { project } from "./config.js";
|
|
2
|
+
import { followEntries, machineDir, readMachineConfig, stateDir } from "./entries.js";
|
|
3
|
+
import { VERSION, compareVersions } from "./package.js";
|
|
4
|
+
import { createServer } from "./server.js";
|
|
5
|
+
import fs from "node:fs";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
// src/hub.ts
|
|
8
|
+
function createHub({ dir = machineDir(), state = stateDir(), home, interval, onShutdown, version = VERSION } = {}) {
|
|
9
|
+
const served = /* @__PURE__ */ new Map();
|
|
10
|
+
const fallbacks = /* @__PURE__ */ new Map();
|
|
11
|
+
function sync(projects) {
|
|
12
|
+
setImmediate(() => touch());
|
|
13
|
+
const wanted = new Map(projects.map((p) => [`${p.base}\n${p.dir}`, p]));
|
|
14
|
+
for (const [key, s] of served) if (!wanted.has(key)) {
|
|
15
|
+
s.server.close();
|
|
16
|
+
served.delete(key);
|
|
17
|
+
console.log(`whiteboard: ${s.base} is gone (${s.dir})`);
|
|
18
|
+
}
|
|
19
|
+
for (const [key, p] of wanted) {
|
|
20
|
+
if (served.has(key)) continue;
|
|
21
|
+
const project$1 = project(p.dir);
|
|
22
|
+
if (!project$1) continue;
|
|
23
|
+
const server = createServer(project$1, {
|
|
24
|
+
base: p.base,
|
|
25
|
+
entry: p.entry,
|
|
26
|
+
label: [p.group, p.name].filter(Boolean).join(" › "),
|
|
27
|
+
onChange: () => touch()
|
|
28
|
+
});
|
|
29
|
+
const fallback = fallbacks.get(p.entry);
|
|
30
|
+
served.set(key, {
|
|
31
|
+
...p,
|
|
32
|
+
project: project$1,
|
|
33
|
+
server,
|
|
34
|
+
...fallback && { fallback: server.addListener(fallback) }
|
|
35
|
+
});
|
|
36
|
+
console.log(`whiteboard: ${p.base} → ${p.dir}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const overviewDir = path.join(state, "overview");
|
|
40
|
+
fs.mkdirSync(path.join(overviewDir, ".whiteboard", "pages"), { recursive: true });
|
|
41
|
+
const root = createServer(project(overviewDir), {
|
|
42
|
+
name: "whiteboard",
|
|
43
|
+
files: false,
|
|
44
|
+
overview: async () => {
|
|
45
|
+
const all = await Promise.all([...served.values()].map(async (s) => ({
|
|
46
|
+
title: [s.group, s.name].filter(Boolean).join(" › "),
|
|
47
|
+
base: s.base,
|
|
48
|
+
dir: s.dir,
|
|
49
|
+
...await s.server.overview().catch(() => ({
|
|
50
|
+
pages: [],
|
|
51
|
+
files: false
|
|
52
|
+
}))
|
|
53
|
+
})));
|
|
54
|
+
const boxed = (process.env.WHITEBOARD_SANDBOX ?? "").split(":").filter(Boolean);
|
|
55
|
+
return {
|
|
56
|
+
groups: all.filter((g) => g.pages.length).toSorted((a, b) => (b.pages[0]?.mtime ?? 0) - (a.pages[0]?.mtime ?? 0)),
|
|
57
|
+
empty: all.filter((g) => !g.pages.length).map(({ title, base }) => ({
|
|
58
|
+
title,
|
|
59
|
+
base
|
|
60
|
+
})),
|
|
61
|
+
note: `whiteboard ${version} · ${boxed.length ? `boxed: ${boxed.join(", ")}` : "not boxed"} · ${dir}/config.json`
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
let touching;
|
|
66
|
+
const touch = () => {
|
|
67
|
+
clearTimeout(touching);
|
|
68
|
+
touching = setTimeout(() => root.touch(), 150);
|
|
69
|
+
};
|
|
70
|
+
const entries = followEntries(() => readMachineConfig(dir), sync, {
|
|
71
|
+
interval,
|
|
72
|
+
home
|
|
73
|
+
});
|
|
74
|
+
/** The project a path is under: the longest base that is the path or a folder above it. */
|
|
75
|
+
const owner = (p) => {
|
|
76
|
+
let best = null;
|
|
77
|
+
for (const s of served.values()) if ((p === s.base || p.startsWith(s.base + "/")) && (!best || s.base.length > best.base.length)) best = s;
|
|
78
|
+
return best;
|
|
79
|
+
};
|
|
80
|
+
async function index() {
|
|
81
|
+
return Promise.all([...served.values()].map(async (s) => {
|
|
82
|
+
const at = {
|
|
83
|
+
entry: s.entry,
|
|
84
|
+
group: s.group,
|
|
85
|
+
name: s.name,
|
|
86
|
+
label: [s.group, s.name].filter(Boolean).join(" › "),
|
|
87
|
+
dir: s.dir,
|
|
88
|
+
base: s.base
|
|
89
|
+
};
|
|
90
|
+
let statuses;
|
|
91
|
+
try {
|
|
92
|
+
statuses = await s.server.status();
|
|
93
|
+
} catch (err) {
|
|
94
|
+
return {
|
|
95
|
+
...at,
|
|
96
|
+
pages: [],
|
|
97
|
+
active: 0,
|
|
98
|
+
error: err.message
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const pages = statuses.map((p) => ({
|
|
102
|
+
page: p.page,
|
|
103
|
+
title: p.title,
|
|
104
|
+
url: `${s.base}/${p.page.split("/").map(encodeURIComponent).join("/")}`,
|
|
105
|
+
mtime: mtimeOf(p.file),
|
|
106
|
+
open: p.open,
|
|
107
|
+
unread: p.unread.length,
|
|
108
|
+
unpicked: p.unpicked.length
|
|
109
|
+
})).toSorted((a, b) => b.mtime - a.mtime);
|
|
110
|
+
return {
|
|
111
|
+
...at,
|
|
112
|
+
pages,
|
|
113
|
+
active: pages[0]?.mtime ?? 0
|
|
114
|
+
};
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
const send = (res, status, body, type = "text/html; charset=utf-8") => {
|
|
118
|
+
res.writeHead(status, {
|
|
119
|
+
"content-type": type,
|
|
120
|
+
"cache-control": "no-store"
|
|
121
|
+
});
|
|
122
|
+
res.end(body);
|
|
123
|
+
};
|
|
124
|
+
const sendJson = (res, status, data) => send(res, status, JSON.stringify(data), "application/json");
|
|
125
|
+
const list = () => [...served.values()].map(({ entry, group, name, dir, base }) => ({
|
|
126
|
+
entry,
|
|
127
|
+
group,
|
|
128
|
+
name,
|
|
129
|
+
dir,
|
|
130
|
+
base
|
|
131
|
+
}));
|
|
132
|
+
function readBody(req) {
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
let body = "";
|
|
135
|
+
req.setEncoding("utf8");
|
|
136
|
+
req.on("data", (c) => {
|
|
137
|
+
body += c;
|
|
138
|
+
if (body.length > 1e4) req.destroy();
|
|
139
|
+
});
|
|
140
|
+
req.on("end", () => resolve(body));
|
|
141
|
+
req.on("error", reject);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
async function handle(req, res) {
|
|
145
|
+
try {
|
|
146
|
+
const url = new URL((req.url ?? "/").replace(/^\/+/, "/"), "http://localhost");
|
|
147
|
+
const p = decodeURIComponent(url.pathname);
|
|
148
|
+
if (p === "/api/health") {
|
|
149
|
+
if (url.searchParams.has("rescan")) entries.rescan();
|
|
150
|
+
return sendJson(res, 200, {
|
|
151
|
+
ok: true,
|
|
152
|
+
hub: true,
|
|
153
|
+
pid: process.pid,
|
|
154
|
+
version,
|
|
155
|
+
dir,
|
|
156
|
+
projects: list()
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
if (p === "/api/index") {
|
|
160
|
+
const entry = url.searchParams.get("entry");
|
|
161
|
+
return sendJson(res, 200, (await index()).filter((x) => !entry || x.entry === entry));
|
|
162
|
+
}
|
|
163
|
+
if (p === "/api/shutdown" && req.method === "POST") {
|
|
164
|
+
if (req.headers.origin) return sendJson(res, 403, { error: "not from a browser" });
|
|
165
|
+
let asker = "";
|
|
166
|
+
try {
|
|
167
|
+
asker = String(JSON.parse(await readBody(req)).version ?? "");
|
|
168
|
+
} catch {}
|
|
169
|
+
if (!asker || compareVersions(asker, version) <= 0) return sendJson(res, 409, { error: `whiteboard ${version} runs here; ${asker || "that"} is not newer` });
|
|
170
|
+
sendJson(res, 200, {
|
|
171
|
+
ok: true,
|
|
172
|
+
version
|
|
173
|
+
});
|
|
174
|
+
setImmediate(() => onShutdown?.());
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (p === "/api/listen" && url.searchParams.has("entry")) return listenEntry(req, res, url.searchParams);
|
|
178
|
+
if (p === "/favicon.ico") {
|
|
179
|
+
res.writeHead(204);
|
|
180
|
+
res.end();
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
let s = owner(p);
|
|
184
|
+
let rest;
|
|
185
|
+
if (s) {
|
|
186
|
+
if (p === s.base) {
|
|
187
|
+
res.writeHead(302, { location: `${s.base}/${url.search}` });
|
|
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(/^\/+/, "/");
|
|
197
|
+
}
|
|
198
|
+
req.url = rest;
|
|
199
|
+
return s.server.handle(req, res);
|
|
200
|
+
} catch (err) {
|
|
201
|
+
console.error(err);
|
|
202
|
+
send(res, 500, String(err?.stack || err), "text/plain");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* The fallback of a whole entry (a `wait` or `watch` without --page in one of its projects): one
|
|
207
|
+
* per entry, refused (409) while another runs, or while a project of it has a fallback of its own
|
|
208
|
+
* (from a whiteboard before entries). Each project holds it as a listener, so its pages say an
|
|
209
|
+
* agent listens; the command itself reads the projects' logs.
|
|
210
|
+
*/
|
|
211
|
+
function listenEntry(req, res, q) {
|
|
212
|
+
const entry = q.get("entry");
|
|
213
|
+
const projects = [...served.values()].filter((s) => s.entry === entry);
|
|
214
|
+
if (fallbacks.has(entry)) return sendJson(res, 409, { error: `another agent is the fallback of ${entry} already` });
|
|
215
|
+
const own = projects.find((s) => s.server.listeners().some((x) => x.fallback));
|
|
216
|
+
if (own) return sendJson(res, 409, { error: `${own.base} has a fallback of its own already (a wait or watch without --page)` });
|
|
217
|
+
const l = {
|
|
218
|
+
page: "",
|
|
219
|
+
type: q.get("type") || null,
|
|
220
|
+
command: q.get("command") === "wait" ? "wait" : "watch",
|
|
221
|
+
fallback: true
|
|
222
|
+
};
|
|
223
|
+
fallbacks.set(entry, l);
|
|
224
|
+
for (const s of projects) s.fallback = s.server.addListener(l);
|
|
225
|
+
res.writeHead(200, {
|
|
226
|
+
"content-type": "text/event-stream",
|
|
227
|
+
"cache-control": "no-store",
|
|
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;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
closers.add(() => {
|
|
242
|
+
end();
|
|
243
|
+
res.end();
|
|
244
|
+
});
|
|
245
|
+
req.on("close", end);
|
|
246
|
+
}
|
|
247
|
+
const closers = /* @__PURE__ */ new Set();
|
|
248
|
+
return {
|
|
249
|
+
handle,
|
|
250
|
+
projects: list,
|
|
251
|
+
rescan() {
|
|
252
|
+
entries.rescan();
|
|
253
|
+
return list();
|
|
254
|
+
},
|
|
255
|
+
close() {
|
|
256
|
+
clearTimeout(touching);
|
|
257
|
+
root.close();
|
|
258
|
+
for (const c of closers) c();
|
|
259
|
+
entries.stop();
|
|
260
|
+
for (const s of served.values()) s.server.close();
|
|
261
|
+
served.clear();
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
const mtimeOf = (file) => {
|
|
266
|
+
try {
|
|
267
|
+
return fs.statSync(file).mtimeMs;
|
|
268
|
+
} catch {
|
|
269
|
+
return 0;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
export { createHub };
|
package/dist/src/loader.js
CHANGED
|
@@ -28,6 +28,7 @@ const BUILTIN_COMMANDS = /* @__PURE__ */ new Set([
|
|
|
28
28
|
"build",
|
|
29
29
|
"edit",
|
|
30
30
|
"skill",
|
|
31
|
+
"paths",
|
|
31
32
|
"help"
|
|
32
33
|
]);
|
|
33
34
|
const RESERVED = /* @__PURE__ */ new Set([
|
|
@@ -98,11 +99,13 @@ function createConfigLoader(proj, { pages: withPages = true } = {}) {
|
|
|
98
99
|
file: inPublic(publicDirs.get(p.name), p.style, `plugin ${p.name}: style`)
|
|
99
100
|
}));
|
|
100
101
|
const links = plugins.flatMap((p) => p.links ? [p.links.bind(p)] : []);
|
|
102
|
+
const composers = new Map(plugins.flatMap((p) => typeof p.compose === "function" ? [[p.name, p.compose.bind(p)]] : []));
|
|
101
103
|
const pages = withPages ? createPages({
|
|
102
104
|
blocks,
|
|
103
105
|
style: config?.style,
|
|
104
106
|
styles,
|
|
105
|
-
links
|
|
107
|
+
links,
|
|
108
|
+
composers
|
|
106
109
|
}) : null;
|
|
107
110
|
const declared = (flag) => new Set(plugins.flatMap((p) => Object.entries(p.events ?? {}).filter(([, spec]) => spec?.[flag]).map(([type]) => type)));
|
|
108
111
|
return {
|
package/dist/src/package.js
CHANGED
|
@@ -27,4 +27,26 @@ const FROM_SOURCE = fileURLToPath(import.meta.url).endsWith(".ts");
|
|
|
27
27
|
*/
|
|
28
28
|
/** A built-in plugin's public folder: its built browser modules and copied styles (build.ts). */
|
|
29
29
|
const builtinPublic = (name) => packagePath("dist", "plugins", name, "public");
|
|
30
|
-
|
|
30
|
+
/** This package's version, from its package.json: what a shared server records, and its takeover compares. */
|
|
31
|
+
const VERSION = JSON.parse(fs.readFileSync(path.join(PACKAGE_DIR, "package.json"), "utf8")).version;
|
|
32
|
+
/** Compare two versions, x.y.z with an optional -pre: < 0 when a is older, 0 when the same, > 0 when newer. */
|
|
33
|
+
function compareVersions(a, b) {
|
|
34
|
+
const parse = (v) => {
|
|
35
|
+
const [main, pre] = String(v).split("-", 2);
|
|
36
|
+
return {
|
|
37
|
+
nums: main.split(".").map((n) => Number(n) || 0),
|
|
38
|
+
pre: pre ?? null
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
const x = parse(a);
|
|
42
|
+
const y = parse(b);
|
|
43
|
+
for (let i = 0; i < Math.max(x.nums.length, y.nums.length); i++) {
|
|
44
|
+
const d = (x.nums[i] ?? 0) - (y.nums[i] ?? 0);
|
|
45
|
+
if (d) return Math.sign(d);
|
|
46
|
+
}
|
|
47
|
+
if (x.pre === y.pre) return 0;
|
|
48
|
+
if (x.pre == null) return 1;
|
|
49
|
+
if (y.pre == null) return -1;
|
|
50
|
+
return x.pre < y.pre ? -1 : 1;
|
|
51
|
+
}
|
|
52
|
+
export { FROM_SOURCE, PACKAGE_DIR, VERSION, builtinPublic, compareVersions, packagePath };
|
package/dist/src/pages/blocks.js
CHANGED
|
@@ -20,6 +20,7 @@ function normalize({ name, block: b, plugin, public: dir }) {
|
|
|
20
20
|
fences: b.fence === true || b.fence === "any",
|
|
21
21
|
props: typeof b.props === "function" ? b.props.bind(b) : null,
|
|
22
22
|
html: typeof b.html === "function" ? b.html.bind(b) : null,
|
|
23
|
+
open: typeof b.open === "function" ? b.open.bind(b) : null,
|
|
23
24
|
...files,
|
|
24
25
|
source: plugin
|
|
25
26
|
};
|
package/dist/src/pages/index.js
CHANGED
|
@@ -6,7 +6,7 @@ function createPages(options) {
|
|
|
6
6
|
const resolved = resolveBlocks(options.blocks);
|
|
7
7
|
const blocks = () => resolved;
|
|
8
8
|
return {
|
|
9
|
-
render: createRender(blocks, options.style, options.styles ?? [], options.links ?? []),
|
|
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
12
|
questions: (page) => formNames(page.source)
|
|
@@ -50,12 +50,17 @@ function parseMarkdown(source) {
|
|
|
50
50
|
return tree;
|
|
51
51
|
}
|
|
52
52
|
const toHast = unified().use(remarkRehype, { allowDangerousHtml: true }).use(rehypeStringify, { allowDangerousHtml: true }).freeze();
|
|
53
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* `cols=3 open title="a b"` → { cols: '3', open: '', title: 'a b' } (bare words get '' like directive
|
|
55
|
+
* attrs). In braces too, as a fence writes them: `{compose=window}` (an unquoted value ends at `}`).
|
|
56
|
+
*/
|
|
54
57
|
function parseAttrs(s) {
|
|
55
58
|
const out = {};
|
|
56
|
-
for (const m of String(s ?? "").matchAll(/([\w-]+)(?:=("[^"]*"|'[^']*'
|
|
59
|
+
for (const m of String(s ?? "").matchAll(/([\w-]+)(?:=("[^"]*"|'[^']*'|[^\s}]+))?/g)) out[m[1]] = m[2] ? m[2].replace(/^["']|["']$/g, "") : "";
|
|
57
60
|
return out;
|
|
58
61
|
}
|
|
62
|
+
/** A heading's text as a key: lowercase, words joined by - */
|
|
63
|
+
const slugOf = (text) => text.toLowerCase().trim().replace(/[^\p{L}\p{N}]+/gu, "-").replace(/^-|-$/g, "");
|
|
59
64
|
const DIRECTIVES = /* @__PURE__ */ new Set([
|
|
60
65
|
"containerDirective",
|
|
61
66
|
"leafDirective",
|
|
@@ -215,7 +220,7 @@ function formNames(source) {
|
|
|
215
220
|
* blocks. Returns the page's HTML, its first heading, the islands to mount and the blocks'
|
|
216
221
|
* stylesheets.
|
|
217
222
|
*/
|
|
218
|
-
async function renderMarkdown(source, { page, file, resolve, code = null, linkFor, textLinks = [], message = false }) {
|
|
223
|
+
async function renderMarkdown(source, { page, file, resolve, code = null, linkFor, textLinks = [], message = false, composers }) {
|
|
219
224
|
let nextId = 0;
|
|
220
225
|
const islands = {};
|
|
221
226
|
const keyCount = {};
|
|
@@ -241,48 +246,97 @@ async function renderMarkdown(source, { page, file, resolve, code = null, linkFo
|
|
|
241
246
|
});
|
|
242
247
|
return stringify(nodes);
|
|
243
248
|
};
|
|
244
|
-
|
|
249
|
+
/**
|
|
250
|
+
* A block's items (DirectiveContext.items): a heading at the block's shallowest heading level, or a
|
|
251
|
+
* `---`, starts one; what comes before the first is one too, untitled; content with neither is one
|
|
252
|
+
* item. Deeper headings stay inside their item. An untitled item with nothing in it (a `---` right
|
|
253
|
+
* before a heading) is none.
|
|
254
|
+
*/
|
|
255
|
+
async function itemsOf(children) {
|
|
245
256
|
await transform({
|
|
246
257
|
type: "root",
|
|
247
258
|
children
|
|
248
259
|
});
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
title: null,
|
|
254
|
-
text: null,
|
|
255
|
-
html: stringify([n])
|
|
256
|
-
}))
|
|
257
|
-
};
|
|
258
|
-
const depth = Math.min(...heads.map((h) => h.depth));
|
|
259
|
-
const out = [];
|
|
260
|
-
const intro = [];
|
|
260
|
+
const content = children.filter((n) => !(n.type === "text" && !String(n.value ?? "").trim()));
|
|
261
|
+
const heads = content.filter((n) => n.type === "heading");
|
|
262
|
+
const depth = heads.length ? Math.min(...heads.map((h) => h.depth)) : null;
|
|
263
|
+
const groups = [];
|
|
261
264
|
let current = null;
|
|
262
|
-
for (const n of
|
|
263
|
-
|
|
264
|
-
|
|
265
|
+
for (const n of content) if (n.type === "heading" && n.depth === depth) groups.push(current = {
|
|
266
|
+
heading: n,
|
|
267
|
+
body: []
|
|
268
|
+
});
|
|
269
|
+
else if (n.type === "thematicBreak") groups.push(current = {
|
|
270
|
+
heading: null,
|
|
271
|
+
body: []
|
|
272
|
+
});
|
|
273
|
+
else {
|
|
274
|
+
if (!current) groups.push(current = {
|
|
275
|
+
heading: null,
|
|
265
276
|
body: []
|
|
277
|
+
});
|
|
278
|
+
current.body.push(n);
|
|
279
|
+
}
|
|
280
|
+
const taken = /* @__PURE__ */ new Map();
|
|
281
|
+
return groups.filter((g) => g.heading || g.body.length).map((g, i) => {
|
|
282
|
+
const text = g.heading ? toString(g.heading) : null;
|
|
283
|
+
const base = text && slugOf(text) || String(i + 1);
|
|
284
|
+
const n = (taken.get(base) ?? 0) + 1;
|
|
285
|
+
taken.set(base, n);
|
|
286
|
+
return {
|
|
287
|
+
title: g.heading ? stringify(g.heading.children ?? []) : null,
|
|
288
|
+
text,
|
|
289
|
+
html: stringify(g.body),
|
|
290
|
+
nodes: g.body,
|
|
291
|
+
key: n > 1 ? `${base}-${n}` : base
|
|
266
292
|
};
|
|
267
|
-
|
|
268
|
-
} else if (current) current.body.push(n);
|
|
269
|
-
else intro.push(n);
|
|
270
|
-
return {
|
|
271
|
-
intro: stringify(intro),
|
|
272
|
-
sections: out.map((s) => ({
|
|
273
|
-
title: stringify(s.heading.children ?? []),
|
|
274
|
-
text: toString(s.heading),
|
|
275
|
-
html: stringify(s.body)
|
|
276
|
-
}))
|
|
277
|
-
};
|
|
293
|
+
});
|
|
278
294
|
}
|
|
279
295
|
/** HTML into the markdown tree, where the rest of the pipeline takes it as it is: the one place it becomes a string. */
|
|
280
296
|
const htmlNode = (markup) => ({
|
|
281
297
|
type: "html",
|
|
282
298
|
value: String(markup)
|
|
283
299
|
});
|
|
284
|
-
/**
|
|
300
|
+
/**
|
|
301
|
+
* A block's output for one directive or fence: static HTML, or an island around it; then put in
|
|
302
|
+
* what the plugins its `compose` attribute names compose around it (Plugin.compose), in the order
|
|
303
|
+
* written, the first innermost: `{compose=lightbox,window}`. A name no plugin composes is said in
|
|
304
|
+
* place. Not inline blocks, not a block of the composing plugin's own name, not one that renders
|
|
305
|
+
* nothing (left out of a build), and not one that says what's wrong with it instead.
|
|
306
|
+
*/
|
|
285
307
|
async function run(block, ctx, fallback) {
|
|
308
|
+
let out = await output(block, ctx, fallback);
|
|
309
|
+
const attrs = ctx.attrs ?? {};
|
|
310
|
+
const names = String(attrs.compose ?? "").split(/[\s,]+/).filter(Boolean);
|
|
311
|
+
const shown = String(out).trim();
|
|
312
|
+
if (!names.length || ctx.kind === "textDirective" || !shown || shown.startsWith("<pre class=\"wb-error\"")) return out;
|
|
313
|
+
let open;
|
|
314
|
+
for (const name of names) {
|
|
315
|
+
if (block.name === name) continue;
|
|
316
|
+
const compose = composers?.get(name);
|
|
317
|
+
if (!compose) {
|
|
318
|
+
out = html`<pre class="wb-error">compose: no plugin named "${name}" composes around blocks</pre>
|
|
319
|
+
${out}`;
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
try {
|
|
323
|
+
open ??= block.open?.(ctx) ?? null;
|
|
324
|
+
out = raw(String(await compose({
|
|
325
|
+
children: out,
|
|
326
|
+
block: block.name,
|
|
327
|
+
attrs,
|
|
328
|
+
label: ctx.label ?? null,
|
|
329
|
+
open
|
|
330
|
+
})));
|
|
331
|
+
} catch (err) {
|
|
332
|
+
console.error(`plugin ${name}: compose:`, err);
|
|
333
|
+
out = html`<pre class="wb-error">
|
|
334
|
+
plugin "${name}" failed to compose around block "${block.name}": ${err.message}</pre>`;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return out;
|
|
338
|
+
}
|
|
339
|
+
async function output(block, ctx, fallback) {
|
|
286
340
|
used.set(block.name, block);
|
|
287
341
|
try {
|
|
288
342
|
const own = block.html ? await block.html(ctx) : null;
|
|
@@ -348,7 +402,7 @@ async function renderMarkdown(source, { page, file, resolve, code = null, linkFo
|
|
|
348
402
|
}) : null,
|
|
349
403
|
children,
|
|
350
404
|
node,
|
|
351
|
-
|
|
405
|
+
items: () => itemsOf(children),
|
|
352
406
|
defaults: () => defaults ??= (async () => {
|
|
353
407
|
const content = await toHtml(children);
|
|
354
408
|
return {
|
|
@@ -356,7 +410,7 @@ async function renderMarkdown(source, { page, file, resolve, code = null, linkFo
|
|
|
356
410
|
attrs: { ...node.attributes },
|
|
357
411
|
label: labelHtml,
|
|
358
412
|
html: content,
|
|
359
|
-
|
|
413
|
+
items: (await itemsOf(children)).map(({ nodes: _, ...item }) => item)
|
|
360
414
|
};
|
|
361
415
|
})()
|
|
362
416
|
};
|