@bigmistqke/workpad 0.0.0-stage → 0.18.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.
Files changed (277) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +781 -2
  3. package/bin/workpad.ts +1509 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  5. package/dist/bin/workpad.js +1190 -0
  6. package/dist/client/runtime.js +1194 -0
  7. package/dist/define.js +27 -0
  8. package/dist/plugins/alert/README.md +18 -0
  9. package/dist/plugins/alert/index.js +40 -0
  10. package/dist/plugins/alert/public/style.css +40 -0
  11. package/dist/plugins/ansi/README.md +36 -0
  12. package/dist/plugins/ansi/index.js +63 -0
  13. package/dist/plugins/ansi/public/style.css +28 -0
  14. package/dist/plugins/badge/README.md +32 -0
  15. package/dist/plugins/badge/index.js +28 -0
  16. package/dist/plugins/badge/public/style.css +30 -0
  17. package/dist/plugins/chat/README.md +81 -0
  18. package/dist/plugins/chat/index.js +96 -0
  19. package/dist/plugins/chat/public/client.js +3 -0
  20. package/dist/plugins/chat/public/style.css +143 -0
  21. package/dist/plugins/claude/README.md +116 -0
  22. package/dist/plugins/claude/channel.js +130 -0
  23. package/dist/plugins/claude/doctor.js +316 -0
  24. package/dist/plugins/claude/index.js +199 -0
  25. package/dist/plugins/claude/session.js +68 -0
  26. package/dist/plugins/code/README.md +54 -0
  27. package/dist/plugins/code/diff.js +75 -0
  28. package/dist/plugins/code/index.js +105 -0
  29. package/dist/plugins/code/public/client.js +1 -0
  30. package/dist/plugins/code/public/style.css +99 -0
  31. package/dist/plugins/comments/README.md +30 -0
  32. package/dist/plugins/comments/index.js +39 -0
  33. package/dist/plugins/comments/public/client.js +24 -0
  34. package/dist/plugins/comments/public/style.css +224 -0
  35. package/dist/plugins/details/README.md +24 -0
  36. package/dist/plugins/details/index.js +28 -0
  37. package/dist/plugins/details/public/style.css +7 -0
  38. package/dist/plugins/draft/README.md +14 -0
  39. package/dist/plugins/draft/index.js +27 -0
  40. package/dist/plugins/draft/public/style.css +21 -0
  41. package/dist/plugins/forge/README.md +22 -0
  42. package/dist/plugins/forge/index.js +83 -0
  43. package/dist/plugins/form/README.md +126 -0
  44. package/dist/plugins/form/index.js +179 -0
  45. package/dist/plugins/form/public/style.css +172 -0
  46. package/dist/plugins/grid/README.md +45 -0
  47. package/dist/plugins/grid/index.js +35 -0
  48. package/dist/plugins/grid/public/style.css +98 -0
  49. package/dist/plugins/iframe/README.md +18 -0
  50. package/dist/plugins/iframe/index.js +49 -0
  51. package/dist/plugins/iframe/public/style.css +28 -0
  52. package/dist/plugins/index.js +44 -0
  53. package/dist/plugins/lightbox/README.md +24 -0
  54. package/dist/plugins/lightbox/index.js +20 -0
  55. package/dist/plugins/lightbox/public/client.js +4 -0
  56. package/dist/plugins/lightbox/public/style.css +66 -0
  57. package/dist/plugins/mermaid/README.md +21 -0
  58. package/dist/plugins/mermaid/index.js +42 -0
  59. package/dist/plugins/mermaid/public/style.css +21 -0
  60. package/dist/plugins/opencode/README.md +48 -0
  61. package/dist/plugins/opencode/channel.js +92 -0
  62. package/dist/plugins/opencode/index.js +64 -0
  63. package/dist/plugins/opencode/shim.js +150 -0
  64. package/dist/plugins/quiet/README.md +26 -0
  65. package/dist/plugins/quiet/index.js +23 -0
  66. package/dist/plugins/tabs/README.md +44 -0
  67. package/dist/plugins/tabs/index.js +75 -0
  68. package/dist/plugins/tabs/public/client.js +1 -0
  69. package/dist/plugins/tabs/public/style.css +98 -0
  70. package/dist/plugins/terminal/README.md +63 -0
  71. package/dist/plugins/terminal/host.js +338 -0
  72. package/dist/plugins/terminal/index.js +170 -0
  73. package/dist/plugins/terminal/modes.js +32 -0
  74. package/dist/plugins/terminal/public/client.js +41 -0
  75. package/dist/plugins/terminal/public/style.css +511 -0
  76. package/dist/plugins/terminal/server.js +256 -0
  77. package/dist/plugins/terminal/sizes.js +58 -0
  78. package/dist/plugins/timeline/README.md +48 -0
  79. package/dist/plugins/timeline/index.js +83 -0
  80. package/dist/plugins/timeline/public/style.css +138 -0
  81. package/dist/plugins/toc/README.md +40 -0
  82. package/dist/plugins/toc/index.js +29 -0
  83. package/dist/plugins/toc/public/client.js +1 -0
  84. package/dist/plugins/toc/public/style.css +117 -0
  85. package/dist/plugins/window/README.md +51 -0
  86. package/dist/plugins/window/index.js +91 -0
  87. package/dist/solid/index.js +41 -0
  88. package/dist/src/agents.js +81 -0
  89. package/dist/src/anchors.js +52 -0
  90. package/dist/src/api.js +282 -0
  91. package/dist/src/archive.js +49 -0
  92. package/dist/src/auth.js +67 -0
  93. package/dist/src/build.js +159 -0
  94. package/dist/src/client/page/base.css +1762 -0
  95. package/dist/src/client/page/main.js +2098 -0
  96. package/dist/src/config.js +76 -0
  97. package/dist/src/document.js +93 -0
  98. package/dist/src/entries.js +264 -0
  99. package/dist/src/events.js +322 -0
  100. package/dist/src/files.js +47 -0
  101. package/dist/src/formstate.js +65 -0
  102. package/dist/src/help.js +211 -0
  103. package/dist/src/html.js +34 -0
  104. package/dist/src/hub.js +341 -0
  105. package/dist/src/icons.js +6 -0
  106. package/dist/src/listen.js +132 -0
  107. package/dist/src/listeners.js +64 -0
  108. package/dist/src/load.js +81 -0
  109. package/dist/src/loader.js +179 -0
  110. package/dist/src/migrate.js +314 -0
  111. package/dist/src/package.js +52 -0
  112. package/dist/src/pages/blocks.js +67 -0
  113. package/dist/src/pages/board.js +59 -0
  114. package/dist/src/pages/index.js +27 -0
  115. package/dist/src/pages/markdown.js +863 -0
  116. package/dist/src/pages/render.js +665 -0
  117. package/dist/src/pages/window.js +29 -0
  118. package/dist/src/plugins.js +283 -0
  119. package/dist/src/public.js +24 -0
  120. package/dist/src/rename.js +130 -0
  121. package/dist/src/running.js +146 -0
  122. package/dist/src/serve.js +188 -0
  123. package/dist/src/server.js +1105 -0
  124. package/dist/src/site.js +179 -0
  125. package/dist/src/state.js +155 -0
  126. package/dist/src/status.js +323 -0
  127. package/dist/src/thread.js +68 -0
  128. package/dist/src/watchdog.js +38 -0
  129. package/dist/src/ws.js +128 -0
  130. package/package.json +154 -4
  131. package/plugins/README.md +304 -0
  132. package/plugins/alert/README.md +18 -0
  133. package/plugins/alert/index.ts +50 -0
  134. package/plugins/alert/public/style.css +40 -0
  135. package/plugins/ansi/README.md +36 -0
  136. package/plugins/ansi/index.ts +95 -0
  137. package/plugins/ansi/public/style.css +28 -0
  138. package/plugins/badge/README.md +32 -0
  139. package/plugins/badge/index.ts +36 -0
  140. package/plugins/badge/public/style.css +30 -0
  141. package/plugins/chat/README.md +81 -0
  142. package/plugins/chat/autosize.ts +139 -0
  143. package/plugins/chat/client.tsx +187 -0
  144. package/plugins/chat/index.ts +146 -0
  145. package/plugins/chat/public/style.css +143 -0
  146. package/plugins/claude/README.md +116 -0
  147. package/plugins/claude/channel.ts +136 -0
  148. package/plugins/claude/doctor.ts +274 -0
  149. package/plugins/claude/index.ts +248 -0
  150. package/plugins/claude/session.ts +70 -0
  151. package/plugins/code/README.md +54 -0
  152. package/plugins/code/client.ts +73 -0
  153. package/plugins/code/diff.ts +96 -0
  154. package/plugins/code/index.ts +136 -0
  155. package/plugins/code/public/style.css +99 -0
  156. package/plugins/comments/README.md +30 -0
  157. package/plugins/comments/client.ts +723 -0
  158. package/plugins/comments/index.ts +48 -0
  159. package/plugins/comments/public/style.css +224 -0
  160. package/plugins/details/README.md +24 -0
  161. package/plugins/details/index.ts +30 -0
  162. package/plugins/details/public/style.css +7 -0
  163. package/plugins/draft/README.md +14 -0
  164. package/plugins/draft/index.ts +23 -0
  165. package/plugins/draft/public/style.css +21 -0
  166. package/plugins/forge/README.md +22 -0
  167. package/plugins/forge/index.ts +84 -0
  168. package/plugins/form/README.md +126 -0
  169. package/plugins/form/index.ts +199 -0
  170. package/plugins/form/public/style.css +172 -0
  171. package/plugins/grid/README.md +45 -0
  172. package/plugins/grid/index.ts +41 -0
  173. package/plugins/grid/public/style.css +98 -0
  174. package/plugins/iframe/README.md +18 -0
  175. package/plugins/iframe/index.ts +53 -0
  176. package/plugins/iframe/public/style.css +28 -0
  177. package/plugins/index.ts +49 -0
  178. package/plugins/lightbox/README.md +24 -0
  179. package/plugins/lightbox/client.ts +109 -0
  180. package/plugins/lightbox/index.ts +25 -0
  181. package/plugins/lightbox/public/style.css +66 -0
  182. package/plugins/mermaid/README.md +21 -0
  183. package/plugins/mermaid/index.ts +46 -0
  184. package/plugins/mermaid/public/style.css +21 -0
  185. package/plugins/opencode/README.md +48 -0
  186. package/plugins/opencode/channel.ts +112 -0
  187. package/plugins/opencode/index.ts +91 -0
  188. package/plugins/opencode/shim.ts +166 -0
  189. package/plugins/quiet/README.md +26 -0
  190. package/plugins/quiet/index.ts +28 -0
  191. package/plugins/tabs/README.md +44 -0
  192. package/plugins/tabs/client.ts +96 -0
  193. package/plugins/tabs/index.ts +103 -0
  194. package/plugins/tabs/public/style.css +98 -0
  195. package/plugins/terminal/README.md +63 -0
  196. package/plugins/terminal/client.ts +391 -0
  197. package/plugins/terminal/host.ts +434 -0
  198. package/plugins/terminal/index.ts +199 -0
  199. package/plugins/terminal/modes.ts +30 -0
  200. package/plugins/terminal/public/style.css +225 -0
  201. package/plugins/terminal/server.ts +266 -0
  202. package/plugins/terminal/sizes.ts +72 -0
  203. package/plugins/timeline/README.md +48 -0
  204. package/plugins/timeline/index.ts +82 -0
  205. package/plugins/timeline/public/style.css +138 -0
  206. package/plugins/toc/README.md +40 -0
  207. package/plugins/toc/client.ts +236 -0
  208. package/plugins/toc/index.ts +55 -0
  209. package/plugins/toc/public/style.css +117 -0
  210. package/plugins/window/README.md +51 -0
  211. package/plugins/window/index.ts +92 -0
  212. package/skills/workpad/SKILL.md +588 -0
  213. package/solid/index.tsx +80 -0
  214. package/src/agents.ts +133 -0
  215. package/src/anchors.ts +85 -0
  216. package/src/api.ts +444 -0
  217. package/src/archive.ts +75 -0
  218. package/src/auth.ts +99 -0
  219. package/src/build.ts +213 -0
  220. package/src/client/base.ts +19 -0
  221. package/src/client/forms.ts +420 -0
  222. package/src/client/markdown.ts +147 -0
  223. package/src/client/page/base.css +1762 -0
  224. package/src/client/page/board.ts +137 -0
  225. package/src/client/page/elsewhere.ts +153 -0
  226. package/src/client/page/fresh.ts +343 -0
  227. package/src/client/page/heading-link.ts +32 -0
  228. package/src/client/page/live.ts +134 -0
  229. package/src/client/page/main.ts +398 -0
  230. package/src/client/page/orphans.ts +64 -0
  231. package/src/client/page/place.ts +260 -0
  232. package/src/client/page/sort.ts +49 -0
  233. package/src/client/page/sticky.ts +58 -0
  234. package/src/client/page/theme.ts +36 -0
  235. package/src/client/page/view.ts +9 -0
  236. package/src/client/page/waiting.ts +278 -0
  237. package/src/client/page/window.ts +40 -0
  238. package/src/client/reactive.ts +19 -0
  239. package/src/client/read.ts +111 -0
  240. package/src/client/runtime.ts +499 -0
  241. package/src/client/scroll.ts +90 -0
  242. package/src/client/toast.ts +99 -0
  243. package/src/config.ts +111 -0
  244. package/src/define.ts +997 -0
  245. package/src/document.ts +191 -0
  246. package/src/entries.ts +352 -0
  247. package/src/events.ts +463 -0
  248. package/src/files.ts +74 -0
  249. package/src/formstate.ts +72 -0
  250. package/src/help.ts +238 -0
  251. package/src/html.ts +44 -0
  252. package/src/hub.ts +426 -0
  253. package/src/icons.ts +18 -0
  254. package/src/listen.ts +171 -0
  255. package/src/listeners.ts +89 -0
  256. package/src/load.ts +113 -0
  257. package/src/loader.ts +227 -0
  258. package/src/migrate.ts +352 -0
  259. package/src/package.ts +62 -0
  260. package/src/pages/blocks.ts +106 -0
  261. package/src/pages/board.ts +89 -0
  262. package/src/pages/index.ts +79 -0
  263. package/src/pages/markdown.ts +976 -0
  264. package/src/pages/render.ts +748 -0
  265. package/src/pages/window.ts +69 -0
  266. package/src/plugins.ts +319 -0
  267. package/src/public.ts +39 -0
  268. package/src/rename.ts +147 -0
  269. package/src/running.ts +171 -0
  270. package/src/serve.ts +221 -0
  271. package/src/server.ts +1385 -0
  272. package/src/site.ts +170 -0
  273. package/src/state.ts +327 -0
  274. package/src/status.ts +466 -0
  275. package/src/thread.ts +98 -0
  276. package/src/watchdog.ts +53 -0
  277. package/src/ws.ts +155 -0
