@bigmistqke/whiteboard 0.0.0-stage → 0.2.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 +628 -2
- package/bin/whiteboard.ts +488 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/whiteboard.js +542 -0
- package/dist/client/runtime.js +544 -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 +46 -0
- package/dist/plugins/chat/client.js +3 -0
- package/dist/plugins/chat/index.js +47 -0
- package/dist/plugins/chat/style.css +24 -0
- package/dist/plugins/claude-code/index.js +120 -0
- package/dist/plugins/code/README.md +42 -0
- package/dist/plugins/code/diff.js +65 -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 +54 -0
- package/dist/plugins/comments/style.css +40 -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/forge/README.md +12 -0
- package/dist/plugins/forge/index.js +83 -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/iframe/README.md +18 -0
- package/dist/plugins/iframe/client.js +1 -0
- package/dist/plugins/iframe/index.js +40 -0
- package/dist/plugins/iframe/style.css +29 -0
- package/dist/plugins/index.js +30 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +35 -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 +36 -0
- package/dist/src/auth.js +54 -0
- package/dist/src/client/page/base.css +295 -0
- package/dist/src/client/page/main.js +1228 -0
- package/dist/src/config.js +76 -0
- package/dist/src/events.js +183 -0
- package/dist/src/html.js +34 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/load.js +77 -0
- package/dist/src/loader.js +126 -0
- package/dist/src/package.js +33 -0
- package/dist/src/pages/blocks.js +50 -0
- package/dist/src/pages/index.js +15 -0
- package/dist/src/pages/markdown.js +410 -0
- package/dist/src/pages/render.js +311 -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 +725 -0
- package/dist/src/status.js +71 -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 +46 -0
- package/plugins/chat/autosize.ts +101 -0
- package/plugins/chat/client.tsx +93 -0
- package/plugins/chat/index.ts +61 -0
- package/plugins/chat/style.css +24 -0
- package/plugins/claude-code/index.ts +136 -0
- package/plugins/code/README.md +42 -0
- package/plugins/code/diff.ts +64 -0
- package/plugins/code/index.ts +94 -0
- package/plugins/code/style.css +29 -0
- package/plugins/comments/client.ts +492 -0
- package/plugins/comments/index.ts +54 -0
- package/plugins/comments/style.css +40 -0
- package/plugins/details/README.md +17 -0
- package/plugins/details/index.ts +21 -0
- package/plugins/details/style.css +3 -0
- package/plugins/forge/README.md +12 -0
- package/plugins/forge/index.ts +71 -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/iframe/README.md +18 -0
- package/plugins/iframe/client.ts +46 -0
- package/plugins/iframe/index.ts +54 -0
- package/plugins/iframe/style.css +29 -0
- package/plugins/index.ts +21 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +40 -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 +302 -0
- package/solid/index.tsx +59 -0
- package/src/auth.ts +66 -0
- package/src/client/forms.ts +185 -0
- package/src/client/markdown.ts +36 -0
- package/src/client/page/base.css +295 -0
- package/src/client/page/elsewhere.ts +70 -0
- package/src/client/page/fresh.ts +205 -0
- package/src/client/page/live.ts +30 -0
- package/src/client/page/main.ts +272 -0
- package/src/client/page/orphans.ts +46 -0
- package/src/client/page/sort.ts +38 -0
- package/src/client/page/theme.ts +32 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +182 -0
- package/src/client/read.ts +73 -0
- package/src/client/runtime.ts +183 -0
- package/src/client/toast.ts +25 -0
- package/src/config.ts +96 -0
- package/src/define.ts +612 -0
- package/src/events.ts +218 -0
- package/src/html.ts +40 -0
- package/src/icons.ts +8 -0
- package/src/load.ts +89 -0
- package/src/loader.ts +112 -0
- package/src/package.ts +39 -0
- package/src/pages/blocks.ts +70 -0
- package/src/pages/index.ts +42 -0
- package/src/pages/markdown.ts +364 -0
- package/src/pages/render.ts +233 -0
- package/src/plugins.ts +197 -0
- package/src/running.ts +22 -0
- package/src/serve.ts +52 -0
- package/src/server.ts +667 -0
- package/src/status.ts +113 -0
|
@@ -0,0 +1,410 @@
|
|
|
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 HIDDEN_COLON = "";
|
|
15
|
+
const FENCE = /^(\s*)(`{3,}|~{3,})/;
|
|
16
|
+
function hideColonsInFences(source) {
|
|
17
|
+
let open = null;
|
|
18
|
+
return source.split("\n").map((line) => {
|
|
19
|
+
const fence = FENCE.exec(line);
|
|
20
|
+
if (!open) {
|
|
21
|
+
if (fence) open = {
|
|
22
|
+
char: fence[2][0],
|
|
23
|
+
length: fence[2].length
|
|
24
|
+
};
|
|
25
|
+
return line;
|
|
26
|
+
}
|
|
27
|
+
if (fence && fence[2][0] === open.char && fence[2].length >= open.length && !line.slice(fence[0].length).trim()) {
|
|
28
|
+
open = null;
|
|
29
|
+
return line;
|
|
30
|
+
}
|
|
31
|
+
return line.replace(/^(\s*)(:+)/, (_, space, colons) => space + HIDDEN_COLON.repeat(colons.length));
|
|
32
|
+
}).join("\n");
|
|
33
|
+
}
|
|
34
|
+
function showColons(node) {
|
|
35
|
+
if (typeof node.value === "string" && node.value.includes(HIDDEN_COLON)) node.value = node.value.replaceAll(HIDDEN_COLON, ":");
|
|
36
|
+
node.children?.forEach(showColons);
|
|
37
|
+
}
|
|
38
|
+
/** A message's raw HTML, as text: it shows as written, and runs nothing. */
|
|
39
|
+
function asText(node) {
|
|
40
|
+
if (node.type === "html") {
|
|
41
|
+
node.type = "text";
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
node.children?.forEach(asText);
|
|
45
|
+
}
|
|
46
|
+
/** Markdown → mdast: GFM and directives, with code fences safe inside containers. */
|
|
47
|
+
function parseMarkdown(source) {
|
|
48
|
+
const tree = parser.parse(hideColonsInFences(source));
|
|
49
|
+
showColons(tree);
|
|
50
|
+
return tree;
|
|
51
|
+
}
|
|
52
|
+
const toHast = unified().use(remarkRehype, { allowDangerousHtml: true }).use(rehypeStringify, { allowDangerousHtml: true }).freeze();
|
|
53
|
+
/** `cols=3 open title="a b"` → { cols: '3', open: '', title: 'a b' } (bare words get '' like directive attrs) */
|
|
54
|
+
function parseAttrs(s) {
|
|
55
|
+
const out = {};
|
|
56
|
+
for (const m of String(s ?? "").matchAll(/([\w-]+)(?:=("[^"]*"|'[^']*'|\S+))?/g)) out[m[1]] = m[2] ? m[2].replace(/^["']|["']$/g, "") : "";
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
const DIRECTIVES = /* @__PURE__ */ new Set([
|
|
60
|
+
"containerDirective",
|
|
61
|
+
"leafDirective",
|
|
62
|
+
"textDirective"
|
|
63
|
+
]);
|
|
64
|
+
const PHRASING = /* @__PURE__ */ new Set([
|
|
65
|
+
"text",
|
|
66
|
+
"emphasis",
|
|
67
|
+
"strong",
|
|
68
|
+
"delete",
|
|
69
|
+
"inlineCode",
|
|
70
|
+
"break",
|
|
71
|
+
"link",
|
|
72
|
+
"linkReference",
|
|
73
|
+
"image",
|
|
74
|
+
"imageReference",
|
|
75
|
+
"footnoteReference",
|
|
76
|
+
"textDirective"
|
|
77
|
+
]);
|
|
78
|
+
const SCHEME = /^[a-z][a-z0-9+.-]*:/i;
|
|
79
|
+
/**
|
|
80
|
+
* Point relative links and images at what they name on disk: each is resolved from the markdown
|
|
81
|
+
* file's own folder and handed to `linkFor(absolutePath)`, which returns the URL that serves it
|
|
82
|
+
* (or null to leave the link as written).
|
|
83
|
+
*/
|
|
84
|
+
const NO_TEXT_LINKS = /* @__PURE__ */ new Set([
|
|
85
|
+
"link",
|
|
86
|
+
"linkReference",
|
|
87
|
+
"inlineCode",
|
|
88
|
+
"code",
|
|
89
|
+
"html",
|
|
90
|
+
"textDirective",
|
|
91
|
+
"definition"
|
|
92
|
+
]);
|
|
93
|
+
/** Turn the plugins' text links (`#12`) into links, in plain text only. */
|
|
94
|
+
function linkText(tree, links) {
|
|
95
|
+
if (!links.length) return;
|
|
96
|
+
const split = (text) => {
|
|
97
|
+
let parts = [{
|
|
98
|
+
type: "text",
|
|
99
|
+
value: text
|
|
100
|
+
}];
|
|
101
|
+
for (const link of links) {
|
|
102
|
+
const flags = link.pattern.flags.includes("g") ? link.pattern.flags : link.pattern.flags + "g";
|
|
103
|
+
parts = parts.flatMap((part) => {
|
|
104
|
+
if (part.type !== "text") return [part];
|
|
105
|
+
const out = [];
|
|
106
|
+
const value = part.value;
|
|
107
|
+
const re = new RegExp(link.pattern.source, flags);
|
|
108
|
+
let at = 0;
|
|
109
|
+
for (let m = re.exec(value); m; m = re.exec(value)) {
|
|
110
|
+
if (!m[0]) {
|
|
111
|
+
re.lastIndex++;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
const href = link.href(m);
|
|
115
|
+
if (!href) continue;
|
|
116
|
+
if (m.index > at) out.push({
|
|
117
|
+
type: "text",
|
|
118
|
+
value: value.slice(at, m.index)
|
|
119
|
+
});
|
|
120
|
+
out.push({
|
|
121
|
+
type: "link",
|
|
122
|
+
url: href,
|
|
123
|
+
title: link.title?.(m) ?? null,
|
|
124
|
+
children: [{
|
|
125
|
+
type: "text",
|
|
126
|
+
value: m[0]
|
|
127
|
+
}]
|
|
128
|
+
});
|
|
129
|
+
at = m.index + m[0].length;
|
|
130
|
+
}
|
|
131
|
+
if (at < value.length) out.push({
|
|
132
|
+
type: "text",
|
|
133
|
+
value: value.slice(at)
|
|
134
|
+
});
|
|
135
|
+
return out;
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
return parts;
|
|
139
|
+
};
|
|
140
|
+
const walk = (node) => {
|
|
141
|
+
if (!node.children || NO_TEXT_LINKS.has(node.type)) return;
|
|
142
|
+
node.children = node.children.flatMap((child) => child.type === "text" ? split(child.value) : (walk(child), [child]));
|
|
143
|
+
};
|
|
144
|
+
walk(tree);
|
|
145
|
+
}
|
|
146
|
+
function rewriteLinks(tree, file, linkFor) {
|
|
147
|
+
if (!linkFor || !file) return;
|
|
148
|
+
const walk = (node) => {
|
|
149
|
+
if ((node.type === "link" || node.type === "image" || node.type === "definition") && node.url && !SCHEME.test(node.url) && !/^[/#]/.test(node.url)) {
|
|
150
|
+
const [, target, rest] = /^([^?#]*)(.*)$/s.exec(node.url);
|
|
151
|
+
let decoded = target;
|
|
152
|
+
try {
|
|
153
|
+
decoded = decodeURIComponent(target);
|
|
154
|
+
} catch {}
|
|
155
|
+
const url = decoded && linkFor(path.resolve(path.dirname(file), decoded));
|
|
156
|
+
if (url) node.url = url + rest;
|
|
157
|
+
}
|
|
158
|
+
node.children?.forEach(walk);
|
|
159
|
+
};
|
|
160
|
+
walk(tree);
|
|
161
|
+
}
|
|
162
|
+
/** Separate a directive's `[label]` (remark-directive stores it as a flagged first paragraph). */
|
|
163
|
+
function splitLabel(node) {
|
|
164
|
+
const [head, ...rest] = node.children ?? [];
|
|
165
|
+
if (node.type === "containerDirective" && head?.data?.directiveLabel) return {
|
|
166
|
+
label: head.children ?? null,
|
|
167
|
+
children: rest
|
|
168
|
+
};
|
|
169
|
+
if (node.type !== "containerDirective") return {
|
|
170
|
+
label: node.children?.length ? node.children : null,
|
|
171
|
+
children: []
|
|
172
|
+
};
|
|
173
|
+
return {
|
|
174
|
+
label: null,
|
|
175
|
+
children: node.children ?? []
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const MARKER = {
|
|
179
|
+
textDirective: ":",
|
|
180
|
+
leafDirective: "::",
|
|
181
|
+
containerDirective: ":::"
|
|
182
|
+
};
|
|
183
|
+
function literalDirective(node, label) {
|
|
184
|
+
const marker = MARKER[node.type];
|
|
185
|
+
const attrs = Object.entries(node.attributes ?? {}).map(([k, v]) => v ? `${k}="${v}"` : k).join(" ");
|
|
186
|
+
return `${marker}${node.name}${label ? `[${toString({
|
|
187
|
+
type: "root",
|
|
188
|
+
children: label
|
|
189
|
+
})}]` : ""}${attrs ? `{${attrs}}` : ""}`;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* The forms a page asks the reader to answer, by name: `:::form{name=…}` containers and
|
|
193
|
+
* hand-written <form name="…"> without an action. Parsed, so a form shown as an example inside a
|
|
194
|
+
* code block doesn't count.
|
|
195
|
+
*/
|
|
196
|
+
function formNames(source) {
|
|
197
|
+
const tree = parseMarkdown(String(source ?? "").replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, ""));
|
|
198
|
+
const names = [];
|
|
199
|
+
const walk = (node) => {
|
|
200
|
+
if (node.type === "containerDirective" && node.name === "form") {
|
|
201
|
+
const attrs = node.attributes ?? {};
|
|
202
|
+
names.push(attrs.name || attrs.id || "form");
|
|
203
|
+
} else if (node.type === "html") for (const m of (node.value ?? "").matchAll(/<form\b([^>]*)>/gi)) {
|
|
204
|
+
if (/\baction\s*=/i.test(m[1])) continue;
|
|
205
|
+
const name = /\bname\s*=\s*("([^"]*)"|'([^']*)'|([^\s>]+))/i.exec(m[1]);
|
|
206
|
+
if (name) names.push(name[2] ?? name[3] ?? name[4]);
|
|
207
|
+
}
|
|
208
|
+
for (const child of node.children ?? []) walk(child);
|
|
209
|
+
};
|
|
210
|
+
walk(tree);
|
|
211
|
+
return [...new Set(names)].filter(isQuestionName);
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Markdown (or an mdast root, rendered as it is) → HTML, with directives and fences handed to
|
|
215
|
+
* blocks. Returns the page's HTML, its first heading, the islands to mount and the blocks'
|
|
216
|
+
* stylesheets.
|
|
217
|
+
*/
|
|
218
|
+
async function renderMarkdown(source, { page, file, resolve, code = null, linkFor, textLinks = [], message = false }) {
|
|
219
|
+
let nextId = 0;
|
|
220
|
+
const islands = {};
|
|
221
|
+
const keyCount = {};
|
|
222
|
+
const used = /* @__PURE__ */ new Map();
|
|
223
|
+
const stringify = (nodes) => {
|
|
224
|
+
if (nodes.length && nodes.every((n) => PHRASING.has(n.type))) return toHast.stringify(toHast.runSync({
|
|
225
|
+
type: "root",
|
|
226
|
+
children: [{
|
|
227
|
+
type: "paragraph",
|
|
228
|
+
children: nodes
|
|
229
|
+
}]
|
|
230
|
+
})).replace(/^<p>([\s\S]*)<\/p>$/, "$1");
|
|
231
|
+
return toHast.stringify(toHast.runSync({
|
|
232
|
+
type: "root",
|
|
233
|
+
children: nodes
|
|
234
|
+
}));
|
|
235
|
+
};
|
|
236
|
+
/** Render mdast nodes to HTML. Transforms nested directives first; safe to call twice on the same nodes. */
|
|
237
|
+
const toHtml = async (nodes) => {
|
|
238
|
+
await transform({
|
|
239
|
+
type: "root",
|
|
240
|
+
children: nodes
|
|
241
|
+
});
|
|
242
|
+
return stringify(nodes);
|
|
243
|
+
};
|
|
244
|
+
async function sectionsOf(children) {
|
|
245
|
+
await transform({
|
|
246
|
+
type: "root",
|
|
247
|
+
children
|
|
248
|
+
});
|
|
249
|
+
const heads = children.filter((n) => n.type === "heading");
|
|
250
|
+
if (!heads.length) return {
|
|
251
|
+
intro: "",
|
|
252
|
+
sections: children.map((n) => ({
|
|
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 = [];
|
|
261
|
+
let current = null;
|
|
262
|
+
for (const n of children) if (n.type === "heading" && n.depth === depth) {
|
|
263
|
+
current = {
|
|
264
|
+
heading: n,
|
|
265
|
+
body: []
|
|
266
|
+
};
|
|
267
|
+
out.push(current);
|
|
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
|
+
};
|
|
278
|
+
}
|
|
279
|
+
/** HTML into the markdown tree, where the rest of the pipeline takes it as it is: the one place it becomes a string. */
|
|
280
|
+
const htmlNode = (markup) => ({
|
|
281
|
+
type: "html",
|
|
282
|
+
value: String(markup)
|
|
283
|
+
});
|
|
284
|
+
/** A block's output for one directive or fence: static HTML, or an island around it. */
|
|
285
|
+
async function run(block, ctx, fallback) {
|
|
286
|
+
used.set(block.name, block);
|
|
287
|
+
try {
|
|
288
|
+
const own = block.html ? await block.html(ctx) : null;
|
|
289
|
+
const body = raw(own ?? await fallback());
|
|
290
|
+
const tag = raw(ctx.kind === "textDirective" ? "span" : "div");
|
|
291
|
+
if (!block.client) return own != null ? body : html`<${tag} data-block="${ctx.name}">${body}</${tag}>`;
|
|
292
|
+
const props = block.props ? await block.props(ctx) : await ctx.defaults();
|
|
293
|
+
const id = String(++nextId);
|
|
294
|
+
islands[id] = {
|
|
295
|
+
block: block.name,
|
|
296
|
+
client: block.client,
|
|
297
|
+
props
|
|
298
|
+
};
|
|
299
|
+
const hash = crypto.createHash("sha1").update(block.name + "\0" + JSON.stringify(props)).digest("hex").slice(0, 12);
|
|
300
|
+
const key = `${hash}-${keyCount[hash] = (keyCount[hash] ?? 0) + 1}`;
|
|
301
|
+
return html`<${tag} class="wb-island" data-block="${ctx.name}" data-wb-island="${id}" data-wb-key="${key}">${body}</${tag}>`;
|
|
302
|
+
} catch (err) {
|
|
303
|
+
console.error(`block ${block.name}:`, err);
|
|
304
|
+
return html`<pre class="wb-error">block "${block.name}" failed: ${err.message}</pre>`;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const parse = (src) => {
|
|
308
|
+
const tree = parseMarkdown(String(src ?? ""));
|
|
309
|
+
if (message) asText(tree);
|
|
310
|
+
rewriteLinks(tree, file, linkFor);
|
|
311
|
+
linkText(tree, textLinks);
|
|
312
|
+
return tree;
|
|
313
|
+
};
|
|
314
|
+
const markdown = async (src) => toHtml(parse(src).children ?? []);
|
|
315
|
+
const inline = async (src) => (await markdown(src)).trim().replace(/^<p>([\s\S]*)<\/p>$/, "$1");
|
|
316
|
+
const base = {
|
|
317
|
+
page,
|
|
318
|
+
file,
|
|
319
|
+
dir: path.dirname(file),
|
|
320
|
+
parseAttrs,
|
|
321
|
+
toHtml,
|
|
322
|
+
toString,
|
|
323
|
+
markdown,
|
|
324
|
+
inline
|
|
325
|
+
};
|
|
326
|
+
async function directive(node) {
|
|
327
|
+
const { label, children } = splitLabel(node);
|
|
328
|
+
const block = resolve(node.name);
|
|
329
|
+
if (!block) {
|
|
330
|
+
if (node.type === "textDirective") return {
|
|
331
|
+
type: "text",
|
|
332
|
+
value: literalDirective(node, label)
|
|
333
|
+
};
|
|
334
|
+
const labelHtml = label && html`<p class="wb-label"><strong>${raw(await toHtml(label))}</strong></p>`;
|
|
335
|
+
return htmlNode(html`<div class="wb-unknown" data-directive="${node.name}">${labelHtml}${raw(await toHtml(children))}</div>`);
|
|
336
|
+
}
|
|
337
|
+
const labelHtml = label ? await toHtml(label) : null;
|
|
338
|
+
let defaults;
|
|
339
|
+
const ctx = {
|
|
340
|
+
...base,
|
|
341
|
+
kind: node.type,
|
|
342
|
+
name: node.name,
|
|
343
|
+
attrs: { ...node.attributes },
|
|
344
|
+
label: labelHtml,
|
|
345
|
+
labelText: label ? toString({
|
|
346
|
+
type: "root",
|
|
347
|
+
children: label
|
|
348
|
+
}) : null,
|
|
349
|
+
children,
|
|
350
|
+
node,
|
|
351
|
+
sections: () => sectionsOf(children),
|
|
352
|
+
defaults: () => defaults ??= (async () => {
|
|
353
|
+
const content = await toHtml(children);
|
|
354
|
+
return {
|
|
355
|
+
name: node.name,
|
|
356
|
+
attrs: { ...node.attributes },
|
|
357
|
+
label: labelHtml,
|
|
358
|
+
html: content,
|
|
359
|
+
...await sectionsOf(children)
|
|
360
|
+
};
|
|
361
|
+
})()
|
|
362
|
+
};
|
|
363
|
+
return htmlNode(await run(block, ctx, () => toHtml(children)));
|
|
364
|
+
}
|
|
365
|
+
async function fence(node) {
|
|
366
|
+
const lang = node.lang || null;
|
|
367
|
+
const plain = async () => html`<pre><code${lang && html` class="language-${lang}"`}>${node.value}</code></pre>`;
|
|
368
|
+
const named = lang ? resolve(lang) : null;
|
|
369
|
+
const block = (named?.fences ? named : null) || code;
|
|
370
|
+
if (!block) return htmlNode(await plain());
|
|
371
|
+
const name = code && block === code && lang !== code.name ? code.name : lang;
|
|
372
|
+
const attrs = parseAttrs(node.meta);
|
|
373
|
+
const ctx = {
|
|
374
|
+
...base,
|
|
375
|
+
kind: "fence",
|
|
376
|
+
name,
|
|
377
|
+
lang,
|
|
378
|
+
attrs,
|
|
379
|
+
text: node.value,
|
|
380
|
+
node,
|
|
381
|
+
defaults: async () => ({
|
|
382
|
+
name,
|
|
383
|
+
attrs,
|
|
384
|
+
text: node.value
|
|
385
|
+
})
|
|
386
|
+
};
|
|
387
|
+
return htmlNode(await run(block, ctx, plain));
|
|
388
|
+
}
|
|
389
|
+
async function transform(parent) {
|
|
390
|
+
const kids = parent.children;
|
|
391
|
+
if (!kids) return;
|
|
392
|
+
for (let i = 0; i < kids.length; i++) {
|
|
393
|
+
const node = kids[i];
|
|
394
|
+
if (DIRECTIVES.has(node.type)) kids[i] = await directive(node);
|
|
395
|
+
else if (node.type === "code") kids[i] = await fence(node);
|
|
396
|
+
else await transform(node);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
const tree = typeof source === "string" ? parse(source) : source;
|
|
400
|
+
const h1 = tree.children?.find((n) => n.type === "heading" && n.depth === 1);
|
|
401
|
+
const title = h1 ? toString(h1) : null;
|
|
402
|
+
await transform(tree);
|
|
403
|
+
return {
|
|
404
|
+
html: stringify(tree.children ?? []),
|
|
405
|
+
title,
|
|
406
|
+
islands,
|
|
407
|
+
styles: [...used.values()].map((t) => t.style).filter((s) => !!s)
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
export { formNames, parseAttrs, renderMarkdown };
|