@pathmx/core 0.5.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 (195) hide show
  1. package/LICENSE.md +172 -0
  2. package/README.md +111 -0
  3. package/actions/actions.ts +307 -0
  4. package/actions/draft.ts +105 -0
  5. package/actions/transaction.ts +164 -0
  6. package/assets.ts +149 -0
  7. package/authority/access.ts +820 -0
  8. package/authority/actors.ts +271 -0
  9. package/authority/agents.ts +95 -0
  10. package/authority/credentials.ts +144 -0
  11. package/authority/model.ts +71 -0
  12. package/authority/sharing.ts +89 -0
  13. package/browser-bundle.ts +61 -0
  14. package/canonical.ts +41 -0
  15. package/cli/dev.ts +134 -0
  16. package/cli/export.ts +74 -0
  17. package/cli/index.ts +52 -0
  18. package/cli/lint.ts +164 -0
  19. package/cli/mv.ts +30 -0
  20. package/cli/perms.ts +182 -0
  21. package/cli/plugin-packages.ts +159 -0
  22. package/cli/plugins.ts +95 -0
  23. package/cli/query.ts +225 -0
  24. package/cli/readme.md +95 -0
  25. package/cli/release.ts +75 -0
  26. package/cli/rm.ts +22 -0
  27. package/cli/serve.ts +100 -0
  28. package/cli/tree.ts +131 -0
  29. package/cli/update.ts +78 -0
  30. package/cli/utils.ts +53 -0
  31. package/core.ts +96 -0
  32. package/database.ts +189 -0
  33. package/dependencies.ts +60 -0
  34. package/dist/pathmx.js +1224 -0
  35. package/document.ts +194 -0
  36. package/environment.ts +110 -0
  37. package/environments/bun-plugins.ts +68 -0
  38. package/environments/bun-publish.ts +194 -0
  39. package/environments/bun.ts +223 -0
  40. package/globals.d.ts +14 -0
  41. package/graph.ts +153 -0
  42. package/host/compiler.ts +314 -0
  43. package/host/create.ts +66 -0
  44. package/host/engine.ts +630 -0
  45. package/host/plugin-host.ts +487 -0
  46. package/host/render.ts +102 -0
  47. package/host/response.ts +20 -0
  48. package/host/shell.html +15 -0
  49. package/host/source-sync.ts +254 -0
  50. package/html-attributes.ts +15 -0
  51. package/html.ts +48 -0
  52. package/icons/LICENSE +43 -0
  53. package/icons/lucide.ts +54 -0
  54. package/index.ts +75 -0
  55. package/invalidation.ts +85 -0
  56. package/log.ts +84 -0
  57. package/ops/href.ts +87 -0
  58. package/ops/index.ts +9 -0
  59. package/ops/move-source.ts +114 -0
  60. package/ops/plan.ts +47 -0
  61. package/ops/remove-source.ts +70 -0
  62. package/package.json +46 -0
  63. package/path.ts +75 -0
  64. package/pattern.ts +314 -0
  65. package/perf.ts +88 -0
  66. package/plugins/actor-path.ts +43 -0
  67. package/plugins/actor-profile.ts +55 -0
  68. package/plugins/agent-browsing.ts +183 -0
  69. package/plugins/app-component.ts +23 -0
  70. package/plugins/block-layout/index.ts +78 -0
  71. package/plugins/block-layout/theme.css +90 -0
  72. package/plugins/callout/index.ts +236 -0
  73. package/plugins/callout/theme.css +138 -0
  74. package/plugins/canonical-links.ts +144 -0
  75. package/plugins/code-highlight/client.ts +89 -0
  76. package/plugins/code-highlight/index.ts +147 -0
  77. package/plugins/code-highlight/theme.css +89 -0
  78. package/plugins/context.ts +131 -0
  79. package/plugins/datatype/Datatype[wdth,wght].woff2 +0 -0
  80. package/plugins/datatype/OFL.txt +96 -0
  81. package/plugins/datatype/index.ts +287 -0
  82. package/plugins/definition-list.ts +236 -0
  83. package/plugins/directive-definitions.ts +13 -0
  84. package/plugins/embedding/client.ts +88 -0
  85. package/plugins/embedding/index.ts +43 -0
  86. package/plugins/image/index.ts +68 -0
  87. package/plugins/image/theme.css +43 -0
  88. package/plugins/include.ts +119 -0
  89. package/plugins/index.ts +91 -0
  90. package/plugins/input/authoring.ts +236 -0
  91. package/plugins/input/config.ts +85 -0
  92. package/plugins/input/index.ts +359 -0
  93. package/plugins/input/model.ts +173 -0
  94. package/plugins/input/state.ts +272 -0
  95. package/plugins/layout.ts +313 -0
  96. package/plugins/literate/client.ts +208 -0
  97. package/plugins/literate/index.ts +742 -0
  98. package/plugins/math/index.ts +140 -0
  99. package/plugins/meta.ts +42 -0
  100. package/plugins/navigation/index.ts +158 -0
  101. package/plugins/navigation/model.ts +364 -0
  102. package/plugins/preset.ts +65 -0
  103. package/plugins/query/index.ts +322 -0
  104. package/plugins/rss/index.ts +46 -0
  105. package/plugins/runtime/actions.ts +62 -0
  106. package/plugins/runtime/client.ts +8 -0
  107. package/plugins/runtime/document.ts +506 -0
  108. package/plugins/runtime/error.ts +154 -0
  109. package/plugins/runtime/heading-links.ts +31 -0
  110. package/plugins/runtime/index.ts +104 -0
  111. package/plugins/runtime/live.ts +51 -0
  112. package/plugins/runtime/navigation.ts +113 -0
  113. package/plugins/runtime/pending.ts +53 -0
  114. package/plugins/runtime/publication.ts +19 -0
  115. package/plugins/runtime/start.ts +177 -0
  116. package/plugins/runtime/static-client.ts +6 -0
  117. package/plugins/runtime/static-live.ts +188 -0
  118. package/plugins/runtime/view-transition.ts +72 -0
  119. package/plugins/runtime/viewport.ts +110 -0
  120. package/plugins/schema.ts +24 -0
  121. package/plugins/scripts.ts +140 -0
  122. package/plugins/source-preview/client.ts +256 -0
  123. package/plugins/source-preview/index.ts +132 -0
  124. package/plugins/source-preview/model.ts +11 -0
  125. package/plugins/source-preview/preview.css +107 -0
  126. package/plugins/styles.ts +145 -0
  127. package/plugins/theme/components.ts +10 -0
  128. package/plugins/theme/foundation.css +358 -0
  129. package/plugins/theme/index.ts +333 -0
  130. package/plugins/theme/prose.css +284 -0
  131. package/plugins/theme/scheme.js +63 -0
  132. package/plugins/theme/toggle.css +44 -0
  133. package/plugins/theme/view-transition.css +68 -0
  134. package/plugins/toc/client.ts +146 -0
  135. package/plugins/toc/index.ts +232 -0
  136. package/plugins/toc/theme.css +172 -0
  137. package/plugins/types.ts +538 -0
  138. package/problem.ts +41 -0
  139. package/project.ts +6 -0
  140. package/publish/build.ts +402 -0
  141. package/publish/concurrent.ts +32 -0
  142. package/publish/identity.ts +63 -0
  143. package/publish/index.ts +11 -0
  144. package/publish/location.ts +167 -0
  145. package/publish/model.ts +58 -0
  146. package/publish/plan.ts +300 -0
  147. package/publish/validate.ts +150 -0
  148. package/query/candidate-index.ts +101 -0
  149. package/query/dependency-index.ts +99 -0
  150. package/query/fields.ts +216 -0
  151. package/query/invalidation.ts +48 -0
  152. package/query/kernel.ts +776 -0
  153. package/query/model.ts +79 -0
  154. package/repository.ts +374 -0
  155. package/runtime-profile.ts +34 -0
  156. package/schema/model.ts +92 -0
  157. package/schema/registry.ts +642 -0
  158. package/schema/standard.ts +226 -0
  159. package/server/context.ts +94 -0
  160. package/server/css-imports.ts +66 -0
  161. package/server/http.ts +333 -0
  162. package/server/page.ts +21 -0
  163. package/server/protocols.ts +12 -0
  164. package/server/representations.ts +66 -0
  165. package/server/router.ts +131 -0
  166. package/server/routes.ts +202 -0
  167. package/server/serve.ts +5 -0
  168. package/server/watch.ts +120 -0
  169. package/source/blocks.ts +71 -0
  170. package/source/format.ts +164 -0
  171. package/source/index.ts +67 -0
  172. package/source/interpolate.ts +63 -0
  173. package/source/markdown/blocks.ts +46 -0
  174. package/source/markdown/comments.ts +32 -0
  175. package/source/markdown/fences.ts +67 -0
  176. package/source/markdown/headings.ts +88 -0
  177. package/source/markdown/index.ts +34 -0
  178. package/source/markdown/inline-code.ts +6 -0
  179. package/source/markdown/links.ts +326 -0
  180. package/source/markdown/lists.ts +223 -0
  181. package/source/markdown/prose.ts +74 -0
  182. package/source/markdown/scan.ts +79 -0
  183. package/source/markdown/text.ts +82 -0
  184. package/source/meta.ts +87 -0
  185. package/source/source.ts +121 -0
  186. package/source/topmatter.ts +57 -0
  187. package/source/utils.ts +21 -0
  188. package/source-diagnostic.ts +63 -0
  189. package/text-file.ts +26 -0
  190. package/util.ts +35 -0
  191. package/view/components.ts +499 -0
  192. package/view/model.ts +91 -0
  193. package/view/projection.ts +162 -0
  194. package/view/session.ts +146 -0
  195. package/view/syntax.ts +372 -0
