@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
package/src/migrate.ts ADDED
@@ -0,0 +1,352 @@
1
+ // `workpad migrate`: pages written for an older workpad, rewritten to the syntax this one reads.
2
+ // Each migration is one syntax change, applied to the page's parsed markdown (so code fences and
3
+ // inline code, where a page shows syntax as an example, are never touched), and a page it has
4
+ // already done is one it leaves as it is: running it again changes nothing. A release that renames
5
+ // something adds its migration at the end of MIGRATIONS; nothing in a page records which ran.
6
+ import fs from 'node:fs'
7
+ import path from 'node:path'
8
+ import { parseMarkdown, parseAttrs, type MdNode } from './pages/markdown.ts'
9
+
10
+ export interface Migration {
11
+ /** a short name, for the summary: `steps → tabs` */
12
+ name: string
13
+ /** what it rewrites, a line */
14
+ description: string
15
+ /** The page's markdown rewritten, and how many places it changed (0: the page as it was). */
16
+ apply(source: string): { source: string; changes: number }
17
+ }
18
+
19
+ const start = (n: MdNode): number => n.position.start.offset
20
+ const end = (n: MdNode): number => n.position.end.offset
21
+
22
+ /** Every node of the tree (depth first, in source order) that `match` picks. */
23
+ function nodes(tree: MdNode, match: (n: MdNode) => boolean): MdNode[] {
24
+ const found: MdNode[] = []
25
+ const walk = (n: MdNode) => {
26
+ if (match(n)) found.push(n)
27
+ n.children?.forEach(walk)
28
+ }
29
+ walk(tree)
30
+ return found
31
+ }
32
+
33
+ interface Edit {
34
+ from: number
35
+ to: number
36
+ text: string
37
+ }
38
+
39
+ /** `source` with the edits made (they don't overlap). */
40
+ function edit(source: string, edits: Edit[]): string {
41
+ let out = source
42
+ for (const e of [...edits].sort((a, b) => b.from - a.from)) out = out.slice(0, e.from) + e.text + out.slice(e.to)
43
+ return out
44
+ }
45
+
46
+ const directive =
47
+ (name: string, type = 'containerDirective') =>
48
+ (n: MdNode) =>
49
+ n.type === type && n.name === name
50
+
51
+ /** What is before `offset` on its line, when that is only indentation and quote markers (` > `); else ''. */
52
+ function prefixAt(source: string, offset: number): string {
53
+ const before = source.slice(source.lastIndexOf('\n', offset - 1) + 1, offset)
54
+ return /^[ \t>]*$/.test(before) ? before : ''
55
+ }
56
+
57
+ /** The end of the line `offset` is on (before its newline). */
58
+ function lineEnd(source: string, offset: number): number {
59
+ const at = source.indexOf('\n', offset)
60
+ return at < 0 ? source.length : at
61
+ }
62
+
63
+ /** The label and the content of a container: remark-directive keeps the label as a flagged first paragraph. */
64
+ function parts(node: MdNode): { label: MdNode | null; kids: MdNode[] } {
65
+ const [head, ...rest] = node.children ?? []
66
+ return head?.data?.directiveLabel ? { label: head, kids: rest } : { label: null, kids: node.children ?? [] }
67
+ }
68
+
69
+ /** The label of a container as written, between its brackets; '' without one. */
70
+ function labelSource(source: string, label: MdNode | null): string {
71
+ const kids = label?.children ?? []
72
+ return kids.length ? source.slice(start(kids[0]), end(kids[kids.length - 1])) : ''
73
+ }
74
+
75
+ // ---- steps → tabs --------------------------------------------------------------------------------
76
+
77
+ /** `:::steps` is `:::tabs` (its `count` is gone), and a link's `#step:` is `#tab:`. */
78
+ const stepsToTabs: Migration = {
79
+ name: 'steps → tabs',
80
+ description: ':::steps is :::tabs (count dropped); a link to #step:x goes to #tab:x',
81
+ apply(source) {
82
+ const tree = parseMarkdown(source)
83
+ const edits: Edit[] = []
84
+ for (const n of nodes(tree, directive('steps'))) {
85
+ const from = start(n)
86
+ const to = lineEnd(source, from)
87
+ const line = source
88
+ .slice(from, to)
89
+ .replace(/^(:+)steps/, '$1tabs')
90
+ // the attributes at the line's end: count goes, and with it braces left empty
91
+ .replace(/\{([^}]*)\}(\s*)$/, (_, attrs: string, space: string) => {
92
+ const kept = attrs.replace(/(^|\s)count(=("[^"]*"|'[^']*'|[^\s}]*))?(?=\s|$)/g, '').trim()
93
+ return kept ? `{${kept}}${space}` : space
94
+ })
95
+ edits.push({ from, to, text: line })
96
+ }
97
+ for (const n of nodes(tree, n => (n.type === 'link' || n.type === 'definition') && /#step:/.test(n.url ?? ''))) {
98
+ const text = source.slice(start(n), end(n))
99
+ edits.push({ from: start(n), to: end(n), text: text.replaceAll('#step:', '#tab:') })
100
+ }
101
+ return { source: edit(source, edits), changes: edits.length }
102
+ },
103
+ }
104
+
105
+ // ---- callout → alert -----------------------------------------------------------------------------
106
+
107
+ const CALLOUT_TYPES = new Set(['note', 'tip', 'success', 'warning', 'danger', 'question'])
108
+
109
+ /** A `:::callout` as the alert it is now: `> [!TYPE] Title` and its lines behind `> `. */
110
+ function alertFor(source: string, node: MdNode): string {
111
+ const { label, kids } = parts(node)
112
+ const attrs = node.attributes ?? {}
113
+ const type = [attrs.type, ...String(attrs.class ?? '').split(/\s+/)].find(t => t && CALLOUT_TYPES.has(t)) ?? 'note'
114
+ const title = labelSource(source, label).trim()
115
+ const prefix = prefixAt(source, start(node))
116
+ const lines = [`[!${type.toUpperCase()}]${title ? ` ${title}` : ''}`]
117
+ if (kids.length) {
118
+ // its content's lines, from the line after the opening one, without the indentation they share
119
+ // with the container (a quote's markers included)
120
+ const from = lineEnd(source, start(node)) + 1
121
+ const content = source.slice(from, end(kids[kids.length - 1]))
122
+ for (const line of content.split('\n')) {
123
+ const own = line.startsWith(prefix) ? line.slice(prefix.length) : line.trimStart()
124
+ lines.push(own)
125
+ }
126
+ }
127
+ // the first line goes where the container started (its prefix is already there), the rest each behind it
128
+ return lines.map((l, i) => `${i ? prefix : ''}${l ? `> ${l}` : '>'}`.replace(/[ \t]+$/, '')).join('\n')
129
+ }
130
+
131
+ /** `:::callout[Title]{.warning}` is a GitHub alert, `> [!WARNING] Title`. */
132
+ const calloutToAlert: Migration = {
133
+ name: 'callout → alert',
134
+ description: ':::callout[Title]{.warning} is > [!WARNING] Title (or type=…; a NOTE without)',
135
+ apply(source) {
136
+ let changes = 0
137
+ // innermost first, then again: a callout in a callout becomes an alert in an alert
138
+ for (;;) {
139
+ const tree = parseMarkdown(source)
140
+ const callouts = nodes(tree, directive('callout'))
141
+ const innermost = callouts.filter(c => !callouts.some(d => d !== c && start(d) >= start(c) && end(d) <= end(c)))
142
+ if (!innermost.length) break
143
+ source = edit(
144
+ source,
145
+ innermost.map(n => ({ from: start(n), to: end(n), text: alertFor(source, n) })),
146
+ )
147
+ changes += innermost.length
148
+ }
149
+ return { source, changes }
150
+ },
151
+ }
152
+
153
+ // ---- items ---------------------------------------------------------------------------------------
154
+
155
+ const ITEM_BLOCKS = new Set(['grid', 'tabs', 'timeline'])
156
+ /** Directly in a block of items, these are an item as they are. */
157
+ const AS_IS = new Set(['form', 'details'])
158
+ const FENCE = /^[ \t>]*(`{3,}|~{3,})/
159
+
160
+ /** The most colons a container fence in `text` has, outside code fences; 0 for none. */
161
+ function maxColons(text: string): number {
162
+ let most = 0
163
+ let open: { char: string; length: number } | null = null
164
+ for (const line of text.split('\n')) {
165
+ const fence = FENCE.exec(line)
166
+ if (open) {
167
+ if (fence && fence[1][0] === open.char && fence[1].length >= open.length && !line.slice(fence[0].length).trim())
168
+ open = null
169
+ continue
170
+ }
171
+ if (fence) {
172
+ open = { char: fence[1][0], length: fence[1].length }
173
+ continue
174
+ }
175
+ const colons = /^[ \t>]*(:{3,})/.exec(line)
176
+ if (colons) most = Math.max(most, colons[1].length)
177
+ }
178
+ return most
179
+ }
180
+
181
+ /** A container's first line with at least `colons` colons, and its closing line likewise. */
182
+ function withColons(text: string, colons: number, closed: boolean): string {
183
+ const lines = text.split('\n')
184
+ const more = (line: string) =>
185
+ line.replace(
186
+ /^([ \t>]*)(:{3,})/,
187
+ (_, space: string, has: string) => space + ':'.repeat(Math.max(has.length, colons)),
188
+ )
189
+ lines[0] = more(lines[0])
190
+ if (closed && lines.length > 1) lines[lines.length - 1] = more(lines[lines.length - 1])
191
+ return lines.join('\n')
192
+ }
193
+
194
+ /**
195
+ * The items of a grid, tabs or a timeline as `:::item[Title]` containers, where a heading at the
196
+ * block's shallowest heading level started one (titled by it) and a `---` did (untitled). What came
197
+ * before the first is an untitled item; a lone form or details is an item as it is. Every container
198
+ * around them takes the colons it needs now (only ever more than it had).
199
+ */
200
+ function itemsIn(source: string): { source: string; changes: number } {
201
+ const tree = parseMarkdown(source)
202
+ let changes = 0
203
+
204
+ /** The containers in `node` that aren't inside another: what `rebuild` emits afresh. */
205
+ const containersIn = (node: MdNode): MdNode[] =>
206
+ (node.children ?? []).flatMap(c => (c.type === 'containerDirective' ? [c] : containersIn(c)))
207
+
208
+ /** source[from, to), its containers emitted afresh. */
209
+ function rebuild(from: number, to: number, within: MdNode[]): string {
210
+ const inside = within.flatMap(n => (n.type === 'containerDirective' ? [n] : containersIn(n)))
211
+ let out = ''
212
+ let at = from
213
+ for (const c of inside.filter(c => start(c) >= from && end(c) <= to).sort((a, b) => start(a) - start(b))) {
214
+ out += source.slice(at, start(c)) + emit(c)
215
+ at = end(c)
216
+ }
217
+ return out + source.slice(at, to)
218
+ }
219
+
220
+ function emit(node: MdNode): string {
221
+ const { kids } = parts(node)
222
+ const last = kids[kids.length - 1]
223
+ // closed: it ends in a closing fence of its own (else it ends where what holds it does)
224
+ const closed = /\n[ \t>]*:{3,}[ \t]*$/.test(source.slice(start(node), end(node)))
225
+ const already = kids.every(k => k.type === 'containerDirective' && (k.name === 'item' || AS_IS.has(k.name)))
226
+ if (!ITEM_BLOCKS.has(node.name) || already) {
227
+ if (!kids.length) return source.slice(start(node), end(node))
228
+ const body = rebuild(start(kids[0]), end(last), kids)
229
+ const text = source.slice(start(node), start(kids[0])) + body + source.slice(end(last), end(node))
230
+ return withColons(text, maxColons(body) + 1, closed)
231
+ }
232
+
233
+ changes++
234
+ const prefix = prefixAt(source, start(node))
235
+ const headings = kids.filter(k => k.type === 'heading')
236
+ const depth = headings.length ? Math.min(...headings.map(h => h.depth)) : null
237
+ const groups: { heading: MdNode | null; body: MdNode[] }[] = []
238
+ let current: { heading: MdNode | null; body: MdNode[] } | null = null
239
+ for (const k of kids) {
240
+ if (k.type === 'heading' && k.depth === depth) groups.push((current = { heading: k, body: [] }))
241
+ else if (k.type === 'thematicBreak') groups.push((current = { heading: null, body: [] }))
242
+ else {
243
+ if (!current) groups.push((current = { heading: null, body: [] }))
244
+ current.body.push(k)
245
+ }
246
+ }
247
+ const items = groups
248
+ .filter(g => g.heading || g.body.length)
249
+ .map(g => {
250
+ const body = g.body.length ? rebuild(start(g.body[0]), end(g.body[g.body.length - 1]), g.body) : ''
251
+ const lone = g.body[0]
252
+ if (!g.heading && g.body.length === 1 && lone.type === 'containerDirective' && AS_IS.has(lone.name)) return body
253
+ const label = g.heading ? labelSource(source, g.heading) : ''
254
+ const colons = ':'.repeat(Math.max(3, maxColons(body) + 1))
255
+ return `${colons}item${label ? `[${label}]` : ''}\n${body ? `${prefix}${body}\n` : ''}${prefix}${colons}`
256
+ })
257
+ const content = items.join(`\n${prefix.trimEnd()}\n${prefix}`)
258
+ const open = source.slice(start(node), lineEnd(source, start(node)))
259
+ const text = `${open}\n${prefix}${content}${closed ? `\n${prefix}:::` : ''}`
260
+ return withColons(text, maxColons(content) + 1, closed)
261
+ }
262
+
263
+ return { source: rebuild(0, source.length, tree.children ?? []), changes }
264
+ }
265
+
266
+ /** A grid's, tabs' or timeline's items split at headings and `---` are `:::item[Title]` containers. */
267
+ const items: Migration = {
268
+ name: 'items',
269
+ description: 'grid, tabs and timeline items started by a heading or --- are :::item[Title] containers',
270
+ apply: itemsIn,
271
+ }
272
+
273
+ // ---- :check → :checkbox --------------------------------------------------------------------------
274
+
275
+ /** The form's check input is `:checkbox`. */
276
+ const checkToCheckbox: Migration = {
277
+ name: 'check → checkbox',
278
+ description: "a form's :check[…] is :checkbox[…]",
279
+ apply(source) {
280
+ const found = nodes(parseMarkdown(source), directive('check', 'textDirective')).filter(n =>
281
+ source.startsWith(':check', start(n)),
282
+ )
283
+ return {
284
+ source: edit(
285
+ source,
286
+ found.map(n => ({ from: start(n), to: start(n) + ':check'.length, text: ':checkbox' })),
287
+ ),
288
+ changes: found.length,
289
+ }
290
+ },
291
+ }
292
+
293
+ // ---- ansi title → compose=window -----------------------------------------------------------------
294
+
295
+ /** An ansi fence's `title` only labels now: the window it made is `compose=window`. */
296
+ const ansiWindow: Migration = {
297
+ name: 'ansi window',
298
+ description: 'an ```ansi fence with a title gets compose=window, to stay a window',
299
+ apply(source) {
300
+ const fences = nodes(parseMarkdown(source), n => {
301
+ if (n.type !== 'code' || n.lang !== 'ansi') return false
302
+ const attrs = parseAttrs(n.meta)
303
+ return 'title' in attrs && !('compose' in attrs)
304
+ })
305
+ const edits = fences.flatMap(n => {
306
+ const from = start(n)
307
+ const line = source.slice(from, lineEnd(source, from))
308
+ const at = line.search(/(^|[\s{])title\b/)
309
+ if (at < 0) return []
310
+ const where = from + at + (/[\s{]/.test(line[at]) ? 1 : 0)
311
+ return [{ from: where, to: where, text: 'compose=window ' }]
312
+ })
313
+ return { source: edit(source, edits), changes: edits.length }
314
+ },
315
+ }
316
+
317
+ /**
318
+ * The migrations, in the order they run: each release's renames at the end. steps → tabs comes
319
+ * before items (tabs take items), and callouts are quotes before items count colons.
320
+ */
321
+ export const MIGRATIONS: Migration[] = [stepsToTabs, calloutToAlert, items, checkToCheckbox, ansiWindow]
322
+
323
+ /** A page's markdown in the current syntax, and what changed it: each migration that did, with how often. */
324
+ export function migrate(
325
+ source: string,
326
+ migrations: Migration[] = MIGRATIONS,
327
+ ): { source: string; applied: { migration: Migration; changes: number }[] } {
328
+ const applied: { migration: Migration; changes: number }[] = []
329
+ for (const migration of migrations) {
330
+ const result = migration.apply(source)
331
+ if (result.source !== source) applied.push({ migration, changes: Math.max(1, result.changes) })
332
+ source = result.source
333
+ }
334
+ return { source, applied }
335
+ }
336
+
337
+ /** The markdown files of a file or folder (in a folder: those below it, dot folders and node_modules left out). */
338
+ export function markdownFiles(target: string): string[] {
339
+ const stat = fs.statSync(target)
340
+ if (stat.isFile()) return [target]
341
+ return fs
342
+ .readdirSync(target, { withFileTypes: true })
343
+ .filter(e => !e.name.startsWith('.') && e.name !== 'node_modules')
344
+ .sort((a, b) => a.name.localeCompare(b.name))
345
+ .flatMap(e =>
346
+ e.isDirectory()
347
+ ? markdownFiles(path.join(target, e.name))
348
+ : e.name.endsWith('.md')
349
+ ? [path.join(target, e.name)]
350
+ : [],
351
+ )
352
+ }
package/src/package.ts ADDED
@@ -0,0 +1,62 @@
1
+ // Where this package is on disk. Its modules run from source in the repository and from dist/ once
2
+ // built, at different depths, so a path into the package is found from its root, never by counting
3
+ // folders up from a module.
4
+ import fs from 'node:fs'
5
+ import path from 'node:path'
6
+ import { fileURLToPath } from 'node:url'
7
+
8
+ function findRoot(from: string): string {
9
+ for (let dir = from; ; dir = path.dirname(dir)) {
10
+ try {
11
+ if (JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf8')).name === '@bigmistqke/workpad')
12
+ return dir
13
+ } catch {
14
+ /* no package.json here, or not ours */
15
+ }
16
+ if (path.dirname(dir) === dir)
17
+ throw new Error(`workpad: no package.json of @bigmistqke/workpad above ${from}`)
18
+ }
19
+ }
20
+
21
+ /** The package's folder: where its package.json is. */
22
+ export const PACKAGE_DIR = findRoot(path.dirname(fileURLToPath(import.meta.url)))
23
+
24
+ /** A path inside the package. */
25
+ export const packagePath = (...parts: string[]) => path.join(PACKAGE_DIR, ...parts)
26
+
27
+ /**
28
+ * Is the engine running from its TypeScript source (`bin/workpad.ts`), rather than from its build
29
+ * (`dist/bin/workpad.js`, what an installed package runs)? Then what a config imports from the
30
+ * package comes from source too (the `source` export conditions), so the two never mix.
31
+ */
32
+ export const FROM_SOURCE = fileURLToPath(import.meta.url).endsWith('.ts')
33
+
34
+ /**
35
+ * A browser file built from the module at `moduleUrl`'s folder: `name` in that folder's place in
36
+ * dist/. The same whether the module runs from source (plugins/form/index.ts gives
37
+ * dist/plugins/form/client.js) or from dist/ itself.
38
+ */
39
+ /** A built-in plugin's public folder: its built browser modules and copied styles (build.ts). */
40
+ export const builtinPublic = (name: string): string => packagePath('dist', 'plugins', name, 'public')
41
+
42
+ /** This package's version, from its package.json: what a shared server records, and its takeover compares. */
43
+ export const VERSION: string = JSON.parse(fs.readFileSync(path.join(PACKAGE_DIR, 'package.json'), 'utf8')).version
44
+
45
+ /** Compare two versions, x.y.z with an optional -pre: < 0 when a is older, 0 when the same, > 0 when newer. */
46
+ export function compareVersions(a: string, b: string): number {
47
+ const parse = (v: string) => {
48
+ const [main, pre] = String(v).split('-', 2)
49
+ return { nums: main!.split('.').map(n => Number(n) || 0), pre: pre ?? null }
50
+ }
51
+ const x = parse(a)
52
+ const y = parse(b)
53
+ for (let i = 0; i < Math.max(x.nums.length, y.nums.length); i++) {
54
+ const d = (x.nums[i] ?? 0) - (y.nums[i] ?? 0)
55
+ if (d) return Math.sign(d)
56
+ }
57
+ // a release is newer than its pre-releases
58
+ if (x.pre === y.pre) return 0
59
+ if (x.pre == null) return 1
60
+ if (y.pre == null) return -1
61
+ return x.pre < y.pre ? -1 : 1
62
+ }
@@ -0,0 +1,106 @@
1
+ // The blocks of a project's plugins, checked and resolved into what the markdown engine uses.
2
+ import fs from 'node:fs'
3
+ import path from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
5
+ import type { Html } from '../html.ts'
6
+ import type { Block, BlockGroup } from '../define.ts'
7
+ import { inPublic } from '../public.ts'
8
+
9
+ /** A block as the plugins bring it: its name, and the plugin it comes from. */
10
+ /** A block of a plugin, with the plugin's public folder, where its files are. */
11
+ export interface BlockEntry {
12
+ name: string
13
+ block: Block<any>
14
+ plugin: string
15
+ public: string
16
+ }
17
+
18
+ const NAME = /^[\w-]+$/
19
+
20
+ /** A block as the engine uses it: checked, with its files as absolute paths. */
21
+ export interface ResolvedBlock {
22
+ name: string
23
+ /** the block for code in general (`fence: 'any'`) */
24
+ code: boolean
25
+ /** it renders the code fences of its name (`fence: true` or `'any'`) */
26
+ fences: boolean
27
+ /** it renders `> [!TYPE]` alerts, and is no directive (Block.alert) */
28
+ alerts: boolean
29
+ /** the attributes it reads, when it says (Block.attrs) */
30
+ attrs: string[] | null
31
+ /** its line on the docs menu (Block.menu) */
32
+ menu: { group: BlockGroup; summary: string } | null
33
+ props: ((ctx: any) => unknown) | null
34
+ html: ((ctx: any) => string | Html | null | Promise<string | Html | null>) | null
35
+ /** what it shows on its own, for the ↗ of a window around it (Block.open) */
36
+ open: ((ctx: any) => string | null) | null
37
+ client: string | null
38
+ style: string | null
39
+ /** the plugin it comes from */
40
+ source: string
41
+ }
42
+
43
+ export interface ResolvedBlocks {
44
+ blocks: Map<string, ResolvedBlock>
45
+ code: ResolvedBlock | null
46
+ /** the block for `> [!TYPE]` alerts: the last with `alert: true` */
47
+ alert: ResolvedBlock | null
48
+ }
49
+
50
+ const GROUPS: readonly BlockGroup[] = ['ask', 'show', 'arrange', 'mark']
51
+
52
+ /** A block's line on the docs menu, checked: a group of the four, and a summary. */
53
+ function menuOf(menu: unknown, where: string): { group: BlockGroup; summary: string } | null {
54
+ if (menu == null) return null
55
+ const m = menu as { group?: unknown; summary?: unknown }
56
+ if (!GROUPS.includes(m.group as BlockGroup))
57
+ throw new Error(`${where}: menu.group is ${JSON.stringify(m.group)}, one of ${GROUPS.join(', ')}`)
58
+ if (typeof m.summary !== 'string' || !m.summary.trim()) throw new Error(`${where}: menu.summary is empty`)
59
+ return { group: m.group as BlockGroup, summary: m.summary.trim() }
60
+ }
61
+
62
+ /** Check and resolve one block into what the engine uses: its files in its plugin's public folder. */
63
+ function normalize({ name, block: b, plugin, public: dir }: BlockEntry): ResolvedBlock {
64
+ if (typeof b !== 'object' || !b) throw new Error(`plugin ${plugin}: block "${name}" is not an object`)
65
+ if (!NAME.test(name))
66
+ throw new Error(`plugin ${plugin}: block name ${JSON.stringify(name)}: use letters, digits, - and _`)
67
+ const file = (ref: URL | string | undefined, field: string) =>
68
+ ref == null ? null : inPublic(dir, ref, `plugin ${plugin}, block "${name}": ${field}`)
69
+ const files = { client: file(b.client, 'client'), style: file(b.style, 'style') }
70
+ for (const [field, file] of Object.entries(files))
71
+ if (file && !fs.existsSync(file))
72
+ throw new Error(`plugin ${plugin}, block "${name}": ${field} file not found: ${file}`)
73
+ return {
74
+ name,
75
+ code: b.fence === 'any',
76
+ fences: b.fence === true || b.fence === 'any',
77
+ alerts: b.alert === true,
78
+ attrs: Array.isArray(b.attrs) ? b.attrs.map(String) : null,
79
+ menu: menuOf(b.menu, `plugin ${plugin}, block "${name}"`),
80
+ props: typeof b.props === 'function' ? b.props.bind(b) : null,
81
+ html: typeof b.html === 'function' ? b.html.bind(b) : null,
82
+ open: typeof b.open === 'function' ? b.open.bind(b) : null,
83
+ ...files,
84
+ source: plugin,
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Resolve the plugins' blocks. A block name is one plugin's: two plugins bringing the same block is
90
+ * an error. `code` is the block for code fences no block claims by name: the last one with
91
+ * `fence: 'any'`.
92
+ */
93
+ export function resolveBlocks(entries: BlockEntry[]): ResolvedBlocks {
94
+ const blocks = new Map<string, ResolvedBlock>()
95
+ for (const entry of entries) {
96
+ const n = normalize(entry)
97
+ const other = blocks.get(n.name)
98
+ if (other) throw new Error(`block "${n.name}" comes from two plugins: ${other.source} and ${n.source}`)
99
+ blocks.set(n.name, n)
100
+ }
101
+ return {
102
+ blocks,
103
+ code: [...blocks.values()].findLast(t => t.code) ?? null,
104
+ alert: [...blocks.values()].findLast(t => t.alerts) ?? null,
105
+ }
106
+ }
@@ -0,0 +1,89 @@
1
+ // The shared server's overview as columns, side by side (see the overview in DESIGN.md): what goes
2
+ // in which column, with nothing of HTML or the disk in it. Two ways to lay the same projects out:
3
+ // a column per state (what waits for the reader, what changed lately, the rest), the pages of every
4
+ // project mixed; or a column per project, its pages under it. Projects without pages go in neither:
5
+ // they are folded apart, at the end.
6
+ import type { IndexPage, IndexProject } from '../define.ts'
7
+
8
+ /** A page changed this recently, with nothing waiting on it, is active; older, quiet. */
9
+ export const ACTIVE_FOR = 3 * 60 * 60 * 1000
10
+
11
+ /** What waits for the reader on a page: open questions, unread messages, answers no agent received. */
12
+ export interface Waiting {
13
+ open: number
14
+ unread: number
15
+ unreceived: number
16
+ }
17
+
18
+ export const waitingOn = (p: IndexPage): Waiting => ({
19
+ open: p.open.length,
20
+ unread: p.unread,
21
+ unreceived: p.unreceived,
22
+ })
23
+
24
+ export const total = (w: Waiting): number => w.open + w.unread + w.unreceived
25
+
26
+ /** A page on the board, with the project it is in. */
27
+ export interface Card<P extends IndexProject = IndexProject> {
28
+ page: IndexPage
29
+ project: P
30
+ waiting: Waiting
31
+ }
32
+
33
+ export interface StateColumns<P extends IndexProject = IndexProject> {
34
+ /** something on it waits for the reader */
35
+ waiting: Card<P>[]
36
+ /** nothing waits, and it changed within `activeFor` */
37
+ active: Card<P>[]
38
+ quiet: Card<P>[]
39
+ }
40
+
41
+ /** Every project's pages by state, each column newest first. */
42
+ export function byState<P extends IndexProject>(
43
+ projects: readonly P[],
44
+ now: number,
45
+ activeFor: number = ACTIVE_FOR,
46
+ ): StateColumns<P> {
47
+ const columns: StateColumns<P> = { waiting: [], active: [], quiet: [] }
48
+ for (const project of projects)
49
+ for (const page of project.pages) {
50
+ const card = { page, project, waiting: waitingOn(page) }
51
+ const column = total(card.waiting) > 0 ? 'waiting' : now - page.mtime <= activeFor ? 'active' : 'quiet'
52
+ columns[column].push(card)
53
+ }
54
+ for (const cards of [columns.waiting, columns.active, columns.quiet]) cards.sort((a, b) => b.page.mtime - a.page.mtime)
55
+ return columns
56
+ }
57
+
58
+ /** A project's column: its pages, newest first, and what waits on them all. */
59
+ export interface ProjectColumn<P extends IndexProject = IndexProject> {
60
+ project: P
61
+ cards: Card<P>[]
62
+ waiting: Waiting
63
+ }
64
+
65
+ /** The projects with pages, a column each, the one changed last first. */
66
+ export function byProject<P extends IndexProject>(projects: readonly P[]): ProjectColumn<P>[] {
67
+ return projects
68
+ .filter(p => p.pages.length)
69
+ .toSorted((a, b) => b.active - a.active)
70
+ .map(project => {
71
+ const cards = project.pages
72
+ .map(page => ({ page, project, waiting: waitingOn(page) }))
73
+ .sort((a, b) => b.page.mtime - a.page.mtime)
74
+ const waiting = cards.reduce(
75
+ (sum, c) => ({
76
+ open: sum.open + c.waiting.open,
77
+ unread: sum.unread + c.waiting.unread,
78
+ unreceived: sum.unreceived + c.waiting.unreceived,
79
+ }),
80
+ { open: 0, unread: 0, unreceived: 0 },
81
+ )
82
+ return { project, cards, waiting }
83
+ })
84
+ }
85
+
86
+ /** The projects without pages, folded at the end of either layout: by name. */
87
+ export function withoutPages<P extends IndexProject>(projects: readonly P[]): P[] {
88
+ return projects.filter(p => !p.pages.length).toSorted((a, b) => a.label.localeCompare(b.label))
89
+ }