@@ -0,0 +1,748 @@
1
+ // A page on the server: a stored page → the HTML the reader sees. Markdown goes through remark with
2
+ // directives and fences handed to the plugins' blocks (markdown.ts); repository files show as highlighted
3
+ // code, folders as listings with their README on top; around all of it goes the page's frame.
4
+ // What the page draws itself is built with `html` (../html.ts), which escapes every value.
5
+ import { fileFilter } from '../files.ts'
6
+ import { FRONTMATTER } from '../site.ts'
7
+ import fs from 'node:fs'
8
+ import os from 'node:os'
9
+ import path from 'node:path'
10
+ import { fileURLToPath } from 'node:url'
11
+ import { execFileSync } from 'node:child_process'
12
+ import { renderMarkdown, type Island, type MdNode } from './markdown.ts'
13
+ import { html, raw, type Html } from '../html.ts'
14
+ import { INBOX_ICON, awaitingLabel } from '../icons.ts'
15
+ import type { BlockGroup, Plugin, RenderContext, Rendered, StoredPage, TextLink } from '../define.ts'
16
+ import type { ResolvedBlock, ResolvedBlocks } from './blocks.ts'
17
+ import { ACTIVE_FOR, byProject, byState, total, withoutPages, type Card, type ProjectColumn, type Waiting } from './board.ts'
18
+
19
+ type PagesPage = Extract<StoredPage, { kind: 'pages' }>
20
+ type FolderPage = Extract<StoredPage, { kind: 'folder' }>
21
+ interface Content {
22
+ html: string | Html
23
+ title?: string | null
24
+ islands: Record<string, Island>
25
+ styles: string[]
26
+ problems?: string[]
27
+ }
28
+
29
+ const BASE_CSS = new URL('../client/page/base.css', import.meta.url)
30
+
31
+ // before the stylesheets, so the page never flashes the wrong theme, nor the shared server's
32
+ // overview the wrong columns (client/page/board.ts). A plain string, not part of an
33
+ // `html` template: the bundler writes `</script` in strings as `<\/script`, which in a tagged
34
+ // template would change the raw text the tag gets, so it rewrites the template into a helper call
35
+ const THEME_SCRIPT = raw(
36
+ `<script>try{const t=localStorage.getItem('wp-theme');if(t==='light'||t==='dark')document.documentElement.dataset.theme=t;const b=new URLSearchParams(location.search).get('columns')||localStorage.getItem('wp-board');if(b==='project')document.documentElement.dataset.wpBoard=b}catch{}</script>`,
37
+ )
38
+
39
+ const CODE_LANG: Record<string, string> = {
40
+ js: 'javascript',
41
+ mjs: 'javascript',
42
+ cjs: 'javascript',
43
+ ts: 'typescript',
44
+ tsx: 'tsx',
45
+ jsx: 'jsx',
46
+ json: 'json',
47
+ css: 'css',
48
+ html: 'html',
49
+ sh: 'bash',
50
+ zsh: 'bash',
51
+ py: 'python',
52
+ rs: 'rust',
53
+ go: 'go',
54
+ toml: 'toml',
55
+ yml: 'yaml',
56
+ yaml: 'yaml',
57
+ }
58
+
59
+ /** Frontmatter is the core's; the page starts after it (a page's own is cut off by the site: PageSource.body). */
60
+ const body = (source: string) => source.replace(FRONTMATTER, '')
61
+
62
+ /** A page's title: its first heading, else its name. */
63
+ export function titleOf(file: string, fallback: string) {
64
+ try {
65
+ const h1 = /^#\s+(.+)$/m.exec(body(fs.readFileSync(file, 'utf8')))
66
+ return h1 ? h1[1].replace(/[*_`]/g, '') : fallback
67
+ } catch {
68
+ return fallback
69
+ }
70
+ }
71
+
72
+ function crumbs(id: string, name: string, base: string, entry?: RenderContext['entry']) {
73
+ const parts = id ? id.split('/') : []
74
+ // under a shared server, the overview of every project comes first, by its own name
75
+ const above = entry ? html`<a href="/">workpad</a> / ` : ''
76
+ return html`${above}<a href="${base}/">${name}</a
77
+ >${parts.map((p, i) => html` / <a href="${base}/${parts.slice(0, i + 1).join('/')}">${p}</a>`)}`
78
+ }
79
+
80
+ const time = (ts: number) => html`<time data-ts="${ts}"></time>`
81
+
82
+ function pageList(pages: PagesPage['pages'], base: string) {
83
+ return html`<ul class="wp-index">
84
+ ${pages.map(
85
+ p =>
86
+ html`<li>
87
+ <a href="${base}/${p.id}">${titleOf(p.file, path.basename(p.id))}</a>${
88
+ p.awaiting > 0 &&
89
+ html` <a
90
+ class="wp-awaiting"
91
+ href="${base}/${p.id}#wp-waiting"
92
+ title="${awaitingLabel(p.awaiting)}"
93
+ aria-label="${awaitingLabel(p.awaiting)}"
94
+ >${raw(INBOX_ICON)}${p.awaiting}</a
95
+ >`
96
+ }
97
+ <span class="wp-meta"
98
+ ><code>${p.id}</code> · edited
99
+ ${time(p.mtime)}${p.lastEvent && html` · last submit ${time(Date.parse(p.lastEvent))}`}</span
100
+ >
101
+ </li>`,
102
+ )}
103
+ </ul>`
104
+ }
105
+
106
+ /** The folder's README, when it has one. */
107
+ function readmeOf(page: FolderPage) {
108
+ const name = ['README.md', 'readme.md', 'Readme.md'].find(n => page.entries.some(e => !e.dir && e.name === n))
109
+ if (!name) return null
110
+ const file = path.join(page.file, name)
111
+ return { file, source: fs.readFileSync(file, 'utf8') }
112
+ }
113
+
114
+ /** A stylesheet reference (URL, file: URL string, or absolute path) → absolute path. */
115
+ function stylePath(ref: URL | string | undefined) {
116
+ if (ref == null) return null
117
+ const file = ref instanceof URL || String(ref).startsWith('file:') ? fileURLToPath(ref) : String(ref)
118
+ if (!path.isAbsolute(file)) throw new Error(`style: use a URL, e.g. new URL('./style.css', import.meta.url)`)
119
+ if (!fs.existsSync(file)) throw new Error(`style: file not found: ${file}`)
120
+ return file
121
+ }
122
+
123
+ const SKIP = new Set(['node_modules', '.git', '.workpad', 'dist', 'build'])
124
+ const MARKDOWN = /\.(md|markdown)$/i
125
+
126
+ /**
127
+ * Every markdown file in the project, which renders as a page: the top folder's first,
128
+ * then by path. In a git repository, the files git knows (tracked, or new and not ignored), so
129
+ * built copies and dependencies stay out; elsewhere a walk that skips the usual build and
130
+ * dependency folders. The project's own pages (.workpad/) are listed apart.
131
+ */
132
+ function documentsIn(dir: string, shows: (rel: string) => boolean = rel => fileFilter().shows(rel)) {
133
+ let files: string[]
134
+ try {
135
+ files = execFileSync(
136
+ 'git',
137
+ ['ls-files', '--cached', '--others', '--exclude-standard', '--', ':(icase)*.md', ':(icase)*.markdown'],
138
+ { cwd: dir, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] },
139
+ )
140
+ .split('\n')
141
+ .filter(f => f && !f.startsWith('.workpad/') && fs.existsSync(path.join(dir, f)))
142
+ } catch {
143
+ files = []
144
+ const walk = (rel: string) => {
145
+ let entries: fs.Dirent[] = []
146
+ try {
147
+ entries = fs.readdirSync(path.join(dir, rel), { withFileTypes: true })
148
+ } catch {
149
+ return
150
+ }
151
+ for (const e of entries) {
152
+ if (e.isDirectory()) {
153
+ if (!SKIP.has(e.name) && !e.name.startsWith('.')) walk(path.posix.join(rel, e.name))
154
+ } else if (MARKDOWN.test(e.name)) files.push(path.posix.join(rel, e.name))
155
+ }
156
+ }
157
+ walk('')
158
+ }
159
+ const depth = (f: string) => f.split('/').length
160
+ // what the config's `files` leaves out isn't listed (src/files.ts)
161
+ return [...new Set(files)].filter(shows).toSorted((a, b) => depth(a) - depth(b) || a.localeCompare(b))
162
+ }
163
+
164
+ /**
165
+ * The overview's list of the project's documents: each by its title, linked to its rendered page,
166
+ * with when its file last changed. The list can be sorted in the browser (sort.ts): by folder (as
167
+ * written here), by when last updated, or by title; each row carries what it is sorted by.
168
+ */
169
+ function documentList(ctx: RenderContext) {
170
+ return documentsOf(ctx.project.dir, ctx.base ?? '', ctx.urlFor, false, ctx.project.shows)
171
+ }
172
+
173
+ /**
174
+ * A project's documents by the folder they are in, each folder a fold with how many it holds, the
175
+ * top folder's own documents first: the shared server's overview, where a repository's many
176
+ * documents would otherwise be one long list. A folder holding only one folder is shown as one
177
+ * (`docs/adr/`), so the tree is no deeper than what tells folders apart.
178
+ */
179
+ function documentTree(
180
+ files: string[],
181
+ dir: string,
182
+ base: string,
183
+ urlFor: (abs: string) => string | null,
184
+ open = false,
185
+ ) {
186
+ interface Folder {
187
+ files: string[]
188
+ folders: Map<string, Folder>
189
+ count: number
190
+ }
191
+ const root: Folder = { files: [], folders: new Map(), count: 0 }
192
+ for (const f of files) {
193
+ let at = root
194
+ at.count++
195
+ const parts = f.split('/')
196
+ for (const part of parts.slice(0, -1)) {
197
+ if (!at.folders.has(part)) at.folders.set(part, { files: [], folders: new Map(), count: 0 })
198
+ at = at.folders.get(part)!
199
+ at.count++
200
+ }
201
+ at.files.push(f)
202
+ }
203
+ const row = (f: string) => {
204
+ const abs = path.join(dir, f)
205
+ const name = path.posix.basename(f)
206
+ const title = titleOf(abs, name)
207
+ let mtime = 0
208
+ try {
209
+ mtime = fs.statSync(abs).mtimeMs
210
+ } catch {
211
+ /* gone meanwhile */
212
+ }
213
+ return html`<li>
214
+ <a href="${urlFor(abs) ?? `${base}/-/${f}`}">${title}</a>
215
+ <span class="wp-meta">${title !== name && html`<code>${name}</code> · `}edited ${time(mtime)}</span>
216
+ </li>`
217
+ }
218
+ const folder = (name: string, f: Folder): Html => {
219
+ // a folder with only one folder in it is shown as one
220
+ while (!f.files.length && f.folders.size === 1) {
221
+ const [inner, next] = [...f.folders][0]!
222
+ name = `${name}/${inner}`
223
+ f = next
224
+ }
225
+ return html`<details class="wp-folder">
226
+ <summary>${name}/ <span class="wp-meta">${f.count}</span></summary>
227
+ ${contents(f)}
228
+ </details>`
229
+ }
230
+ const contents = (f: Folder): Html =>
231
+ html`${
232
+ f.files.length > 0 &&
233
+ html`<ul class="wp-index">
234
+ ${f.files.map(row)}
235
+ </ul>`
236
+ }${[...f.folders].toSorted(([a], [b]) => a.localeCompare(b)).map(([name, sub]) => folder(name, sub))}`
237
+ return html`<details class="wp-documents wp-folded-documents"${raw(open ? ' open' : '')}>
238
+ <summary>Files <span class="wp-meta">${files.length}</span></summary>
239
+ ${contents(root)}
240
+ </details>`
241
+ }
242
+
243
+ /**
244
+ * A project's documents, as its overview lists them: from its folder, linked where it serves them.
245
+ * `folded`: in a fold that says how many (the shared server's overview, where each project has them).
246
+ */
247
+ function documentsOf(
248
+ dir: string,
249
+ base: string,
250
+ urlFor: (abs: string) => string | null,
251
+ folded = false,
252
+ shows?: (rel: string) => boolean,
253
+ open = false,
254
+ ) {
255
+ const files = documentsIn(dir, shows)
256
+ if (!files.length) return ''
257
+ const ctx = { project: { dir }, base, urlFor }
258
+ const rows = files.map((f, i) => {
259
+ const abs = path.join(ctx.project.dir, f)
260
+ // a file without a heading goes by its name, and the line under it says the folder it is in
261
+ const name = path.posix.basename(f)
262
+ const title = titleOf(abs, name)
263
+ const where = title === name ? path.posix.dirname(f) : f
264
+ let mtime = 0
265
+ try {
266
+ mtime = fs.statSync(abs).mtimeMs
267
+ } catch {
268
+ /* gone meanwhile */
269
+ }
270
+ return html`<li data-order="${i}" data-updated="${mtime}" data-title="${title.toLowerCase()}">
271
+ <a href="${ctx.urlFor(abs) ?? `${ctx.base ?? ''}/-/${f}`}">${title}</a>
272
+ <span class="wp-meta"
273
+ >${where !== '.' && html`<code>${where === f ? f : `${where}/`}</code> · `}edited ${time(mtime)}</span
274
+ >
275
+ </li>`
276
+ })
277
+ if (folded) return documentTree(files, dir, base, urlFor, open)
278
+ // in a section, so the heading isn't numbered like the page's own sections
279
+ return html`<section class="wp-documents">
280
+ <div class="wp-list-head">
281
+ <h2>Documents</h2>
282
+ <label class="wp-sort"
283
+ >sort
284
+ <select data-wp-sort="documents" aria-label="Sort the documents">
285
+ <option value="order">by folder</option>
286
+ <option value="updated">last updated</option>
287
+ <option value="title">by title</option>
288
+ </select></label
289
+ >
290
+ </div>
291
+ <ul class="wp-index">
292
+ ${rows}
293
+ </ul>
294
+ </section>`
295
+ }
296
+
297
+ type BoardProject = NonNullable<PagesPage['projects']>[number]
298
+
299
+ /** What waits on a page or a project, as small badges; nothing when nothing waits. */
300
+ function counts(w: Waiting, href: string | null) {
301
+ const badge = (kind: string, n: number, text: string, title: string) =>
302
+ n > 0 &&
303
+ (href
304
+ ? html`<a class="wp-count wp-count-${kind}" href="${href}" title="${title}">${n} ${text}</a>`
305
+ : html`<span class="wp-count wp-count-${kind}" title="${title}">${n} ${text}</span>`)
306
+ if (!total(w)) return ''
307
+ return html`<span class="wp-counts"
308
+ >${badge('open', w.open, 'open', w.open === 1 ? '1 question waiting for you' : `${w.open} questions waiting for you`)}${badge(
309
+ 'unread',
310
+ w.unread,
311
+ 'unread',
312
+ w.unread === 1 ? '1 message you haven’t read' : `${w.unread} messages you haven’t read`,
313
+ )}${badge(
314
+ 'unreceived',
315
+ w.unreceived,
316
+ 'not received',
317
+ w.unreceived === 1 ? '1 answer no agent received' : `${w.unreceived} answers no agent received`,
318
+ )}</span
319
+ >`
320
+ }
321
+
322
+ /** A page on the board: its title, what waits on it, and (in a column of states) its project. */
323
+ function card(c: Card<BoardProject>, withProject: boolean) {
324
+ return html`<li class="wp-board-card${total(c.waiting) > 0 ? ' wp-board-waits' : ''}">
325
+ <a class="wp-board-title" href="${c.page.url}">${c.page.title}</a>
326
+ ${counts(c.waiting, `${c.page.url}#wp-waiting`)}
327
+ ${withProject && html`<span class="wp-board-project">${c.project.label}</span>`}
328
+ <span class="wp-meta wp-board-when">edited ${time(c.page.mtime)}</span>
329
+ </li>`
330
+ }
331
+
332
+ function cards(list: Card<BoardProject>[], withProject: boolean) {
333
+ return list.length
334
+ ? html`<ul class="wp-board-cards">
335
+ ${list.map(c => card(c, withProject))}
336
+ </ul>`
337
+ : html`<p class="wp-meta wp-board-none">Nothing here.</p>`
338
+ }
339
+
340
+ const pagesCount = (n: number) => (n === 1 ? '1 page' : `${n} pages`)
341
+
342
+ /** The projects without pages, folded into one column at the end. */
343
+ function emptyColumn(projects: BoardProject[]) {
344
+ if (!projects.length) return ''
345
+ return html`<details class="wp-board-column wp-board-empty">
346
+ <summary>${projects.length} without pages</summary>
347
+ <ul>
348
+ ${projects.map(p => html`<li><a href="${p.base}/" title="${p.error ?? p.dir}">${p.label}</a></li>`)}
349
+ </ul>
350
+ </details>`
351
+ }
352
+
353
+ /**
354
+ * The shared server's overview: its projects' pages as columns you scroll sideways, a column per
355
+ * state (what waits for you, what changed lately, the rest) or a column per project; the reader
356
+ * switches between them (board.ts in the page), both are here. Projects without pages are folded
357
+ * at the end of either.
358
+ */
359
+ function board(projects: BoardProject[], now: number) {
360
+ const states = byState(projects, now)
361
+ // of the projects without pages, those with documents to read get a column of their own in the
362
+ // per-project view (a worktree's README and docs), and only the truly empty fold at the end
363
+ const docsOf = new Map(withoutPages(projects).map(p => [p, p.files ? documentsIn(p.dir).length : 0]))
364
+ const empty = withoutPages(projects)
365
+ const withDocs = [...docsOf].filter(([, n]) => n > 0).map(([p]) => p)
366
+ const bare = empty.filter(p => !docsOf.get(p))
367
+ const hours = ACTIVE_FOR / 3_600_000
368
+ const stateColumn = (key: keyof typeof states, title: string, note: string) =>
369
+ html`<section class="wp-board-column wp-board-${key}${key === 'waiting' && states.waiting.length ? ' wp-board-waits' : ''}">
370
+ <header class="wp-board-head">
371
+ <h2>${title}</h2>
372
+ <span class="wp-meta">${pagesCount(states[key].length)}${note && ` · ${note}`}</span>
373
+ </header>
374
+ ${cards(states[key], true)}
375
+ </section>`
376
+ const projectColumn = (col: ProjectColumn<BoardProject>) => {
377
+ const p = col.project
378
+ return html`<section class="wp-board-column${total(col.waiting) > 0 ? ' wp-board-waits' : ''}">
379
+ <header class="wp-board-head" title="${p.dir.replace(os.homedir(), '~')}">
380
+ <h2>${p.label}</h2>
381
+ <span class="wp-meta">edited ${time(p.active)} · ${pagesCount(col.cards.length)}</span>
382
+ ${counts(col.waiting, null)}
383
+ </header>
384
+ ${cards(col.cards, false)} ${p.files && documentsOf(p.dir, p.base, () => null, true)}
385
+ </section>`
386
+ }
387
+ // a project with documents and no pages: its column holds its files, open
388
+ const docsColumn = (p: BoardProject) =>
389
+ html`<section class="wp-board-column wp-board-docs">
390
+ <header class="wp-board-head" title="${p.dir.replace(os.homedir(), '~')}">
391
+ <h2><a href="${p.base}/">${p.label}</a></h2>
392
+ <span class="wp-meta">no pages · ${docsOf.get(p)} file${docsOf.get(p) === 1 ? '' : 's'}</span>
393
+ </header>
394
+ ${documentsOf(p.dir, p.base, () => null, true, undefined, true)}
395
+ </section>`
396
+ return html`<div class="wp-board-bar">
397
+ <div class="wp-board-switch" role="group" aria-label="Columns">
398
+ <button type="button" data-wp-board="state">By state</button
399
+ ><button type="button" data-wp-board="project">By project</button>
400
+ </div>
401
+ <input
402
+ type="search"
403
+ class="wp-board-search"
404
+ placeholder="Search pages, projects, files /"
405
+ aria-label="Search the overview"
406
+ autocomplete="off"
407
+ spellcheck="false"
408
+ />
409
+ </div>
410
+ <p class="wp-board-nomatch" hidden>Nothing here matches.</p>
411
+ <div class="wp-board" data-board="state">
412
+ ${stateColumn('waiting', 'Waiting for you', '')}
413
+ ${stateColumn('active', 'Active', `changed in the last ${hours} hours`)}
414
+ ${stateColumn('quiet', 'Quiet', '')} ${emptyColumn(empty)}
415
+ </div>
416
+ <div class="wp-board" data-board="project">
417
+ ${byProject(projects).map(projectColumn)} ${withDocs.map(docsColumn)} ${emptyColumn(bare)}
418
+ </div>`
419
+ }
420
+
421
+ /** The plugins' text links for a project folder, asked once per folder. */
422
+ function textLinksOf(links: ((ctx: { dir: string }) => TextLink[])[]) {
423
+ const linksFor = new Map<string, TextLink[]>()
424
+ return (dir: string) => {
425
+ let got = linksFor.get(dir)
426
+ if (!got) {
427
+ got = links.flatMap(fn => {
428
+ try {
429
+ return fn({ dir })
430
+ } catch (err) {
431
+ console.error('links:', err)
432
+ return []
433
+ }
434
+ })
435
+ linksFor.set(dir, got)
436
+ }
437
+ return got
438
+ }
439
+ }
440
+
441
+ /**
442
+ * A message (a comment, a chat message) as HTML: markdown as on a page, but its raw HTML as text and
443
+ * its directives as written. Code fences go to the code block, so `styles` are what it needs linked.
444
+ */
445
+ /**
446
+ * An agent's chat message as a small page (Pages.agentMessage): every block, as on the page, in the
447
+ * chat it is in (RenderOptions.chat).
448
+ */
449
+ /** Islands as the browser loads them: each client module at the address the server serves it from. */
450
+ const served = (islands: Record<string, Island>, ctx: RenderContext) =>
451
+ Object.fromEntries(Object.entries(islands).map(([id, island]) => [id, { ...island, client: ctx.asset(island.client) }]))
452
+
453
+ export function createAgentMessageRender(
454
+ blocks: () => ResolvedBlocks,
455
+ links: ((ctx: { dir: string }) => TextLink[])[] = [],
456
+ composers: Map<string, NonNullable<Plugin['compose']>> = new Map(),
457
+ ) {
458
+ const textLinks = textLinksOf(links)
459
+ return async (
460
+ text: string,
461
+ ctx: RenderContext,
462
+ where: { page: string; file: string; chat: string; first: number; prefix: string },
463
+ ) => {
464
+ const { blocks: set, code, alert } = blocks()
465
+ const out = await renderMarkdown(String(text ?? ''), {
466
+ page: where.page,
467
+ file: where.file,
468
+ resolve: name => set.get(name) ?? null,
469
+ code,
470
+ alert: alert?.name ?? null,
471
+ linkFor: ctx.urlFor,
472
+ textLinks: textLinks(ctx.project.dir),
473
+ composers,
474
+ chat: { name: where.chat, first: where.first, prefix: where.prefix },
475
+ })
476
+ return { html: String(out.html), islands: served(out.islands, ctx), styles: out.styles.map(s => ctx.asset(s)) }
477
+ }
478
+ }
479
+
480
+ /**
481
+ * Markdown a plugin's client asks for (Api.blocks): every block, as on the page `where.page`, files
482
+ * resolved from it, its islands' ids starting with `where.prefix`.
483
+ */
484
+ export function createBlocksRender(
485
+ blocks: () => ResolvedBlocks,
486
+ links: ((ctx: { dir: string }) => TextLink[])[] = [],
487
+ composers: Map<string, NonNullable<Plugin['compose']>> = new Map(),
488
+ ) {
489
+ const textLinks = textLinksOf(links)
490
+ return async (text: string, ctx: RenderContext, where: { page: string; file: string; prefix: string }) => {
491
+ const { blocks: set, code, alert } = blocks()
492
+ const out = await renderMarkdown(String(text ?? ''), {
493
+ page: where.page,
494
+ file: where.file,
495
+ resolve: name => set.get(name) ?? null,
496
+ code,
497
+ alert: alert?.name ?? null,
498
+ linkFor: ctx.urlFor,
499
+ textLinks: textLinks(ctx.project.dir),
500
+ composers,
501
+ prefix: where.prefix,
502
+ })
503
+ return { html: String(out.html), islands: served(out.islands, ctx), styles: out.styles.map(s => ctx.asset(s)) }
504
+ }
505
+ }
506
+
507
+ export function createMessageRender(
508
+ blocks: () => ResolvedBlocks,
509
+ links: ((ctx: { dir: string }) => TextLink[])[] = [],
510
+ ) {
511
+ const textLinks = textLinksOf(links)
512
+ return async (text: string, ctx: RenderContext) => {
513
+ const out = await renderMarkdown(String(text ?? ''), {
514
+ page: '',
515
+ file: '',
516
+ // the blocks that take fences (a ```mermaid diagram), as on a page, the way a forge's comments
517
+ // draw them; directives stay as written (see message)
518
+ resolve: name => {
519
+ const block = blocks().blocks.get(name) ?? null
520
+ return block?.fences ? block : null
521
+ },
522
+ code: blocks().code,
523
+ textLinks: textLinks(ctx.project.dir),
524
+ message: true,
525
+ })
526
+ return { html: out.html, styles: out.styles.map(s => ctx.asset(s)) }
527
+ }
528
+ }
529
+
530
+ /** What a block left out of a build (RenderContext.skip) is: nothing, for its directives and fences alike. */
531
+ const skipped = (name: string): ResolvedBlock => ({
532
+ name,
533
+ code: false,
534
+ fences: true,
535
+ alerts: false,
536
+ attrs: null,
537
+ menu: null,
538
+ props: null,
539
+ html: () => '',
540
+ open: null,
541
+ client: null,
542
+ style: null,
543
+ source: 'build',
544
+ })
545
+
546
+ export function createRender(
547
+ blocks: () => ResolvedBlocks,
548
+ style?: URL | string,
549
+ pluginStyles: { plugin: string; file: URL | string }[] = [],
550
+ links: ((ctx: { dir: string }) => TextLink[])[] = [],
551
+ composers: Map<string, NonNullable<Plugin['compose']>> = new Map(),
552
+ ) {
553
+ const textLinks = textLinksOf(links)
554
+ const STYLE = stylePath(style)
555
+ const PLUGIN_STYLES = pluginStyles
556
+ .map(s => ({ plugin: s.plugin, file: stylePath(s.file) }))
557
+ .filter((s): s is { plugin: string; file: string } => !!s.file)
558
+ /** `content`: the markdown without its frontmatter. */
559
+ async function markdown(content: string, page: string, file: string, ctx: RenderContext) {
560
+ const { blocks: set, code, alert } = blocks()
561
+ const skip = ctx.skip
562
+ return renderMarkdown(content, {
563
+ page,
564
+ file,
565
+ resolve: name => {
566
+ const block = set.get(name) ?? null
567
+ if (skip && (skip.has(name) || (block && skip.has(block.source))))
568
+ return { ...skipped(name), alerts: block?.alerts ?? false }
569
+ return block
570
+ },
571
+ code,
572
+ alert: alert?.name ?? null,
573
+ linkFor: ctx.urlFor,
574
+ textLinks: textLinks(ctx.project.dir),
575
+ // a build leaves out a skipped plugin's composing too: the block as it is
576
+ composers: skip
577
+ ? new Map(
578
+ [...composers].map(([name, compose]) =>
579
+ skip.has(name) ? [name, ({ children }: { children: unknown }) => String(children)] : [name, compose],
580
+ ),
581
+ )
582
+ : composers,
583
+ })
584
+ }
585
+
586
+ async function content(page: StoredPage, ctx: RenderContext): Promise<Content> {
587
+ const { project } = ctx
588
+ switch (page.kind) {
589
+ case 'page':
590
+ return markdown(page.body, page.id, page.file, ctx)
591
+ case 'pages': {
592
+ // the shared server's overview: every project's pages as columns side by side
593
+ if (page.projects) {
594
+ const view = html`<h1>${project.name}</h1>
595
+ ${board(page.projects, Date.now())} ${page.note && html`<p class="wp-meta">${page.note}</p>`}`
596
+ return { html: view, title: project.name, islands: {}, styles: [] }
597
+ }
598
+ const list = page.pages.length
599
+ ? pageList(page.pages, ctx.base ?? '')
600
+ : html`<p class="wp-meta">No pages yet.</p>`
601
+ const view = page.id
602
+ ? html`<h1>${page.id}</h1>
603
+ ${list}
604
+ <p class="wp-meta">${page.file}</p>`
605
+ : html`<h1>${project.name}</h1>
606
+ ${list}${project.files && documentList(ctx)}
607
+ <p class="wp-meta">
608
+ <code>${project.dir}</code>${project.files && html` · <a href="${ctx.base ?? ''}/-/">files</a>`} ·
609
+ <a href="${ctx.base ?? ''}/_/docs">how to write pages</a>
610
+ </p>`
611
+ return { html: view, title: page.id || project.name, islands: {}, styles: [] }
612
+ }
613
+ case 'folder': {
614
+ const rel = page.id.slice(2)
615
+ const rows = page.entries.map(
616
+ e =>
617
+ html`<li>
618
+ <a href="${encodeURIComponent(e.name) + (e.dir ? '/' : '')}">${e.name + (e.dir ? '/' : '')}</a>
619
+ </li>`,
620
+ )
621
+ const listing = html`<h1>${rel || project.name}</h1>
622
+ <ul class="wp-files">
623
+ ${rel && html`<li><a href="../">../</a></li>`}${rows}
624
+ </ul>
625
+ <p class="wp-meta">${page.file} · <code>${page.id}</code></p>`
626
+ const readme = readmeOf(page)
627
+ if (!readme) return { html: listing, title: rel || project.name, islands: {}, styles: [] }
628
+ // markdown's output is HTML already
629
+ const r = await markdown(body(readme.source), page.id, readme.file, ctx)
630
+ return {
631
+ ...r,
632
+ html: html`${raw(r.html)}
633
+ <hr />
634
+ ${listing}`,
635
+ }
636
+ }
637
+ case 'file': {
638
+ // the file as one code block, rendered by the code block (or as plain code)
639
+ const { code } = blocks()
640
+ const ext = path.extname(page.file).slice(1).toLowerCase()
641
+ const tree: MdNode = {
642
+ type: 'root',
643
+ children: [{ type: 'code', lang: CODE_LANG[ext] ?? (ext || null), meta: null, value: page.source }],
644
+ }
645
+ const out = await renderMarkdown(tree, {
646
+ page: page.id,
647
+ file: page.file,
648
+ resolve: () => null,
649
+ code,
650
+ linkFor: ctx.urlFor,
651
+ })
652
+ const view = html`<h1 class="wp-code-title wp-wide">${page.id.slice(2)}</h1>
653
+ <div class="wp-code wp-wide">${raw(out.html)}</div>`
654
+ return { html: view, title: path.basename(page.file), islands: out.islands, styles: out.styles }
655
+ }
656
+ }
657
+ }
658
+
659
+ return async function render(page: StoredPage, ctx: RenderContext): Promise<Rendered> {
660
+ let out: Content
661
+ try {
662
+ out = await content(page, ctx)
663
+ } catch (err) {
664
+ // blocks that do not resolve (a bad block in a plugin)
665
+ out = { html: html`<pre class="wp-error">${(err as Error).message}</pre>`, islands: {}, styles: [] }
666
+ }
667
+ const islands = Object.fromEntries(
668
+ Object.entries(out.islands).map(([id, island]) => [id, { ...island, client: ctx.asset(island.client) }]),
669
+ )
670
+ const styles = out.styles.map(s => ctx.asset(s))
671
+ // a built page: the project's name and the theme; nothing of the server's (crumbs into its
672
+ // folders, when it was updated, whether it is live)
673
+ const nav =
674
+ ctx.live === false
675
+ ? html`<nav class="wp-nav">
676
+ <span>${ctx.project.name}</span><span data-wp-slot="header"></span
677
+ ><button
678
+ type="button"
679
+ class="wp-theme"
680
+ title="Theme: follow the system, light or dark"
681
+ aria-label="Theme"
682
+ ></button>
683
+ </nav>`
684
+ : html`<nav class="wp-nav">
685
+ <span>${crumbs(page.id, ctx.entry?.label || ctx.project.name, ctx.base ?? '', ctx.entry)}</span
686
+ >${page.mtime != null && html`<span class="wp-meta">updated <time id="wp-updated" data-ts="${page.mtime}"></time></span>`}<span
687
+ data-wp-slot="header"
688
+ ></span
689
+ ><button
690
+ type="button"
691
+ class="wp-theme"
692
+ title="Theme: follow the system, light or dark"
693
+ aria-label="Theme"
694
+ ></button
695
+ ><span class="wp-live" role="img" title="Connecting…" aria-label="Connecting…"></span>
696
+ </nav>`
697
+ // the page's own, the plugins' (every page), the blocks' on this page, then the project's
698
+ const links = [
699
+ ctx.asset(BASE_CSS),
700
+ ...PLUGIN_STYLES.filter(s => !ctx.skip?.has(s.plugin)).map(s => ctx.asset(s.file)),
701
+ ...styles,
702
+ ...(STYLE ? [ctx.asset(STYLE)] : []),
703
+ ]
704
+ return {
705
+ title: out.title || page.id || ctx.project.name,
706
+ head: html`${THEME_SCRIPT} ${links.map(href => html`<link rel="stylesheet" href="${href}" />`)}`,
707
+ // the page's content is HTML already: markdown's output, or what was built above
708
+ // the reading area: the content, and what the browser lists after it (chats and comments no
709
+ // longer on the page), which go in it as the content's siblings
710
+ html: html`${nav}
711
+ <div class="wp-body"><main id="wp-main">${raw(out.html)}</main></div>`,
712
+ // the client mounts `islands` and links `styles`
713
+ data: { islands, styles, mtime: page.mtime },
714
+ problems: out.problems ?? [],
715
+ }
716
+ }
717
+ }
718
+
719
+ /** The menu's groups, in order: what an agent writing a page is trying to do. */
720
+ const GROUPS: [BlockGroup, string][] = [
721
+ ['ask', 'Ask: get something back from the reader'],
722
+ ['show', 'Show: put something in front of the reader'],
723
+ ['arrange', 'Arrange: several things, or one out of the way'],
724
+ ['mark', 'Mark: how something stands, or how much it counts'],
725
+ ]
726
+
727
+ /**
728
+ * What `workpad docs` prints first: how pages are written, then the menu: a line per block that
729
+ * has one (Block.menu), under what it is for. How to write each is in its plugin's docs.
730
+ */
731
+ export function createDocs(blocks: () => ResolvedBlocks) {
732
+ return function docs(_ctx: RenderContext) {
733
+ const all = [...blocks().blocks.values()].filter(t => t.menu)
734
+ const width = Math.max(0, ...all.map(t => t.name.length)) + 3
735
+ const groups = GROUPS.map(([group, title]) => {
736
+ const lines = all.filter(t => t.menu!.group === group).map(t => ` ${t.name.padEnd(width)}${t.menu!.summary}`)
737
+ return lines.length ? `${title}\n${lines.join('\n')}` : ''
738
+ }).filter(Boolean)
739
+ return `# Pages
740
+
741
+ A page is markdown: GFM tables, \`<details>\`, raw HTML and a hand-written \`<form name="…">\` work as they are. Blocks: \`:::name\` around content, \`::name\` on a line, \`:name\` inside a line, and code fences (\`\`\`\` \`\`\`name \`\`\`\`). What the reader answers comes back as events: through your session's channel once \`workpad agent ping\` confirmed it (once a session), else with \`workpad listen\`. A block nobody brings renders its content, so a typo never breaks a page.
742
+
743
+ \`\`\`\`text
744
+ ${groups.join('\n\n')}
745
+ \`\`\`\`
746
+ `
747
+ }
748
+ }