@kayjs/kay 1.105.0 → 1.107.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 (467) hide show
  1. package/README.md +2 -2
  2. package/dist/eslint.js +777 -210
  3. package/editors/zed/extension.toml +2 -2
  4. package/llms/action-protection.md +2 -2
  5. package/llms/actions-from-islands.md +2 -2
  6. package/llms/actions-refused-fields.md +22 -0
  7. package/llms/actions.md +24 -41
  8. package/llms/analytics.md +1 -1
  9. package/llms/api-from-islands.md +1 -1
  10. package/llms/api-routes.md +6 -6
  11. package/llms/browser-only.md +1 -1
  12. package/llms/check-and-tools.md +1 -1
  13. package/llms/code-lints.md +2 -2
  14. package/llms/compiler-api.md +1 -1
  15. package/llms/component-libraries.md +1 -0
  16. package/llms/components.md +1 -1
  17. package/llms/content-cms.md +1 -1
  18. package/llms/content-markdown-to-caching.md +1 -1
  19. package/llms/content-preview.md +2 -2
  20. package/llms/content.md +21 -21
  21. package/llms/deprecated-lints-1-105-libraries.md +2 -0
  22. package/llms/deprecated-lints-1-105.md +3 -3
  23. package/llms/deprecated-lints.md +4 -4
  24. package/llms/dev-tool.md +2 -2
  25. package/llms/environment.md +22 -22
  26. package/llms/files.md +5 -5
  27. package/llms/island-and-form-lints.md +1 -0
  28. package/llms/islands-state-to-ids.md +2 -1
  29. package/llms/islands.md +10 -1
  30. package/llms/local-collections.md +2 -2
  31. package/llms/mcp.md +2 -1
  32. package/llms/navigation-from-code.md +1 -1
  33. package/llms/notifications.md +4 -2
  34. package/llms/project-new-and-add.md +1 -1
  35. package/llms/project.md +3 -3
  36. package/llms/rate-limits.md +1 -1
  37. package/llms/recipes.md +16 -16
  38. package/llms/rooms.md +4 -4
  39. package/llms/routing-endpoints-and-base.md +1 -1
  40. package/llms/routing.md +5 -5
  41. package/llms/schemas.md +14 -7
  42. package/llms/server.md +7 -7
  43. package/llms/site.md +9 -9
  44. package/llms/sitemap-search-fonts.md +2 -0
  45. package/llms/styles-and-assets.md +2 -1
  46. package/llms/templates-elements-slots-snippets.md +2 -2
  47. package/llms/testing-browser.md +20 -0
  48. package/llms/testing.md +1 -1
  49. package/llms/upgrading-1-105-config.md +2 -2
  50. package/llms/upgrading.md +37 -4
  51. package/llms/whats-new-archive.md +35 -66
  52. package/llms/whats-new.md +47 -28
  53. package/llms.txt +1 -1
  54. package/package.json +24 -11
  55. package/src/add.ts +1 -1
  56. package/src/build/class-table.ts +2 -2
  57. package/src/build/client.ts +27 -30
  58. package/src/build/entry-files.ts +19 -0
  59. package/src/build/lazy-components.ts +130 -0
  60. package/src/build/lazy-manifest.ts +56 -0
  61. package/src/build/lazy-stubs.ts +31 -0
  62. package/src/build/plugin.ts +16 -11
  63. package/src/build/runtime-chunk.ts +1 -1
  64. package/src/build/styles.ts +3 -3
  65. package/src/build/transform.ts +19 -7
  66. package/src/build/ui-layers.ts +30 -0
  67. package/src/build/ui.ts +108 -11
  68. package/src/build/variants.ts +125 -46
  69. package/src/build/worker-bundle.ts +6 -0
  70. package/src/check/action-options.ts +56 -0
  71. package/src/check/boot.ts +22 -1
  72. package/src/check/call-result.ts +242 -0
  73. package/src/check/config-objects.ts +17 -2
  74. package/src/check/diagnose.ts +22 -0
  75. package/src/check/handler-events.ts +149 -0
  76. package/src/check/index.ts +6 -2
  77. package/src/check/link-flag-tests.ts +130 -0
  78. package/src/check/project.ts +4 -3
  79. package/src/check/refusal-calls.ts +2 -2
  80. package/src/check/regroup.ts +5 -3
  81. package/src/check/renamed-packages.ts +58 -14
  82. package/src/check/schema-builders.ts +256 -0
  83. package/src/check/schema-json.ts +226 -0
  84. package/src/check/secret-keys.ts +15 -2
  85. package/src/check/shadow-types.ts +13 -9
  86. package/src/check/template-hints.ts +1 -1
  87. package/src/check/tool-labels.ts +11 -9
  88. package/src/check/ui-types.ts +4 -3
  89. package/src/check/ui.ts +14 -11
  90. package/src/commands/env-declare.ts +18 -6
  91. package/src/commands/routes.ts +1 -1
  92. package/src/compiler/a11y.ts +8 -1
  93. package/src/compiler/ast.ts +1 -1
  94. package/src/compiler/bound-props.ts +1 -1
  95. package/src/compiler/codegen.ts +13 -23
  96. package/src/compiler/deprecated-lints-1-105.ts +41 -0
  97. package/src/compiler/deprecated-lints-1-106.ts +14 -0
  98. package/src/compiler/deprecated-lints-1-107.ts +14 -0
  99. package/src/compiler/deprecated-lints.ts +17 -60
  100. package/src/compiler/deprecated-typed-lints.ts +23 -0
  101. package/src/compiler/dom-here.ts +72 -0
  102. package/src/compiler/dom.ts +23 -23
  103. package/src/compiler/fence-checks.ts +12 -11
  104. package/src/compiler/field-described.ts +2 -2
  105. package/src/compiler/helper-imports.ts +1 -1
  106. package/src/compiler/html-fields.ts +92 -0
  107. package/src/compiler/html-initial.ts +5 -3
  108. package/src/compiler/html.ts +14 -31
  109. package/src/compiler/import-edits.ts +15 -0
  110. package/src/compiler/index.ts +10 -3
  111. package/src/compiler/island-declaration.ts +15 -0
  112. package/src/compiler/items.ts +5 -5
  113. package/src/compiler/kay-exports.ts +1 -0
  114. package/src/compiler/lazy-imports.ts +357 -0
  115. package/src/compiler/link-flags.ts +6 -5
  116. package/src/compiler/lints.ts +12 -12
  117. package/src/compiler/parser.ts +2 -2
  118. package/src/compiler/rest-omits.ts +15 -0
  119. package/src/compiler/route-lints.ts +2 -0
  120. package/src/compiler/schema-lints.ts +13 -0
  121. package/src/compiler/script-server.ts +6 -3
  122. package/src/config-loader.ts +10 -2
  123. package/src/config-resolve.ts +3 -1
  124. package/src/config-shape.ts +2 -2
  125. package/src/config.ts +20 -4
  126. package/src/content/markdown.ts +2 -3
  127. package/src/content/validate.ts +1 -1
  128. package/src/generate-resource.ts +9 -9
  129. package/src/generate.ts +1 -1
  130. package/src/mcp/guards.ts +1 -1
  131. package/src/mcp/tools.ts +1 -1
  132. package/src/migrate/content.ts +61 -55
  133. package/src/new.ts +6 -2
  134. package/src/runtime/action-shared.ts +2 -2
  135. package/src/runtime/action.ts +51 -4
  136. package/src/runtime/client.ts +10 -0
  137. package/src/runtime/dom-lazy.ts +276 -0
  138. package/src/runtime/field-errors.ts +58 -0
  139. package/src/runtime/field-refusal.ts +179 -0
  140. package/src/runtime/form-types.ts +99 -0
  141. package/src/runtime/form.ts +25 -9
  142. package/src/runtime/html.ts +42 -6
  143. package/src/runtime/index.ts +2 -0
  144. package/src/runtime/island.server.ts +36 -2
  145. package/src/runtime/markdown-autolink.ts +69 -0
  146. package/src/runtime/markdown-render.client.ts +39 -3
  147. package/src/runtime/markdown.client.ts +6 -3
  148. package/src/runtime/markdown.server.ts +6 -3
  149. package/src/runtime/markdown.ts +125 -1
  150. package/src/runtime/navigate-api.ts +10 -5
  151. package/src/runtime/persist.ts +7 -7
  152. package/src/runtime/query-param.ts +1 -1
  153. package/src/runtime/reactive.ts +14 -0
  154. package/src/runtime/sanitize.ts +3 -1
  155. package/src/runtime/schema-record.ts +32 -0
  156. package/src/runtime/schema.client.ts +101 -14
  157. package/src/runtime/schema.ts +328 -26
  158. package/src/runtime/standard-schema.ts +32 -0
  159. package/src/runtime/swap-focus.ts +59 -11
  160. package/src/runtime/validate-run.client.ts +179 -0
  161. package/src/runtime/validate.client.ts +11 -0
  162. package/src/runtime/validate.ts +4 -137
  163. package/src/server/action-module.ts +16 -0
  164. package/src/server/actions.ts +54 -57
  165. package/src/server/answer.ts +102 -0
  166. package/src/server/api-input.ts +166 -0
  167. package/src/server/api-modules.ts +1 -1
  168. package/src/server/api.ts +12 -32
  169. package/src/server/app-stages.ts +38 -26
  170. package/src/server/app.ts +5 -2
  171. package/src/server/client.ts +2 -0
  172. package/src/server/dev-ui.ts +2 -2
  173. package/src/server/dev.ts +7 -9
  174. package/src/server/env-state.ts +1 -1
  175. package/src/server/env.ts +11 -3
  176. package/src/server/favicon.ts +47 -1
  177. package/src/server/file-limit.ts +7 -1
  178. package/src/server/forms.ts +1 -1
  179. package/src/server/host-allowlist.ts +7 -7
  180. package/src/server/image-metadata-bmff.ts +1 -1
  181. package/src/server/index.ts +2 -0
  182. package/src/server/kay-routes.ts +221 -0
  183. package/src/server/kay-server.ts +228 -0
  184. package/src/server/kept-exports.ts +1 -0
  185. package/src/server/kept-pages.ts +3 -3
  186. package/src/server/library.ts +2 -2
  187. package/src/server/mcp.ts +14 -13
  188. package/src/server/notifications.ts +4 -2
  189. package/src/server/offline.ts +24 -7
  190. package/src/server/openapi.ts +12 -3
  191. package/src/server/phases.ts +249 -0
  192. package/src/server/protection.ts +6 -6
  193. package/src/server/proxy-key.ts +8 -8
  194. package/src/server/push-settings.ts +16 -2
  195. package/src/server/push.ts +12 -4
  196. package/src/server/query.ts +17 -31
  197. package/src/server/receive.ts +43 -0
  198. package/src/server/render-document.ts +13 -3
  199. package/src/server/render.ts +1 -1
  200. package/src/server/request-context.ts +137 -0
  201. package/src/server/schema-check.ts +1 -1
  202. package/src/server/scope.ts +20 -13
  203. package/src/server/signals.ts +4 -4
  204. package/src/server/socket.ts +19 -47
  205. package/src/server/stream.ts +1 -1
  206. package/src/server/validate.ts +346 -0
  207. package/src/template-ws.ts +1 -1
  208. package/src/test/a11y-dom.ts +70 -0
  209. package/src/test/html-snapshot.ts +103 -0
  210. package/src/test/index.ts +2 -1
  211. package/src/test/mount.ts +14 -3
  212. package/src/test/page-checks.ts +12 -0
  213. package/src/test/screenshot-match.ts +2 -2
  214. package/src/test/shared-modules.ts +27 -5
  215. package/src/test/site-types.ts +5 -1
  216. package/types/build/class-table.d.ts +1 -1
  217. package/types/build/class-table.d.ts.map +1 -1
  218. package/types/build/client.d.ts +3 -2
  219. package/types/build/client.d.ts.map +1 -1
  220. package/types/build/entry-files.d.ts +4 -0
  221. package/types/build/entry-files.d.ts.map +1 -0
  222. package/types/build/lazy-components.d.ts +6 -0
  223. package/types/build/lazy-components.d.ts.map +1 -0
  224. package/types/build/lazy-manifest.d.ts +19 -0
  225. package/types/build/lazy-manifest.d.ts.map +1 -0
  226. package/types/build/lazy-stubs.d.ts +5 -0
  227. package/types/build/lazy-stubs.d.ts.map +1 -0
  228. package/types/build/plugin.d.ts +1 -0
  229. package/types/build/plugin.d.ts.map +1 -1
  230. package/types/build/transform.d.ts +4 -1
  231. package/types/build/transform.d.ts.map +1 -1
  232. package/types/build/ui-layers.d.ts +5 -0
  233. package/types/build/ui-layers.d.ts.map +1 -0
  234. package/types/build/ui.d.ts +7 -0
  235. package/types/build/ui.d.ts.map +1 -1
  236. package/types/build/variants.d.ts +1 -1
  237. package/types/build/variants.d.ts.map +1 -1
  238. package/types/build/worker-bundle.d.ts +1 -0
  239. package/types/build/worker-bundle.d.ts.map +1 -1
  240. package/types/check/action-options.d.ts +3 -0
  241. package/types/check/action-options.d.ts.map +1 -0
  242. package/types/check/boot.d.ts.map +1 -1
  243. package/types/check/call-result.d.ts +7 -0
  244. package/types/check/call-result.d.ts.map +1 -0
  245. package/types/check/config-objects.d.ts.map +1 -1
  246. package/types/check/diagnose.d.ts.map +1 -1
  247. package/types/check/handler-events.d.ts +9 -0
  248. package/types/check/handler-events.d.ts.map +1 -0
  249. package/types/check/index.d.ts +1 -0
  250. package/types/check/index.d.ts.map +1 -1
  251. package/types/check/link-flag-tests.d.ts +4 -0
  252. package/types/check/link-flag-tests.d.ts.map +1 -0
  253. package/types/check/project.d.ts.map +1 -1
  254. package/types/check/regroup.d.ts +1 -1
  255. package/types/check/regroup.d.ts.map +1 -1
  256. package/types/check/renamed-packages.d.ts +1 -0
  257. package/types/check/renamed-packages.d.ts.map +1 -1
  258. package/types/check/schema-builders.d.ts +3 -0
  259. package/types/check/schema-builders.d.ts.map +1 -0
  260. package/types/check/schema-json.d.ts +3 -0
  261. package/types/check/schema-json.d.ts.map +1 -0
  262. package/types/check/secret-keys.d.ts.map +1 -1
  263. package/types/check/shadow-types.d.ts.map +1 -1
  264. package/types/check/template-hints.d.ts +1 -0
  265. package/types/check/template-hints.d.ts.map +1 -1
  266. package/types/check/tool-labels.d.ts.map +1 -1
  267. package/types/check/ui-types.d.ts.map +1 -1
  268. package/types/check/ui.d.ts.map +1 -1
  269. package/types/commands/env-declare.d.ts.map +1 -1
  270. package/types/compiler/a11y.d.ts.map +1 -1
  271. package/types/compiler/bound-props.d.ts +1 -0
  272. package/types/compiler/bound-props.d.ts.map +1 -1
  273. package/types/compiler/codegen.d.ts +3 -0
  274. package/types/compiler/codegen.d.ts.map +1 -1
  275. package/types/compiler/deprecated-lints-1-105.d.ts +39 -0
  276. package/types/compiler/deprecated-lints-1-105.d.ts.map +1 -0
  277. package/types/compiler/deprecated-lints-1-106.d.ts +12 -0
  278. package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -0
  279. package/types/compiler/deprecated-lints-1-107.d.ts +12 -0
  280. package/types/compiler/deprecated-lints-1-107.d.ts.map +1 -0
  281. package/types/compiler/deprecated-lints.d.ts +41 -22
  282. package/types/compiler/deprecated-lints.d.ts.map +1 -1
  283. package/types/compiler/deprecated-typed-lints.d.ts +21 -0
  284. package/types/compiler/deprecated-typed-lints.d.ts.map +1 -0
  285. package/types/compiler/dom-here.d.ts +14 -0
  286. package/types/compiler/dom-here.d.ts.map +1 -0
  287. package/types/compiler/dom.d.ts +1 -0
  288. package/types/compiler/dom.d.ts.map +1 -1
  289. package/types/compiler/fence-checks.d.ts.map +1 -1
  290. package/types/compiler/field-described.d.ts +1 -1
  291. package/types/compiler/field-described.d.ts.map +1 -1
  292. package/types/compiler/html-fields.d.ts +19 -0
  293. package/types/compiler/html-fields.d.ts.map +1 -0
  294. package/types/compiler/html-initial.d.ts +3 -2
  295. package/types/compiler/html-initial.d.ts.map +1 -1
  296. package/types/compiler/html.d.ts.map +1 -1
  297. package/types/compiler/import-edits.d.ts +1 -0
  298. package/types/compiler/import-edits.d.ts.map +1 -1
  299. package/types/compiler/index.d.ts +3 -0
  300. package/types/compiler/index.d.ts.map +1 -1
  301. package/types/compiler/island-declaration.d.ts +5 -0
  302. package/types/compiler/island-declaration.d.ts.map +1 -1
  303. package/types/compiler/items.d.ts +4 -1
  304. package/types/compiler/items.d.ts.map +1 -1
  305. package/types/compiler/kay-exports.d.ts.map +1 -1
  306. package/types/compiler/lazy-imports.d.ts +33 -0
  307. package/types/compiler/lazy-imports.d.ts.map +1 -0
  308. package/types/compiler/link-flags.d.ts +6 -0
  309. package/types/compiler/link-flags.d.ts.map +1 -1
  310. package/types/compiler/lints.d.ts +61 -34
  311. package/types/compiler/lints.d.ts.map +1 -1
  312. package/types/compiler/rest-omits.d.ts +4 -0
  313. package/types/compiler/rest-omits.d.ts.map +1 -0
  314. package/types/compiler/route-lints.d.ts +8 -0
  315. package/types/compiler/route-lints.d.ts.map +1 -1
  316. package/types/compiler/schema-lints.d.ts +11 -0
  317. package/types/compiler/schema-lints.d.ts.map +1 -0
  318. package/types/compiler/script-server.d.ts.map +1 -1
  319. package/types/config-loader.d.ts.map +1 -1
  320. package/types/config-resolve.d.ts.map +1 -1
  321. package/types/config.d.ts +15 -2
  322. package/types/config.d.ts.map +1 -1
  323. package/types/content/markdown.d.ts.map +1 -1
  324. package/types/migrate/content.d.ts.map +1 -1
  325. package/types/new.d.ts +1 -0
  326. package/types/new.d.ts.map +1 -1
  327. package/types/runtime/action-shared.d.ts +1 -1
  328. package/types/runtime/action-shared.d.ts.map +1 -1
  329. package/types/runtime/action.d.ts +29 -1
  330. package/types/runtime/action.d.ts.map +1 -1
  331. package/types/runtime/client.d.ts.map +1 -1
  332. package/types/runtime/dom-lazy.d.ts +6 -0
  333. package/types/runtime/dom-lazy.d.ts.map +1 -0
  334. package/types/runtime/field-errors.d.ts +4 -0
  335. package/types/runtime/field-errors.d.ts.map +1 -0
  336. package/types/runtime/field-refusal.d.ts +37 -0
  337. package/types/runtime/field-refusal.d.ts.map +1 -0
  338. package/types/runtime/form-types.d.ts +6 -0
  339. package/types/runtime/form-types.d.ts.map +1 -0
  340. package/types/runtime/form.d.ts +4 -2
  341. package/types/runtime/form.d.ts.map +1 -1
  342. package/types/runtime/html.d.ts +3 -0
  343. package/types/runtime/html.d.ts.map +1 -1
  344. package/types/runtime/index.d.ts +2 -0
  345. package/types/runtime/index.d.ts.map +1 -1
  346. package/types/runtime/island.server.d.ts +2 -0
  347. package/types/runtime/island.server.d.ts.map +1 -1
  348. package/types/runtime/markdown-autolink.d.ts +11 -0
  349. package/types/runtime/markdown-autolink.d.ts.map +1 -0
  350. package/types/runtime/markdown-render.client.d.ts +2 -1
  351. package/types/runtime/markdown-render.client.d.ts.map +1 -1
  352. package/types/runtime/markdown.client.d.ts +3 -1
  353. package/types/runtime/markdown.client.d.ts.map +1 -1
  354. package/types/runtime/markdown.d.ts +11 -0
  355. package/types/runtime/markdown.d.ts.map +1 -1
  356. package/types/runtime/markdown.server.d.ts +3 -1
  357. package/types/runtime/markdown.server.d.ts.map +1 -1
  358. package/types/runtime/navigate-api.d.ts +1 -1
  359. package/types/runtime/navigate-api.d.ts.map +1 -1
  360. package/types/runtime/persist.d.ts +2 -2
  361. package/types/runtime/persist.d.ts.map +1 -1
  362. package/types/runtime/reactive.d.ts +3 -0
  363. package/types/runtime/reactive.d.ts.map +1 -1
  364. package/types/runtime/sanitize.d.ts.map +1 -1
  365. package/types/runtime/schema-record.d.ts +9 -0
  366. package/types/runtime/schema-record.d.ts.map +1 -0
  367. package/types/runtime/schema.client.d.ts.map +1 -1
  368. package/types/runtime/schema.d.ts +99 -10
  369. package/types/runtime/schema.d.ts.map +1 -1
  370. package/types/runtime/standard-schema.d.ts +37 -0
  371. package/types/runtime/standard-schema.d.ts.map +1 -0
  372. package/types/runtime/swap-focus.d.ts.map +1 -1
  373. package/types/runtime/validate-run.client.d.ts +4 -0
  374. package/types/runtime/validate-run.client.d.ts.map +1 -0
  375. package/types/runtime/validate.client.d.ts +4 -0
  376. package/types/runtime/validate.client.d.ts.map +1 -0
  377. package/types/runtime/validate.d.ts +1 -21
  378. package/types/runtime/validate.d.ts.map +1 -1
  379. package/types/server/action-module.d.ts +2 -0
  380. package/types/server/action-module.d.ts.map +1 -0
  381. package/types/server/actions.d.ts +1 -0
  382. package/types/server/actions.d.ts.map +1 -1
  383. package/types/server/answer.d.ts +6 -0
  384. package/types/server/answer.d.ts.map +1 -0
  385. package/types/server/api-input.d.ts +19 -0
  386. package/types/server/api-input.d.ts.map +1 -0
  387. package/types/server/api.d.ts.map +1 -1
  388. package/types/server/app-stages.d.ts +7 -3
  389. package/types/server/app-stages.d.ts.map +1 -1
  390. package/types/server/app.d.ts +2 -0
  391. package/types/server/app.d.ts.map +1 -1
  392. package/types/server/client.d.ts +1 -0
  393. package/types/server/client.d.ts.map +1 -1
  394. package/types/server/dev.d.ts +1 -1
  395. package/types/server/dev.d.ts.map +1 -1
  396. package/types/server/env-state.d.ts.map +1 -1
  397. package/types/server/env.d.ts +5 -1
  398. package/types/server/env.d.ts.map +1 -1
  399. package/types/server/favicon.d.ts +3 -1
  400. package/types/server/favicon.d.ts.map +1 -1
  401. package/types/server/file-limit.d.ts.map +1 -1
  402. package/types/server/host-allowlist.d.ts +3 -3
  403. package/types/server/host-allowlist.d.ts.map +1 -1
  404. package/types/server/index.d.ts +2 -0
  405. package/types/server/index.d.ts.map +1 -1
  406. package/types/server/kay-routes.d.ts +47 -0
  407. package/types/server/kay-routes.d.ts.map +1 -0
  408. package/types/server/kay-server.d.ts +25 -0
  409. package/types/server/kay-server.d.ts.map +1 -0
  410. package/types/server/kept-exports.d.ts.map +1 -1
  411. package/types/server/kept-pages.d.ts +1 -1
  412. package/types/server/kept-pages.d.ts.map +1 -1
  413. package/types/server/library.d.ts +2 -1
  414. package/types/server/library.d.ts.map +1 -1
  415. package/types/server/mcp.d.ts +1 -1
  416. package/types/server/mcp.d.ts.map +1 -1
  417. package/types/server/notifications.d.ts +2 -1
  418. package/types/server/notifications.d.ts.map +1 -1
  419. package/types/server/offline.d.ts.map +1 -1
  420. package/types/server/openapi.d.ts.map +1 -1
  421. package/types/server/phases.d.ts +36 -0
  422. package/types/server/phases.d.ts.map +1 -0
  423. package/types/server/protection.d.ts +3 -2
  424. package/types/server/protection.d.ts.map +1 -1
  425. package/types/server/proxy-key.d.ts +3 -3
  426. package/types/server/proxy-key.d.ts.map +1 -1
  427. package/types/server/push-settings.d.ts +1 -1
  428. package/types/server/push-settings.d.ts.map +1 -1
  429. package/types/server/push.d.ts +1 -0
  430. package/types/server/push.d.ts.map +1 -1
  431. package/types/server/query.d.ts.map +1 -1
  432. package/types/server/receive.d.ts +9 -0
  433. package/types/server/receive.d.ts.map +1 -0
  434. package/types/server/render-document.d.ts.map +1 -1
  435. package/types/server/request-context.d.ts +29 -0
  436. package/types/server/request-context.d.ts.map +1 -0
  437. package/types/server/schema-check.d.ts +1 -0
  438. package/types/server/schema-check.d.ts.map +1 -1
  439. package/types/server/scope.d.ts +1 -1
  440. package/types/server/scope.d.ts.map +1 -1
  441. package/types/server/socket.d.ts +3 -5
  442. package/types/server/socket.d.ts.map +1 -1
  443. package/types/server/validate.d.ts +26 -0
  444. package/types/server/validate.d.ts.map +1 -0
  445. package/types/test/a11y-dom.d.ts +20 -0
  446. package/types/test/a11y-dom.d.ts.map +1 -0
  447. package/types/test/html-snapshot.d.ts +4 -0
  448. package/types/test/html-snapshot.d.ts.map +1 -0
  449. package/types/test/index.d.ts.map +1 -1
  450. package/types/test/mount.d.ts +3 -0
  451. package/types/test/mount.d.ts.map +1 -1
  452. package/types/test/page-checks.d.ts +6 -0
  453. package/types/test/page-checks.d.ts.map +1 -0
  454. package/types/test/screenshot-match.d.ts +1 -1
  455. package/types/test/screenshot-match.d.ts.map +1 -1
  456. package/types/test/shared-modules.d.ts.map +1 -1
  457. package/types/test/site-types.d.ts +3 -1
  458. package/types/test/site-types.d.ts.map +1 -1
  459. package/src/check/schema-type.ts +0 -9
  460. package/src/compiler/deprecated-script-fence.ts +0 -26
  461. package/src/server/field-errors.ts +0 -136
  462. package/types/check/schema-type.d.ts +0 -3
  463. package/types/check/schema-type.d.ts.map +0 -1
  464. package/types/compiler/deprecated-script-fence.d.ts +0 -3
  465. package/types/compiler/deprecated-script-fence.d.ts.map +0 -1
  466. package/types/server/field-errors.d.ts +0 -6
  467. package/types/server/field-errors.d.ts.map +0 -1
