@bigmistqke/whiteboard 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +609 -2
- package/bin/whiteboard.ts +431 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +471 -0
- package/dist/client/runtime.js +391 -0
- package/dist/define.js +7 -0
- package/dist/plugins/callout/README.md +13 -0
- package/dist/plugins/callout/index.js +27 -0
- package/dist/plugins/callout/style.css +12 -0
- package/dist/plugins/chat/README.md +45 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +46 -0
- package/dist/plugins/chat/style.css +17 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +64 -0
- package/dist/plugins/code/index.js +77 -0
- package/dist/plugins/code/style.css +29 -0
- package/dist/plugins/comments/client.js +10 -0
- package/dist/plugins/comments/index.js +51 -0
- package/dist/plugins/comments/style.css +32 -0
- package/dist/plugins/details/README.md +17 -0
- package/dist/plugins/details/index.js +17 -0
- package/dist/plugins/details/style.css +3 -0
- package/dist/plugins/form/README.md +111 -0
- package/dist/plugins/form/index.js +102 -0
- package/dist/plugins/form/style.css +41 -0
- package/dist/plugins/gallery/README.md +30 -0
- package/dist/plugins/gallery/client.js +1 -0
- package/dist/plugins/gallery/index.js +74 -0
- package/dist/plugins/gallery/style.css +27 -0
- package/dist/plugins/grid/README.md +33 -0
- package/dist/plugins/grid/index.js +33 -0
- package/dist/plugins/grid/style.css +25 -0
- package/dist/plugins/index.js +26 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +34 -0
- package/dist/plugins/mermaid/style.css +16 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +17 -0
- package/dist/plugins/terminal/client.js +41 -0
- package/dist/plugins/terminal/host.js +332 -0
- package/dist/plugins/terminal/index.js +171 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +57 -0
- package/dist/plugins/terminal/style.css +357 -0
- package/dist/solid/index.js +35 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +274 -0
- package/dist/src/client/page/main.js +1004 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +162 -0
- package/dist/src/html.js +34 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +120 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +49 -0
- package/dist/src/pages/index.js +14 -0
- package/dist/src/pages/markdown.js +307 -0
- package/dist/src/pages/render.js +257 -0
- package/dist/src/plugins.js +238 -0
- package/dist/src/running.js +24 -0
- package/dist/src/serve.js +71 -0
- package/dist/src/server.js +700 -0
- package/dist/src/status.js +43 -0
- package/package.json +131 -4
- package/plugins/callout/README.md +13 -0
- package/plugins/callout/index.ts +25 -0
- package/plugins/callout/style.css +12 -0
- package/plugins/chat/README.md +45 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +81 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +17 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +58 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +448 -0
- package/plugins/comments/index.ts +52 -0
- package/plugins/comments/style.css +32 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/form/README.md +111 -0
- package/plugins/form/index.ts +104 -0
- package/plugins/form/style.css +41 -0
- package/plugins/gallery/README.md +30 -0
- package/plugins/gallery/client.tsx +64 -0
- package/plugins/gallery/index.ts +67 -0
- package/plugins/gallery/style.css +27 -0
- package/plugins/grid/README.md +33 -0
- package/plugins/grid/index.ts +36 -0
- package/plugins/grid/style.css +25 -0
- package/plugins/index.ts +19 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +39 -0
- package/plugins/mermaid/style.css +16 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +27 -0
- package/plugins/terminal/client.ts +317 -0
- package/plugins/terminal/host.ts +326 -0
- package/plugins/terminal/index.ts +138 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/server.ts +215 -0
- package/plugins/terminal/sizes.ts +60 -0
- package/plugins/terminal/style.css +71 -0
- package/skills/whiteboard/SKILL.md +292 -0
- package/solid/index.tsx +56 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +186 -0
- package/src/client/page/base.css +274 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/main.ts +249 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +109 -0
- package/src/client/runtime.ts +170 -0
- package/src/config.ts +96 -0
- package/src/define.ts +546 -0
- package/src/events.ts +194 -0
- package/src/html.ts +40 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +109 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +67 -0
- package/src/pages/index.ts +36 -0
- package/src/pages/markdown.ts +276 -0
- package/src/pages/render.ts +196 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +644 -0
- package/src/status.ts +69 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { html, raw } from "../html.js";
|
|
2
|
+
import { isQuestionName } from "../status.js";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import crypto from "node:crypto";
|
|
5
|
+
import { unified } from "unified";
|
|
6
|
+
import remarkParse from "remark-parse";
|
|
7
|
+
import remarkGfm from "remark-gfm";
|
|
8
|
+
import remarkDirective from "remark-directive";
|
|
9
|
+
import remarkRehype from "remark-rehype";
|
|
10
|
+
import rehypeStringify from "rehype-stringify";
|
|
11
|
+
import { toString } from "mdast-util-to-string";
|
|
12
|
+
// src/pages/markdown.ts
|
|
13
|
+
const parser = unified().use(remarkParse).use(remarkGfm).use(remarkDirective).freeze();
|
|
14
|
+
const toHast = unified().use(remarkRehype, { allowDangerousHtml: true }).use(rehypeStringify, { allowDangerousHtml: true }).freeze();
|
|
15
|
+
/** `cols=3 open title="a b"` → { cols: '3', open: '', title: 'a b' } (bare words get '' like directive attrs) */
|
|
16
|
+
function parseAttrs(s) {
|
|
17
|
+
const out = {};
|
|
18
|
+
for (const m of String(s ?? "").matchAll(/([\w-]+)(?:=("[^"]*"|'[^']*'|\S+))?/g)) out[m[1]] = m[2] ? m[2].replace(/^["']|["']$/g, "") : "";
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
const DIRECTIVES = /* @__PURE__ */ new Set([
|
|
22
|
+
"containerDirective",
|
|
23
|
+
"leafDirective",
|
|
24
|
+
"textDirective"
|
|
25
|
+
]);
|
|
26
|
+
const PHRASING = /* @__PURE__ */ new Set([
|
|
27
|
+
"text",
|
|
28
|
+
"emphasis",
|
|
29
|
+
"strong",
|
|
30
|
+
"delete",
|
|
31
|
+
"inlineCode",
|
|
32
|
+
"break",
|
|
33
|
+
"link",
|
|
34
|
+
"linkReference",
|
|
35
|
+
"image",
|
|
36
|
+
"imageReference",
|
|
37
|
+
"footnoteReference",
|
|
38
|
+
"textDirective"
|
|
39
|
+
]);
|
|
40
|
+
const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
|
|
41
|
+
/**
|
|
42
|
+
* Point relative links and images at what they name on disk: each is resolved from the markdown
|
|
43
|
+
* file's own folder and handed to `linkFor(absolutePath)`, which returns the URL that serves it
|
|
44
|
+
* (or null to leave the link as written).
|
|
45
|
+
*/
|
|
46
|
+
function rewriteLinks(tree, file, linkFor) {
|
|
47
|
+
if (!linkFor || !file) return;
|
|
48
|
+
const walk = (node) => {
|
|
49
|
+
if ((node.type === "link" || node.type === "image" || node.type === "definition") && node.url && !SCHEME.test(node.url) && !/^[/#]/.test(node.url)) {
|
|
50
|
+
const [, target, rest] = /^([^?#]*)(.*)$/s.exec(node.url);
|
|
51
|
+
let decoded = target;
|
|
52
|
+
try {
|
|
53
|
+
decoded = decodeURIComponent(target);
|
|
54
|
+
} catch {}
|
|
55
|
+
const url = decoded && linkFor(path.resolve(path.dirname(file), decoded));
|
|
56
|
+
if (url) node.url = url + rest;
|
|
57
|
+
}
|
|
58
|
+
node.children?.forEach(walk);
|
|
59
|
+
};
|
|
60
|
+
walk(tree);
|
|
61
|
+
}
|
|
62
|
+
/** Separate a directive's `[label]` (remark-directive stores it as a flagged first paragraph). */
|
|
63
|
+
function splitLabel(node) {
|
|
64
|
+
const [head, ...rest] = node.children ?? [];
|
|
65
|
+
if (node.type === "containerDirective" && head?.data?.directiveLabel) return {
|
|
66
|
+
label: head.children ?? null,
|
|
67
|
+
children: rest
|
|
68
|
+
};
|
|
69
|
+
if (node.type !== "containerDirective") return {
|
|
70
|
+
label: node.children?.length ? node.children : null,
|
|
71
|
+
children: []
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
label: null,
|
|
75
|
+
children: node.children ?? []
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const MARKER = {
|
|
79
|
+
textDirective: ":",
|
|
80
|
+
leafDirective: "::",
|
|
81
|
+
containerDirective: ":::"
|
|
82
|
+
};
|
|
83
|
+
function literalDirective(node, label) {
|
|
84
|
+
const marker = MARKER[node.type];
|
|
85
|
+
const attrs = Object.entries(node.attributes ?? {}).map(([k, v]) => v ? `${k}="${v}"` : k).join(" ");
|
|
86
|
+
return `${marker}${node.name}${label ? `[${toString({
|
|
87
|
+
type: "root",
|
|
88
|
+
children: label
|
|
89
|
+
})}]` : ""}${attrs ? `{${attrs}}` : ""}`;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* The forms a page asks the reader to answer, by name: `:::form{name=…}` containers and
|
|
93
|
+
* hand-written <form name="…"> without an action. Parsed, so a form shown as an example inside a
|
|
94
|
+
* code block doesn't count.
|
|
95
|
+
*/
|
|
96
|
+
function formNames(source) {
|
|
97
|
+
const tree = parser.parse(String(source ?? "").replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, ""));
|
|
98
|
+
const names = [];
|
|
99
|
+
const walk = (node) => {
|
|
100
|
+
if (node.type === "containerDirective" && node.name === "form") {
|
|
101
|
+
const attrs = node.attributes ?? {};
|
|
102
|
+
names.push(attrs.name || attrs.id || "form");
|
|
103
|
+
} else if (node.type === "html") for (const m of (node.value ?? "").matchAll(/<form\b([^>]*)>/gi)) {
|
|
104
|
+
if (/\baction\s*=/i.test(m[1])) continue;
|
|
105
|
+
const name = /\bname\s*=\s*("([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(m[1]);
|
|
106
|
+
if (name) names.push(name[2] ?? name[3] ?? name[4]);
|
|
107
|
+
}
|
|
108
|
+
for (const child of node.children ?? []) walk(child);
|
|
109
|
+
};
|
|
110
|
+
walk(tree);
|
|
111
|
+
return [...new Set(names)].filter(isQuestionName);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Markdown (or an mdast root, rendered as it is) → HTML, with directives and fences handed to
|
|
115
|
+
* blocks. Returns the page's HTML, its first heading, the islands to mount and the blocks'
|
|
116
|
+
* stylesheets.
|
|
117
|
+
*/
|
|
118
|
+
async function renderMarkdown(source, { page, file, resolve, code = null, linkFor }) {
|
|
119
|
+
let nextId = 0;
|
|
120
|
+
const islands = {};
|
|
121
|
+
const keyCount = {};
|
|
122
|
+
const used = /* @__PURE__ */ new Map();
|
|
123
|
+
const stringify = (nodes) => {
|
|
124
|
+
if (nodes.length && nodes.every((n) => PHRASING.has(n.type))) return toHast.stringify(toHast.runSync({
|
|
125
|
+
type: "root",
|
|
126
|
+
children: [{
|
|
127
|
+
type: "paragraph",
|
|
128
|
+
children: nodes
|
|
129
|
+
}]
|
|
130
|
+
})).replace(/^<p>([\s\S]*)<\/p>$/, "$1");
|
|
131
|
+
return toHast.stringify(toHast.runSync({
|
|
132
|
+
type: "root",
|
|
133
|
+
children: nodes
|
|
134
|
+
}));
|
|
135
|
+
};
|
|
136
|
+
/** Render mdast nodes to HTML. Transforms nested directives first; safe to call twice on the same nodes. */
|
|
137
|
+
const toHtml = async (nodes) => {
|
|
138
|
+
await transform({
|
|
139
|
+
type: "root",
|
|
140
|
+
children: nodes
|
|
141
|
+
});
|
|
142
|
+
return stringify(nodes);
|
|
143
|
+
};
|
|
144
|
+
async function sectionsOf(children) {
|
|
145
|
+
await transform({
|
|
146
|
+
type: "root",
|
|
147
|
+
children
|
|
148
|
+
});
|
|
149
|
+
const heads = children.filter((n) => n.type === "heading");
|
|
150
|
+
if (!heads.length) return {
|
|
151
|
+
intro: "",
|
|
152
|
+
sections: children.map((n) => ({
|
|
153
|
+
title: null,
|
|
154
|
+
text: null,
|
|
155
|
+
html: stringify([n])
|
|
156
|
+
}))
|
|
157
|
+
};
|
|
158
|
+
const depth = Math.min(...heads.map((h) => h.depth));
|
|
159
|
+
const out = [];
|
|
160
|
+
const intro = [];
|
|
161
|
+
let current = null;
|
|
162
|
+
for (const n of children) if (n.type === "heading" && n.depth === depth) {
|
|
163
|
+
current = {
|
|
164
|
+
heading: n,
|
|
165
|
+
body: []
|
|
166
|
+
};
|
|
167
|
+
out.push(current);
|
|
168
|
+
} else if (current) current.body.push(n);
|
|
169
|
+
else intro.push(n);
|
|
170
|
+
return {
|
|
171
|
+
intro: stringify(intro),
|
|
172
|
+
sections: out.map((s) => ({
|
|
173
|
+
title: stringify(s.heading.children ?? []),
|
|
174
|
+
text: toString(s.heading),
|
|
175
|
+
html: stringify(s.body)
|
|
176
|
+
}))
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
/** HTML into the markdown tree, where the rest of the pipeline takes it as it is: the one place it becomes a string. */
|
|
180
|
+
const htmlNode = (markup) => ({
|
|
181
|
+
type: "html",
|
|
182
|
+
value: String(markup)
|
|
183
|
+
});
|
|
184
|
+
/** A block's output for one directive or fence: static HTML, or an island around it. */
|
|
185
|
+
async function run(block, ctx, fallback) {
|
|
186
|
+
used.set(block.name, block);
|
|
187
|
+
try {
|
|
188
|
+
const own = block.html ? await block.html(ctx) : null;
|
|
189
|
+
const body = raw(own ?? await fallback());
|
|
190
|
+
const tag = raw(ctx.kind === "textDirective" ? "span" : "div");
|
|
191
|
+
if (!block.client) return own != null ? body : html`<${tag} data-block="${ctx.name}">${body}</${tag}>`;
|
|
192
|
+
const props = block.props ? await block.props(ctx) : await ctx.defaults();
|
|
193
|
+
const id = String(++nextId);
|
|
194
|
+
islands[id] = {
|
|
195
|
+
block: block.name,
|
|
196
|
+
client: block.client,
|
|
197
|
+
props
|
|
198
|
+
};
|
|
199
|
+
const hash = crypto.createHash("sha1").update(block.name + "\0" + JSON.stringify(props)).digest("hex").slice(0, 12);
|
|
200
|
+
const key = `${hash}-${keyCount[hash] = (keyCount[hash] ?? 0) + 1}`;
|
|
201
|
+
return html`<${tag} class="wb-island" data-block="${ctx.name}" data-wb-island="${id}" data-wb-key="${key}">${body}</${tag}>`;
|
|
202
|
+
} catch (err) {
|
|
203
|
+
console.error(`block ${block.name}:`, err);
|
|
204
|
+
return html`<pre class="wb-error">block "${block.name}" failed: ${err.message}</pre>`;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
const parse = (src) => {
|
|
208
|
+
const tree = parser.parse(String(src ?? ""));
|
|
209
|
+
rewriteLinks(tree, file, linkFor);
|
|
210
|
+
return tree;
|
|
211
|
+
};
|
|
212
|
+
const markdown = async (src) => toHtml(parse(src).children ?? []);
|
|
213
|
+
const inline = async (src) => (await markdown(src)).trim().replace(/^<p>([\s\S]*)<\/p>$/, "$1");
|
|
214
|
+
const base = {
|
|
215
|
+
page,
|
|
216
|
+
file,
|
|
217
|
+
dir: path.dirname(file),
|
|
218
|
+
parseAttrs,
|
|
219
|
+
toHtml,
|
|
220
|
+
toString,
|
|
221
|
+
markdown,
|
|
222
|
+
inline
|
|
223
|
+
};
|
|
224
|
+
async function directive(node) {
|
|
225
|
+
const { label, children } = splitLabel(node);
|
|
226
|
+
const block = resolve(node.name);
|
|
227
|
+
if (!block) {
|
|
228
|
+
if (node.type === "textDirective") return {
|
|
229
|
+
type: "text",
|
|
230
|
+
value: literalDirective(node, label)
|
|
231
|
+
};
|
|
232
|
+
const labelHtml = label && html`<p class="wb-label"><strong>${raw(await toHtml(label))}</strong></p>`;
|
|
233
|
+
return htmlNode(html`<div class="wb-unknown" data-directive="${node.name}">${labelHtml}${raw(await toHtml(children))}</div>`);
|
|
234
|
+
}
|
|
235
|
+
const labelHtml = label ? await toHtml(label) : null;
|
|
236
|
+
let defaults;
|
|
237
|
+
const ctx = {
|
|
238
|
+
...base,
|
|
239
|
+
kind: node.type,
|
|
240
|
+
name: node.name,
|
|
241
|
+
attrs: { ...node.attributes },
|
|
242
|
+
label: labelHtml,
|
|
243
|
+
labelText: label ? toString({
|
|
244
|
+
type: "root",
|
|
245
|
+
children: label
|
|
246
|
+
}) : null,
|
|
247
|
+
children,
|
|
248
|
+
node,
|
|
249
|
+
sections: () => sectionsOf(children),
|
|
250
|
+
defaults: () => defaults ??= (async () => {
|
|
251
|
+
const content = await toHtml(children);
|
|
252
|
+
return {
|
|
253
|
+
name: node.name,
|
|
254
|
+
attrs: { ...node.attributes },
|
|
255
|
+
label: labelHtml,
|
|
256
|
+
html: content,
|
|
257
|
+
...await sectionsOf(children)
|
|
258
|
+
};
|
|
259
|
+
})()
|
|
260
|
+
};
|
|
261
|
+
return htmlNode(await run(block, ctx, () => toHtml(children)));
|
|
262
|
+
}
|
|
263
|
+
async function fence(node) {
|
|
264
|
+
const lang = node.lang || null;
|
|
265
|
+
const plain = async () => html`<pre><code${lang && html` class="language-${lang}"`}>${node.value}</code></pre>`;
|
|
266
|
+
const block = lang && resolve(lang) || code;
|
|
267
|
+
if (!block) return htmlNode(await plain());
|
|
268
|
+
const name = code && block === code && lang !== code.name ? code.name : lang;
|
|
269
|
+
const attrs = parseAttrs(node.meta);
|
|
270
|
+
const ctx = {
|
|
271
|
+
...base,
|
|
272
|
+
kind: "fence",
|
|
273
|
+
name,
|
|
274
|
+
lang,
|
|
275
|
+
attrs,
|
|
276
|
+
text: node.value,
|
|
277
|
+
node,
|
|
278
|
+
defaults: async () => ({
|
|
279
|
+
name,
|
|
280
|
+
attrs,
|
|
281
|
+
text: node.value
|
|
282
|
+
})
|
|
283
|
+
};
|
|
284
|
+
return htmlNode(await run(block, ctx, plain));
|
|
285
|
+
}
|
|
286
|
+
async function transform(parent) {
|
|
287
|
+
const kids = parent.children;
|
|
288
|
+
if (!kids) return;
|
|
289
|
+
for (let i = 0; i < kids.length; i++) {
|
|
290
|
+
const node = kids[i];
|
|
291
|
+
if (DIRECTIVES.has(node.type)) kids[i] = await directive(node);
|
|
292
|
+
else if (node.type === "code") kids[i] = await fence(node);
|
|
293
|
+
else await transform(node);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const tree = typeof source === "string" ? parse(source) : source;
|
|
297
|
+
const h1 = tree.children?.find((n) => n.type === "heading" && n.depth === 1);
|
|
298
|
+
const title = h1 ? toString(h1) : null;
|
|
299
|
+
await transform(tree);
|
|
300
|
+
return {
|
|
301
|
+
html: stringify(tree.children ?? []),
|
|
302
|
+
title,
|
|
303
|
+
islands,
|
|
304
|
+
styles: [...used.values()].map((t) => t.style).filter((s) => !!s)
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
export { formNames, parseAttrs, renderMarkdown };
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { renderMarkdown } from "./markdown.js";
|
|
2
|
+
import { html, raw } from "../html.js";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { execFileSync } from "node:child_process";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
// src/pages/render.ts
|
|
8
|
+
const BASE_CSS = new URL("../client/page/base.css", import.meta.url);
|
|
9
|
+
const THEME_SCRIPT = raw(`<script>try{const t=localStorage.getItem('wb-theme');if(t==='light'||t==='dark')document.documentElement.dataset.theme=t}catch{}<\/script>`);
|
|
10
|
+
const CODE_LANG = {
|
|
11
|
+
js: "javascript",
|
|
12
|
+
mjs: "javascript",
|
|
13
|
+
cjs: "javascript",
|
|
14
|
+
ts: "typescript",
|
|
15
|
+
tsx: "tsx",
|
|
16
|
+
jsx: "jsx",
|
|
17
|
+
json: "json",
|
|
18
|
+
css: "css",
|
|
19
|
+
html: "html",
|
|
20
|
+
sh: "bash",
|
|
21
|
+
zsh: "bash",
|
|
22
|
+
py: "python",
|
|
23
|
+
rs: "rust",
|
|
24
|
+
go: "go",
|
|
25
|
+
toml: "toml",
|
|
26
|
+
yml: "yaml",
|
|
27
|
+
yaml: "yaml"
|
|
28
|
+
};
|
|
29
|
+
/** Frontmatter is the core's; the page starts after it. */
|
|
30
|
+
const body = (source) => source.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, "");
|
|
31
|
+
/** A page's title: its first heading, else its name. */
|
|
32
|
+
function titleOf(file, fallback) {
|
|
33
|
+
try {
|
|
34
|
+
const h1 = /^#\s+(.+)$/m.exec(body(fs.readFileSync(file, "utf8")));
|
|
35
|
+
return h1 ? h1[1].replace(/[*_`]/g, "") : fallback;
|
|
36
|
+
} catch {
|
|
37
|
+
return fallback;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function crumbs(id, name) {
|
|
41
|
+
const parts = id ? id.split("/") : [];
|
|
42
|
+
return html`<a href="/">${name}</a>${parts.map((p, i) => html` / <a href="/${parts.slice(0, i + 1).join("/")}">${p}</a>`)}`;
|
|
43
|
+
}
|
|
44
|
+
const time = (ts) => html`<time data-ts="${ts}"></time>`;
|
|
45
|
+
function pageList(pages) {
|
|
46
|
+
return html`<ul class="wb-index">${pages.map((p) => html`<li><a href="/${p.id}">${titleOf(p.file, path.basename(p.id))}</a> <span class="wb-meta"><code>${p.id}</code> · edited ${time(p.mtime)}${p.lastEvent && html` · last submit ${time(Date.parse(p.lastEvent))}`}${p.listeners > 0 && html` · <span class="wb-waiting">agent waiting</span>`}</span></li>`)}</ul>`;
|
|
47
|
+
}
|
|
48
|
+
/** The folder's README, when it has one. */
|
|
49
|
+
function readmeOf(page) {
|
|
50
|
+
const name = [
|
|
51
|
+
"README.md",
|
|
52
|
+
"readme.md",
|
|
53
|
+
"Readme.md"
|
|
54
|
+
].find((n) => page.entries.some((e) => !e.dir && e.name === n));
|
|
55
|
+
if (!name) return null;
|
|
56
|
+
const file = path.join(page.file, name);
|
|
57
|
+
return {
|
|
58
|
+
file,
|
|
59
|
+
source: fs.readFileSync(file, "utf8")
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** A stylesheet reference (URL, file: URL string, or absolute path) → absolute path. */
|
|
63
|
+
function stylePath(ref) {
|
|
64
|
+
if (ref == null) return null;
|
|
65
|
+
const file = ref instanceof URL || String(ref).startsWith("file:") ? fileURLToPath(ref) : String(ref);
|
|
66
|
+
if (!path.isAbsolute(file)) throw new Error(`style: use a URL, e.g. new URL('./style.css', import.meta.url)`);
|
|
67
|
+
if (!fs.existsSync(file)) throw new Error(`style: file not found: ${file}`);
|
|
68
|
+
return file;
|
|
69
|
+
}
|
|
70
|
+
const SKIP = /* @__PURE__ */ new Set([
|
|
71
|
+
"node_modules",
|
|
72
|
+
".git",
|
|
73
|
+
".whiteboard",
|
|
74
|
+
"dist",
|
|
75
|
+
"build"
|
|
76
|
+
]);
|
|
77
|
+
const MARKDOWN = /\.(md|markdown)$/i;
|
|
78
|
+
/**
|
|
79
|
+
* Every markdown file in the project, which renders as a page: the top folder's first,
|
|
80
|
+
* then by path. In a git repository, the files git knows (tracked, or new and not ignored), so
|
|
81
|
+
* built copies and dependencies stay out; elsewhere a walk that skips the usual build and
|
|
82
|
+
* dependency folders. The project's own pages (.whiteboard/) are listed apart.
|
|
83
|
+
*/
|
|
84
|
+
function documentsIn(dir) {
|
|
85
|
+
let files;
|
|
86
|
+
try {
|
|
87
|
+
files = execFileSync("git", [
|
|
88
|
+
"ls-files",
|
|
89
|
+
"--cached",
|
|
90
|
+
"--others",
|
|
91
|
+
"--exclude-standard",
|
|
92
|
+
"--",
|
|
93
|
+
":(icase)*.md",
|
|
94
|
+
":(icase)*.markdown"
|
|
95
|
+
], {
|
|
96
|
+
cwd: dir,
|
|
97
|
+
encoding: "utf8",
|
|
98
|
+
stdio: [
|
|
99
|
+
"ignore",
|
|
100
|
+
"pipe",
|
|
101
|
+
"ignore"
|
|
102
|
+
]
|
|
103
|
+
}).split("\n").filter((f) => f && !f.startsWith(".whiteboard/") && fs.existsSync(path.join(dir, f)));
|
|
104
|
+
} catch {
|
|
105
|
+
files = [];
|
|
106
|
+
const walk = (rel) => {
|
|
107
|
+
let entries = [];
|
|
108
|
+
try {
|
|
109
|
+
entries = fs.readdirSync(path.join(dir, rel), { withFileTypes: true });
|
|
110
|
+
} catch {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
for (const e of entries) if (e.isDirectory()) {
|
|
114
|
+
if (!SKIP.has(e.name) && !e.name.startsWith(".")) walk(path.posix.join(rel, e.name));
|
|
115
|
+
} else if (MARKDOWN.test(e.name)) files.push(path.posix.join(rel, e.name));
|
|
116
|
+
};
|
|
117
|
+
walk("");
|
|
118
|
+
}
|
|
119
|
+
const depth = (f) => f.split("/").length;
|
|
120
|
+
return [...new Set(files)].sort((a, b) => depth(a) - depth(b) || a.localeCompare(b));
|
|
121
|
+
}
|
|
122
|
+
/** The overview's list of the project's documents: each by its title, linked to its rendered page. */
|
|
123
|
+
function documentList(ctx) {
|
|
124
|
+
const files = documentsIn(ctx.project.dir);
|
|
125
|
+
if (!files.length) return "";
|
|
126
|
+
const rows = files.map((f) => {
|
|
127
|
+
const abs = path.join(ctx.project.dir, f);
|
|
128
|
+
return html`<li><a href="${ctx.urlFor(abs) ?? `/-/${f}`}">${titleOf(abs, f)}</a> <span class="wb-meta"><code>${f}</code></span></li>`;
|
|
129
|
+
});
|
|
130
|
+
return html`<section class="wb-documents"><h2>Documents</h2><ul class="wb-index">${rows}</ul></section>`;
|
|
131
|
+
}
|
|
132
|
+
function createRender(blocks, style, pluginStyles = []) {
|
|
133
|
+
const STYLE = stylePath(style);
|
|
134
|
+
const PLUGIN_STYLES = pluginStyles.map(stylePath).filter((f) => !!f);
|
|
135
|
+
async function markdown(source, page, file, ctx) {
|
|
136
|
+
const { blocks: set, code } = blocks();
|
|
137
|
+
return renderMarkdown(body(source), {
|
|
138
|
+
page,
|
|
139
|
+
file,
|
|
140
|
+
resolve: (name) => set.get(name) ?? null,
|
|
141
|
+
code,
|
|
142
|
+
linkFor: ctx.urlFor
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async function content(page, ctx) {
|
|
146
|
+
const { project } = ctx;
|
|
147
|
+
switch (page.kind) {
|
|
148
|
+
case "page": return markdown(page.source, page.id, page.file, ctx);
|
|
149
|
+
case "pages": {
|
|
150
|
+
const list = page.pages.length ? pageList(page.pages) : html`<p class="wb-meta">No pages yet.</p>`;
|
|
151
|
+
return {
|
|
152
|
+
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>`,
|
|
153
|
+
title: page.id || project.name,
|
|
154
|
+
islands: {},
|
|
155
|
+
styles: []
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
case "folder": {
|
|
159
|
+
const rel = page.id.slice(2);
|
|
160
|
+
const rows = page.entries.map((e) => html`<li><a href="${encodeURIComponent(e.name) + (e.dir ? "/" : "")}">${e.name + (e.dir ? "/" : "")}</a></li>`);
|
|
161
|
+
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>`;
|
|
162
|
+
const readme = readmeOf(page);
|
|
163
|
+
if (!readme) return {
|
|
164
|
+
html: listing,
|
|
165
|
+
title: rel || project.name,
|
|
166
|
+
islands: {},
|
|
167
|
+
styles: []
|
|
168
|
+
};
|
|
169
|
+
const r = await markdown(readme.source, page.id, readme.file, ctx);
|
|
170
|
+
return {
|
|
171
|
+
...r,
|
|
172
|
+
html: html`${raw(r.html)}<hr>${listing}`
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
case "file": {
|
|
176
|
+
const { code } = blocks();
|
|
177
|
+
const ext = path.extname(page.file).slice(1).toLowerCase();
|
|
178
|
+
const tree = {
|
|
179
|
+
type: "root",
|
|
180
|
+
children: [{
|
|
181
|
+
type: "code",
|
|
182
|
+
lang: CODE_LANG[ext] ?? (ext || null),
|
|
183
|
+
meta: null,
|
|
184
|
+
value: page.source
|
|
185
|
+
}]
|
|
186
|
+
};
|
|
187
|
+
const out = await renderMarkdown(tree, {
|
|
188
|
+
page: page.id,
|
|
189
|
+
file: page.file,
|
|
190
|
+
resolve: () => null,
|
|
191
|
+
code,
|
|
192
|
+
linkFor: ctx.urlFor
|
|
193
|
+
});
|
|
194
|
+
return {
|
|
195
|
+
html: html`<h1 class="wb-code-title wb-wide">${page.id.slice(2)}</h1><div class="wb-code wb-wide">${raw(out.html)}</div>`,
|
|
196
|
+
title: path.basename(page.file),
|
|
197
|
+
islands: out.islands,
|
|
198
|
+
styles: out.styles
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return async function render(page, ctx) {
|
|
204
|
+
let out;
|
|
205
|
+
try {
|
|
206
|
+
out = await content(page, ctx);
|
|
207
|
+
} catch (err) {
|
|
208
|
+
out = {
|
|
209
|
+
html: html`<pre class="wb-error">${err.message}</pre>`,
|
|
210
|
+
islands: {},
|
|
211
|
+
styles: []
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const islands = Object.fromEntries(Object.entries(out.islands).map(([id, island]) => [id, {
|
|
215
|
+
...island,
|
|
216
|
+
client: ctx.asset(island.client)
|
|
217
|
+
}]));
|
|
218
|
+
const styles = out.styles.map((s) => ctx.asset(s));
|
|
219
|
+
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">auto</button><span class="wb-live" title="live reload"></span></nav>`;
|
|
220
|
+
const links = [
|
|
221
|
+
ctx.asset(BASE_CSS),
|
|
222
|
+
...PLUGIN_STYLES.map((f) => ctx.asset(f)),
|
|
223
|
+
...styles,
|
|
224
|
+
...STYLE ? [ctx.asset(STYLE)] : []
|
|
225
|
+
];
|
|
226
|
+
return {
|
|
227
|
+
title: out.title || page.id || ctx.project.name,
|
|
228
|
+
head: html`${THEME_SCRIPT}
|
|
229
|
+
${links.map((href) => html`<link rel="stylesheet" href="${href}">`)}`,
|
|
230
|
+
html: html`${nav}<main id="wb-main">${raw(out.html)}</main>`,
|
|
231
|
+
data: {
|
|
232
|
+
islands,
|
|
233
|
+
styles,
|
|
234
|
+
mtime: page.mtime
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* What `whiteboard docs` prints first: how pages are written, and which blocks the project's plugins
|
|
241
|
+
* bring. Each plugin's own docs, printed after it, say how to write its blocks.
|
|
242
|
+
*/
|
|
243
|
+
function createDocs(blocks) {
|
|
244
|
+
return function docs(_ctx) {
|
|
245
|
+
const byPlugin = /* @__PURE__ */ new Map();
|
|
246
|
+
for (const t of blocks().blocks.values()) byPlugin.set(t.source, [...byPlugin.get(t.source) ?? [], t.code ? `${t.name} (every other code fence)` : t.name]);
|
|
247
|
+
return `# Pages
|
|
248
|
+
|
|
249
|
+
A page is markdown: GFM tables, \`<details>\`, raw HTML and a hand-written \`<form name="…">\` work as they are. Plugins add blocks to it: \`:::name\` around content, \`::name\` on a line, \`:name\` inside a line, and code fences (\`\`\`\` \`\`\`name \`\`\`\`). A block nobody brings renders its content, so a typo never breaks a page.
|
|
250
|
+
|
|
251
|
+
The blocks here, by the plugin that brings them (each plugin's section below says how to write them):
|
|
252
|
+
|
|
253
|
+
${[...byPlugin].sort(([a], [b]) => a.localeCompare(b)).map(([plugin, blocks]) => `- **${plugin}**: ${blocks.map((b) => `\`${b}\``).join(", ")}`).join("\n")}
|
|
254
|
+
`;
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
export { createDocs, createRender };
|