@@ -0,0 +1,358 @@
1
+ :root {
2
+ color-scheme: light dark;
3
+ }
4
+
5
+ :root[data-pmx-scheme="light"] {
6
+ color-scheme: light;
7
+ }
8
+
9
+ :root[data-pmx-scheme="dark"] {
10
+ color-scheme: dark;
11
+ }
12
+
13
+ :root {
14
+ --pmx-default-light-color-bg: oklch(0.985 0 0);
15
+ --pmx-default-light-color-fg: oklch(0.22 0.01 250);
16
+ --pmx-default-light-color-muted: oklch(0.48 0.02 250);
17
+ --pmx-default-light-color-surface: oklch(0.96 0.01 250);
18
+ --pmx-default-light-color-link: oklch(0.48 0.16 255);
19
+ --pmx-default-light-color-accent: oklch(0.54 0.18 255);
20
+ --pmx-default-light-color-danger: oklch(0.54 0.2 25);
21
+ --pmx-default-light-color-border: oklch(0.86 0.02 250);
22
+ --pmx-default-light-color-focus: oklch(0.54 0.18 255);
23
+ --pmx-default-dark-color-bg: oklch(0.18 0.01 250);
24
+ --pmx-default-dark-color-fg: oklch(0.94 0.01 250);
25
+ --pmx-default-dark-color-muted: oklch(0.72 0.02 250);
26
+ --pmx-default-dark-color-surface: oklch(0.24 0.015 250);
27
+ --pmx-default-dark-color-link: oklch(0.76 0.13 250);
28
+ --pmx-default-dark-color-accent: oklch(0.72 0.16 250);
29
+ --pmx-default-dark-color-danger: oklch(0.72 0.18 25);
30
+ --pmx-default-dark-color-border: oklch(0.36 0.02 250);
31
+ --pmx-default-dark-color-focus: oklch(0.76 0.13 250);
32
+
33
+ --pmx-color-bg: light-dark(
34
+ var(--pmx-default-light-color-bg),
35
+ var(--pmx-default-dark-color-bg)
36
+ );
37
+ --pmx-color-fg: light-dark(
38
+ var(--pmx-default-light-color-fg),
39
+ var(--pmx-default-dark-color-fg)
40
+ );
41
+ --pmx-color-muted: light-dark(
42
+ var(--pmx-default-light-color-muted),
43
+ var(--pmx-default-dark-color-muted)
44
+ );
45
+ --pmx-color-surface: light-dark(
46
+ var(--pmx-default-light-color-surface),
47
+ var(--pmx-default-dark-color-surface)
48
+ );
49
+ --pmx-color-link: light-dark(
50
+ var(--pmx-default-light-color-link),
51
+ var(--pmx-default-dark-color-link)
52
+ );
53
+ --pmx-color-accent: light-dark(
54
+ var(--pmx-default-light-color-accent),
55
+ var(--pmx-default-dark-color-accent)
56
+ );
57
+ --pmx-color-danger: light-dark(
58
+ var(--pmx-default-light-color-danger),
59
+ var(--pmx-default-dark-color-danger)
60
+ );
61
+ --pmx-color-border: light-dark(
62
+ var(--pmx-default-light-color-border),
63
+ var(--pmx-default-dark-color-border)
64
+ );
65
+ --pmx-color-focus: light-dark(
66
+ var(--pmx-default-light-color-focus),
67
+ var(--pmx-default-dark-color-focus)
68
+ );
69
+
70
+ --pmx-font-body: ui-sans-serif, system-ui, sans-serif;
71
+ --pmx-font-heading: var(--pmx-font-body);
72
+ --pmx-font-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
73
+ --pmx-prose-size: 1rem;
74
+ --pmx-prose-leading: 1.6;
75
+ --pmx-prose-flow: 1em;
76
+ --pmx-prose-gutter: 1.75rem;
77
+ --pmx-radius: 0.5rem;
78
+ --pmx-radius-sm: 0.25rem;
79
+ --pmx-radius-md: 0.35rem;
80
+ --pmx-radius-lg: 0.5rem;
81
+ --pmx-radius-xl: 0.75rem;
82
+ --pmx-radius-2xl: 1rem;
83
+ --pmx-radius-3xl: 1.5rem;
84
+ --pmx-radius-4xl: 2rem;
85
+ --pmx-radius-full: 9999px;
86
+ --pmx-document-measure: 72ch;
87
+
88
+ /* Shared viewport chrome contract. Runtime-measured chrome updates insets. */
89
+ --pmx-viewport-inset-block-start: 0px;
90
+ --pmx-viewport-inset-block-end: 0px;
91
+ --pmx-viewport-gutter-block-start: max(1rem, env(safe-area-inset-top, 0px));
92
+ --pmx-viewport-gutter-block-end: max(1rem, env(safe-area-inset-bottom, 0px));
93
+ --pmx-scroll-offset-block-start: calc(
94
+ var(--pmx-viewport-inset-block-start) +
95
+ var(--pmx-viewport-gutter-block-start)
96
+ );
97
+ --pmx-sticky-block-start: var(--pmx-scroll-offset-block-start);
98
+ --pmx-sticky-block-end: calc(
99
+ var(--pmx-viewport-inset-block-end) + var(--pmx-viewport-gutter-block-end)
100
+ );
101
+ --pmx-sticky-max-block-size: max(
102
+ 0px,
103
+ calc(100dvh - var(--pmx-sticky-block-start) - var(--pmx-sticky-block-end))
104
+ );
105
+ --pmx-sticky-header-transition-duration: 180ms;
106
+
107
+ --pmx-default-light-code-bg: oklch(0.955 0.01 250);
108
+ --pmx-default-light-code-fg: oklch(0.22 0.01 250);
109
+ --pmx-default-light-code-comment: oklch(0.48 0.02 250);
110
+ --pmx-default-light-code-keyword: oklch(0.47 0.16 255);
111
+ --pmx-default-light-code-string: oklch(0.43 0.12 145);
112
+ --pmx-default-light-code-punctuation: oklch(0.42 0.02 250);
113
+ --pmx-default-dark-code-bg: oklch(0.22 0.015 250);
114
+ --pmx-default-dark-code-fg: oklch(0.9 0.01 250);
115
+ --pmx-default-dark-code-comment: oklch(0.68 0.02 250);
116
+ --pmx-default-dark-code-keyword: oklch(0.78 0.13 255);
117
+ --pmx-default-dark-code-string: oklch(0.78 0.12 145);
118
+ --pmx-default-dark-code-punctuation: oklch(0.78 0.02 250);
119
+ --pmx-code-bg: light-dark(
120
+ var(--pmx-default-light-code-bg),
121
+ var(--pmx-default-dark-code-bg)
122
+ );
123
+ --pmx-code-fg: light-dark(
124
+ var(--pmx-default-light-code-fg),
125
+ var(--pmx-default-dark-code-fg)
126
+ );
127
+ --pmx-code-comment: light-dark(
128
+ var(--pmx-default-light-code-comment),
129
+ var(--pmx-default-dark-code-comment)
130
+ );
131
+ --pmx-code-keyword: light-dark(
132
+ var(--pmx-default-light-code-keyword),
133
+ var(--pmx-default-dark-code-keyword)
134
+ );
135
+ --pmx-code-string: light-dark(
136
+ var(--pmx-default-light-code-string),
137
+ var(--pmx-default-dark-code-string)
138
+ );
139
+ --pmx-code-punctuation: light-dark(
140
+ var(--pmx-default-light-code-punctuation),
141
+ var(--pmx-default-dark-code-punctuation)
142
+ );
143
+
144
+ /* border radii */
145
+ --pmx-radius-sm: 0.25rem;
146
+ --pmx-radius-md: 0.35rem;
147
+ --pmx-radius-lg: 0.5rem;
148
+ --pmx-radius-xl: 0.75rem;
149
+ --pmx-radius-2xl: 1rem;
150
+ --pmx-radius-3xl: 1.5rem;
151
+ --pmx-radius-full: 9999px;
152
+
153
+ /* Shadows follow a compact Tailwind-like elevation scale. */
154
+ --pmx-shadow-2xs: 0 1px rgb(0 0 0 / 5%);
155
+ --pmx-shadow-xs: 0 1px 2px 0 rgb(0 0 0 / 5%);
156
+ --pmx-shadow-sm:
157
+ 0 1px 3px 0 rgb(0 0 0 / 10%), 0 1px 2px -1px rgb(0 0 0 / 10%);
158
+ --pmx-shadow-md:
159
+ 0 4px 6px -1px rgb(0 0 0 / 10%), 0 2px 4px -2px rgb(0 0 0 / 10%);
160
+ --pmx-shadow-lg:
161
+ 0 10px 15px -3px rgb(0 0 0 / 10%), 0 4px 6px -4px rgb(0 0 0 / 10%);
162
+ --pmx-shadow-xl:
163
+ 0 20px 25px -5px rgb(0 0 0 / 10%), 0 8px 10px -6px rgb(0 0 0 / 10%);
164
+ --pmx-shadow-2xl: 0 25px 50px -12px rgb(0 0 0 / 25%);
165
+ --pmx-shadow: var(--pmx-shadow-sm);
166
+
167
+ /* Material: frosted-glass fills. Thickness is how much sits behind shows through. */
168
+ --pmx-material-ultra-thin: color-mix(
169
+ in oklch,
170
+ var(--pmx-color-bg) 42%,
171
+ transparent
172
+ );
173
+ --pmx-material-thin: color-mix(
174
+ in oklch,
175
+ var(--pmx-color-bg) 56%,
176
+ transparent
177
+ );
178
+ --pmx-material-regular: color-mix(
179
+ in oklch,
180
+ var(--pmx-color-bg) 72%,
181
+ transparent
182
+ );
183
+ --pmx-material-thick: color-mix(
184
+ in oklch,
185
+ var(--pmx-color-bg) 86%,
186
+ transparent
187
+ );
188
+ --pmx-material-ultra-thick: color-mix(
189
+ in oklch,
190
+ var(--pmx-color-bg) 96%,
191
+ transparent
192
+ );
193
+ --pmx-material: var(--pmx-material-regular);
194
+ --pmx-material-filter: blur(1.25rem) saturate(1.8);
195
+ }
196
+
197
+ @media (prefers-reduced-transparency: reduce) {
198
+ :root {
199
+ --pmx-material-ultra-thin: color-mix(
200
+ in oklch,
201
+ var(--pmx-color-bg) 88%,
202
+ transparent
203
+ );
204
+ --pmx-material-thin: color-mix(
205
+ in oklch,
206
+ var(--pmx-color-bg) 92%,
207
+ transparent
208
+ );
209
+ --pmx-material-regular: var(--pmx-color-bg);
210
+ --pmx-material-thick: var(--pmx-color-bg);
211
+ --pmx-material-ultra-thick: var(--pmx-color-bg);
212
+ --pmx-material-filter: none;
213
+ }
214
+ }
215
+
216
+ html {
217
+ scroll-padding-block-start: var(--pmx-scroll-offset-block-start);
218
+ scroll-padding-block-end: var(--pmx-sticky-block-end);
219
+ }
220
+
221
+ body {
222
+ margin: 0;
223
+ min-height: 100svh;
224
+ background: var(--pmx-color-bg);
225
+ color: var(--pmx-color-fg);
226
+ font-family: var(--pmx-font-body);
227
+ font-size: var(--pmx-prose-size);
228
+ line-height: var(--pmx-prose-leading);
229
+ }
230
+
231
+ :where([data-pmx-sticky-header]) {
232
+ position: sticky;
233
+ z-index: 10;
234
+ inset-block-start: 0;
235
+ transition:
236
+ opacity var(--pmx-sticky-header-transition-duration) ease-out,
237
+ transform var(--pmx-sticky-header-transition-duration) ease-out;
238
+ }
239
+
240
+ :where([data-pmx-sticky-header][data-pmx-hidden]) {
241
+ opacity: 0;
242
+ pointer-events: none;
243
+ transform: translateY(calc(-100% - 1px));
244
+ }
245
+
246
+ :where([data-pmx-sticky-header]:focus-within) {
247
+ opacity: 1;
248
+ pointer-events: auto;
249
+ transform: none;
250
+ }
251
+
252
+ @media (prefers-reduced-motion: reduce) {
253
+ :where([data-pmx-sticky-header]) {
254
+ transition: none;
255
+ }
256
+ }
257
+
258
+ .pmx-document {
259
+ max-width: var(--pmx-document-measure);
260
+ margin-inline: auto;
261
+ padding-block: 1.5rem 3rem;
262
+ padding-inline: max(1rem, var(--pmx-prose-gutter));
263
+ }
264
+
265
+ .pmx-document > * + * {
266
+ margin-block-start: var(--pmx-prose-flow);
267
+ }
268
+
269
+ .pmx-document > [data-pmx-block]:not(:last-of-type) {
270
+ padding-block-end: var(--pmx-prose-flow);
271
+ border-block-end: 1px solid var(--pmx-color-border);
272
+ }
273
+
274
+ .pmx-error {
275
+ margin-block: 0.75rem;
276
+ padding: 0.75rem 1rem;
277
+ border: 1px solid var(--pmx-color-danger);
278
+ border-radius: var(--pmx-radius);
279
+ background: color-mix(
280
+ in oklch,
281
+ var(--pmx-color-danger) 8%,
282
+ var(--pmx-color-bg)
283
+ );
284
+ color: var(--pmx-color-fg);
285
+ }
286
+
287
+ .pmx-error strong {
288
+ color: var(--pmx-color-danger);
289
+ }
290
+
291
+ .pmx-error button {
292
+ display: block;
293
+ margin-block-start: 0.75rem;
294
+ }
295
+
296
+ /* links */
297
+
298
+ .pmx-link {
299
+ color: var(--pmx-color-link);
300
+ text-decoration: none;
301
+ }
302
+
303
+ .pmx-link:hover {
304
+ text-decoration: underline;
305
+ }
306
+
307
+ .pmx-link-external {
308
+ color: var(--pmx-color-link);
309
+ text-decoration: none;
310
+ }
311
+
312
+ .pmx-link-external:hover {
313
+ text-decoration: underline;
314
+ }
315
+
316
+ .pmx-link-new-tab::after {
317
+ content: "";
318
+ display: inline-block;
319
+ width: 0.8em;
320
+ height: 0.8em;
321
+ margin-inline-start: 0.2em;
322
+ background: currentColor;
323
+ vertical-align: 0.05em;
324
+ -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M15 3h6v6'/%3E%3Cpath d='m10 14 11-11'/%3E%3Cpath d='M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6'/%3E%3C/svg%3E")
325
+ center / contain no-repeat;
326
+ mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M15 3h6v6'/%3E%3Cpath d='m10 14 11-11'/%3E%3Cpath d='M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6'/%3E%3C/svg%3E")
327
+ center / contain no-repeat;
328
+ }
329
+
330
+ :where(
331
+ .pmx-material-ultra-thin,
332
+ .pmx-material-thin,
333
+ .pmx-material-regular,
334
+ .pmx-material-thick,
335
+ .pmx-material-ultra-thick
336
+ ) {
337
+ backdrop-filter: var(--pmx-material-filter);
338
+ }
339
+
340
+ :where(.pmx-material-ultra-thin) {
341
+ background: var(--pmx-material-ultra-thin);
342
+ }
343
+
344
+ :where(.pmx-material-thin) {
345
+ background: var(--pmx-material-thin);
346
+ }
347
+
348
+ :where(.pmx-material-regular) {
349
+ background: var(--pmx-material);
350
+ }
351
+
352
+ :where(.pmx-material-thick) {
353
+ background: var(--pmx-material-thick);
354
+ }
355
+
356
+ :where(.pmx-material-ultra-thick) {
357
+ background: var(--pmx-material-ultra-thick);
358
+ }
@@ -0,0 +1,333 @@
1
+ import { cachedRouteTargets } from "../../invalidation.ts"
2
+ import type { HtmlElement } from "../../environment.ts"
3
+ import { posixPath } from "../../path.ts"
4
+ import { isIndexSource } from "../../server/router.ts"
5
+ import type { Source } from "../../source/index.ts"
6
+ import type { Plugin, PluginLog } from "../types.ts"
7
+ import foundation from "./foundation.css" with { type: "text" }
8
+ import prose from "./prose.css" with { type: "text" }
9
+ import viewTransition from "./view-transition.css" with { type: "text" }
10
+ import { SchemeToggleComponent } from "./components.ts"
11
+
12
+ type ThemeGroup = Record<string, string | number | undefined>
13
+
14
+ export type PathMXTheme = {
15
+ color?: ThemeGroup
16
+ font?: ThemeGroup
17
+ prose?: ThemeGroup
18
+ shape?: ThemeGroup
19
+ code?: ThemeGroup
20
+ measure?: string
21
+ light?: PathMXTheme
22
+ dark?: PathMXTheme
23
+ }
24
+
25
+ const colorTokens: Record<string, string> = {
26
+ bg: "--pmx-color-bg",
27
+ fg: "--pmx-color-fg",
28
+ muted: "--pmx-color-muted",
29
+ surface: "--pmx-color-surface",
30
+ link: "--pmx-color-link",
31
+ accent: "--pmx-color-accent",
32
+ danger: "--pmx-color-danger",
33
+ border: "--pmx-color-border",
34
+ focus: "--pmx-color-focus",
35
+ }
36
+
37
+ const fontTokens: Record<string, string> = {
38
+ body: "--pmx-font-body",
39
+ heading: "--pmx-font-heading",
40
+ mono: "--pmx-font-mono",
41
+ }
42
+
43
+ const proseTokens: Record<string, string> = {
44
+ size: "--pmx-prose-size",
45
+ leading: "--pmx-prose-leading",
46
+ flow: "--pmx-prose-flow",
47
+ gutter: "--pmx-prose-gutter",
48
+ }
49
+
50
+ const shapeTokens: Record<string, string> = {
51
+ radius: "--pmx-radius",
52
+ }
53
+
54
+ const codeTokens: Record<string, string> = {
55
+ bg: "--pmx-code-bg",
56
+ fg: "--pmx-code-fg",
57
+ comment: "--pmx-code-comment",
58
+ keyword: "--pmx-code-keyword",
59
+ string: "--pmx-code-string",
60
+ punctuation: "--pmx-code-punctuation",
61
+ }
62
+
63
+ const variantKeys = ["color", "font", "prose", "shape", "code"] as const
64
+ const themeKeys = [...variantKeys, "measure", "light", "dark"] as const
65
+
66
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
67
+ return Boolean(value) && typeof value === "object" && !Array.isArray(value)
68
+ }
69
+
70
+ function isRootIndex(source: Source) {
71
+ return isIndexSource(source) && posixPath.dirname(source.path) === "/"
72
+ }
73
+
74
+ function readGroup(
75
+ value: unknown,
76
+ tokens: Record<string, string>,
77
+ log: PluginLog | undefined,
78
+ path: string,
79
+ ): ThemeGroup | undefined {
80
+ if (value === undefined) return
81
+ if (!isPlainObject(value)) {
82
+ log?.warn("invalid", { path, detail: "Expected an object." })
83
+ return
84
+ }
85
+ const group: ThemeGroup = {}
86
+ for (const [key, raw] of Object.entries(value)) {
87
+ if (!tokens[key]) {
88
+ // TODO: move this to zod/standard schema based validation
89
+ log?.warn("unknown", { path: `${path}.${key}` })
90
+ continue
91
+ }
92
+ if (typeof raw === "string" || typeof raw === "number") group[key] = raw
93
+ }
94
+ return Object.keys(group).length ? group : undefined
95
+ }
96
+
97
+ function readVariant(
98
+ value: unknown,
99
+ log: PluginLog | undefined,
100
+ path: string,
101
+ extra: readonly string[] = [],
102
+ ): PathMXTheme | undefined {
103
+ if (value === undefined) return
104
+ if (!isPlainObject(value)) {
105
+ log?.warn("invalid", { path, detail: "Expected an object." })
106
+ return
107
+ }
108
+ for (const key of Object.keys(value)) {
109
+ if (
110
+ ![...variantKeys, ...extra].includes(key as (typeof themeKeys)[number])
111
+ ) {
112
+ // TODO: move this to zod/standard schema based validation
113
+ log?.warn("unknown", { path: `${path}.${key}` })
114
+ }
115
+ }
116
+ const variant: PathMXTheme = {
117
+ color: readGroup(value.color, colorTokens, log, `${path}.color`),
118
+ font: readGroup(value.font, fontTokens, log, `${path}.font`),
119
+ prose: readGroup(value.prose, proseTokens, log, `${path}.prose`),
120
+ shape: readGroup(value.shape, shapeTokens, log, `${path}.shape`),
121
+ code: readGroup(value.code, codeTokens, log, `${path}.code`),
122
+ }
123
+ return Object.values(variant).some(Boolean) ? variant : undefined
124
+ }
125
+
126
+ export function normalizeTheme(
127
+ value: unknown,
128
+ log?: PluginLog,
129
+ ): PathMXTheme | undefined {
130
+ if (value === undefined) return
131
+ if (!isPlainObject(value)) {
132
+ log?.warn("invalid", { detail: "Expected an object." })
133
+ return
134
+ }
135
+ const base = readVariant(value, log, "theme", ["measure", "light", "dark"])
136
+ const measure = typeof value.measure === "string" ? value.measure : undefined
137
+ const light = readVariant(value.light, log, "theme.light")
138
+ const dark = readVariant(value.dark, log, "theme.dark")
139
+ const theme: PathMXTheme = {
140
+ ...(base ?? {}),
141
+ ...(measure ? { measure } : {}),
142
+ ...(light ? { light } : {}),
143
+ ...(dark ? { dark } : {}),
144
+ }
145
+ return Object.keys(theme).length ? theme : undefined
146
+ }
147
+
148
+ function declarations(theme: PathMXTheme | undefined) {
149
+ if (!theme) return []
150
+ const out: string[] = []
151
+ const add = (
152
+ group: ThemeGroup | undefined,
153
+ tokens: Record<string, string>,
154
+ ) => {
155
+ if (!group) return
156
+ for (const [key, value] of Object.entries(group)) {
157
+ const name = tokens[key]
158
+ if (name && value !== undefined) out.push(`${name}: ${value};`)
159
+ }
160
+ }
161
+ add(theme.color, colorTokens)
162
+ add(theme.font, fontTokens)
163
+ add(theme.prose, proseTokens)
164
+ add(theme.shape, shapeTokens)
165
+ add(theme.code, codeTokens)
166
+ if (theme.measure) out.push(`--pmx-document-measure: ${theme.measure};`)
167
+ return out
168
+ }
169
+
170
+ function renderRule(selector: string, decls: readonly string[]) {
171
+ if (!decls.length) return ""
172
+ return `${selector} {\n${decls.map((line) => ` ${line}`).join("\n")}\n}`
173
+ }
174
+
175
+ function indentRule(css: string) {
176
+ return css
177
+ .split("\n")
178
+ .map((line) => (line ? ` ${line}` : line))
179
+ .join("\n")
180
+ }
181
+
182
+ function forcedSelector(selector: string, scheme: "light" | "dark") {
183
+ return selector === ":root"
184
+ ? `:root[data-pmx-scheme="${scheme}"]`
185
+ : `[data-pmx-scheme="${scheme}"] ${selector}`
186
+ }
187
+
188
+ function systemSelector(selector: string, scheme: "light" | "dark") {
189
+ const opposite = scheme === "light" ? "dark" : "light"
190
+ return selector === ":root"
191
+ ? `:root:not([data-pmx-scheme="${opposite}"])`
192
+ : `:root:not([data-pmx-scheme="${opposite}"]) ${selector}`
193
+ }
194
+
195
+ function renderSchemeVariants(
196
+ selector: string,
197
+ scheme: "light" | "dark",
198
+ decls: readonly string[],
199
+ ) {
200
+ if (!decls.length) return []
201
+ return [
202
+ renderRule(forcedSelector(selector, scheme), decls),
203
+ `@media (prefers-color-scheme: ${scheme}) {\n${indentRule(
204
+ renderRule(systemSelector(selector, scheme), decls),
205
+ )}\n}`,
206
+ ]
207
+ }
208
+
209
+ export function renderThemeStyle(theme: PathMXTheme, selector: string) {
210
+ return [
211
+ renderRule(selector, declarations(theme)),
212
+ ...renderSchemeVariants(selector, "light", declarations(theme.light)),
213
+ ...renderSchemeVariants(selector, "dark", declarations(theme.dark)),
214
+ ]
215
+ .filter(Boolean)
216
+ .join("\n\n")
217
+ }
218
+
219
+ export const THEME_LIGHT_BG = "oklch(0.985 0 0)"
220
+ export const THEME_DARK_BG = "oklch(0.18 0.01 250)"
221
+
222
+ export function themeBackground(
223
+ theme: PathMXTheme | undefined,
224
+ scheme: "light" | "dark",
225
+ ) {
226
+ const authored = theme?.[scheme]?.color?.bg ?? theme?.color?.bg
227
+ if (typeof authored === "string" || typeof authored === "number")
228
+ return String(authored)
229
+ return scheme === "light" ? THEME_LIGHT_BG : THEME_DARK_BG
230
+ }
231
+
232
+ function rootThemeSignature(source?: Source) {
233
+ if (!source || !isRootIndex(source)) return ""
234
+ const theme = normalizeTheme(source.data.theme)
235
+ if (!theme) return ""
236
+ return [
237
+ renderThemeStyle(theme, ":root"),
238
+ themeBackground(theme, "light"),
239
+ themeBackground(theme, "dark"),
240
+ ].join("\n")
241
+ }
242
+
243
+ function appendCriticalBackground(
244
+ element: HtmlElement,
245
+ theme: PathMXTheme | undefined,
246
+ ) {
247
+ const current = element.getAttribute("style")?.trim() ?? ""
248
+ const scheme = element.getAttribute("data-pmx-scheme")
249
+ const declarations = current
250
+ ? [current.replace(/;?$/, ";")]
251
+ : [
252
+ `color-scheme: ${scheme === "light" || scheme === "dark" ? scheme : "light dark"};`,
253
+ ]
254
+ declarations.push(
255
+ `background-color: light-dark(${themeBackground(theme, "light")}, ${themeBackground(theme, "dark")});`,
256
+ )
257
+ element.setAttribute("style", declarations.join(" "))
258
+ }
259
+
260
+ export function ThemePlugin(): Plugin {
261
+ let rootTheme: PathMXTheme | undefined
262
+ let rootKeys: string[] = []
263
+
264
+ return {
265
+ id: "theme",
266
+ index(source, app) {
267
+ if (!isRootIndex(source)) return
268
+ rootKeys = [source.path, source.id]
269
+ rootTheme = normalizeTheme(source.data.theme, app.log)
270
+ },
271
+
272
+ unindex(sourcePath) {
273
+ if (!rootKeys.includes(sourcePath)) return
274
+ rootTheme = undefined
275
+ rootKeys = []
276
+ },
277
+
278
+ components: [SchemeToggleComponent],
279
+
280
+ documentAssets(ctx) {
281
+ const text = rootTheme
282
+ ? `${foundation}\n\n${prose}\n\n${viewTransition}\n\n${renderThemeStyle(rootTheme, ":root")}`
283
+ : `${foundation}\n\n${prose}\n\n${viewTransition}`
284
+ ctx.addAsset(
285
+ { type: "stylesheet", name: "theme.css", text },
286
+ {
287
+ type: "meta",
288
+ attrs: { name: "color-scheme", content: "light dark" },
289
+ },
290
+ {
291
+ type: "meta",
292
+ attrs: {
293
+ name: "theme-color",
294
+ media: "(prefers-color-scheme: light)",
295
+ content: themeBackground(rootTheme, "light"),
296
+ },
297
+ },
298
+ {
299
+ type: "meta",
300
+ attrs: {
301
+ name: "theme-color",
302
+ media: "(prefers-color-scheme: dark)",
303
+ content: themeBackground(rootTheme, "dark"),
304
+ },
305
+ },
306
+ )
307
+ },
308
+
309
+ streamSelectors() {
310
+ return {
311
+ html: {
312
+ element: (element) => appendCriticalBackground(element, rootTheme),
313
+ },
314
+ }
315
+ },
316
+
317
+ invalidate({ before, after }, app) {
318
+ if (rootThemeSignature(before) === rootThemeSignature(after)) return
319
+ return { targets: cachedRouteTargets(app.sources.builtPages()) }
320
+ },
321
+
322
+ page(html, ctx, app) {
323
+ if (isRootIndex(ctx.source)) return html
324
+ const theme = normalizeTheme(ctx.source.data.theme, app.log)
325
+ if (theme)
326
+ ctx.addAsset({
327
+ type: "style",
328
+ text: renderThemeStyle(theme, ".pmx-document"),
329
+ })
330
+ return html
331
+ },
332
+ }
333
+ }