@bigmistqke/workpad 0.0.0-stage → 0.18.1

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 +1526 -0
  4. package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
  5. package/dist/bin/workpad.js +1202 -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 +2121 -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 +213 -0
  103. package/dist/src/html.js +34 -0
  104. package/dist/src/hub.js +348 -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 +761 -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 +592 -0
  213. package/solid/index.tsx +80 -0
  214. package/src/agents.ts +134 -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 +166 -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 +240 -0
  251. package/src/html.ts +44 -0
  252. package/src/hub.ts +435 -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 +845 -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,138 @@
1
+ .wp-timeline {
2
+ margin: var(--gap-block) 0;
3
+ text-align: left;
4
+ }
5
+ .wp-tl-caption {
6
+ font-family: var(--sans);
7
+ font-size: var(--ui-small);
8
+ color: var(--muted);
9
+ text-transform: uppercase;
10
+ letter-spacing: 0.06em;
11
+ margin-bottom: var(--space-2xl);
12
+ }
13
+ .wp-tl-steps {
14
+ list-style: none;
15
+ margin: 0;
16
+ padding: 0;
17
+ }
18
+
19
+ /* a line down the left, a dot per step */
20
+ .wp-tl-steps {
21
+ position: relative;
22
+ padding-left: 28px;
23
+ }
24
+ .wp-tl-steps::before {
25
+ content: '';
26
+ position: absolute;
27
+ left: 7px;
28
+ top: 6px;
29
+ bottom: 6px;
30
+ width: 2px;
31
+ background: var(--border);
32
+ }
33
+ .wp-tl-step {
34
+ position: relative;
35
+ }
36
+ /* between steps: a subsection's distance, at the rhythm inside a step (base.css), more than a
37
+ step's title is from its body */
38
+ .wp-tl-step:not(:last-child) > :last-child {
39
+ margin-bottom: var(--gap-subsection);
40
+ }
41
+ /* the dot sits on the title's first line; a step without a title has it at its top */
42
+ .wp-tl-step:has(> .wp-tl-title)::before {
43
+ display: none;
44
+ }
45
+ .wp-tl-title {
46
+ position: relative;
47
+ }
48
+ .wp-tl-step::before,
49
+ .wp-tl-title::before {
50
+ content: '';
51
+ position: absolute;
52
+ left: -27px;
53
+ top: 0.45em;
54
+ width: 12px;
55
+ height: 12px;
56
+ border-radius: 50%;
57
+ background: var(--bg);
58
+ border: 2px solid var(--accent);
59
+ box-sizing: border-box;
60
+ }
61
+ .wp-tl-when {
62
+ display: block;
63
+ font-family: var(--mono);
64
+ font-size: var(--ui-tiny);
65
+ color: var(--muted);
66
+ line-height: 1.4;
67
+ }
68
+ /* centred on the title's first line (line-height 1.4: its middle is 0.7em down) */
69
+ .wp-tl-title::before {
70
+ top: calc(0.7em - 6px);
71
+ }
72
+ .wp-tl-title {
73
+ font-weight: 600;
74
+ line-height: 1.4;
75
+ }
76
+ /* the body under its title as a heading's content under it; inside, the page's steps (base.css) */
77
+ .wp-tl-body {
78
+ font-size: var(--ui-text);
79
+ line-height: 1.6;
80
+ margin-top: var(--gap-heading);
81
+ }
82
+ .wp-timeline .wp-tl-body > :first-child {
83
+ margin-top: 0;
84
+ }
85
+ /* a body that starts with a heading: two headings in a row, closer */
86
+ .wp-tl-body:has(> :first-child:is(h2, h3, h4, h5, h6)) {
87
+ margin-top: var(--gap-heading-next);
88
+ }
89
+ .wp-timeline .wp-tl-body > :last-child {
90
+ margin-bottom: 0;
91
+ }
92
+
93
+ /* a timeline inside a step is its sub steps: a level in, a thinner line and smaller rings, the
94
+ same at every depth */
95
+ /* its sub steps hold headings and steps of their own: around them the space between steps, so the
96
+ space outside them is no smaller than any inside */
97
+ .wp-tl-body .wp-timeline {
98
+ margin-block: var(--gap-subsection);
99
+ }
100
+ .wp-tl-body .wp-tl-steps {
101
+ padding-left: 20px;
102
+ }
103
+ .wp-tl-body .wp-tl-steps::before {
104
+ left: 4px;
105
+ width: 1px;
106
+ }
107
+ .wp-tl-body .wp-tl-step::before,
108
+ .wp-tl-body .wp-tl-title::before {
109
+ left: -20px;
110
+ width: 9px;
111
+ height: 9px;
112
+ border-width: 1.5px;
113
+ }
114
+ .wp-tl-body .wp-tl-title::before {
115
+ top: calc(0.7em - 4.5px);
116
+ }
117
+ .wp-tl-body .wp-tl-title {
118
+ font-size: var(--ui-text);
119
+ }
120
+
121
+ /* a :when in any other heading: above it, wherever it was written in it; the room it takes comes
122
+ on top of the heading's own margin */
123
+ :is(h1, h2, h3, h4, h5, h6):has(> .wp-when) {
124
+ position: relative;
125
+ padding-top: calc(var(--ui-tiny) * 1.4);
126
+ }
127
+ :is(h1, h2, h3, h4, h5, h6) > .wp-when {
128
+ position: absolute;
129
+ top: 0;
130
+ left: 0;
131
+ font-family: var(--mono);
132
+ font-size: var(--ui-tiny);
133
+ font-weight: normal;
134
+ font-style: normal;
135
+ color: var(--muted);
136
+ line-height: 1.4;
137
+ white-space: nowrap;
138
+ }
@@ -0,0 +1,40 @@
1
+ A table of contents beside the page, from its headings (`##` down to
2
+ `######`), numbered as the page numbers them, each section's subsections on a
3
+ line under it. The section being read is marked, a click
4
+ goes to a section, and a section with an unanswered question or an unread
5
+ message has a dot. It sits in the margin left of the text when the window has
6
+ room; else a button in the header opens it. It is built in: a page has it
7
+ when its frontmatter asks, under `plugins:` (where every plugin's settings for
8
+ a page go, by the plugin's name):
9
+
10
+ ```markdown
11
+ ---
12
+ plugins:
13
+ toc: true
14
+ ---
15
+ # A long page
16
+ ```
17
+
18
+ To have it on every page, replace the built-in one in the config; a page then
19
+ goes without with `toc: false` there:
20
+
21
+ ```ts
22
+ import toc from '@bigmistqke/workpad/toc'
23
+ export default defineConfig({ plugins: [toc({ always: true })] })
24
+ ```
25
+
26
+ The panel fits its items, between a minimum and a maximum width (160px and
27
+ 320px by default). A project sets its own in the options, and a page in its
28
+ frontmatter, which is then also how it asks for the contents:
29
+
30
+ ```ts
31
+ toc({ minWidth: 180, maxWidth: '24rem' }) // pixels, or a CSS length
32
+ ```
33
+
34
+ ```markdown
35
+ ---
36
+ plugins:
37
+ toc:
38
+ maxWidth: 240px
39
+ ---
40
+ ```
@@ -0,0 +1,236 @@
1
+ // The table of contents (index.ts): built from the page's ## to ###### headings, again after each
2
+ // update; the section being read is the last heading above the top of the view; a section is
3
+ // marked when an open question (data-wp-open) or an unread message (data-wp-message, or the element
4
+ // holding one, data-wp-holds) is in it. A click scrolls to the heading, below the header.
5
+ import type { Api, PageHandle, PluginMount } from '../../src/define.ts'
6
+
7
+ const panel = document.createElement('nav')
8
+ panel.className = 'wp-toc'
9
+ panel.setAttribute('aria-label', 'Contents')
10
+ const list = document.createElement('ol')
11
+ panel.append(list)
12
+
13
+ const toggle = document.createElement('button')
14
+ toggle.type = 'button'
15
+ toggle.className = 'wp-toc-toggle'
16
+ toggle.title = toggle.ariaLabel = 'Contents'
17
+ toggle.setAttribute('aria-expanded', 'false')
18
+ toggle.innerHTML =
19
+ '<svg viewBox="0 0 16 16" width="14" height="14" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"><path d="M2.5 4h11M2.5 8h11M2.5 12h7"/></svg>'
20
+
21
+ let main: HTMLElement
22
+ let api: Api
23
+ let headings: HTMLElement[] = []
24
+
25
+ const navHeight = () => document.querySelector<HTMLElement>('.wp-nav')?.getBoundingClientRect().height ?? 0
26
+
27
+ /** The gap between the panel and the text, and between the panel and the window's edge. */
28
+ const GAP = 40
29
+ const EDGE = 16
30
+
31
+ /** The room beside the text for the panel: the window, less the page and the gaps around the panel. */
32
+ const room = () => document.documentElement.clientWidth - main.offsetWidth - EDGE - GAP
33
+
34
+ /** Is there room beside the text for the panel at its narrowest? Else it opens from the header. */
35
+ function fits() {
36
+ const narrowest = parseFloat(getComputedStyle(panel).minWidth) || 0
37
+ return room() >= narrowest
38
+ }
39
+
40
+ /** A width as the settings or the options give it: a CSS length, or a number of pixels. */
41
+ const length = (value: unknown): string | null => {
42
+ const m = /^(\d+(?:\.\d+)?)(px|em|rem|ch|vw)?$/.exec(String(value ?? '').trim())
43
+ return m && Number(m[1]) > 0 ? m[1] + (m[2] ?? 'px') : null
44
+ }
45
+
46
+ /**
47
+ * How wide the panel may be: it fits its items, between a minimum and a maximum, which the page's
48
+ * settings can set (`plugins: { toc: { minWidth: 160px, maxWidth: 320px } }`), else the project's options.
49
+ */
50
+ function bounds() {
51
+ const meta = document.querySelector<HTMLMetaElement>('meta[name=wp-toc]')
52
+ const said = handle.settings()
53
+ const own = said && typeof said === 'object' ? (said as Record<string, unknown>) : {}
54
+ for (const [name, prop] of [
55
+ ['minWidth', '--wp-toc-min-width'],
56
+ ['maxWidth', '--wp-toc-max-width'],
57
+ ] as const) {
58
+ const value = length(own[name]) ?? length(meta?.dataset[name])
59
+ if (value) panel.style.setProperty(prop, value)
60
+ else panel.style.removeProperty(prop)
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Docked: at the window's left edge, as wide as its items allow within the room there is, and the
66
+ * page centred in the space right of it: the room it takes is --wp-aside on the root (base.css).
67
+ * Not docked, it takes none.
68
+ */
69
+ function place(docked: boolean) {
70
+ const root = document.documentElement.style
71
+ panel.style.setProperty('--wp-toc-room', `${room()}px`)
72
+ if (!docked) return root.removeProperty('--wp-aside')
73
+ // its edge and itself: the page centres in the rest, as far from it as from the window's right edge
74
+ root.setProperty('--wp-aside', `${EDGE + panel.getBoundingClientRect().width}px`)
75
+ }
76
+
77
+ let handle: PageHandle
78
+
79
+ /** Opened from the header (or closed again): the panel shows, and the button says it is open. */
80
+ function setOpen(open: boolean) {
81
+ panel.classList.toggle('wp-toc-open', open)
82
+ panel.hidden = !open
83
+ toggle.setAttribute('aria-expanded', String(open))
84
+ }
85
+
86
+ /** Does this page have it: what its settings say, and whether it is on everywhere. */
87
+ function wanted() {
88
+ // its settings (`plugins: { toc: … }`): true, or { minWidth, maxWidth }: on; false: off; else as
89
+ // the project says
90
+ const said = handle.settings()
91
+ const always = document.querySelector('meta[name=wp-toc]')?.getAttribute('content') === 'always'
92
+ if (said === false) return false
93
+ if (said === true || (said && typeof said === 'object')) return true
94
+ return always
95
+ }
96
+
97
+ /** A heading's text, without a badge that ends it or a when above it (not part of the title). */
98
+ function titleOf(h: HTMLElement) {
99
+ const copy = h.cloneNode(true) as HTMLElement
100
+ copy.querySelectorAll('.wp-badge, .wp-when').forEach(b => b.remove())
101
+ return copy.textContent?.trim() ?? ''
102
+ }
103
+
104
+ const SECTIONS = ':scope > :is(h2, h3, h4, h5, h6)'
105
+
106
+ /**
107
+ * The outline as nested lists: a heading's subsections in a list inside its item, which the style
108
+ * hangs on a rail under it. A level skipped (## then ####) nests once, under the nearest above.
109
+ */
110
+ function build() {
111
+ headings = wanted() ? [...main.querySelectorAll<HTMLElement>(SECTIONS)] : []
112
+ // the page's numbers: 1, 1.1, 1.1.1 (base.css counts ## to ###### alike)
113
+ const counts = [0, 0, 0, 0, 0]
114
+ const open: { level: number; list: HTMLOListElement; item?: HTMLLIElement }[] = [{ level: 1, list }]
115
+ list.replaceChildren()
116
+ headings.forEach((h, i) => {
117
+ const level = Number(h.tagName[1])
118
+ counts[level - 2]++
119
+ counts.fill(0, level - 1)
120
+ while (open.length > 1 && open.at(-1)!.level >= level) open.pop()
121
+ const parent = open.at(-1)!
122
+ if (parent.item && !parent.item.querySelector(':scope > ol')) parent.item.append(document.createElement('ol'))
123
+ const into = parent.item ? parent.item.querySelector<HTMLOListElement>(':scope > ol')! : parent.list
124
+ const li = document.createElement('li')
125
+ li.dataset.level = String(level)
126
+ const a = document.createElement('a')
127
+ a.href = '#'
128
+ a.dataset.index = String(i)
129
+ a.innerHTML = `<span class="wp-toc-number"></span><span class="wp-toc-text"></span>`
130
+ a.querySelector('.wp-toc-number')!.textContent = counts.slice(0, level - 1).join('.')
131
+ a.querySelector('.wp-toc-text')!.textContent = titleOf(h)
132
+ li.append(a)
133
+ into.append(li)
134
+ open.push({ level, list: into, item: li })
135
+ })
136
+ const any = headings.length > 1
137
+ bounds()
138
+ const docked = any && fits()
139
+ toggle.hidden = !any || docked
140
+ panel.hidden = !any || (!docked && !panel.classList.contains('wp-toc-open'))
141
+ panel.classList.toggle('wp-toc-docked', docked)
142
+ place(docked)
143
+ mark()
144
+ current()
145
+ }
146
+
147
+ /** The section each element is in: the last heading before it. */
148
+ function sectionOf(el: Element): number {
149
+ let at = -1
150
+ for (let i = 0; i < headings.length; i++) {
151
+ if (headings[i].compareDocumentPosition(el) & Node.DOCUMENT_POSITION_FOLLOWING) at = i
152
+ else break
153
+ }
154
+ return at
155
+ }
156
+
157
+ /** A dot on the sections where something waits for the reader. */
158
+ function mark() {
159
+ const waiting = new Set<number>()
160
+ const unread = new Set(api.unread().map(e => e.id))
161
+ const els = [
162
+ ...main.querySelectorAll('form[data-wp-open]'),
163
+ ...[...main.querySelectorAll<HTMLElement>('[data-wp-message]')].filter(el => unread.has(el.dataset.wpMessage!)),
164
+ ]
165
+ for (const el of els) waiting.add(sectionOf(el))
166
+ // a reply in a closed comment thread: its badge sits outside the page's flow, beside the passage
167
+ for (const badge of document.querySelectorAll<HTMLElement>('[data-wp-holds]')) {
168
+ if (!badge.dataset.wpHolds!.split(' ').some(id => unread.has(id))) continue
169
+ const y = badge.getBoundingClientRect().top
170
+ let at = -1
171
+ headings.forEach((h, i) => {
172
+ if (h.getBoundingClientRect().top <= y) at = i
173
+ })
174
+ waiting.add(at)
175
+ }
176
+ list.querySelectorAll<HTMLElement>('a').forEach((a, i) => a.toggleAttribute('data-waiting', waiting.has(i)))
177
+ }
178
+
179
+ /** The section being read: the last heading above the top of the view. */
180
+ function current() {
181
+ const line = navHeight() + 40
182
+ let at = 0
183
+ headings.forEach((h, i) => {
184
+ if (h.getBoundingClientRect().top <= line) at = i
185
+ })
186
+ list
187
+ .querySelectorAll<HTMLElement>('a')
188
+ .forEach((a, i) => a.toggleAttribute('aria-current', i === at && headings.length > 0))
189
+ }
190
+
191
+ function go(i: number) {
192
+ const h = headings[i]
193
+ if (!h) return
194
+ scrollTo({ top: Math.max(0, scrollY + h.getBoundingClientRect().top - navHeight() - 12), behavior: 'smooth' })
195
+ if (!fits()) setOpen(false)
196
+ }
197
+
198
+ const mount: PluginMount = (root, page, core) => {
199
+ if (page.kind !== 'page' && page.kind !== 'file') return
200
+ handle = page
201
+ main = root.querySelector<HTMLElement>('#wp-main') ?? root
202
+ api = core
203
+ // the panel beside the text, its button in the header bar
204
+ core.slot('aside', place => {
205
+ place.append(panel)
206
+ return () => panel.remove()
207
+ })
208
+ core.slot('header', place => {
209
+ place.append(toggle)
210
+ return () => toggle.remove()
211
+ })
212
+ list.addEventListener('click', e => {
213
+ const a = (e.target as HTMLElement).closest<HTMLElement>('a[data-index]')
214
+ if (!a) return
215
+ e.preventDefault()
216
+ go(Number(a.dataset.index))
217
+ })
218
+ toggle.addEventListener('click', () => setOpen(!panel.classList.contains('wp-toc-open')))
219
+ document.addEventListener('keydown', e => {
220
+ if (e.key === 'Escape' && panel.classList.contains('wp-toc-open')) toggle.click()
221
+ })
222
+ let frame = 0
223
+ const soon = (fn: () => void) => () => {
224
+ cancelAnimationFrame(frame)
225
+ frame = requestAnimationFrame(fn)
226
+ }
227
+ addEventListener('scroll', soon(current), { passive: true })
228
+ addEventListener('resize', soon(build))
229
+ api.onRendered(build)
230
+ page.settings.on(build)
231
+ api.forms.on(() => mark())
232
+ api.events.on(() => requestAnimationFrame(mark))
233
+ build()
234
+ }
235
+
236
+ export default mount
@@ -0,0 +1,55 @@
1
+ // A table of contents beside the page: its sections (## to ###### headings), numbered as the page
2
+ // numbers them, each one's subsections on a rail under it, the one being read marked, a dot where
3
+ // something waits for the reader (an unanswered question, an unread message). In the margin left
4
+ // of the text when the window has room for it; else a button in the header that opens it.
5
+ //
6
+ // Built in (plugins/index.ts): a page has it when its frontmatter says so, under `plugins:`
7
+ // (`plugins: { toc: true }`). A config that replaces it with `toc({ always: true })` puts it on
8
+ // every page, unless a page's frontmatter says `toc: false` there.
9
+ import { builtinPublic } from '../../src/package.ts'
10
+ import { html } from '../../src/html.ts'
11
+ import type { Plugin } from '../../src/define.ts'
12
+
13
+ const DOCS = `## Table of contents
14
+
15
+ Pages here have a table of contents beside them, made from their headings,
16
+ \`##\` down to \`######\`: give a long page headings, and the reader can find
17
+ their way in it. Each section's subsections hang on a line under it, so the
18
+ nesting shows. A section with an unanswered question or an unread message has
19
+ a dot.
20
+ `
21
+
22
+ export interface TocOptions {
23
+ /** on every page, not only the ones that ask (`plugins: { toc: true }`); `toc: false` there still goes without */
24
+ always?: boolean
25
+ /** the panel fits its items, but never narrower than this: a CSS length, or pixels (default 160px) */
26
+ minWidth?: string | number
27
+ /** nor wider than this (default 320px); a page can say otherwise: `plugins: { toc: { maxWidth: 280px } }` */
28
+ maxWidth?: string | number
29
+ }
30
+
31
+ export default function toc(options: TocOptions = {}): Plugin {
32
+ return {
33
+ name: 'toc',
34
+ docs: () =>
35
+ options.always
36
+ ? DOCS +
37
+ '\nIn this project every page has it; a page goes without with `toc: false` under `plugins:` in its frontmatter:\n\n```markdown\n---\nplugins:\n toc: false\n---\n```\n'
38
+ : DOCS +
39
+ '\nA page has it when its frontmatter asks, under `plugins:`:\n\n```markdown\n---\nplugins:\n toc: true\n---\n# A long page\n```\n',
40
+ public: builtinPublic('toc'),
41
+ style: 'style.css',
42
+ setup: () => ({
43
+ client: 'client.js',
44
+ // how the browser part knows: on every page, or only where the frontmatter asks
45
+ slots: () => ({
46
+ head: html`<meta
47
+ name="wp-toc"
48
+ content="${options.always ? 'always' : 'opt-in'}"
49
+ ${options.minWidth != null ? html` data-min-width="${String(options.minWidth)}"` : ''}
50
+ ${options.maxWidth != null ? html` data-max-width="${String(options.maxWidth)}"` : ''}
51
+ />`,
52
+ }),
53
+ }),
54
+ }
55
+ }
@@ -0,0 +1,117 @@
1
+ /* the table of contents (client.ts): in the margin left of the text, or opened from the header */
2
+ /* it fits its items, between a minimum and a maximum (client.ts sets them from the options and the
3
+ page's frontmatter); docked, never wider than the room left of the text */
4
+ .wp-toc {
5
+ position: fixed;
6
+ z-index: var(--layer-aside);
7
+ font: var(--ui-small) / var(--ui-line-height) var(--sans, sans-serif);
8
+ box-sizing: border-box;
9
+ width: max-content;
10
+ min-width: var(--wp-toc-min-width, 160px);
11
+ max-width: var(--wp-toc-max-width, 320px);
12
+ }
13
+ .wp-toc[hidden],
14
+ .wp-toc-toggle[hidden] {
15
+ display: none;
16
+ }
17
+ /* docked: at the window's left edge; the page centres in the room right of it (client.ts, --wp-aside) */
18
+ .wp-toc-docked {
19
+ top: 72px;
20
+ left: 16px;
21
+ max-width: min(var(--wp-toc-max-width, 320px), var(--wp-toc-room, 320px));
22
+ max-height: calc(100vh - 120px - var(--wp-inset-bottom, 0px));
23
+ overflow: auto;
24
+ }
25
+ .wp-toc-open:not(.wp-toc-docked) {
26
+ top: 52px;
27
+ left: 16px;
28
+ max-width: min(var(--wp-toc-max-width, 320px), 100vw - 32px);
29
+ max-height: calc(100vh - 100px - var(--wp-inset-bottom, 0px));
30
+ overflow: auto;
31
+ padding: var(--space-sm);
32
+ background: var(--bg, Canvas);
33
+ border: var(--hairline) solid var(--border, #e3e5e9);
34
+ border-radius: var(--radius);
35
+ box-shadow: var(--shadow-pop);
36
+ }
37
+ .wp-toc ol {
38
+ list-style: none;
39
+ margin: 0;
40
+ padding: 0;
41
+ }
42
+ /* the same space above every item, also the first of a nested list (the page's li + li alone
43
+ would leave that one tight against its section) */
44
+ .wp-toc li {
45
+ margin-top: 0.8em;
46
+ }
47
+ .wp-toc > ol > li:first-child {
48
+ margin-top: 0;
49
+ }
50
+ /* a section's subsections hang on a rail under its number, one rail per level */
51
+ .wp-toc ol ol {
52
+ margin-left: var(--space-md);
53
+ padding-left: var(--space-sm);
54
+ border-left: var(--hairline) solid var(--border, #e3e5e9);
55
+ }
56
+ .wp-toc a {
57
+ display: flex;
58
+ gap: var(--space-md);
59
+ align-items: baseline;
60
+ padding: var(--space-2xs) var(--space-md);
61
+ border-radius: var(--radius-sm);
62
+ color: var(--muted, #696e76);
63
+ text-decoration: none;
64
+ border-left: 2px solid transparent;
65
+ }
66
+ .wp-toc a:hover {
67
+ color: var(--text, CanvasText);
68
+ background: color-mix(in srgb, var(--text, CanvasText) var(--tint-faint), transparent);
69
+ }
70
+ .wp-toc a[aria-current] {
71
+ color: var(--text, CanvasText);
72
+ border-left-color: var(--accent, #1d5bbf);
73
+ font-weight: var(--weight-strong);
74
+ }
75
+ .wp-toc-number {
76
+ font-variant-numeric: tabular-nums;
77
+ opacity: 0.7;
78
+ flex: none;
79
+ }
80
+ .wp-toc-text {
81
+ overflow: hidden;
82
+ text-overflow: ellipsis;
83
+ }
84
+ /* something waits for the reader in this section */
85
+ .wp-toc a[data-waiting]::after {
86
+ content: '';
87
+ flex: none;
88
+ width: 7px;
89
+ height: 7px;
90
+ margin-left: auto;
91
+ align-self: center;
92
+ border-radius: 50%;
93
+ background: var(--accent, #1d5bbf);
94
+ }
95
+ .wp-toc-toggle {
96
+ flex: none;
97
+ display: inline-grid;
98
+ place-items: center;
99
+ height: 24px;
100
+ box-sizing: border-box;
101
+ padding: 0 var(--space-sm);
102
+ border-radius: var(--radius-pill);
103
+ border: var(--hairline) solid var(--border, #e3e5e9);
104
+ background: none;
105
+ color: var(--muted, #696e76);
106
+ cursor: pointer;
107
+ }
108
+ .wp-toc-toggle:hover {
109
+ color: var(--text, CanvasText);
110
+ border-color: var(--muted, #696e76);
111
+ }
112
+ /* the panel is open from it */
113
+ .wp-toc-toggle[aria-expanded='true'] {
114
+ color: var(--accent, #1d5bbf);
115
+ border-color: var(--accent, #1d5bbf);
116
+ background: color-mix(in srgb, var(--accent, #1d5bbf) var(--tint-faint), transparent);
117
+ }
@@ -0,0 +1,51 @@
1
+ A window: a header with a label, a button that fills the browser window with
2
+ what it shows (the same button, or Esc, puts it back), and one that opens it on
3
+ its own.
4
+
5
+ **Any block goes in one with `compose=window`**: an image, a video, another
6
+ page, a diagram, a grid. Its label is the window's title (or `title="A
7
+ title"`, for a block without a label, such as a fence), and ↗ opens what it
8
+ shows on its own, an image's file or a page's address. `wide` makes it wider
9
+ than the text column.
10
+
11
+ ````markdown
12
+ ::image[The new onboarding]{src="onboarding.png" compose=window}
13
+
14
+ ```mermaid {compose=window title="How a page is served"}
15
+ graph LR; file --> server --> browser
16
+ ```
17
+
18
+ ::::grid{cols=2 compose=window title="Two options"}
19
+ :::item[A]
20
+ …
21
+ :::
22
+ ::::
23
+ ````
24
+
25
+ **One image or video**, without it, is just itself:
26
+
27
+ ```markdown
28
+ ::image[The new onboarding]{src="onboarding.png"}
29
+
30
+ ::video[The bug, recorded]{src="bug.mp4" loop muted poster="bug.png"}
31
+ ```
32
+
33
+ A video has its controls; `loop`, `autoplay` (which starts it muted, as
34
+ browsers require) and `poster` are as for a `<video>`. A plain markdown image,
35
+ `![…](shot.png)`, is the same as `::image` without it.
36
+
37
+ **Any markdown**, in `:::window`: a table, a screenshot with words under it,
38
+ what has no block of its own. When it holds one image or video, ↗ opens that
39
+ file. `height` (pixels or a CSS length) fixes its height, scrolling inside.
40
+
41
+ ```markdown
42
+ :::window[What each one takes]
43
+ | block | what |
44
+ | --- | --- |
45
+ | `::image` | one image |
46
+ :::
47
+ ```
48
+
49
+ `compose=window` is this plugin's `compose`: any plugin can compose itself
50
+ around blocks the same way, and a block names several in order:
51
+ `compose=lightbox,window`.