@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,665 @@
1
+ import { fileFilter } from "../files.js";
2
+ import { FRONTMATTER } from "../site.js";
3
+ import { renderMarkdown } from "./markdown.js";
4
+ import { html, raw } from "../html.js";
5
+ import { awaitingLabel } from "../icons.js";
6
+ import { ACTIVE_FOR, byProject, byState, total, withoutPages } from "./board.js";
7
+ import fs from "node:fs";
8
+ import path from "node:path";
9
+ import os from "node:os";
10
+ import { execFileSync } from "node:child_process";
11
+ import { fileURLToPath } from "node:url";
12
+ // src/pages/render.ts
13
+ const BASE_CSS = new URL("../client/page/base.css", import.meta.url);
14
+ const THEME_SCRIPT = raw(`<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>`);
15
+ const CODE_LANG = {
16
+ js: "javascript",
17
+ mjs: "javascript",
18
+ cjs: "javascript",
19
+ ts: "typescript",
20
+ tsx: "tsx",
21
+ jsx: "jsx",
22
+ json: "json",
23
+ css: "css",
24
+ html: "html",
25
+ sh: "bash",
26
+ zsh: "bash",
27
+ py: "python",
28
+ rs: "rust",
29
+ go: "go",
30
+ toml: "toml",
31
+ yml: "yaml",
32
+ yaml: "yaml"
33
+ };
34
+ /** Frontmatter is the core's; the page starts after it (a page's own is cut off by the site: PageSource.body). */
35
+ const body = (source) => source.replace(FRONTMATTER, "");
36
+ /** A page's title: its first heading, else its name. */
37
+ function titleOf(file, fallback) {
38
+ try {
39
+ const h1 = /^#\s+(.+)$/m.exec(body(fs.readFileSync(file, "utf8")));
40
+ return h1 ? h1[1].replace(/[*_`]/g, "") : fallback;
41
+ } catch {
42
+ return fallback;
43
+ }
44
+ }
45
+ function crumbs(id, name, base, entry) {
46
+ const parts = id ? id.split("/") : [];
47
+ const above = entry ? html`<a href="/">workpad</a> / ` : "";
48
+ return html`${above}<a href="${base}/">${name}</a
49
+ >${parts.map((p, i) => html` / <a href="${base}/${parts.slice(0, i + 1).join("/")}">${p}</a>`)}`;
50
+ }
51
+ const time = (ts) => html`<time data-ts="${ts}"></time>`;
52
+ function pageList(pages, base) {
53
+ return html`<ul class="wp-index">
54
+ ${pages.map((p) => html`<li>
55
+ <a href="${base}/${p.id}">${titleOf(p.file, path.basename(p.id))}</a>${p.awaiting > 0 && html` <a
56
+ class="wp-awaiting"
57
+ href="${base}/${p.id}#wp-waiting"
58
+ title="${awaitingLabel(p.awaiting)}"
59
+ aria-label="${awaitingLabel(p.awaiting)}"
60
+ >${raw("<svg viewBox=\"0 0 16 16\" width=\"13\" height=\"13\" aria-hidden=\"true\"><path d=\"M2 9.5 3.8 3.2A1 1 0 0 1 4.8 2.5h6.4a1 1 0 0 1 1 .7L14 9.5V13a.5.5 0 0 1-.5.5h-11A.5.5 0 0 1 2 13Zm0 0h3.5l1 1.5h3l1-1.5H14\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"1.4\" stroke-linejoin=\"round\"/></svg>")}${p.awaiting}</a
61
+ >`}
62
+ <span class="wp-meta"
63
+ ><code>${p.id}</code> · edited
64
+ ${time(p.mtime)}${p.lastEvent && html` · last submit ${time(Date.parse(p.lastEvent))}`}</span
65
+ >
66
+ </li>`)}
67
+ </ul>`;
68
+ }
69
+ /** The folder's README, when it has one. */
70
+ function readmeOf(page) {
71
+ const name = [
72
+ "README.md",
73
+ "readme.md",
74
+ "Readme.md"
75
+ ].find((n) => page.entries.some((e) => !e.dir && e.name === n));
76
+ if (!name) return null;
77
+ const file = path.join(page.file, name);
78
+ return {
79
+ file,
80
+ source: fs.readFileSync(file, "utf8")
81
+ };
82
+ }
83
+ /** A stylesheet reference (URL, file: URL string, or absolute path) → absolute path. */
84
+ function stylePath(ref) {
85
+ if (ref == null) return null;
86
+ const file = ref instanceof URL || String(ref).startsWith("file:") ? fileURLToPath(ref) : String(ref);
87
+ if (!path.isAbsolute(file)) throw new Error(`style: use a URL, e.g. new URL('./style.css', import.meta.url)`);
88
+ if (!fs.existsSync(file)) throw new Error(`style: file not found: ${file}`);
89
+ return file;
90
+ }
91
+ const SKIP = /* @__PURE__ */ new Set([
92
+ "node_modules",
93
+ ".git",
94
+ ".workpad",
95
+ "dist",
96
+ "build"
97
+ ]);
98
+ const MARKDOWN = /\.(md|markdown)$/i;
99
+ /**
100
+ * Every markdown file in the project, which renders as a page: the top folder's first,
101
+ * then by path. In a git repository, the files git knows (tracked, or new and not ignored), so
102
+ * built copies and dependencies stay out; elsewhere a walk that skips the usual build and
103
+ * dependency folders. The project's own pages (.workpad/) are listed apart.
104
+ */
105
+ function documentsIn(dir, shows = (rel) => fileFilter().shows(rel)) {
106
+ let files;
107
+ try {
108
+ files = execFileSync("git", [
109
+ "ls-files",
110
+ "--cached",
111
+ "--others",
112
+ "--exclude-standard",
113
+ "--",
114
+ ":(icase)*.md",
115
+ ":(icase)*.markdown"
116
+ ], {
117
+ cwd: dir,
118
+ encoding: "utf8",
119
+ stdio: [
120
+ "ignore",
121
+ "pipe",
122
+ "ignore"
123
+ ]
124
+ }).split("\n").filter((f) => f && !f.startsWith(".workpad/") && fs.existsSync(path.join(dir, f)));
125
+ } catch {
126
+ files = [];
127
+ const walk = (rel) => {
128
+ let entries = [];
129
+ try {
130
+ entries = fs.readdirSync(path.join(dir, rel), { withFileTypes: true });
131
+ } catch {
132
+ return;
133
+ }
134
+ for (const e of entries) if (e.isDirectory()) {
135
+ if (!SKIP.has(e.name) && !e.name.startsWith(".")) walk(path.posix.join(rel, e.name));
136
+ } else if (MARKDOWN.test(e.name)) files.push(path.posix.join(rel, e.name));
137
+ };
138
+ walk("");
139
+ }
140
+ const depth = (f) => f.split("/").length;
141
+ return [...new Set(files)].filter(shows).toSorted((a, b) => depth(a) - depth(b) || a.localeCompare(b));
142
+ }
143
+ /**
144
+ * The overview's list of the project's documents: each by its title, linked to its rendered page,
145
+ * with when its file last changed. The list can be sorted in the browser (sort.ts): by folder (as
146
+ * written here), by when last updated, or by title; each row carries what it is sorted by.
147
+ */
148
+ function documentList(ctx) {
149
+ return documentsOf(ctx.project.dir, ctx.base ?? "", ctx.urlFor, false, ctx.project.shows);
150
+ }
151
+ /**
152
+ * A project's documents by the folder they are in, each folder a fold with how many it holds, the
153
+ * top folder's own documents first: the shared server's overview, where a repository's many
154
+ * documents would otherwise be one long list. A folder holding only one folder is shown as one
155
+ * (`docs/adr/`), so the tree is no deeper than what tells folders apart.
156
+ */
157
+ function documentTree(files, dir, base, urlFor, open = false) {
158
+ const root = {
159
+ files: [],
160
+ folders: /* @__PURE__ */ new Map(),
161
+ count: 0
162
+ };
163
+ for (const f of files) {
164
+ let at = root;
165
+ at.count++;
166
+ const parts = f.split("/");
167
+ for (const part of parts.slice(0, -1)) {
168
+ if (!at.folders.has(part)) at.folders.set(part, {
169
+ files: [],
170
+ folders: /* @__PURE__ */ new Map(),
171
+ count: 0
172
+ });
173
+ at = at.folders.get(part);
174
+ at.count++;
175
+ }
176
+ at.files.push(f);
177
+ }
178
+ const row = (f) => {
179
+ const abs = path.join(dir, f);
180
+ const name = path.posix.basename(f);
181
+ const title = titleOf(abs, name);
182
+ let mtime = 0;
183
+ try {
184
+ mtime = fs.statSync(abs).mtimeMs;
185
+ } catch {}
186
+ return html`<li>
187
+ <a href="${urlFor(abs) ?? `${base}/-/${f}`}">${title}</a>
188
+ <span class="wp-meta">${title !== name && html`<code>${name}</code> · `}edited ${time(mtime)}</span>
189
+ </li>`;
190
+ };
191
+ const folder = (name, f) => {
192
+ while (!f.files.length && f.folders.size === 1) {
193
+ const [inner, next] = [...f.folders][0];
194
+ name = `${name}/${inner}`;
195
+ f = next;
196
+ }
197
+ return html`<details class="wp-folder">
198
+ <summary>${name}/ <span class="wp-meta">${f.count}</span></summary>
199
+ ${contents(f)}
200
+ </details>`;
201
+ };
202
+ const contents = (f) => html`${f.files.length > 0 && html`<ul class="wp-index">
203
+ ${f.files.map(row)}
204
+ </ul>`}${[...f.folders].toSorted(([a], [b]) => a.localeCompare(b)).map(([name, sub]) => folder(name, sub))}`;
205
+ return html`<details class="wp-documents wp-folded-documents"${raw(open ? " open" : "")}>
206
+ <summary>Files <span class="wp-meta">${files.length}</span></summary>
207
+ ${contents(root)}
208
+ </details>`;
209
+ }
210
+ /**
211
+ * A project's documents, as its overview lists them: from its folder, linked where it serves them.
212
+ * `folded`: in a fold that says how many (the shared server's overview, where each project has them).
213
+ */
214
+ function documentsOf(dir, base, urlFor, folded = false, shows, open = false) {
215
+ const files = documentsIn(dir, shows);
216
+ if (!files.length) return "";
217
+ const ctx = {
218
+ project: { dir },
219
+ base,
220
+ urlFor
221
+ };
222
+ const rows = files.map((f, i) => {
223
+ const abs = path.join(ctx.project.dir, f);
224
+ const name = path.posix.basename(f);
225
+ const title = titleOf(abs, name);
226
+ const where = title === name ? path.posix.dirname(f) : f;
227
+ let mtime = 0;
228
+ try {
229
+ mtime = fs.statSync(abs).mtimeMs;
230
+ } catch {}
231
+ return html`<li data-order="${i}" data-updated="${mtime}" data-title="${title.toLowerCase()}">
232
+ <a href="${ctx.urlFor(abs) ?? `${ctx.base ?? ""}/-/${f}`}">${title}</a>
233
+ <span class="wp-meta"
234
+ >${where !== "." && html`<code>${where === f ? f : `${where}/`}</code> · `}edited ${time(mtime)}</span
235
+ >
236
+ </li>`;
237
+ });
238
+ if (folded) return documentTree(files, dir, base, urlFor, open);
239
+ return html`<section class="wp-documents">
240
+ <div class="wp-list-head">
241
+ <h2>Documents</h2>
242
+ <label class="wp-sort"
243
+ >sort
244
+ <select data-wp-sort="documents" aria-label="Sort the documents">
245
+ <option value="order">by folder</option>
246
+ <option value="updated">last updated</option>
247
+ <option value="title">by title</option>
248
+ </select></label
249
+ >
250
+ </div>
251
+ <ul class="wp-index">
252
+ ${rows}
253
+ </ul>
254
+ </section>`;
255
+ }
256
+ /** What waits on a page or a project, as small badges; nothing when nothing waits. */
257
+ function counts(w, href) {
258
+ const badge = (kind, n, text, title) => n > 0 && (href ? html`<a class="wp-count wp-count-${kind}" href="${href}" title="${title}">${n} ${text}</a>` : html`<span class="wp-count wp-count-${kind}" title="${title}">${n} ${text}</span>`);
259
+ if (!total(w)) return "";
260
+ return html`<span class="wp-counts"
261
+ >${badge("open", w.open, "open", w.open === 1 ? "1 question waiting for you" : `${w.open} questions waiting for you`)}${badge("unread", w.unread, "unread", w.unread === 1 ? "1 message you haven’t read" : `${w.unread} messages you haven’t read`)}${badge("unreceived", w.unreceived, "not received", w.unreceived === 1 ? "1 answer no agent received" : `${w.unreceived} answers no agent received`)}</span
262
+ >`;
263
+ }
264
+ /** A page on the board: its title, what waits on it, and (in a column of states) its project. */
265
+ function card(c, withProject) {
266
+ return html`<li class="wp-board-card${total(c.waiting) > 0 ? " wp-board-waits" : ""}">
267
+ <a class="wp-board-title" href="${c.page.url}">${c.page.title}</a>
268
+ ${counts(c.waiting, `${c.page.url}#wp-waiting`)}
269
+ ${withProject && html`<span class="wp-board-project">${c.project.label}</span>`}
270
+ <span class="wp-meta wp-board-when">edited ${time(c.page.mtime)}</span>
271
+ </li>`;
272
+ }
273
+ function cards(list, withProject) {
274
+ return list.length ? html`<ul class="wp-board-cards">
275
+ ${list.map((c) => card(c, withProject))}
276
+ </ul>` : html`<p class="wp-meta wp-board-none">Nothing here.</p>`;
277
+ }
278
+ const pagesCount = (n) => n === 1 ? "1 page" : `${n} pages`;
279
+ /** The projects without pages, folded into one column at the end. */
280
+ function emptyColumn(projects) {
281
+ if (!projects.length) return "";
282
+ return html`<details class="wp-board-column wp-board-empty">
283
+ <summary>${projects.length} without pages</summary>
284
+ <ul>
285
+ ${projects.map((p) => html`<li><a href="${p.base}/" title="${p.error ?? p.dir}">${p.label}</a></li>`)}
286
+ </ul>
287
+ </details>`;
288
+ }
289
+ /**
290
+ * The shared server's overview: its projects' pages as columns you scroll sideways, a column per
291
+ * state (what waits for you, what changed lately, the rest) or a column per project; the reader
292
+ * switches between them (board.ts in the page), both are here. Projects without pages are folded
293
+ * at the end of either.
294
+ */
295
+ function board(projects, now) {
296
+ const states = byState(projects, now);
297
+ const docsOf = new Map(withoutPages(projects).map((p) => [p, p.files ? documentsIn(p.dir).length : 0]));
298
+ const empty = withoutPages(projects);
299
+ const withDocs = [...docsOf].filter(([, n]) => n > 0).map(([p]) => p);
300
+ const bare = empty.filter((p) => !docsOf.get(p));
301
+ const hours = ACTIVE_FOR / 36e5;
302
+ const stateColumn = (key, title, note) => html`<section class="wp-board-column wp-board-${key}${key === "waiting" && states.waiting.length ? " wp-board-waits" : ""}">
303
+ <header class="wp-board-head">
304
+ <h2>${title}</h2>
305
+ <span class="wp-meta">${pagesCount(states[key].length)}${note && ` · ${note}`}</span>
306
+ </header>
307
+ ${cards(states[key], true)}
308
+ </section>`;
309
+ const projectColumn = (col) => {
310
+ const p = col.project;
311
+ return html`<section class="wp-board-column${total(col.waiting) > 0 ? " wp-board-waits" : ""}">
312
+ <header class="wp-board-head" title="${p.dir.replace(os.homedir(), "~")}">
313
+ <h2>${p.label}</h2>
314
+ <span class="wp-meta">edited ${time(p.active)} · ${pagesCount(col.cards.length)}</span>
315
+ ${counts(col.waiting, null)}
316
+ </header>
317
+ ${cards(col.cards, false)} ${p.files && documentsOf(p.dir, p.base, () => null, true)}
318
+ </section>`;
319
+ };
320
+ const docsColumn = (p) => html`<section class="wp-board-column wp-board-docs">
321
+ <header class="wp-board-head" title="${p.dir.replace(os.homedir(), "~")}">
322
+ <h2><a href="${p.base}/">${p.label}</a></h2>
323
+ <span class="wp-meta">no pages · ${docsOf.get(p)} file${docsOf.get(p) === 1 ? "" : "s"}</span>
324
+ </header>
325
+ ${documentsOf(p.dir, p.base, () => null, true, void 0, true)}
326
+ </section>`;
327
+ return html`<div class="wp-board-bar">
328
+ <div class="wp-board-switch" role="group" aria-label="Columns">
329
+ <button type="button" data-wp-board="state">By state</button
330
+ ><button type="button" data-wp-board="project">By project</button>
331
+ </div>
332
+ <input
333
+ type="search"
334
+ class="wp-board-search"
335
+ placeholder="Search pages, projects, files /"
336
+ aria-label="Search the overview"
337
+ autocomplete="off"
338
+ spellcheck="false"
339
+ />
340
+ </div>
341
+ <p class="wp-board-nomatch" hidden>Nothing here matches.</p>
342
+ <div class="wp-board" data-board="state">
343
+ ${stateColumn("waiting", "Waiting for you", "")}
344
+ ${stateColumn("active", "Active", `changed in the last ${hours} hours`)}
345
+ ${stateColumn("quiet", "Quiet", "")} ${emptyColumn(empty)}
346
+ </div>
347
+ <div class="wp-board" data-board="project">
348
+ ${byProject(projects).map(projectColumn)} ${withDocs.map(docsColumn)} ${emptyColumn(bare)}
349
+ </div>`;
350
+ }
351
+ /** The plugins' text links for a project folder, asked once per folder. */
352
+ function textLinksOf(links) {
353
+ const linksFor = /* @__PURE__ */ new Map();
354
+ return (dir) => {
355
+ let got = linksFor.get(dir);
356
+ if (!got) {
357
+ got = links.flatMap((fn) => {
358
+ try {
359
+ return fn({ dir });
360
+ } catch (err) {
361
+ console.error("links:", err);
362
+ return [];
363
+ }
364
+ });
365
+ linksFor.set(dir, got);
366
+ }
367
+ return got;
368
+ };
369
+ }
370
+ /**
371
+ * A message (a comment, a chat message) as HTML: markdown as on a page, but its raw HTML as text and
372
+ * its directives as written. Code fences go to the code block, so `styles` are what it needs linked.
373
+ */
374
+ /**
375
+ * An agent's chat message as a small page (Pages.agentMessage): every block, as on the page, in the
376
+ * chat it is in (RenderOptions.chat).
377
+ */
378
+ /** Islands as the browser loads them: each client module at the address the server serves it from. */
379
+ const served = (islands, ctx) => Object.fromEntries(Object.entries(islands).map(([id, island]) => [id, {
380
+ ...island,
381
+ client: ctx.asset(island.client)
382
+ }]));
383
+ function createAgentMessageRender(blocks, links = [], composers = /* @__PURE__ */ new Map()) {
384
+ const textLinks = textLinksOf(links);
385
+ return async (text, ctx, where) => {
386
+ const { blocks: set, code, alert } = blocks();
387
+ const out = await renderMarkdown(String(text ?? ""), {
388
+ page: where.page,
389
+ file: where.file,
390
+ resolve: (name) => set.get(name) ?? null,
391
+ code,
392
+ alert: alert?.name ?? null,
393
+ linkFor: ctx.urlFor,
394
+ textLinks: textLinks(ctx.project.dir),
395
+ composers,
396
+ chat: {
397
+ name: where.chat,
398
+ first: where.first,
399
+ prefix: where.prefix
400
+ }
401
+ });
402
+ return {
403
+ html: String(out.html),
404
+ islands: served(out.islands, ctx),
405
+ styles: out.styles.map((s) => ctx.asset(s))
406
+ };
407
+ };
408
+ }
409
+ /**
410
+ * Markdown a plugin's client asks for (Api.blocks): every block, as on the page `where.page`, files
411
+ * resolved from it, its islands' ids starting with `where.prefix`.
412
+ */
413
+ function createBlocksRender(blocks, links = [], composers = /* @__PURE__ */ new Map()) {
414
+ const textLinks = textLinksOf(links);
415
+ return async (text, ctx, where) => {
416
+ const { blocks: set, code, alert } = blocks();
417
+ const out = await renderMarkdown(String(text ?? ""), {
418
+ page: where.page,
419
+ file: where.file,
420
+ resolve: (name) => set.get(name) ?? null,
421
+ code,
422
+ alert: alert?.name ?? null,
423
+ linkFor: ctx.urlFor,
424
+ textLinks: textLinks(ctx.project.dir),
425
+ composers,
426
+ prefix: where.prefix
427
+ });
428
+ return {
429
+ html: String(out.html),
430
+ islands: served(out.islands, ctx),
431
+ styles: out.styles.map((s) => ctx.asset(s))
432
+ };
433
+ };
434
+ }
435
+ function createMessageRender(blocks, links = []) {
436
+ const textLinks = textLinksOf(links);
437
+ return async (text, ctx) => {
438
+ const out = await renderMarkdown(String(text ?? ""), {
439
+ page: "",
440
+ file: "",
441
+ resolve: (name) => {
442
+ const block = blocks().blocks.get(name) ?? null;
443
+ return block?.fences ? block : null;
444
+ },
445
+ code: blocks().code,
446
+ textLinks: textLinks(ctx.project.dir),
447
+ message: true
448
+ });
449
+ return {
450
+ html: out.html,
451
+ styles: out.styles.map((s) => ctx.asset(s))
452
+ };
453
+ };
454
+ }
455
+ /** What a block left out of a build (RenderContext.skip) is: nothing, for its directives and fences alike. */
456
+ const skipped = (name) => ({
457
+ name,
458
+ code: false,
459
+ fences: true,
460
+ alerts: false,
461
+ attrs: null,
462
+ menu: null,
463
+ props: null,
464
+ html: () => "",
465
+ open: null,
466
+ client: null,
467
+ style: null,
468
+ source: "build"
469
+ });
470
+ function createRender(blocks, style, pluginStyles = [], links = [], composers = /* @__PURE__ */ new Map()) {
471
+ const textLinks = textLinksOf(links);
472
+ const STYLE = stylePath(style);
473
+ const PLUGIN_STYLES = pluginStyles.map((s) => ({
474
+ plugin: s.plugin,
475
+ file: stylePath(s.file)
476
+ })).filter((s) => !!s.file);
477
+ /** `content`: the markdown without its frontmatter. */
478
+ async function markdown(content, page, file, ctx) {
479
+ const { blocks: set, code, alert } = blocks();
480
+ const skip = ctx.skip;
481
+ return renderMarkdown(content, {
482
+ page,
483
+ file,
484
+ resolve: (name) => {
485
+ const block = set.get(name) ?? null;
486
+ if (skip && (skip.has(name) || block && skip.has(block.source))) return {
487
+ ...skipped(name),
488
+ alerts: block?.alerts ?? false
489
+ };
490
+ return block;
491
+ },
492
+ code,
493
+ alert: alert?.name ?? null,
494
+ linkFor: ctx.urlFor,
495
+ textLinks: textLinks(ctx.project.dir),
496
+ composers: skip ? new Map([...composers].map(([name, compose]) => skip.has(name) ? [name, ({ children }) => String(children)] : [name, compose])) : composers
497
+ });
498
+ }
499
+ async function content(page, ctx) {
500
+ const { project } = ctx;
501
+ switch (page.kind) {
502
+ case "page": return markdown(page.body, page.id, page.file, ctx);
503
+ case "pages": {
504
+ if (page.projects) return {
505
+ html: html`<h1>${project.name}</h1>
506
+ ${board(page.projects, Date.now())} ${page.note && html`<p class="wp-meta">${page.note}</p>`}`,
507
+ title: project.name,
508
+ islands: {},
509
+ styles: []
510
+ };
511
+ const list = page.pages.length ? pageList(page.pages, ctx.base ?? "") : html`<p class="wp-meta">No pages yet.</p>`;
512
+ return {
513
+ html: page.id ? html`<h1>${page.id}</h1>
514
+ ${list}
515
+ <p class="wp-meta">${page.file}</p>` : html`<h1>${project.name}</h1>
516
+ ${list}${project.files && documentList(ctx)}
517
+ <p class="wp-meta">
518
+ <code>${project.dir}</code>${project.files && html` · <a href="${ctx.base ?? ""}/-/">files</a>`} ·
519
+ <a href="${ctx.base ?? ""}/_/docs">how to write pages</a>
520
+ </p>`,
521
+ title: page.id || project.name,
522
+ islands: {},
523
+ styles: []
524
+ };
525
+ }
526
+ case "folder": {
527
+ const rel = page.id.slice(2);
528
+ const rows = page.entries.map((e) => html`<li>
529
+ <a href="${encodeURIComponent(e.name) + (e.dir ? "/" : "")}">${e.name + (e.dir ? "/" : "")}</a>
530
+ </li>`);
531
+ const listing = html`<h1>${rel || project.name}</h1>
532
+ <ul class="wp-files">
533
+ ${rel && html`<li><a href="../">../</a></li>`}${rows}
534
+ </ul>
535
+ <p class="wp-meta">${page.file} · <code>${page.id}</code></p>`;
536
+ const readme = readmeOf(page);
537
+ if (!readme) return {
538
+ html: listing,
539
+ title: rel || project.name,
540
+ islands: {},
541
+ styles: []
542
+ };
543
+ const r = await markdown(body(readme.source), page.id, readme.file, ctx);
544
+ return {
545
+ ...r,
546
+ html: html`${raw(r.html)}
547
+ <hr />
548
+ ${listing}`
549
+ };
550
+ }
551
+ case "file": {
552
+ const { code } = blocks();
553
+ const ext = path.extname(page.file).slice(1).toLowerCase();
554
+ const tree = {
555
+ type: "root",
556
+ children: [{
557
+ type: "code",
558
+ lang: CODE_LANG[ext] ?? (ext || null),
559
+ meta: null,
560
+ value: page.source
561
+ }]
562
+ };
563
+ const out = await renderMarkdown(tree, {
564
+ page: page.id,
565
+ file: page.file,
566
+ resolve: () => null,
567
+ code,
568
+ linkFor: ctx.urlFor
569
+ });
570
+ return {
571
+ html: html`<h1 class="wp-code-title wp-wide">${page.id.slice(2)}</h1>
572
+ <div class="wp-code wp-wide">${raw(out.html)}</div>`,
573
+ title: path.basename(page.file),
574
+ islands: out.islands,
575
+ styles: out.styles
576
+ };
577
+ }
578
+ }
579
+ }
580
+ return async function render(page, ctx) {
581
+ let out;
582
+ try {
583
+ out = await content(page, ctx);
584
+ } catch (err) {
585
+ out = {
586
+ html: html`<pre class="wp-error">${err.message}</pre>`,
587
+ islands: {},
588
+ styles: []
589
+ };
590
+ }
591
+ const islands = Object.fromEntries(Object.entries(out.islands).map(([id, island]) => [id, {
592
+ ...island,
593
+ client: ctx.asset(island.client)
594
+ }]));
595
+ const styles = out.styles.map((s) => ctx.asset(s));
596
+ const nav = ctx.live === false ? html`<nav class="wp-nav">
597
+ <span>${ctx.project.name}</span><span data-wp-slot="header"></span
598
+ ><button
599
+ type="button"
600
+ class="wp-theme"
601
+ title="Theme: follow the system, light or dark"
602
+ aria-label="Theme"
603
+ ></button>
604
+ </nav>` : html`<nav class="wp-nav">
605
+ <span>${crumbs(page.id, ctx.entry?.label || ctx.project.name, ctx.base ?? "", ctx.entry)}</span
606
+ >${page.mtime != null && html`<span class="wp-meta">updated <time id="wp-updated" data-ts="${page.mtime}"></time></span>`}<span
607
+ data-wp-slot="header"
608
+ ></span
609
+ ><button
610
+ type="button"
611
+ class="wp-theme"
612
+ title="Theme: follow the system, light or dark"
613
+ aria-label="Theme"
614
+ ></button
615
+ ><span class="wp-live" role="img" title="Connecting…" aria-label="Connecting…"></span>
616
+ </nav>`;
617
+ const links = [
618
+ ctx.asset(BASE_CSS),
619
+ ...PLUGIN_STYLES.filter((s) => !ctx.skip?.has(s.plugin)).map((s) => ctx.asset(s.file)),
620
+ ...styles,
621
+ ...STYLE ? [ctx.asset(STYLE)] : []
622
+ ];
623
+ return {
624
+ title: out.title || page.id || ctx.project.name,
625
+ head: html`${THEME_SCRIPT} ${links.map((href) => html`<link rel="stylesheet" href="${href}" />`)}`,
626
+ html: html`${nav}
627
+ <div class="wp-body"><main id="wp-main">${raw(out.html)}</main></div>`,
628
+ data: {
629
+ islands,
630
+ styles,
631
+ mtime: page.mtime
632
+ },
633
+ problems: out.problems ?? []
634
+ };
635
+ };
636
+ }
637
+ /** The menu's groups, in order: what an agent writing a page is trying to do. */
638
+ const GROUPS = [
639
+ ["ask", "Ask: get something back from the reader"],
640
+ ["show", "Show: put something in front of the reader"],
641
+ ["arrange", "Arrange: several things, or one out of the way"],
642
+ ["mark", "Mark: how something stands, or how much it counts"]
643
+ ];
644
+ /**
645
+ * What `workpad docs` prints first: how pages are written, then the menu: a line per block that
646
+ * has one (Block.menu), under what it is for. How to write each is in its plugin's docs.
647
+ */
648
+ function createDocs(blocks) {
649
+ return function docs(_ctx) {
650
+ const all = [...blocks().blocks.values()].filter((t) => t.menu);
651
+ const width = Math.max(0, ...all.map((t) => t.name.length)) + 3;
652
+ return `# Pages
653
+
654
+ 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.
655
+
656
+ \`\`\`\`text
657
+ ${GROUPS.map(([group, title]) => {
658
+ const lines = all.filter((t) => t.menu.group === group).map((t) => ` ${t.name.padEnd(width)}${t.menu.summary}`);
659
+ return lines.length ? `${title}\n${lines.join("\n")}` : "";
660
+ }).filter(Boolean).join("\n\n")}
661
+ \`\`\`\`
662
+ `;
663
+ };
664
+ }
665
+ export { createAgentMessageRender, createBlocksRender, createDocs, createMessageRender, createRender, titleOf };