@@ -1,4 +1,5 @@
1
1
  import type { TSchema } from 'elysia'
2
+ import { autocompleteOf, typeAttributes } from './form-types'
2
3
 
3
4
  // What a top-level field's JSON schema says about the text a form posts for it.
4
5
  export interface FieldSchema {
@@ -37,7 +38,8 @@ const TRAILING_DIGITS = /\d+$/
37
38
  // A form posts text, as a query string is, and each field once per value or not at all: a top-level field is read the way its schema expects before it is validated.
38
39
  // The server reads every post this way, and the browser a form it checks before posting it.
39
40
  // `form` reads the text as a form sent it: a required list or boolean absent as empty or unticked, which only a form means by leaving it out, the spaces around what was typed dropped, and an optional field left blank as left out.
40
- export function formInput(schema: TSchema, body: unknown, form = true): unknown {
41
+ // `trim` false keeps the spaces around a text, as an API route reads what it is posted.
42
+ export function formInput(schema: TSchema, body: unknown, form = true, trim = form): unknown {
41
43
  const { properties, required = [] } = schema as { properties?: Record<string, FieldSchema>, required?: string[] }
42
44
 
43
45
  if (!properties || typeof body !== 'object' || body === null)
@@ -51,14 +53,14 @@ export function formInput(schema: TSchema, body: unknown, form = true): unknown
51
53
 
52
54
  if (value === undefined) {
53
55
  // A required list may well be empty, and an unticked checkbox posts nothing; a required file list is no file chosen, refused as missing.
54
- if (form && !optional && ((field.type === 'array' && field.items?.format !== 'binary') || field.type === 'boolean'))
56
+ if (form && !optional && ((field.type === 'array' && field.items?.format !== 'binary') || isBoolean(field)))
55
57
  fields[name] = field.type === 'array' ? [] : false
56
58
 
57
59
  continue
58
60
  }
59
61
 
60
62
  // Spaces around what was typed are no part of it, so `minLength` counts the text.
61
- const text = form && field.trim !== false && !isSecret(name) ? trimmed(value) : value
63
+ const text = trim && field.trim !== false && !isSecret(name) ? trimmed(value) : value
62
64
 
63
65
  if (optional && text === '' && (form || !acceptsBlank(field)))
64
66
  delete fields[name]
@@ -92,26 +94,31 @@ function fromText(field: FieldSchema | undefined, value: unknown): unknown {
92
94
  if (field !== undefined && isNumeric(field))
93
95
  return DECIMAL.test(value) ? Number(value) : Number.NaN
94
96
 
95
- if (field?.type === 'boolean')
97
+ if (field !== undefined && isBoolean(field))
96
98
  return BOOLEAN_TEXT.get(value) ?? value
97
99
 
98
100
  // A form posts a line break as CRLF, where the textarea the visitor typed in holds and counts it as one character: read as LF, a `maxLength` matches what the browser allowed and the text is stored as it was written.
99
101
  return value.includes('\r') ? value.replaceAll(CRLF, '\n') : value
100
102
  }
101
103
 
104
+ // A boolean, or the text of one as `s.boolean()` reads it.
105
+ export function isBoolean(field: FieldSchema): boolean {
106
+ return field.type === 'boolean' || (field.anyOf?.some(member => member.type === 'boolean') ?? false)
107
+ }
108
+
102
109
  // `s.Number()`, or `s.Integer()` and `s.Numeric()`, which Elysia writes as a number or a numeric string; `s.Date()` takes a number too, but not only.
103
110
  function isNumeric(field: FieldSchema): boolean {
104
111
  return field.type === 'number' || field.type === 'integer' || (field.anyOf?.every(member => isNumeric(member) || NUMERIC_FORMATS.has(member.format)) ?? false)
105
112
  }
106
113
 
107
114
  // The attributes a browser checks a field against without JavaScript, where a keyword of its schema has one; `required` only where the server would refuse the field left blank, as it never does an unticked checkbox.
108
- export function nativeConstraints(field: FieldSchema, required: boolean): [name: string, value: string][] {
115
+ export function nativeConstraints(field: FieldSchema, required: boolean, name?: string): [name: string, value: string][] {
109
116
  const found: [string, string][] = []
110
117
 
111
118
  if (required && refusesBlank(field))
112
119
  found.push(['required', ''])
113
120
 
114
- if (field.type === 'string') {
121
+ if (field.type === 'string' && field.format !== 'binary') {
115
122
  if (typeof field.minLength === 'number' && field.minLength > 0)
116
123
  found.push(['minlength', String(field.minLength)])
117
124
 
@@ -122,9 +129,6 @@ export function nativeConstraints(field: FieldSchema, required: boolean): [name:
122
129
 
123
130
  if (pattern !== undefined)
124
131
  found.push(['pattern', pattern])
125
-
126
- if (field.format === 'email')
127
- found.push(['type', 'email'])
128
132
  }
129
133
 
130
134
  if (isNumeric(field)) {
@@ -135,9 +139,21 @@ export function nativeConstraints(field: FieldSchema, required: boolean): [name:
135
139
  found.push(['max', String(field.maximum)])
136
140
  }
137
141
 
142
+ found.push(...typeAttributes(field, isNumeric(field), isBoolean(field)))
143
+
144
+ const autocomplete = name === undefined ? undefined : autocompleteOf(field, name)
145
+
146
+ if (autocomplete !== undefined)
147
+ found.push(['autocomplete', autocomplete])
148
+
138
149
  return found
139
150
  }
140
151
 
152
+ // The `type` an input takes from the field's schema, none for a field with no particular one.
153
+ export function schemaInputType(field: FieldSchema): string | undefined {
154
+ return typeAttributes(field, isNumeric(field), isBoolean(field)).find(([name]) => name === 'type')?.[1]
155
+ }
156
+
141
157
  function refusesBlank(field: FieldSchema): boolean {
142
158
  if (field.const !== undefined)
143
159
  return field.const !== ''
@@ -9,7 +9,8 @@ import { NON_ROOT_TRAILING_SLASH } from './base'
9
9
  import { fieldDescription, fieldInvalid, fieldRequired } from './described'
10
10
  import { escape, escapeAttribute } from './escape'
11
11
  import { fieldMarks, markedAttribute, markedList, markedText, withFieldMarks } from './fields'
12
- import { nativeConstraints } from './form'
12
+ import { isBoolean, nativeConstraints, schemaInputType } from './form'
13
+ import { optionsOf } from './form-types'
13
14
  import { DATA_SCRIPT, headCheckpoint, LEADING_NEWLINE, restoreHead } from './head'
14
15
  import { localizedLink } from './i18n'
15
16
  import { imageSource } from './image'
@@ -286,6 +287,11 @@ export function fieldDescribed(own?: unknown, type?: unknown, ownRequired?: unkn
286
287
  return attribute('aria-describedby', fieldDescription(own)) + requiredAttributes(fieldRequired(), type, ownRequired, ownNative) + invalidAttribute(type, ownInvalid)
287
288
  }
288
289
 
290
+ // A property of a source that a later source sets again is the later one's, and a spread `children` the element's own lines replace is not read at all, as reading it renders it: a closed dialog spreading its other props would draw what it does not show.
291
+ function isSkipped(sources: readonly object[], index: number, name: string, inner: unknown, isVoid: boolean): boolean {
292
+ return sources.some((later, position) => position > index && later != null && Object.hasOwn(later, name)) || ((inner !== undefined || isVoid) && CONTENT_PROPERTIES.has(name))
293
+ }
294
+
289
295
  // An element whose attributes are only known at render time: spreads, or a component tag that names an element.
290
296
  // An attribute set by several sources takes the last one's value and place; `content`, the markup of the lines under the tag, wins over a `children` or `innerHTML` property.
291
297
  export function element(written: string, sources: readonly object[], content?: () => string): Html {
@@ -305,7 +311,7 @@ export function element(written: string, sources: readonly object[], content?: (
305
311
  continue
306
312
 
307
313
  for (const name of Object.keys(source)) {
308
- if (sources.some((later, position) => position > index && later != null && Object.hasOwn(later, name)))
314
+ if (isSkipped(sources, index, name, inner, isVoid))
309
315
  continue
310
316
 
311
317
  const value = (source as Record<string, unknown>)[name]
@@ -639,20 +645,50 @@ export function refillOptions(posted: string | undefined, markup: string, value?
639
645
  return initial === undefined ? markup : selectOptions(initial, markup, true)
640
646
  }
641
647
 
642
- // The constraints a `field` checks in the browser without JavaScript, read off the schema of the action its form posts to; those the template writes itself are left to it.
648
+ interface ActionSchema { properties?: Record<string, FieldSchema>, required?: string[] }
649
+
650
+ function schemaOf(target: unknown): ActionSchema | undefined {
651
+ return isAction(target) ? (target as { schema?: ActionSchema }).schema : undefined
652
+ }
653
+
654
+ // The attributes that only mean something with the `type` the schema gave, so a template writing its own leaves them out.
655
+ const TYPED = new Set(['accept', 'multiple', 'step'])
656
+
657
+ // The constraints and the `type` and `autocomplete` a `field` takes in the browser without JavaScript, read off the schema of the action its form posts to; those the template writes itself are left to it.
643
658
  export function formConstraints(target: unknown, name: string, written: readonly string[]): string {
644
- const schema = isAction(target) ? (target as { schema?: { properties?: Record<string, FieldSchema>, required?: string[] } }).schema : undefined
659
+ const schema = schemaOf(target)
645
660
  const field = schema?.properties?.[name]
646
661
 
647
662
  if (!field)
648
663
  return ''
649
664
 
650
- return nativeConstraints(field, schema.required?.includes(name) === true)
651
- .filter(([attribute]) => !written.includes(attribute))
665
+ return nativeConstraints(field, schema.required?.includes(name) === true, name)
666
+ .filter(([attribute]) => !written.includes(attribute) && !(TYPED.has(attribute) && written.includes('type')))
652
667
  .map(([attribute, value]) => value === '' ? ` ${attribute}` : ` ${attribute}="${escapeAttribute(value)}"`)
653
668
  .join('')
654
669
  }
655
670
 
671
+ // The options of a `select(field=...)` written with none, from the enum of the field in the schema of the action its form posts to.
672
+ export function formOptions(target: unknown, name: string): string {
673
+ const schema = schemaOf(target)
674
+
675
+ return optionsOf(schema?.properties?.[name], schema?.required?.includes(name) === true)
676
+ }
677
+
678
+ // The `type` the schema gives an input that writes none, which a component requiring the field reads to know a file or a checkbox.
679
+ export function formType(target: unknown, name: string): string | undefined {
680
+ const field = schemaOf(target)?.properties?.[name]
681
+
682
+ return field && schemaInputType(field)
683
+ }
684
+
685
+ // Which of two fills an `input(field=...)` with no `type` takes: a ticked box for a boolean of the schema, else a value.
686
+ export function schemaFill(target: unknown, name: string, value: (type: string | undefined) => string, checked: () => string): string {
687
+ const field = schemaOf(target)?.properties?.[name]
688
+
689
+ return field && isBoolean(field) ? checked() : value(field && schemaInputType(field) === 'date' ? 'date' : undefined)
690
+ }
691
+
656
692
  // The `p` an `error` line shows a refused form's message in, nothing while there is none.
657
693
  export function errorParagraph(message: string | undefined, id: string, classes: string): string {
658
694
  return message === undefined ? '' : `<p${id === '' ? '' : ` id="${escapeAttribute(id)}"`} class="${escapeAttribute(classes)}">${escape(message)}</p>`
@@ -1,5 +1,7 @@
1
1
  export { s } from '#schema'
2
2
  export type { Schema, Static } from '#schema'
3
+ export type { Issue, Refusal, Validated, ValidateOptions, Source as ValidateSource } from '#validate'
4
+ export { validate } from '#validate'
3
5
  export type { CookieDeleteOptions } from '../server/cookies'
4
6
  export type { Action, ActionContext, ActionDone, ActionErrorDetail, ActionFailed, ActionFor, ActionForm, ActionHooks, ActionProtect, ActionProtection, ActionRateLimit, ActionRun, ActionStartDetail, ActionStream, ActionSuccessDetail, Answer, Called, CallOptions, CallPromise, CallQueue, CallResult, Failure, FailureData, FailureOf, FieldErrors, FieldName, FormFailure, FormSuccess, HoneypotOptions, InputOf, MinTimeOptions, MinTimeRefusal, NoInputAction, ProofOfWorkOptions, ProofOfWorkRefusal, ProtectionMessages, RateKey, RateKeyContext, RateLimit, RateLimitRefusal, RateStore, RateWindow, Refreshable, ResolvedProtection, RetryOptions, ServerAction, Success, SuccessOf, TrackedCall, TrackHooks } from './action'
5
7
  export { action, ActionError, ActionRedirectError, carrySchema, formErrors, postedChecked, postedValue, postedValues } from './action'
@@ -40,6 +40,8 @@ export interface RenderedIslands {
40
40
  chunks?: Record<string, string> | undefined
41
41
  // The message keys the islands use, which go to the browser with them.
42
42
  messages: Set<string>
43
+ // The keys of the components an eager island drew that load on their own (`lazyNote`), whose chunks the page preloads.
44
+ lazies: Set<string>
43
45
  // Set to what the first island handed an action says of it, which a build that left out the code reviving one cannot serve.
44
46
  actions?: string | undefined
45
47
  }
@@ -57,6 +59,7 @@ export function islandCheckpoint(): (() => void) | undefined {
57
59
  const immediate = islands.immediate.size
58
60
  const waiting = islands.waiting.size
59
61
  const messages = islands.messages.size
62
+ const lazies = islands.lazies.size
60
63
  const actions = islands.actions
61
64
 
62
65
  return () => {
@@ -68,6 +71,7 @@ export function islandCheckpoint(): (() => void) | undefined {
68
71
  trim(islands.immediate, immediate)
69
72
  trim(islands.waiting, waiting)
70
73
  trim(islands.messages, messages)
74
+ trim(islands.lazies, lazies)
71
75
  }
72
76
  }
73
77
 
@@ -89,7 +93,7 @@ function trim(set: Set<string>, size: number): void {
89
93
 
90
94
  // Runs a synchronous page render and lists the islands it rendered.
91
95
  export function collectIslands<T>(render: () => T, salt?: string, chunks?: Record<string, string>): { result: T, islands: RenderedIslands } {
92
- const islands: RenderedIslands = { used: new Set(), eager: new Set(), immediate: new Set(), waiting: new Set(), lazy: 0, count: 0, messages: new Set(), chunks }
96
+ const islands: RenderedIslands = { used: new Set(), eager: new Set(), immediate: new Set(), waiting: new Set(), lazy: 0, count: 0, messages: new Set(), lazies: new Set(), chunks }
93
97
 
94
98
  rendered = islands
95
99
 
@@ -101,6 +105,29 @@ export function collectIslands<T>(render: () => T, salt?: string, chunks?: Recor
101
105
  }
102
106
  }
103
107
 
108
+ // The keys of the components the island being rendered draws that load on their own in the browser.
109
+ let drawn: Set<string> | undefined
110
+
111
+ // A component the browser loads when it draws (`KayLazy` in the island's code): drawing it here is the same call, which tells the island its code is needed before it hydrates.
112
+ // The component is read when it draws, as a module importing it back may still be loading when this one is.
113
+ export function lazyNote<P>(key: string, component: () => (props: P) => unknown): (props: P) => unknown {
114
+ return (props) => {
115
+ drawn?.add(key)
116
+
117
+ return component()(props)
118
+ }
119
+ }
120
+
121
+ // The components the island drew that load on their own, which the browser loads before it hydrates it, and the page preloads unless the island waits.
122
+ function lazyAttribute(keys: Set<string>, waits: boolean): string {
123
+ if (!waits) {
124
+ for (const key of keys)
125
+ rendered?.lazies.add(key)
126
+ }
127
+
128
+ return keys.size === 0 ? '' : ` data-lazy="${escapeAttribute([...keys].join(' '))}"`
129
+ }
130
+
104
131
  // What renders inside an island renders in the browser as well, so its markup carries the markers hydration claims.
105
132
  export function insideIsland(): boolean {
106
133
  return depth > 0
@@ -212,6 +239,11 @@ export function island<P extends object>(id: string, component: ((props: P) => J
212
239
  }
213
240
  }
214
241
 
242
+ const keys = new Set<string>()
243
+ const outer = drawn
244
+
245
+ drawn = keys
246
+
215
247
  try {
216
248
  // Detached, as in the browser, where an island reads nothing the page's components provide.
217
249
  const { result: html, count } = browserOnly ? { result: placeholder((props as Record<string, unknown>)[USE_FALLBACK] as JSX.Element | undefined, ownFallback), count: 0 } : withFieldMarks(undefined, () => renderOnServer(() => scope(() => countIds(prefix, () => issueScope(undefined, () => child(component!(given)))), true)))
@@ -221,12 +253,14 @@ export function island<P extends object>(id: string, component: ((props: P) => J
221
253
  // `load` is written out too, so a visitor asking to save data, whose islands wait by default, sees the one that asked to go at once.
222
254
  const marked = waits || when === 'load' ? ` data-hydrate="${escapeAttribute(when!)}"` : ''
223
255
  const chunk = rendered?.chunks?.[id]
224
- const markup: Html = renderedHtml(`<slot data-component="${escapeAttribute(id)}" data-props=${quotedAttribute(serialized)}${cause === undefined ? '' : ` data-why=${quotedAttribute(cause)}`}${marked}${titled}${shipped}${browserOnly ? ' data-client' : ''}${count === 0 ? '' : ` data-uid="${escapeAttribute(prefix)}"`}${persist === undefined ? '' : ` data-persist="${escapeAttribute(persist)}"`}${chunk === undefined ? '' : ` data-chunk="${escapeAttribute(chunk)}"`}>${html}</slot>`)
256
+ const lazies = lazyAttribute(keys, waits)
257
+ const markup: Html = renderedHtml(`<slot data-component="${escapeAttribute(id)}" data-props=${quotedAttribute(serialized)}${lazies}${cause === undefined ? '' : ` data-why=${quotedAttribute(cause)}`}${marked}${titled}${shipped}${browserOnly ? ' data-client' : ''}${count === 0 ? '' : ` data-uid="${escapeAttribute(prefix)}"`}${persist === undefined ? '' : ` data-persist="${escapeAttribute(persist)}"`}${chunk === undefined ? '' : ` data-chunk="${escapeAttribute(chunk)}"`}>${html}</slot>`)
225
258
 
226
259
  return markup as unknown as JSX.Element
227
260
  }
228
261
  finally {
229
262
  depth--
263
+ drawn = outer
230
264
  }
231
265
  }
232
266
 
@@ -0,0 +1,69 @@
1
+ // Where Bun.markdown (md4c) ends a bare URL, which marked's own rule does not match: a URL starts after a space, a line start or an opening bracket, its host has a dot, and each of its path, query and fragment takes the characters below. A trailing `.`, a `&` before an entity or a closing bracket stays out of the link, and one ending in `_`, `-`, `~`, `=` or any other mark is not a link at all.
2
+ const SCHEME = /(?:https?|ftp):\/\/|www\./iy
3
+ const HOST = /[a-z\d](?:[\w-]|\.(?=[\w-]))*/iy
4
+ const ALNUM_END = /[a-z\d]$/i
5
+ const NEXT = /[\s.,!?;&)\]<}]|$/y
6
+ const LABEL = /(?:https?|ftp):\/\/[\w.-]*|www\.[\w.-]*/iy
7
+ // How far a URL is read: a scan that fails runs to the end of its path, which hostile text repeats, and a longer URL stays text.
8
+ const REACH = 2000
9
+ const OPENERS = /[\s([{]$/
10
+ // Each part, what it may end with and the closers its last character may be followed by instead.
11
+ const PARTS: [RegExp, RegExp, RegExp][] = [
12
+ [/\/(?:[\w~%+*/-]|\.(?=[\w~%+*/.-]))*/y, /[a-z\d/]$/i, /\./],
13
+ [/\?[a-z\d](?:[\w~%+*=-]|\.(?=[\w~%+*=.&()-])|&(?!#?[a-z\d]+;)(?=[\w~%+*=.&()-])|\([\w~%+*=.-]*\)|[()](?=[\w~%+*=.&()-]))*/iy, /[a-z\d)]$/i, /[.&)]/],
14
+ [/#[a-z\d](?:[\w~%+*-]|\.(?=[\w~%+*.-]))*/iy, ALNUM_END, /\./],
15
+ ]
16
+
17
+ function match(pattern: RegExp, text: string, at: number): string | undefined {
18
+ pattern.lastIndex = at
19
+
20
+ return pattern.exec(text)?.[0]
21
+ }
22
+
23
+ /** The length of the bare URL `text` starts with, 0 when it is not one. */
24
+ export function autolinkLength(whole: string): number {
25
+ const text = whole.slice(0, REACH)
26
+ const scheme = match(SCHEME, text, 0)
27
+ const host = scheme === undefined ? undefined : match(HOST, text, scheme.length)
28
+
29
+ if (scheme === undefined || host === undefined || (!ALNUM_END.test(host) && text[scheme.length + host.length] !== '.') || (scheme.length !== 4 && !host.includes('.')))
30
+ return 0
31
+
32
+ let end = scheme.length + host.length
33
+
34
+ for (const [pattern, last, closer] of PARTS) {
35
+ const found = match(pattern, text, end)
36
+
37
+ if (found === undefined)
38
+ continue
39
+
40
+ if (!last.test(found) && !closer.test(text[end + found.length] ?? ''))
41
+ return 0
42
+
43
+ end += found.length
44
+ }
45
+
46
+ return endsAutolink(text, end) && (end < REACH || whole.length === REACH) ? end : 0
47
+ }
48
+
49
+ /** The start of a URL that `text` starts with: the part marked leaves as text when a word is stuck to it. */
50
+ export function gluedLength(text: string): number {
51
+ return match(LABEL, text, 0)?.length ?? 0
52
+ }
53
+
54
+ /** Whether `text` starts like a bare URL, which may still not be one. */
55
+ export function startsAutolink(text: string): boolean {
56
+ return match(SCHEME, text, 0) !== undefined
57
+ }
58
+
59
+ /** Whether `text` goes on after a link of `length` characters the way Bun.markdown lets one end. */
60
+ export function endsAutolink(text: string, length: number): boolean {
61
+ NEXT.lastIndex = length
62
+
63
+ return NEXT.test(text)
64
+ }
65
+
66
+ /** Whether a URL may start after `before`, the text it follows in its line. */
67
+ export function opensAutolink(before: string): boolean {
68
+ return before === '' || OPENERS.test(before)
69
+ }
@@ -1,8 +1,10 @@
1
1
  import type { RendererObject, Token, TokenizerObject, Tokens } from 'marked'
2
2
  import type { SafeHtml } from './checks'
3
+ import type { MarkdownOptions } from './markdown'
3
4
  import { Marked, Tokenizer } from 'marked'
4
5
  import { escapeMarkdownText as escape } from './escape'
5
- import { sanitizedMarkdown } from './markdown'
6
+ import { sanitizedMarkdown, withInline } from './markdown'
7
+ import { autolinkLength, endsAutolink, gluedLength, opensAutolink, startsAutolink } from './markdown-autolink'
6
8
  import { vouch } from './safe-html'
7
9
 
8
10
  const TEXT_ESCAPED = /&(?![a-z][a-z\d]{1,31};)|[<>"]/gi
@@ -10,6 +12,7 @@ const ESCAPES: Record<string, string> = { '&': '&amp;', '<': '&lt;', '>': '&gt;'
10
12
  const REFERENCE = /&(?:#(\d{1,7})|#x([\da-f]{1,6})|(amp|lt|gt|quot|apos|nbsp));/gi
11
13
  const NAMED: Record<string, string> = { amp: '&', lt: '<', gt: '>', quot: '"', apos: '\'', nbsp: '\u00A0' }
12
14
  const FIRST_WORD = /^\S*/
15
+ const PROTOCOL = /^(?:mailto|xmpp):/i
13
16
  const BARE_TASK = /^\[([ x])\]$/i
14
17
  const TRAILING_LINES = /\n+$/
15
18
  const LINE_INDENT = /^[ \t]+/gm
@@ -135,7 +138,35 @@ function within(src: string, maskedSrc: string): [string, string] {
135
138
  return [src.slice(0, DELIMITER_REACH), maskedSrc.slice(0, maskedSrc.length - src.length + Math.min(src.length, DELIMITER_REACH))]
136
139
  }
137
140
 
141
+ // marked's address autolink where Bun.markdown ends it too.
142
+ function emailToken(tokenizer: Tokenizer, src: string): Tokens.Link | undefined {
143
+ const token = DEFAULTS.url.call(tokenizer, src)
144
+
145
+ return token && endsAutolink(src, token.raw.length) ? token : undefined
146
+ }
147
+
148
+ // The tokens Bun.markdown writes for a bare URL: it links where md4c does, and a URL stuck to the text before it stays text.
138
149
  const tokenizer: TokenizerObject = {
150
+ url(src) {
151
+ if (!startsAutolink(src))
152
+ return PROTOCOL.test(src) ? undefined : emailToken(this, src)
153
+
154
+ const length = autolinkLength(src)
155
+ const raw = src.slice(0, length)
156
+
157
+ return length === 0 ? undefined : { type: 'link', raw, text: raw, href: raw.toLowerCase().startsWith('www.') ? `http://${raw}` : raw, autolink: true, tokens: [{ type: 'text', raw, text: raw }] }
158
+ },
159
+ inlineText(src) {
160
+ const token = DEFAULTS.inlineText.call(this, src)
161
+
162
+ if (token === undefined || opensAutolink(token.raw))
163
+ return token
164
+
165
+ const rest = src.slice(token.raw.length)
166
+ const glued = startsAutolink(rest) ? rest.slice(0, gluedLength(rest)) : emailToken(this, rest)?.raw
167
+
168
+ return glued === undefined ? token : { ...token, raw: token.raw + glued, text: token.text + glued }
169
+ },
139
170
  emStrong(src, maskedSrc, prevChar) {
140
171
  return src.length > DELIMITER_REACH && DEFAULTS.emStrong.call(this, ...within(src, maskedSrc), prevChar)
141
172
  },
@@ -149,15 +180,20 @@ const marked = new Marked({ gfm: true, async: false, renderer, tokenizer })
149
180
  const asWritten = (markdown: string) => vouch(`<p>${escape(markdown)}</p>\n`)
150
181
 
151
182
  // Markdown rendered as `renderMarkdown` renders it, headings without anchors, then sanitized for `innerHTML`: formatted text, tables, images and task lists, a link leaving the site opening in a new tab, sync so it renders again on each chunk of a stream.
152
- export function markdownHtml(markdown: string): SafeHtml {
183
+ export function markdownHtml(markdown: string, options?: MarkdownOptions): SafeHtml {
153
184
  if (markdown.length > MAX_LENGTH)
154
185
  return asWritten(markdown)
155
186
 
187
+ let html: SafeHtml
188
+
156
189
  try {
157
- return sanitizedMarkdown(marked.parse(markdown) as string)
190
+ html = sanitizedMarkdown(marked.parse(markdown) as string)
158
191
  }
159
192
  // Thousands of nested quotes overflow marked's stack.
160
193
  catch {
161
194
  return asWritten(markdown)
162
195
  }
196
+
197
+ // Outside the guard above: a hook of the app's that throws is its bug, as it is on the server.
198
+ return withInline(html, options?.inline)
163
199
  }
@@ -1,4 +1,5 @@
1
1
  import type { SafeHtml } from './checks'
2
+ import type { MarkdownOptions } from './markdown'
2
3
  import { claiming, keepServerMarkup } from './dom'
3
4
  import { escapeMarkdownText } from './escape'
4
5
  import { ownSignal, whenHydrated } from './reactive'
@@ -6,7 +7,9 @@ import { vouch } from './safe-html'
6
7
 
7
8
  type Marked = Element & { _$m?: string | undefined }
8
9
 
9
- const loaded = ownSignal<((markdown: string) => SafeHtml) | undefined>(undefined)
10
+ export type { MarkdownInline, MarkdownOptions } from './markdown'
11
+
12
+ const loaded = ownSignal<((markdown: string, options?: MarkdownOptions) => SafeHtml) | undefined>(undefined)
10
13
  let loading: Promise<void> | undefined
11
14
 
12
15
  // The stand-ins an island hydrating gave for Markdown the server drew formatted, until the islands are in: `property` leaves the server's markup in place instead of writing one of them to a claimed element. Tied to the value rather than a flag, so no other write consumes or skips it.
@@ -59,11 +62,11 @@ function hold(standIn: string): void {
59
62
  }
60
63
 
61
64
  // Markdown rendered as `renderMarkdown` renders it, headings without anchors, then sanitized for `innerHTML`: formatted text, tables, images and task lists, a link leaving the site opening in a new tab, sync so it renders again on each chunk of a stream. Until the renderer has loaded it gives the text as written, and a template or effect that called it runs again once it has.
62
- export function markdownHtml(markdown: string): SafeHtml {
65
+ export function markdownHtml(markdown: string, options?: MarkdownOptions): SafeHtml {
63
66
  const render = loaded[0]()
64
67
 
65
68
  if (render)
66
- return render(markdown)
69
+ return render(markdown, options)
67
70
 
68
71
  const standIn = asWritten(markdown)
69
72
 
@@ -1,6 +1,9 @@
1
1
  import type { SafeHtml } from './checks'
2
+ import type { MarkdownOptions } from './markdown'
2
3
  import { markedMarkdown } from './fields'
3
- import { MARKDOWN_OPTIONS, sanitizedMarkdown } from './markdown'
4
+ import { MARKDOWN_OPTIONS, sanitizedMarkdown, withInline } from './markdown'
5
+
6
+ export type { MarkdownInline, MarkdownOptions } from './markdown'
4
7
 
5
8
  // Resolves at once: the server renders Markdown synchronously. `await markdownReady()` in an island waits for the renderer to load.
6
9
  export async function markdownReady(): Promise<void> {
@@ -8,8 +11,8 @@ export async function markdownReady(): Promise<void> {
8
11
  }
9
12
 
10
13
  // Markdown rendered as `renderMarkdown` renders it, headings without anchors, then sanitized for `innerHTML`: formatted text, tables, images and task lists. Islands get the same HTML from marked. A link leaving the site opens in a new tab.
11
- export function markdownHtml(markdown: string): SafeHtml {
12
- const html = sanitizedMarkdown(Bun.markdown.html(markdown, MARKDOWN_OPTIONS))
14
+ export function markdownHtml(markdown: string, options?: MarkdownOptions): SafeHtml {
15
+ const html = withInline(sanitizedMarkdown(Bun.markdown.html(markdown, MARKDOWN_OPTIONS)), options?.inline)
13
16
 
14
17
  return markedMarkdown(markdown, html) as SafeHtml
15
18
  }
@@ -1,5 +1,6 @@
1
1
  import type { SafeHtml } from './checks'
2
2
  import type { SanitizeOptions } from './sanitize'
3
+ import { escapeMarkdownText as escapeText } from './escape'
3
4
  import { labelTasks } from './markdown-task'
4
5
  import { vouch } from './safe-html'
5
6
  import { sanitizeMarkup } from './sanitize'
@@ -7,12 +8,135 @@ import { sanitizeMarkup } from './sanitize'
7
8
  // What a user's Markdown keeps besides formatted text: tables, images and the task lists' checkboxes, which `sanitizeHtml` would drop as inputs.
8
9
  export const MARKDOWN_KEPT: SanitizeOptions = { tags: ['img', 'table', 'thead', 'tbody', 'tr', 'th', 'td'], attributes: { img: ['src', 'alt', 'title'], th: ['align'], td: ['align'] } }
9
10
  // A user's own Markdown, whose images are never the page's hero, so they load when they come into view.
10
- const USER_MARKDOWN: SanitizeOptions = { ...MARKDOWN_KEPT, lazyImages: true }
11
+ const USER_MARKDOWN: SanitizeOptions = { ...MARKDOWN_KEPT, attributes: { ...MARKDOWN_KEPT.attributes, code: ['class'] }, lazyImages: true }
11
12
  const TASK = /<li class="task-list-item"><input type="checkbox" class="task-list-item-checkbox" disabled( checked)?>/g
12
13
  const MARKS = /[\uE000\uE001]/g
13
14
  const MARKED_TASK = /<li>([\uE000\uE001])/g
14
15
  const ODD_ALIGN = / align="(?!(?:left|center|right)")[^"]*"/g
15
16
 
17
+ // What an app's own syntax becomes in `markdownHtml`: `render` gets the match, its groups as plain text, and answers text (escaped, never markup), a link to `href` (kept only when `sanitizeHtml` would keep that URL) or `undefined` to leave the text as it is.
18
+ export interface MarkdownInline {
19
+ // Matched in the text of the rendered page, a code span, a code block and a link left alone; `&`, `<`, `>` and `"` stand as their entities there.
20
+ pattern: RegExp
21
+ render: (match: string[]) => string | { text: string, href: string } | undefined
22
+ }
23
+
24
+ export interface MarkdownOptions {
25
+ // Custom inline syntax, such as `[[name]]` linking to a note, tried in order.
26
+ inline?: readonly MarkdownInline[] | undefined
27
+ }
28
+
29
+ const MARKUP = /<\/?([a-z][a-z\d]*)(?:\s(?:[^>"]|"[^"]*")*)?\/?>/gi
30
+ const ENTITY = /&(?:amp|lt|gt|quot|#39|#(\d{1,7})|#x([\da-f]{1,6}));/gi
31
+ const PLAIN: Record<string, string> = { '&amp;': '&', '&lt;': '<', '&gt;': '>', '&quot;': '"', '&#39;': '\'' }
32
+ const UNTOUCHED = new Set(['a', 'code', 'pre'])
33
+
34
+ function plain(text: string): string {
35
+ return text.replace(ENTITY, (entity, decimal: string | undefined, hex: string | undefined) => {
36
+ const fromHex = hex === undefined ? undefined : Number.parseInt(hex, 16)
37
+ const code = decimal === undefined ? fromHex : Number(decimal)
38
+
39
+ if (code === undefined)
40
+ return PLAIN[entity.toLowerCase()] ?? entity
41
+
42
+ return code > 0 && code <= 0x10FFFF && (code < 0xD800 || code > 0xDFFF) ? String.fromCodePoint(code) : '\uFFFD'
43
+ })
44
+ }
45
+
46
+ const ENTITY_SPAN = /&#?\w+;/g
47
+
48
+ // The entities of a text as `[from, to)`, found in one pass.
49
+ function entitySpans(text: string): [number, number][] {
50
+ return [...text.matchAll(ENTITY_SPAN)].map(({ index, 0: entity }) => [index, index + entity.length])
51
+ }
52
+
53
+ // Whether a match starts or ends inside an entity, which would cut it in two: `amp` of `&amp;`. Matches come in order, so `at` only moves forward over the spans.
54
+ function cutsEntity(spans: [number, number][], at: { index: number }, start: number, end: number): boolean {
55
+ while (at.index < spans.length && spans[at.index]![1] <= start)
56
+ at.index++
57
+
58
+ for (let index = at.index; index < spans.length && spans[index]![0] < end; index++) {
59
+ const [from, to] = spans[index]!
60
+
61
+ if ((start > from && start < to) || (end > from && end < to))
62
+ return true
63
+ }
64
+
65
+ return false
66
+ }
67
+
68
+ function isAnswer(answer: unknown): answer is string | { text: string, href: string } {
69
+ return typeof answer === 'string' || (typeof answer === 'object' && answer !== null && typeof (answer as { text?: unknown }).text === 'string' && typeof (answer as { href?: unknown }).href === 'string')
70
+ }
71
+
72
+ function drawn(answer: string | { text: string, href: string }, opensApart: boolean): string {
73
+ if (typeof answer === 'string')
74
+ return escapeText(answer)
75
+
76
+ // The link goes through the sanitizer as any other, so an app's `href` built from a user's text cannot run script.
77
+ return sanitizeMarkup(`<a href="${escapeText(answer.href)}">${escapeText(answer.text)}</a>`, undefined, opensApart)
78
+ }
79
+
80
+ // The text of `html` with each match of `inline` replaced by what it renders to; every piece the app returns is escaped or sanitized, whatever the text holds.
81
+ function inlined(text: string, inline: readonly { global: RegExp, render: MarkdownInline['render'] }[], opensApart: boolean): string {
82
+ let parts: { text: string, done: boolean }[] = [{ text, done: false }]
83
+
84
+ for (const { global, render } of inline) {
85
+ parts = parts.flatMap((part) => {
86
+ if (part.done)
87
+ return [part]
88
+
89
+ const pieces: typeof parts = []
90
+ const spans = entitySpans(part.text)
91
+ const at = { index: 0 }
92
+ let last = 0
93
+
94
+ for (const match of part.text.matchAll(global)) {
95
+ const answer = match[0] === '' || cutsEntity(spans, at, match.index, match.index + match[0].length) ? undefined : render([...match].map(group => plain(group ?? '')))
96
+
97
+ // A hook answering anything but text or a link, `undefined` included, leaves the match as written.
98
+ if (!isAnswer(answer))
99
+ continue
100
+
101
+ pieces.push({ text: part.text.slice(last, match.index), done: false }, { text: drawn(answer, opensApart), done: true })
102
+ last = match.index + match[0].length
103
+ }
104
+
105
+ pieces.push({ text: part.text.slice(last), done: false })
106
+
107
+ return pieces
108
+ })
109
+ }
110
+
111
+ return parts.map(part => part.text).join('')
112
+ }
113
+
114
+ // Markdown's sanitized HTML with the app's inline syntax drawn in its text, outside code and links.
115
+ export function withInline(html: SafeHtml, inline: readonly MarkdownInline[] | undefined, opensApart = true): SafeHtml {
116
+ if (!inline || inline.length === 0)
117
+ return html
118
+
119
+ const compiled = inline.map(({ pattern, render }) => ({ global: new RegExp(pattern.source, pattern.flags.includes('g') ? pattern.flags : `${pattern.flags}g`), render }))
120
+ let skipped = 0
121
+ let result = ''
122
+ let last = 0
123
+
124
+ for (const match of html.matchAll(MARKUP)) {
125
+ const text = html.slice(last, match.index)
126
+
127
+ result += skipped > 0 ? text : inlined(text, compiled, opensApart)
128
+ result += match[0]
129
+ last = match.index + match[0].length
130
+
131
+ if (UNTOUCHED.has(match[1]!.toLowerCase()))
132
+ skipped = Math.max(0, skipped + (match[0][1] === '/' ? -1 : 1))
133
+ }
134
+
135
+ const tail = html.slice(last)
136
+
137
+ return vouch(result + (skipped > 0 ? tail : inlined(tail, compiled, opensApart)))
138
+ }
139
+
16
140
  // What Bun.markdown renders a user's Markdown with on the server, heading anchors aside.
17
141
  export const MARKDOWN_OPTIONS = { autolinks: true }
18
142