@bigmistqke/whiteboard 0.3.1 → 0.4.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 +67 -17
- package/bin/whiteboard.ts +268 -71
- package/dist/client/runtime.js +88 -41
- package/dist/define.js +21 -2
- package/dist/plugins/callout/index.js +3 -1
- package/dist/plugins/callout/public/style.css +30 -0
- package/dist/plugins/chat/index.js +7 -4
- package/dist/plugins/chat/{client.js → public/client.js} +1 -1
- package/dist/plugins/chat/public/style.css +98 -0
- package/dist/plugins/code/index.js +3 -1
- package/dist/plugins/code/public/style.css +68 -0
- package/dist/plugins/comments/index.js +4 -5
- package/dist/plugins/comments/public/client.js +19 -0
- package/dist/plugins/comments/public/style.css +185 -0
- package/dist/plugins/details/index.js +7 -2
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/form/index.js +37 -8
- package/dist/plugins/form/public/style.css +126 -0
- package/dist/plugins/gallery/index.js +4 -3
- package/dist/plugins/gallery/public/client.js +1 -0
- package/dist/plugins/gallery/public/style.css +90 -0
- package/dist/plugins/grid/index.js +9 -3
- package/dist/plugins/grid/public/style.css +71 -0
- package/dist/plugins/iframe/index.js +13 -14
- package/dist/plugins/iframe/public/style.css +13 -0
- package/dist/plugins/index.js +3 -1
- package/dist/plugins/mermaid/index.js +3 -1
- package/dist/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/dist/plugins/terminal/index.js +10 -9
- package/dist/plugins/terminal/{style.css → public/style.css} +199 -45
- package/dist/plugins/toc/index.js +4 -3
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +93 -0
- package/dist/plugins/window/README.md +29 -0
- package/dist/plugins/window/index.js +64 -0
- package/dist/solid/index.js +14 -11
- package/dist/src/client/page/base.css +1078 -199
- package/dist/src/client/page/main.js +174 -33
- package/dist/src/loader.js +7 -2
- package/dist/src/package.js +3 -6
- package/dist/src/pages/blocks.js +8 -13
- package/dist/src/pages/markdown.js +1 -1
- package/dist/src/pages/render.js +74 -15
- package/dist/src/pages/window.js +30 -0
- package/dist/src/public.js +24 -0
- package/dist/src/server.js +36 -14
- package/package.json +3 -1
- package/plugins/callout/index.ts +3 -1
- package/plugins/callout/public/style.css +30 -0
- package/plugins/chat/autosize.ts +43 -9
- package/plugins/chat/client.tsx +22 -5
- package/plugins/chat/index.ts +22 -8
- package/plugins/chat/public/style.css +98 -0
- package/plugins/claude-code/index.ts +49 -14
- package/plugins/code/diff.ts +28 -8
- package/plugins/code/index.ts +30 -20
- package/plugins/code/public/style.css +68 -0
- package/plugins/comments/client.ts +139 -71
- package/plugins/comments/index.ts +9 -7
- package/plugins/comments/public/style.css +185 -0
- package/plugins/details/index.ts +7 -2
- package/plugins/details/public/style.css +7 -0
- package/plugins/forge/index.ts +16 -3
- package/plugins/form/index.ts +75 -27
- package/plugins/form/public/style.css +126 -0
- package/plugins/gallery/client.tsx +9 -3
- package/plugins/gallery/index.ts +31 -9
- package/plugins/gallery/public/style.css +90 -0
- package/plugins/grid/index.ts +19 -5
- package/plugins/grid/public/style.css +71 -0
- package/plugins/iframe/index.ts +20 -23
- package/plugins/iframe/public/style.css +13 -0
- package/plugins/index.ts +16 -1
- package/plugins/mermaid/index.ts +3 -1
- package/plugins/mermaid/{style.css → public/style.css} +7 -2
- package/plugins/terminal/client.ts +105 -48
- package/plugins/terminal/host.ts +131 -29
- package/plugins/terminal/index.ts +84 -25
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +64 -13
- package/plugins/terminal/sizes.ts +13 -3
- package/plugins/toc/client.ts +45 -26
- package/plugins/toc/index.ts +9 -6
- package/plugins/toc/public/style.css +93 -0
- package/plugins/window/README.md +29 -0
- package/plugins/window/index.ts +85 -0
- package/skills/whiteboard/SKILL.md +3 -1
- package/solid/index.tsx +24 -9
- package/src/auth.ts +25 -7
- package/src/client/forms.ts +74 -28
- package/src/client/markdown.ts +15 -4
- package/src/client/page/base.css +1078 -199
- package/src/client/page/elsewhere.ts +32 -15
- package/src/client/page/fresh.ts +91 -23
- package/src/client/page/hold.ts +11 -2
- package/src/client/page/live.ts +10 -6
- package/src/client/page/main.ts +106 -36
- package/src/client/page/orphans.ts +25 -10
- package/src/client/page/sort.ts +14 -4
- package/src/client/page/sticky.ts +11 -3
- package/src/client/page/theme.ts +11 -7
- package/src/client/page/waiting.ts +141 -34
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +17 -10
- package/src/client/runtime.ts +130 -35
- package/src/client/scroll.ts +14 -4
- package/src/client/toast.ts +19 -5
- package/src/config.ts +20 -5
- package/src/define.ts +65 -25
- package/src/events.ts +111 -24
- package/src/html.ts +6 -2
- package/src/icons.ts +4 -2
- package/src/load.ts +10 -2
- package/src/loader.ts +75 -11
- package/src/package.ts +9 -8
- package/src/pages/blocks.ts +20 -14
- package/src/pages/markdown.ts +113 -34
- package/src/pages/render.ts +199 -39
- package/src/pages/window.ts +71 -0
- package/src/plugins.ts +90 -24
- package/src/public.ts +39 -0
- package/src/running.ts +8 -2
- package/src/serve.ts +29 -7
- package/src/server.ts +348 -107
- package/src/status.ts +15 -9
- package/dist/plugins/callout/style.css +0 -12
- package/dist/plugins/chat/style.css +0 -24
- package/dist/plugins/code/style.css +0 -29
- package/dist/plugins/comments/client.js +0 -10
- package/dist/plugins/comments/style.css +0 -40
- package/dist/plugins/details/style.css +0 -3
- package/dist/plugins/form/style.css +0 -44
- package/dist/plugins/gallery/client.js +0 -1
- package/dist/plugins/gallery/style.css +0 -27
- package/dist/plugins/grid/style.css +0 -25
- package/dist/plugins/iframe/client.js +0 -1
- package/dist/plugins/iframe/style.css +0 -29
- package/dist/plugins/toc/client.js +0 -1
- package/dist/plugins/toc/style.css +0 -19
- package/plugins/callout/style.css +0 -12
- package/plugins/chat/style.css +0 -24
- package/plugins/code/style.css +0 -29
- package/plugins/comments/style.css +0 -40
- package/plugins/details/style.css +0 -3
- package/plugins/form/style.css +0 -44
- package/plugins/gallery/style.css +0 -27
- package/plugins/grid/style.css +0 -25
- package/plugins/iframe/client.ts +0 -46
- package/plugins/iframe/style.css +0 -29
- package/plugins/terminal/style.css +0 -71
- package/plugins/toc/style.css +0 -19
- /package/dist/plugins/terminal/{client.js → public/client.js} +0 -0
package/src/pages/markdown.ts
CHANGED
|
@@ -42,25 +42,38 @@ const FENCE = /^(\s*)(`{3,}|~{3,})/
|
|
|
42
42
|
|
|
43
43
|
function hideColonsInFences(source: string): string {
|
|
44
44
|
let open: { char: string; length: number } | null = null
|
|
45
|
-
return source
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
45
|
+
return source
|
|
46
|
+
.split('\n')
|
|
47
|
+
.map(line => {
|
|
48
|
+
const fence = FENCE.exec(line)
|
|
49
|
+
if (!open) {
|
|
50
|
+
if (fence) open = { char: fence[2][0], length: fence[2].length }
|
|
51
|
+
return line
|
|
52
|
+
}
|
|
53
|
+
if (fence && fence[2][0] === open.char && fence[2].length >= open.length && !line.slice(fence[0].length).trim()) {
|
|
54
|
+
open = null
|
|
55
|
+
return line
|
|
56
|
+
}
|
|
57
|
+
return line.replace(
|
|
58
|
+
/^(\s*)(:+)/,
|
|
59
|
+
(_, space: string, colons: string) => space + HIDDEN_COLON.repeat(colons.length),
|
|
60
|
+
)
|
|
61
|
+
})
|
|
62
|
+
.join('\n')
|
|
54
63
|
}
|
|
55
64
|
|
|
56
65
|
function showColons(node: MdNode) {
|
|
57
|
-
if (typeof node.value === 'string' && node.value.includes(HIDDEN_COLON))
|
|
66
|
+
if (typeof node.value === 'string' && node.value.includes(HIDDEN_COLON))
|
|
67
|
+
node.value = node.value.replaceAll(HIDDEN_COLON, ':')
|
|
58
68
|
node.children?.forEach(showColons)
|
|
59
69
|
}
|
|
60
70
|
|
|
61
71
|
/** A message's raw HTML, as text: it shows as written, and runs nothing. */
|
|
62
72
|
function asText(node: MdNode) {
|
|
63
|
-
if (node.type === 'html') {
|
|
73
|
+
if (node.type === 'html') {
|
|
74
|
+
node.type = 'text'
|
|
75
|
+
return
|
|
76
|
+
}
|
|
64
77
|
node.children?.forEach(asText)
|
|
65
78
|
}
|
|
66
79
|
|
|
@@ -70,7 +83,10 @@ function parseMarkdown(source: string): MdNode {
|
|
|
70
83
|
showColons(tree)
|
|
71
84
|
return tree
|
|
72
85
|
}
|
|
73
|
-
const toHast = unified()
|
|
86
|
+
const toHast = unified()
|
|
87
|
+
.use(remarkRehype, { allowDangerousHtml: true })
|
|
88
|
+
.use(rehypeStringify, { allowDangerousHtml: true })
|
|
89
|
+
.freeze()
|
|
74
90
|
|
|
75
91
|
/** `cols=3 open title="a b"` → { cols: '3', open: '', title: 'a b' } (bare words get '' like directive attrs) */
|
|
76
92
|
export function parseAttrs(s: unknown) {
|
|
@@ -81,8 +97,20 @@ export function parseAttrs(s: unknown) {
|
|
|
81
97
|
}
|
|
82
98
|
|
|
83
99
|
const DIRECTIVES = new Set(['containerDirective', 'leafDirective', 'textDirective'])
|
|
84
|
-
const PHRASING = new Set([
|
|
85
|
-
'
|
|
100
|
+
const PHRASING = new Set([
|
|
101
|
+
'text',
|
|
102
|
+
'emphasis',
|
|
103
|
+
'strong',
|
|
104
|
+
'delete',
|
|
105
|
+
'inlineCode',
|
|
106
|
+
'break',
|
|
107
|
+
'link',
|
|
108
|
+
'linkReference',
|
|
109
|
+
'image',
|
|
110
|
+
'imageReference',
|
|
111
|
+
'footnoteReference',
|
|
112
|
+
'textDirective',
|
|
113
|
+
])
|
|
86
114
|
|
|
87
115
|
const SCHEME = /^[a-z][a-z0-9+.-]*:/i
|
|
88
116
|
|
|
@@ -108,11 +136,19 @@ function linkText(tree: MdNode, links: TextLink[]) {
|
|
|
108
136
|
const re = new RegExp(link.pattern.source, flags)
|
|
109
137
|
let at = 0
|
|
110
138
|
for (let m = re.exec(value); m; m = re.exec(value)) {
|
|
111
|
-
if (!m[0]) {
|
|
139
|
+
if (!m[0]) {
|
|
140
|
+
re.lastIndex++
|
|
141
|
+
continue
|
|
142
|
+
}
|
|
112
143
|
const href = link.href(m)
|
|
113
144
|
if (!href) continue
|
|
114
145
|
if (m.index > at) out.push({ type: 'text', value: value.slice(at, m.index) })
|
|
115
|
-
out.push({
|
|
146
|
+
out.push({
|
|
147
|
+
type: 'link',
|
|
148
|
+
url: href,
|
|
149
|
+
title: link.title?.(m) ?? null,
|
|
150
|
+
children: [{ type: 'text', value: m[0] }],
|
|
151
|
+
})
|
|
116
152
|
at = m.index + m[0].length
|
|
117
153
|
}
|
|
118
154
|
if (at < value.length) out.push({ type: 'text', value: value.slice(at) })
|
|
@@ -123,7 +159,9 @@ function linkText(tree: MdNode, links: TextLink[]) {
|
|
|
123
159
|
}
|
|
124
160
|
const walk = (node: MdNode) => {
|
|
125
161
|
if (!node.children || NO_TEXT_LINKS.has(node.type)) return
|
|
126
|
-
node.children = node.children.flatMap(child =>
|
|
162
|
+
node.children = node.children.flatMap(child =>
|
|
163
|
+
child.type === 'text' ? split(child.value as string) : (walk(child), [child]),
|
|
164
|
+
)
|
|
127
165
|
}
|
|
128
166
|
walk(tree)
|
|
129
167
|
}
|
|
@@ -131,11 +169,19 @@ function linkText(tree: MdNode, links: TextLink[]) {
|
|
|
131
169
|
function rewriteLinks(tree: MdNode, file: string, linkFor?: LinkFor) {
|
|
132
170
|
if (!linkFor || !file) return
|
|
133
171
|
const walk = (node: MdNode) => {
|
|
134
|
-
if (
|
|
135
|
-
|
|
172
|
+
if (
|
|
173
|
+
(node.type === 'link' || node.type === 'image' || node.type === 'definition') &&
|
|
174
|
+
node.url &&
|
|
175
|
+
!SCHEME.test(node.url) &&
|
|
176
|
+
!/^[/#]/.test(node.url)
|
|
177
|
+
) {
|
|
136
178
|
const [, target, rest] = /^([^?#]*)(.*)$/s.exec(node.url)!
|
|
137
179
|
let decoded = target
|
|
138
|
-
try {
|
|
180
|
+
try {
|
|
181
|
+
decoded = decodeURIComponent(target)
|
|
182
|
+
} catch {
|
|
183
|
+
/* keep as written */
|
|
184
|
+
}
|
|
139
185
|
const url = decoded && linkFor(path.resolve(path.dirname(file), decoded))
|
|
140
186
|
if (url) node.url = url + rest
|
|
141
187
|
}
|
|
@@ -147,7 +193,8 @@ function rewriteLinks(tree: MdNode, file: string, linkFor?: LinkFor) {
|
|
|
147
193
|
/** Separate a directive's `[label]` (remark-directive stores it as a flagged first paragraph). */
|
|
148
194
|
function splitLabel(node: MdNode): { label: MdNode[] | null; children: MdNode[] } {
|
|
149
195
|
const [head, ...rest] = node.children ?? []
|
|
150
|
-
if (node.type === 'containerDirective' && head?.data?.directiveLabel)
|
|
196
|
+
if (node.type === 'containerDirective' && head?.data?.directiveLabel)
|
|
197
|
+
return { label: head.children ?? null, children: rest }
|
|
151
198
|
if (node.type !== 'containerDirective') return { label: node.children?.length ? node.children : null, children: [] }
|
|
152
199
|
return { label: null, children: node.children ?? [] }
|
|
153
200
|
}
|
|
@@ -156,7 +203,9 @@ const MARKER: Record<string, string> = { textDirective: ':', leafDirective: '::'
|
|
|
156
203
|
|
|
157
204
|
function literalDirective(node: MdNode, label: MdNode[] | null) {
|
|
158
205
|
const marker = MARKER[node.type]
|
|
159
|
-
const attrs = Object.entries(node.attributes ?? {})
|
|
206
|
+
const attrs = Object.entries(node.attributes ?? {})
|
|
207
|
+
.map(([k, v]) => (v ? `${k}="${v}"` : k))
|
|
208
|
+
.join(' ')
|
|
160
209
|
return `${marker}${node.name}${label ? `[${toString({ type: 'root', children: label } as any)}]` : ''}${attrs ? `{${attrs}}` : ''}`
|
|
161
210
|
}
|
|
162
211
|
|
|
@@ -203,14 +252,21 @@ export interface RenderOptions {
|
|
|
203
252
|
message?: boolean
|
|
204
253
|
}
|
|
205
254
|
|
|
206
|
-
export interface Island {
|
|
255
|
+
export interface Island {
|
|
256
|
+
block: string
|
|
257
|
+
client: string
|
|
258
|
+
props: unknown
|
|
259
|
+
}
|
|
207
260
|
|
|
208
261
|
/**
|
|
209
262
|
* Markdown (or an mdast root, rendered as it is) → HTML, with directives and fences handed to
|
|
210
263
|
* blocks. Returns the page's HTML, its first heading, the islands to mount and the blocks'
|
|
211
264
|
* stylesheets.
|
|
212
265
|
*/
|
|
213
|
-
export async function renderMarkdown(
|
|
266
|
+
export async function renderMarkdown(
|
|
267
|
+
source: string | MdNode,
|
|
268
|
+
{ page, file, resolve, code = null, linkFor, textLinks = [], message = false }: RenderOptions,
|
|
269
|
+
) {
|
|
214
270
|
let nextId = 0
|
|
215
271
|
const islands: Record<string, Island> = {}
|
|
216
272
|
const keyCount: Record<string, number> = {}
|
|
@@ -220,13 +276,18 @@ export async function renderMarkdown(source: string | MdNode, { page, file, reso
|
|
|
220
276
|
// inline content (a label, say) is one line: as children of the root, each node would be a block
|
|
221
277
|
// of its own, separated by newlines, which show as spaces
|
|
222
278
|
if (nodes.length && nodes.every(n => PHRASING.has(n.type))) {
|
|
223
|
-
const out = toHast.stringify(
|
|
279
|
+
const out = toHast.stringify(
|
|
280
|
+
toHast.runSync({ type: 'root', children: [{ type: 'paragraph', children: nodes }] } as any),
|
|
281
|
+
)
|
|
224
282
|
return out.replace(/^<p>([\s\S]*)<\/p>$/, '$1')
|
|
225
283
|
}
|
|
226
284
|
return toHast.stringify(toHast.runSync({ type: 'root', children: nodes } as any))
|
|
227
285
|
}
|
|
228
286
|
/** Render mdast nodes to HTML. Transforms nested directives first; safe to call twice on the same nodes. */
|
|
229
|
-
const toHtml = async (nodes: MdNode[]) => {
|
|
287
|
+
const toHtml = async (nodes: MdNode[]) => {
|
|
288
|
+
await transform({ type: 'root', children: nodes })
|
|
289
|
+
return stringify(nodes)
|
|
290
|
+
}
|
|
230
291
|
|
|
231
292
|
async function sectionsOf(children: MdNode[]) {
|
|
232
293
|
await transform({ type: 'root', children })
|
|
@@ -250,7 +311,11 @@ export async function renderMarkdown(source: string | MdNode, { page, file, reso
|
|
|
250
311
|
}
|
|
251
312
|
return {
|
|
252
313
|
intro: stringify(intro),
|
|
253
|
-
sections: out.map(s => ({
|
|
314
|
+
sections: out.map(s => ({
|
|
315
|
+
title: stringify(s.heading.children ?? []),
|
|
316
|
+
text: toString(s.heading as any),
|
|
317
|
+
html: stringify(s.body),
|
|
318
|
+
})),
|
|
254
319
|
}
|
|
255
320
|
}
|
|
256
321
|
|
|
@@ -271,7 +336,11 @@ export async function renderMarkdown(source: string | MdNode, { page, file, reso
|
|
|
271
336
|
const id = String(++nextId)
|
|
272
337
|
islands[id] = { block: block.name, client: block.client, props }
|
|
273
338
|
// stable across re-renders while block + props are unchanged, so the client keeps the mounted island
|
|
274
|
-
const hash = crypto
|
|
339
|
+
const hash = crypto
|
|
340
|
+
.createHash('sha1')
|
|
341
|
+
.update(block.name + '\0' + JSON.stringify(props))
|
|
342
|
+
.digest('hex')
|
|
343
|
+
.slice(0, 12)
|
|
275
344
|
const key = `${hash}-${(keyCount[hash] = (keyCount[hash] ?? 0) + 1)}`
|
|
276
345
|
return html`<${tag} class="wb-island" data-block="${ctx.name}" data-wb-island="${id}" data-wb-key="${key}">${body}</${tag}>`
|
|
277
346
|
} catch (err) {
|
|
@@ -298,7 +367,9 @@ export async function renderMarkdown(source: string | MdNode, { page, file, reso
|
|
|
298
367
|
if (node.type === 'textDirective') return { type: 'text', value: literalDirective(node, label) }
|
|
299
368
|
// an unknown directive still shows its content: a typo never breaks the page
|
|
300
369
|
const labelHtml = label && html`<p class="wb-label"><strong>${raw(await toHtml(label))}</strong></p>`
|
|
301
|
-
return htmlNode(
|
|
370
|
+
return htmlNode(
|
|
371
|
+
html`<div class="wb-unknown" data-directive="${node.name}">${labelHtml}${raw(await toHtml(children))}</div>`,
|
|
372
|
+
)
|
|
302
373
|
}
|
|
303
374
|
const labelHtml = label ? await toHtml(label) : null
|
|
304
375
|
let defaults: Promise<Record<string, unknown>> | undefined
|
|
@@ -312,10 +383,17 @@ export async function renderMarkdown(source: string | MdNode, { page, file, reso
|
|
|
312
383
|
children,
|
|
313
384
|
node,
|
|
314
385
|
sections: () => sectionsOf(children),
|
|
315
|
-
defaults: () =>
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
386
|
+
defaults: () =>
|
|
387
|
+
(defaults ??= (async () => {
|
|
388
|
+
const content = await toHtml(children)
|
|
389
|
+
return {
|
|
390
|
+
name: node.name,
|
|
391
|
+
attrs: { ...node.attributes },
|
|
392
|
+
label: labelHtml,
|
|
393
|
+
html: content,
|
|
394
|
+
...(await sectionsOf(children)),
|
|
395
|
+
}
|
|
396
|
+
})()),
|
|
319
397
|
}
|
|
320
398
|
return htmlNode(await run(block, ctx, () => toHtml(children)))
|
|
321
399
|
}
|
|
@@ -359,6 +437,7 @@ export async function renderMarkdown(source: string | MdNode, { page, file, reso
|
|
|
359
437
|
const title = h1 ? toString(h1 as any) : null
|
|
360
438
|
await transform(tree)
|
|
361
439
|
const out = stringify(tree.children ?? [])
|
|
362
|
-
|
|
440
|
+
// blocks of one plugin can share a stylesheet (the form and its fields): linked once
|
|
441
|
+
const styles = [...new Set([...used.values()].map(t => t.style).filter((s): s is string => !!s))]
|
|
363
442
|
return { html: out, title, islands, styles }
|
|
364
443
|
}
|
package/src/pages/render.ts
CHANGED
|
@@ -14,16 +14,41 @@ import type { ResolvedBlock, ResolvedBlocks } from './blocks.ts'
|
|
|
14
14
|
|
|
15
15
|
type PagesPage = Extract<StoredPage, { kind: 'pages' }>
|
|
16
16
|
type FolderPage = Extract<StoredPage, { kind: 'folder' }>
|
|
17
|
-
interface Content {
|
|
17
|
+
interface Content {
|
|
18
|
+
html: string | Html
|
|
19
|
+
title?: string | null
|
|
20
|
+
islands: Record<string, Island>
|
|
21
|
+
styles: string[]
|
|
22
|
+
}
|
|
18
23
|
|
|
19
24
|
const BASE_CSS = new URL('../client/page/base.css', import.meta.url)
|
|
20
25
|
|
|
21
26
|
// before the stylesheets, so the page never flashes the wrong theme. A plain string, not part of an
|
|
22
27
|
// `html` template: the bundler writes `</script` in strings as `<\/script`, which in a tagged
|
|
23
28
|
// template would change the raw text the tag gets, so it rewrites the template into a helper call
|
|
24
|
-
const THEME_SCRIPT = raw(
|
|
29
|
+
const THEME_SCRIPT = raw(
|
|
30
|
+
`<script>try{const t=localStorage.getItem('wb-theme');if(t==='light'||t==='dark')document.documentElement.dataset.theme=t}catch{}</script>`,
|
|
31
|
+
)
|
|
25
32
|
|
|
26
|
-
const CODE_LANG: Record<string, string> = {
|
|
33
|
+
const CODE_LANG: Record<string, string> = {
|
|
34
|
+
js: 'javascript',
|
|
35
|
+
mjs: 'javascript',
|
|
36
|
+
cjs: 'javascript',
|
|
37
|
+
ts: 'typescript',
|
|
38
|
+
tsx: 'tsx',
|
|
39
|
+
jsx: 'jsx',
|
|
40
|
+
json: 'json',
|
|
41
|
+
css: 'css',
|
|
42
|
+
html: 'html',
|
|
43
|
+
sh: 'bash',
|
|
44
|
+
zsh: 'bash',
|
|
45
|
+
py: 'python',
|
|
46
|
+
rs: 'rust',
|
|
47
|
+
go: 'go',
|
|
48
|
+
toml: 'toml',
|
|
49
|
+
yml: 'yaml',
|
|
50
|
+
yaml: 'yaml',
|
|
51
|
+
}
|
|
27
52
|
|
|
28
53
|
/** Frontmatter is the core's; the page starts after it. */
|
|
29
54
|
const body = (source: string) => source.replace(/^---\r?\n[\s\S]*?\r?\n---\r?\n/, '')
|
|
@@ -33,20 +58,41 @@ export function titleOf(file: string, fallback: string) {
|
|
|
33
58
|
try {
|
|
34
59
|
const h1 = /^#\s+(.+)$/m.exec(body(fs.readFileSync(file, 'utf8')))
|
|
35
60
|
return h1 ? h1[1].replace(/[*_`]/g, '') : fallback
|
|
36
|
-
} catch {
|
|
61
|
+
} catch {
|
|
62
|
+
return fallback
|
|
63
|
+
}
|
|
37
64
|
}
|
|
38
65
|
|
|
39
66
|
function crumbs(id: string, name: string) {
|
|
40
67
|
const parts = id ? id.split('/') : []
|
|
41
|
-
return html`<a href="/">${name}</a
|
|
68
|
+
return html`<a href="/">${name}</a
|
|
69
|
+
>${parts.map((p, i) => html` / <a href="/${parts.slice(0, i + 1).join('/')}">${p}</a>`)}`
|
|
42
70
|
}
|
|
43
71
|
|
|
44
72
|
const time = (ts: number) => html`<time data-ts="${ts}"></time>`
|
|
45
73
|
|
|
46
74
|
function pageList(pages: PagesPage['pages']) {
|
|
47
|
-
return html`<ul class="wb-index"
|
|
48
|
-
|
|
49
|
-
|
|
75
|
+
return html`<ul class="wb-index">
|
|
76
|
+
${pages.map(
|
|
77
|
+
p =>
|
|
78
|
+
html`<li>
|
|
79
|
+
<a href="/${p.id}">${titleOf(p.file, path.basename(p.id))}</a>${
|
|
80
|
+
p.awaiting > 0 &&
|
|
81
|
+
html` <a
|
|
82
|
+
class="wb-awaiting"
|
|
83
|
+
href="/${p.id}#wb-waiting"
|
|
84
|
+
title="${awaitingLabel(p.awaiting)}"
|
|
85
|
+
aria-label="${awaitingLabel(p.awaiting)}"
|
|
86
|
+
>${raw(INBOX_ICON)}${p.awaiting}</a
|
|
87
|
+
>`
|
|
88
|
+
}
|
|
89
|
+
<span class="wb-meta"
|
|
90
|
+
><code>${p.id}</code> · edited
|
|
91
|
+
${time(p.mtime)}${p.lastEvent && html` · last submit ${time(Date.parse(p.lastEvent))}`}</span
|
|
92
|
+
>
|
|
93
|
+
</li>`,
|
|
94
|
+
)}
|
|
95
|
+
</ul>`
|
|
50
96
|
}
|
|
51
97
|
|
|
52
98
|
/** The folder's README, when it has one. */
|
|
@@ -78,16 +124,26 @@ const MARKDOWN = /\.(md|markdown)$/i
|
|
|
78
124
|
function documentsIn(dir: string) {
|
|
79
125
|
let files: string[]
|
|
80
126
|
try {
|
|
81
|
-
files = execFileSync(
|
|
82
|
-
|
|
127
|
+
files = execFileSync(
|
|
128
|
+
'git',
|
|
129
|
+
['ls-files', '--cached', '--others', '--exclude-standard', '--', ':(icase)*.md', ':(icase)*.markdown'],
|
|
130
|
+
{ cwd: dir, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] },
|
|
131
|
+
)
|
|
132
|
+
.split('\n')
|
|
133
|
+
.filter(f => f && !f.startsWith('.whiteboard/') && fs.existsSync(path.join(dir, f)))
|
|
83
134
|
} catch {
|
|
84
135
|
files = []
|
|
85
136
|
const walk = (rel: string) => {
|
|
86
137
|
let entries: fs.Dirent[] = []
|
|
87
|
-
try {
|
|
138
|
+
try {
|
|
139
|
+
entries = fs.readdirSync(path.join(dir, rel), { withFileTypes: true })
|
|
140
|
+
} catch {
|
|
141
|
+
return
|
|
142
|
+
}
|
|
88
143
|
for (const e of entries) {
|
|
89
|
-
if (e.isDirectory()) {
|
|
90
|
-
|
|
144
|
+
if (e.isDirectory()) {
|
|
145
|
+
if (!SKIP.has(e.name) && !e.name.startsWith('.')) walk(path.posix.join(rel, e.name))
|
|
146
|
+
} else if (MARKDOWN.test(e.name)) files.push(path.posix.join(rel, e.name))
|
|
91
147
|
}
|
|
92
148
|
}
|
|
93
149
|
walk('')
|
|
@@ -111,11 +167,35 @@ function documentList(ctx: RenderContext) {
|
|
|
111
167
|
const title = titleOf(abs, name)
|
|
112
168
|
const where = title === name ? path.posix.dirname(f) : f
|
|
113
169
|
let mtime = 0
|
|
114
|
-
try {
|
|
115
|
-
|
|
170
|
+
try {
|
|
171
|
+
mtime = fs.statSync(abs).mtimeMs
|
|
172
|
+
} catch {
|
|
173
|
+
/* gone meanwhile */
|
|
174
|
+
}
|
|
175
|
+
return html`<li data-order="${i}" data-updated="${mtime}" data-title="${title.toLowerCase()}">
|
|
176
|
+
<a href="${ctx.urlFor(abs) ?? `/-/${f}`}">${title}</a>
|
|
177
|
+
<span class="wb-meta"
|
|
178
|
+
>${where !== '.' && html`<code>${where === f ? f : `${where}/`}</code> · `}edited ${time(mtime)}</span
|
|
179
|
+
>
|
|
180
|
+
</li>`
|
|
116
181
|
})
|
|
117
182
|
// in a section, so the heading isn't numbered like the page's own sections
|
|
118
|
-
return html`<section class="wb-documents"
|
|
183
|
+
return html`<section class="wb-documents">
|
|
184
|
+
<div class="wb-list-head">
|
|
185
|
+
<h2>Documents</h2>
|
|
186
|
+
<label class="wb-sort"
|
|
187
|
+
>sort
|
|
188
|
+
<select data-wb-sort="documents" aria-label="Sort the documents">
|
|
189
|
+
<option value="order">by folder</option>
|
|
190
|
+
<option value="updated">last updated</option>
|
|
191
|
+
<option value="title">by title</option>
|
|
192
|
+
</select></label
|
|
193
|
+
>
|
|
194
|
+
</div>
|
|
195
|
+
<ul class="wb-index">
|
|
196
|
+
${rows}
|
|
197
|
+
</ul>
|
|
198
|
+
</section>`
|
|
119
199
|
}
|
|
120
200
|
|
|
121
201
|
/** The plugins' text links for a project folder, asked once per folder. */
|
|
@@ -124,7 +204,14 @@ function textLinksOf(links: ((ctx: { dir: string }) => TextLink[])[]) {
|
|
|
124
204
|
return (dir: string) => {
|
|
125
205
|
let got = linksFor.get(dir)
|
|
126
206
|
if (!got) {
|
|
127
|
-
got = links.flatMap(fn => {
|
|
207
|
+
got = links.flatMap(fn => {
|
|
208
|
+
try {
|
|
209
|
+
return fn({ dir })
|
|
210
|
+
} catch (err) {
|
|
211
|
+
console.error('links:', err)
|
|
212
|
+
return []
|
|
213
|
+
}
|
|
214
|
+
})
|
|
128
215
|
linksFor.set(dir, got)
|
|
129
216
|
}
|
|
130
217
|
return got
|
|
@@ -135,21 +222,43 @@ function textLinksOf(links: ((ctx: { dir: string }) => TextLink[])[]) {
|
|
|
135
222
|
* A message (a comment, a chat message) as HTML: markdown as on a page, but its raw HTML as text and
|
|
136
223
|
* its directives as written. Code fences go to the code block, so `styles` are what it needs linked.
|
|
137
224
|
*/
|
|
138
|
-
export function createMessageRender(
|
|
225
|
+
export function createMessageRender(
|
|
226
|
+
blocks: () => ResolvedBlocks,
|
|
227
|
+
links: ((ctx: { dir: string }) => TextLink[])[] = [],
|
|
228
|
+
) {
|
|
139
229
|
const textLinks = textLinksOf(links)
|
|
140
230
|
return async (text: string, ctx: RenderContext) => {
|
|
141
|
-
const out = await renderMarkdown(String(text ?? ''), {
|
|
231
|
+
const out = await renderMarkdown(String(text ?? ''), {
|
|
232
|
+
page: '',
|
|
233
|
+
file: '',
|
|
234
|
+
resolve: () => null,
|
|
235
|
+
code: blocks().code,
|
|
236
|
+
textLinks: textLinks(ctx.project.dir),
|
|
237
|
+
message: true,
|
|
238
|
+
})
|
|
142
239
|
return { html: out.html, styles: out.styles.map(s => ctx.asset(s)) }
|
|
143
240
|
}
|
|
144
241
|
}
|
|
145
242
|
|
|
146
|
-
export function createRender(
|
|
243
|
+
export function createRender(
|
|
244
|
+
blocks: () => ResolvedBlocks,
|
|
245
|
+
style?: URL | string,
|
|
246
|
+
pluginStyles: (URL | string)[] = [],
|
|
247
|
+
links: ((ctx: { dir: string }) => TextLink[])[] = [],
|
|
248
|
+
) {
|
|
147
249
|
const textLinks = textLinksOf(links)
|
|
148
250
|
const STYLE = stylePath(style)
|
|
149
251
|
const PLUGIN_STYLES = pluginStyles.map(stylePath).filter((f): f is string => !!f)
|
|
150
252
|
async function markdown(source: string, page: string, file: string, ctx: RenderContext) {
|
|
151
253
|
const { blocks: set, code } = blocks()
|
|
152
|
-
return renderMarkdown(body(source), {
|
|
254
|
+
return renderMarkdown(body(source), {
|
|
255
|
+
page,
|
|
256
|
+
file,
|
|
257
|
+
resolve: name => set.get(name) ?? null,
|
|
258
|
+
code,
|
|
259
|
+
linkFor: ctx.urlFor,
|
|
260
|
+
textLinks: textLinks(ctx.project.dir),
|
|
261
|
+
})
|
|
153
262
|
}
|
|
154
263
|
|
|
155
264
|
async function content(page: StoredPage, ctx: RenderContext): Promise<Content> {
|
|
@@ -160,27 +269,58 @@ export function createRender(blocks: () => ResolvedBlocks, style?: URL | string,
|
|
|
160
269
|
case 'pages': {
|
|
161
270
|
const list = page.pages.length ? pageList(page.pages) : html`<p class="wb-meta">No pages yet.</p>`
|
|
162
271
|
const view = page.id
|
|
163
|
-
? html`<h1>${page.id}</h1
|
|
164
|
-
|
|
272
|
+
? html`<h1>${page.id}</h1>
|
|
273
|
+
${list}
|
|
274
|
+
<p class="wb-meta">${page.file}</p>`
|
|
275
|
+
: html`<h1>${project.name}</h1>
|
|
276
|
+
${list}${project.files && documentList(ctx)}
|
|
277
|
+
<p class="wb-meta">
|
|
278
|
+
<code>${project.dir}</code>${project.files && html` · <a href="/-/">files</a>`} ·
|
|
279
|
+
<a href="/_/docs">how to write pages</a>
|
|
280
|
+
</p>`
|
|
165
281
|
return { html: view, title: page.id || project.name, islands: {}, styles: [] }
|
|
166
282
|
}
|
|
167
283
|
case 'folder': {
|
|
168
284
|
const rel = page.id.slice(2)
|
|
169
|
-
const rows = page.entries.map(
|
|
170
|
-
|
|
285
|
+
const rows = page.entries.map(
|
|
286
|
+
e =>
|
|
287
|
+
html`<li>
|
|
288
|
+
<a href="${encodeURIComponent(e.name) + (e.dir ? '/' : '')}">${e.name + (e.dir ? '/' : '')}</a>
|
|
289
|
+
</li>`,
|
|
290
|
+
)
|
|
291
|
+
const listing = html`<h1>${rel || project.name}</h1>
|
|
292
|
+
<ul class="wb-files">
|
|
293
|
+
${rel && html`<li><a href="../">../</a></li>`}${rows}
|
|
294
|
+
</ul>
|
|
295
|
+
<p class="wb-meta">${page.file} · <code>${page.id}</code></p>`
|
|
171
296
|
const readme = readmeOf(page)
|
|
172
297
|
if (!readme) return { html: listing, title: rel || project.name, islands: {}, styles: [] }
|
|
173
298
|
// markdown's output is HTML already
|
|
174
299
|
const r = await markdown(readme.source, page.id, readme.file, ctx)
|
|
175
|
-
return {
|
|
300
|
+
return {
|
|
301
|
+
...r,
|
|
302
|
+
html: html`${raw(r.html)}
|
|
303
|
+
<hr />
|
|
304
|
+
${listing}`,
|
|
305
|
+
}
|
|
176
306
|
}
|
|
177
307
|
case 'file': {
|
|
178
308
|
// the file as one code block, rendered by the code block (or as plain code)
|
|
179
309
|
const { code } = blocks()
|
|
180
310
|
const ext = path.extname(page.file).slice(1).toLowerCase()
|
|
181
|
-
const tree: MdNode = {
|
|
182
|
-
|
|
183
|
-
|
|
311
|
+
const tree: MdNode = {
|
|
312
|
+
type: 'root',
|
|
313
|
+
children: [{ type: 'code', lang: CODE_LANG[ext] ?? (ext || null), meta: null, value: page.source }],
|
|
314
|
+
}
|
|
315
|
+
const out = await renderMarkdown(tree, {
|
|
316
|
+
page: page.id,
|
|
317
|
+
file: page.file,
|
|
318
|
+
resolve: () => null,
|
|
319
|
+
code,
|
|
320
|
+
linkFor: ctx.urlFor,
|
|
321
|
+
})
|
|
322
|
+
const view = html`<h1 class="wb-code-title wb-wide">${page.id.slice(2)}</h1>
|
|
323
|
+
<div class="wb-code wb-wide">${raw(out.html)}</div>`
|
|
184
324
|
return { html: view, title: path.basename(page.file), islands: out.islands, styles: out.styles }
|
|
185
325
|
}
|
|
186
326
|
}
|
|
@@ -194,20 +334,35 @@ export function createRender(blocks: () => ResolvedBlocks, style?: URL | string,
|
|
|
194
334
|
// blocks that do not resolve (a bad block in a plugin)
|
|
195
335
|
out = { html: html`<pre class="wb-error">${(err as Error).message}</pre>`, islands: {}, styles: [] }
|
|
196
336
|
}
|
|
197
|
-
const islands = Object.fromEntries(
|
|
337
|
+
const islands = Object.fromEntries(
|
|
338
|
+
Object.entries(out.islands).map(([id, island]) => [id, { ...island, client: ctx.asset(island.client) }]),
|
|
339
|
+
)
|
|
198
340
|
const styles = out.styles.map(s => ctx.asset(s))
|
|
199
|
-
const nav = html`<nav class="wb-nav"
|
|
341
|
+
const nav = html`<nav class="wb-nav">
|
|
342
|
+
<span>${crumbs(page.id, ctx.project.name)}</span
|
|
343
|
+
>${page.mtime != null && html`<span class="wb-meta">updated <time id="wb-updated" data-ts="${page.mtime}"></time></span>`}<button
|
|
344
|
+
type="button"
|
|
345
|
+
class="wb-theme"
|
|
346
|
+
title="Theme: follow the system, light or dark"
|
|
347
|
+
aria-label="Theme"
|
|
348
|
+
></button
|
|
349
|
+
><span class="wb-live" role="img" title="Connecting…" aria-label="Connecting…"></span>
|
|
350
|
+
</nav>`
|
|
200
351
|
// the page's own, the plugins' (every page), the blocks' on this page, then the project's
|
|
201
|
-
const links = [
|
|
352
|
+
const links = [
|
|
353
|
+
ctx.asset(BASE_CSS),
|
|
354
|
+
...PLUGIN_STYLES.map(f => ctx.asset(f)),
|
|
355
|
+
...styles,
|
|
356
|
+
...(STYLE ? [ctx.asset(STYLE)] : []),
|
|
357
|
+
]
|
|
202
358
|
return {
|
|
203
359
|
title: out.title || page.id || ctx.project.name,
|
|
204
|
-
head: html`${THEME_SCRIPT}
|
|
205
|
-
${links.map(href => html`<link rel="stylesheet" href="${href}">`)}`,
|
|
360
|
+
head: html`${THEME_SCRIPT} ${links.map(href => html`<link rel="stylesheet" href="${href}" />`)}`,
|
|
206
361
|
// the page's content is HTML already: markdown's output, or what was built above
|
|
207
|
-
html: html`${nav}
|
|
362
|
+
html: html`${nav}
|
|
363
|
+
<main id="wb-main">${raw(out.html)}</main>`,
|
|
208
364
|
// the client mounts `islands` and links `styles`
|
|
209
|
-
|
|
210
|
-
data: { islands, styles, mtime: page.mtime, frontmatter: page.kind === 'page' ? page.frontmatter : {} },
|
|
365
|
+
data: { islands, styles, mtime: page.mtime },
|
|
211
366
|
}
|
|
212
367
|
}
|
|
213
368
|
}
|
|
@@ -219,8 +374,13 @@ ${links.map(href => html`<link rel="stylesheet" href="${href}">`)}`,
|
|
|
219
374
|
export function createDocs(blocks: () => ResolvedBlocks) {
|
|
220
375
|
return function docs(_ctx: RenderContext) {
|
|
221
376
|
const byPlugin = new Map<string, string[]>()
|
|
222
|
-
for (const t of blocks().blocks.values())
|
|
223
|
-
|
|
377
|
+
for (const t of blocks().blocks.values())
|
|
378
|
+
byPlugin.set(t.source, [
|
|
379
|
+
...(byPlugin.get(t.source) ?? []),
|
|
380
|
+
t.code ? `${t.name} (every other code fence)` : t.name,
|
|
381
|
+
])
|
|
382
|
+
const list = [...byPlugin]
|
|
383
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
224
384
|
.map(([plugin, blocks]) => `- **${plugin}**: ${blocks.map(b => `\`${b}\``).join(', ')}`)
|
|
225
385
|
return `# Pages
|
|
226
386
|
|