@kayjs/kay 1.105.0 → 1.106.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.
- package/dist/eslint.js +435 -120
- package/editors/zed/extension.toml +2 -2
- package/llms/action-protection.md +1 -1
- package/llms/actions-from-islands.md +2 -2
- package/llms/actions.md +22 -19
- package/llms/analytics.md +1 -1
- package/llms/api-from-islands.md +1 -1
- package/llms/api-routes.md +6 -6
- package/llms/check-and-tools.md +1 -1
- package/llms/code-lints.md +2 -2
- package/llms/compiler-api.md +1 -1
- package/llms/component-libraries.md +1 -0
- package/llms/content-cms.md +1 -1
- package/llms/content-markdown-to-caching.md +1 -1
- package/llms/content-preview.md +2 -2
- package/llms/content.md +21 -21
- package/llms/deprecated-lints-1-105-libraries.md +2 -0
- package/llms/deprecated-lints-1-105.md +3 -3
- package/llms/deprecated-lints.md +3 -2
- package/llms/dev-tool.md +2 -2
- package/llms/environment.md +22 -22
- package/llms/files.md +5 -5
- package/llms/islands-state-to-ids.md +2 -1
- package/llms/local-collections.md +2 -2
- package/llms/mcp.md +2 -1
- package/llms/navigation-from-code.md +1 -1
- package/llms/notifications.md +4 -2
- package/llms/project.md +1 -1
- package/llms/rate-limits.md +1 -1
- package/llms/recipes.md +16 -16
- package/llms/rooms.md +4 -4
- package/llms/routing-endpoints-and-base.md +1 -1
- package/llms/routing.md +5 -5
- package/llms/schemas.md +10 -5
- package/llms/server.md +7 -7
- package/llms/site.md +9 -9
- package/llms/sitemap-search-fonts.md +2 -0
- package/llms/styles-and-assets.md +2 -1
- package/llms/templates-elements-slots-snippets.md +2 -2
- package/llms/testing-browser.md +20 -0
- package/llms/testing.md +1 -1
- package/llms/upgrading-1-105-config.md +2 -2
- package/llms/upgrading.md +18 -3
- package/llms/whats-new-archive.md +35 -66
- package/llms/whats-new.md +34 -28
- package/llms.txt +1 -1
- package/package.json +15 -11
- package/src/add.ts +1 -1
- package/src/build/class-table.ts +2 -2
- package/src/build/client.ts +7 -6
- package/src/build/plugin.ts +14 -11
- package/src/build/styles.ts +3 -3
- package/src/build/transform.ts +3 -1
- package/src/build/ui-layers.ts +30 -0
- package/src/build/ui.ts +108 -11
- package/src/build/variants.ts +119 -43
- package/src/build/worker-bundle.ts +6 -0
- package/src/check/boot.ts +22 -1
- package/src/check/call-result.ts +242 -0
- package/src/check/config-objects.ts +17 -2
- package/src/check/diagnose.ts +22 -0
- package/src/check/handler-events.ts +149 -0
- package/src/check/index.ts +6 -2
- package/src/check/link-flag-tests.ts +130 -0
- package/src/check/project.ts +4 -3
- package/src/check/refusal-calls.ts +2 -2
- package/src/check/regroup.ts +5 -3
- package/src/check/renamed-packages.ts +58 -14
- package/src/check/schema-builders.ts +256 -0
- package/src/check/secret-keys.ts +15 -2
- package/src/check/shadow-types.ts +2 -0
- package/src/check/template-hints.ts +1 -1
- package/src/check/tool-labels.ts +11 -9
- package/src/check/ui-types.ts +4 -3
- package/src/check/ui.ts +14 -11
- package/src/commands/env-declare.ts +18 -6
- package/src/commands/routes.ts +1 -1
- package/src/compiler/a11y.ts +8 -1
- package/src/compiler/ast.ts +1 -1
- package/src/compiler/bound-props.ts +1 -1
- package/src/compiler/codegen.ts +6 -6
- package/src/compiler/deprecated-lints-1-106.ts +14 -0
- package/src/compiler/deprecated-lints.ts +10 -5
- package/src/compiler/deprecated-typed-lints.ts +23 -0
- package/src/compiler/dom-here.ts +72 -0
- package/src/compiler/dom.ts +23 -23
- package/src/compiler/fence-checks.ts +12 -11
- package/src/compiler/field-described.ts +2 -2
- package/src/compiler/helper-imports.ts +1 -1
- package/src/compiler/html-fields.ts +92 -0
- package/src/compiler/html-initial.ts +5 -3
- package/src/compiler/html.ts +14 -31
- package/src/compiler/index.ts +3 -1
- package/src/compiler/items.ts +5 -5
- package/src/compiler/link-flags.ts +6 -5
- package/src/compiler/lints.ts +12 -12
- package/src/compiler/rest-omits.ts +15 -0
- package/src/config-loader.ts +10 -2
- package/src/config-resolve.ts +3 -1
- package/src/config-shape.ts +2 -2
- package/src/config.ts +20 -4
- package/src/content/markdown.ts +2 -3
- package/src/generate-resource.ts +9 -9
- package/src/generate.ts +1 -1
- package/src/mcp/guards.ts +1 -1
- package/src/mcp/tools.ts +1 -1
- package/src/migrate/content.ts +61 -55
- package/src/runtime/action-shared.ts +2 -2
- package/src/runtime/action.ts +6 -2
- package/src/runtime/form-types.ts +99 -0
- package/src/runtime/form.ts +22 -7
- package/src/runtime/html.ts +36 -5
- package/src/runtime/markdown-autolink.ts +69 -0
- package/src/runtime/markdown-render.client.ts +39 -3
- package/src/runtime/markdown.client.ts +6 -3
- package/src/runtime/markdown.server.ts +6 -3
- package/src/runtime/markdown.ts +125 -1
- package/src/runtime/navigate-api.ts +10 -5
- package/src/runtime/persist.ts +7 -7
- package/src/runtime/query-param.ts +1 -1
- package/src/runtime/reactive.ts +14 -0
- package/src/runtime/sanitize.ts +3 -1
- package/src/runtime/schema-record.ts +31 -0
- package/src/runtime/schema.client.ts +1 -4
- package/src/runtime/schema.ts +321 -26
- package/src/runtime/swap-focus.ts +59 -11
- package/src/server/action-module.ts +16 -0
- package/src/server/actions.ts +6 -14
- package/src/server/api-modules.ts +1 -1
- package/src/server/api.ts +13 -1
- package/src/server/app-stages.ts +23 -18
- package/src/server/dev-ui.ts +2 -2
- package/src/server/env-state.ts +1 -1
- package/src/server/env.ts +11 -3
- package/src/server/favicon.ts +47 -1
- package/src/server/image-metadata-bmff.ts +1 -1
- package/src/server/kept-pages.ts +3 -3
- package/src/server/library.ts +2 -0
- package/src/server/mcp.ts +13 -12
- package/src/server/notifications.ts +4 -2
- package/src/server/offline.ts +24 -7
- package/src/server/push-settings.ts +16 -2
- package/src/server/push.ts +12 -4
- package/src/server/query.ts +13 -6
- package/src/server/render.ts +1 -1
- package/src/server/scope.ts +20 -13
- package/src/server/signals.ts +4 -4
- package/src/template-ws.ts +1 -1
- package/src/test/a11y-dom.ts +70 -0
- package/src/test/html-snapshot.ts +103 -0
- package/src/test/index.ts +2 -1
- package/src/test/mount.ts +8 -1
- package/src/test/page-checks.ts +12 -0
- package/src/test/screenshot-match.ts +2 -2
- package/src/test/shared-modules.ts +27 -5
- package/src/test/site-types.ts +5 -1
- package/types/build/class-table.d.ts +1 -1
- package/types/build/class-table.d.ts.map +1 -1
- package/types/build/client.d.ts.map +1 -1
- package/types/build/plugin.d.ts.map +1 -1
- package/types/build/transform.d.ts +1 -0
- package/types/build/transform.d.ts.map +1 -1
- package/types/build/ui-layers.d.ts +5 -0
- package/types/build/ui-layers.d.ts.map +1 -0
- package/types/build/ui.d.ts +7 -0
- package/types/build/ui.d.ts.map +1 -1
- package/types/build/variants.d.ts +1 -1
- package/types/build/variants.d.ts.map +1 -1
- package/types/build/worker-bundle.d.ts +1 -0
- package/types/build/worker-bundle.d.ts.map +1 -1
- package/types/check/boot.d.ts.map +1 -1
- package/types/check/call-result.d.ts +7 -0
- package/types/check/call-result.d.ts.map +1 -0
- package/types/check/config-objects.d.ts.map +1 -1
- package/types/check/diagnose.d.ts.map +1 -1
- package/types/check/handler-events.d.ts +9 -0
- package/types/check/handler-events.d.ts.map +1 -0
- package/types/check/index.d.ts +1 -0
- package/types/check/index.d.ts.map +1 -1
- package/types/check/link-flag-tests.d.ts +4 -0
- package/types/check/link-flag-tests.d.ts.map +1 -0
- package/types/check/project.d.ts.map +1 -1
- package/types/check/regroup.d.ts +1 -1
- package/types/check/regroup.d.ts.map +1 -1
- package/types/check/renamed-packages.d.ts +1 -0
- package/types/check/renamed-packages.d.ts.map +1 -1
- package/types/check/schema-builders.d.ts +3 -0
- package/types/check/schema-builders.d.ts.map +1 -0
- package/types/check/secret-keys.d.ts.map +1 -1
- package/types/check/shadow-types.d.ts.map +1 -1
- package/types/check/template-hints.d.ts +1 -0
- package/types/check/template-hints.d.ts.map +1 -1
- package/types/check/tool-labels.d.ts.map +1 -1
- package/types/check/ui-types.d.ts.map +1 -1
- package/types/check/ui.d.ts.map +1 -1
- package/types/commands/env-declare.d.ts.map +1 -1
- package/types/compiler/a11y.d.ts.map +1 -1
- package/types/compiler/bound-props.d.ts +1 -0
- package/types/compiler/bound-props.d.ts.map +1 -1
- package/types/compiler/codegen.d.ts +1 -0
- package/types/compiler/codegen.d.ts.map +1 -1
- package/types/compiler/deprecated-lints-1-106.d.ts +12 -0
- package/types/compiler/deprecated-lints-1-106.d.ts.map +1 -0
- package/types/compiler/deprecated-lints.d.ts +32 -5
- package/types/compiler/deprecated-lints.d.ts.map +1 -1
- package/types/compiler/deprecated-typed-lints.d.ts +21 -0
- package/types/compiler/deprecated-typed-lints.d.ts.map +1 -0
- package/types/compiler/dom-here.d.ts +14 -0
- package/types/compiler/dom-here.d.ts.map +1 -0
- package/types/compiler/dom.d.ts +1 -0
- package/types/compiler/dom.d.ts.map +1 -1
- package/types/compiler/fence-checks.d.ts.map +1 -1
- package/types/compiler/field-described.d.ts +1 -1
- package/types/compiler/field-described.d.ts.map +1 -1
- package/types/compiler/html-fields.d.ts +19 -0
- package/types/compiler/html-fields.d.ts.map +1 -0
- package/types/compiler/html-initial.d.ts +3 -2
- package/types/compiler/html-initial.d.ts.map +1 -1
- package/types/compiler/html.d.ts.map +1 -1
- package/types/compiler/index.d.ts +1 -0
- package/types/compiler/index.d.ts.map +1 -1
- package/types/compiler/items.d.ts +4 -1
- package/types/compiler/items.d.ts.map +1 -1
- package/types/compiler/link-flags.d.ts +6 -0
- package/types/compiler/link-flags.d.ts.map +1 -1
- package/types/compiler/lints.d.ts +44 -17
- package/types/compiler/lints.d.ts.map +1 -1
- package/types/compiler/rest-omits.d.ts +4 -0
- package/types/compiler/rest-omits.d.ts.map +1 -0
- package/types/config-loader.d.ts.map +1 -1
- package/types/config-resolve.d.ts.map +1 -1
- package/types/config.d.ts +15 -2
- package/types/config.d.ts.map +1 -1
- package/types/content/markdown.d.ts.map +1 -1
- package/types/migrate/content.d.ts.map +1 -1
- package/types/runtime/action-shared.d.ts +1 -1
- package/types/runtime/action-shared.d.ts.map +1 -1
- package/types/runtime/action.d.ts +3 -0
- package/types/runtime/action.d.ts.map +1 -1
- package/types/runtime/form-types.d.ts +6 -0
- package/types/runtime/form-types.d.ts.map +1 -0
- package/types/runtime/form.d.ts +3 -1
- package/types/runtime/form.d.ts.map +1 -1
- package/types/runtime/html.d.ts +3 -0
- package/types/runtime/html.d.ts.map +1 -1
- package/types/runtime/markdown-autolink.d.ts +11 -0
- package/types/runtime/markdown-autolink.d.ts.map +1 -0
- package/types/runtime/markdown-render.client.d.ts +2 -1
- package/types/runtime/markdown-render.client.d.ts.map +1 -1
- package/types/runtime/markdown.client.d.ts +3 -1
- package/types/runtime/markdown.client.d.ts.map +1 -1
- package/types/runtime/markdown.d.ts +11 -0
- package/types/runtime/markdown.d.ts.map +1 -1
- package/types/runtime/markdown.server.d.ts +3 -1
- package/types/runtime/markdown.server.d.ts.map +1 -1
- package/types/runtime/navigate-api.d.ts +1 -1
- package/types/runtime/navigate-api.d.ts.map +1 -1
- package/types/runtime/persist.d.ts +2 -2
- package/types/runtime/persist.d.ts.map +1 -1
- package/types/runtime/reactive.d.ts +3 -0
- package/types/runtime/reactive.d.ts.map +1 -1
- package/types/runtime/sanitize.d.ts.map +1 -1
- package/types/runtime/schema-record.d.ts +9 -0
- package/types/runtime/schema-record.d.ts.map +1 -0
- package/types/runtime/schema.client.d.ts.map +1 -1
- package/types/runtime/schema.d.ts +94 -10
- package/types/runtime/schema.d.ts.map +1 -1
- package/types/runtime/swap-focus.d.ts.map +1 -1
- package/types/server/action-module.d.ts +2 -0
- package/types/server/action-module.d.ts.map +1 -0
- package/types/server/actions.d.ts +1 -0
- package/types/server/actions.d.ts.map +1 -1
- package/types/server/api.d.ts.map +1 -1
- package/types/server/app-stages.d.ts +3 -2
- package/types/server/app-stages.d.ts.map +1 -1
- package/types/server/env-state.d.ts.map +1 -1
- package/types/server/env.d.ts +5 -1
- package/types/server/env.d.ts.map +1 -1
- package/types/server/favicon.d.ts +3 -1
- package/types/server/favicon.d.ts.map +1 -1
- package/types/server/kept-pages.d.ts +1 -1
- package/types/server/kept-pages.d.ts.map +1 -1
- package/types/server/library.d.ts +2 -0
- package/types/server/library.d.ts.map +1 -1
- package/types/server/mcp.d.ts +1 -1
- package/types/server/mcp.d.ts.map +1 -1
- package/types/server/notifications.d.ts +2 -1
- package/types/server/notifications.d.ts.map +1 -1
- package/types/server/offline.d.ts.map +1 -1
- package/types/server/push-settings.d.ts +1 -1
- package/types/server/push-settings.d.ts.map +1 -1
- package/types/server/push.d.ts +1 -0
- package/types/server/push.d.ts.map +1 -1
- package/types/server/query.d.ts.map +1 -1
- package/types/server/scope.d.ts +1 -1
- package/types/server/scope.d.ts.map +1 -1
- package/types/test/a11y-dom.d.ts +20 -0
- package/types/test/a11y-dom.d.ts.map +1 -0
- package/types/test/html-snapshot.d.ts +4 -0
- package/types/test/html-snapshot.d.ts.map +1 -0
- package/types/test/index.d.ts.map +1 -1
- package/types/test/mount.d.ts +3 -0
- package/types/test/mount.d.ts.map +1 -1
- package/types/test/page-checks.d.ts +6 -0
- package/types/test/page-checks.d.ts.map +1 -0
- package/types/test/screenshot-match.d.ts +1 -1
- package/types/test/screenshot-match.d.ts.map +1 -1
- package/types/test/shared-modules.d.ts.map +1 -1
- package/types/test/site-types.d.ts +3 -1
- package/types/test/site-types.d.ts.map +1 -1
|
@@ -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
|
-
|
|
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
|
}
|
package/src/runtime/markdown.ts
CHANGED
|
@@ -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> = { '&': '&', '<': '<', '>': '>', '"': '"', ''': '\'' }
|
|
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 `&`. 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
|
|
|
@@ -17,16 +17,21 @@ export interface NavigateOptions {
|
|
|
17
17
|
transition?: boolean | undefined
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
// Query params to set on the current page, a null, undefined or empty one dropped.
|
|
21
|
-
export type QueryChanges = Readonly<Record<string, string | number | null | undefined>>
|
|
20
|
+
// Query params to set on the current page, a null, undefined or empty one dropped; a list sets the param once per item (`?tag=a&tag=b`), an empty list dropping it.
|
|
21
|
+
export type QueryChanges = Readonly<Record<string, string | number | readonly (string | number)[] | null | undefined>>
|
|
22
22
|
|
|
23
|
-
// Sets each param of `changes` on `params`, a null, undefined or empty one dropped and the others kept: the rule of `navigate
|
|
23
|
+
// Sets each param of `changes` on `params`, a null, undefined or empty one dropped and the others kept: the rule of `navigate.query` and of `withQuery`.
|
|
24
24
|
export function mergeQuery(params: URLSearchParams, changes: QueryChanges): void {
|
|
25
25
|
for (const [name, value] of Object.entries(changes)) {
|
|
26
|
-
if (value == null || value === '')
|
|
26
|
+
if (value == null || value === '' || typeof value === 'object') {
|
|
27
27
|
params.delete(name)
|
|
28
|
-
|
|
28
|
+
|
|
29
|
+
for (const item of value ?? [])
|
|
30
|
+
params.append(name, String(item))
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
29
33
|
params.set(name, String(value))
|
|
34
|
+
}
|
|
30
35
|
}
|
|
31
36
|
}
|
|
32
37
|
|
package/src/runtime/persist.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { Duration } from './duration'
|
|
2
|
-
import type {
|
|
2
|
+
import type { LoadScope } from './reactive'
|
|
3
3
|
import { TRAILING_SLASH, withBase } from './base'
|
|
4
4
|
import { durationSeconds } from './duration'
|
|
5
5
|
import { KayError } from './kay-error'
|
|
6
6
|
import { kayLog } from './log'
|
|
7
|
-
import { createSignal,
|
|
7
|
+
import { createSignal, currentRequestScope, isHydrating, untrack, whenHydrated } from './reactive'
|
|
8
8
|
import { sendValue } from './send'
|
|
9
9
|
import { decodeValue } from './wire'
|
|
10
10
|
|
|
@@ -28,7 +28,7 @@ export type Stored<T> = { found: true, value: T } | { found: false }
|
|
|
28
28
|
|
|
29
29
|
// Where a kept signal's value lives: what the request being rendered holds, which the server renders with, none for a store only the browser sees; what the browser holds, and how it keeps a new value.
|
|
30
30
|
export interface Store<T> {
|
|
31
|
-
request?: ((scope:
|
|
31
|
+
request?: ((scope: LoadScope) => Stored<T>) | undefined
|
|
32
32
|
stored: () => Stored<T>
|
|
33
33
|
save: (value: T) => void
|
|
34
34
|
// A store the server never sees: an island claiming its markup reads the value it rendered with, the kept one applying once it has hydrated.
|
|
@@ -176,12 +176,12 @@ function inBrowser<T>(store: Store<T>, read: () => T, write: (value: T) => void)
|
|
|
176
176
|
|
|
177
177
|
// The server reads the request being rendered once per request, until a render of it writes the signal.
|
|
178
178
|
function serverSide<T>(request: Store<T>['request'], read: () => T): Persisted<T> {
|
|
179
|
-
const fromRequest = new WeakMap<
|
|
180
|
-
const written = new WeakSet<
|
|
179
|
+
const fromRequest = new WeakMap<LoadScope, Stored<T>>()
|
|
180
|
+
const written = new WeakSet<LoadScope>()
|
|
181
181
|
|
|
182
182
|
return {
|
|
183
183
|
read: () => {
|
|
184
|
-
const scope =
|
|
184
|
+
const scope = currentRequestScope()
|
|
185
185
|
|
|
186
186
|
if (!scope || !request || written.has(scope))
|
|
187
187
|
return read()
|
|
@@ -196,7 +196,7 @@ function serverSide<T>(request: Store<T>['request'], read: () => T): Persisted<T
|
|
|
196
196
|
return stored.found ? stored.value : read()
|
|
197
197
|
},
|
|
198
198
|
save: () => {
|
|
199
|
-
const scope =
|
|
199
|
+
const scope = currentRequestScope()
|
|
200
200
|
|
|
201
201
|
if (scope)
|
|
202
202
|
written.add(scope)
|
|
@@ -106,7 +106,7 @@ export function keepParam<T>(name: string, read: () => T, write: (value: T) => v
|
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
return keep<T>({
|
|
109
|
-
request: scope => found(scope.param(name), values, fallback),
|
|
109
|
+
request: scope => found(scope.param?.(name), values, fallback),
|
|
110
110
|
stored,
|
|
111
111
|
save: value => saveParam(name, value, fallback, push),
|
|
112
112
|
}, read, write)
|
package/src/runtime/reactive.ts
CHANGED
|
@@ -364,6 +364,20 @@ export function currentServerScope(): ServerScope | undefined {
|
|
|
364
364
|
return serverScope?.()
|
|
365
365
|
}
|
|
366
366
|
|
|
367
|
+
// What a page's server fence reads of its request while it loads: its cookies, which a persisted signal answers with; a query param is the render's alone.
|
|
368
|
+
export type LoadScope = Pick<ServerScope, 'cookie'> & Partial<Pick<ServerScope, 'param'>>
|
|
369
|
+
|
|
370
|
+
let requestCookieScope: (() => LoadScope | undefined) | undefined
|
|
371
|
+
|
|
372
|
+
export function setRequestCookieScope(scope: () => LoadScope | undefined): void {
|
|
373
|
+
requestCookieScope = scope
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// The scope a persisted signal reads its cookie through: the render's, or the load's before the render's is made.
|
|
377
|
+
export function currentRequestScope(): LoadScope | undefined {
|
|
378
|
+
return serverScope?.() ?? requestCookieScope?.()
|
|
379
|
+
}
|
|
380
|
+
|
|
367
381
|
// Any write while a request renders, a page's script or an island's, stays with that request; the browser installs no scope.
|
|
368
382
|
function scopedValues(node: { shared: boolean }): WeakMap<object, unknown> | undefined {
|
|
369
383
|
return node.shared ? interceptor?.values?.() : undefined
|
package/src/runtime/sanitize.ts
CHANGED
|
@@ -37,6 +37,8 @@ const TRAILING_COMMAS = /,+$/
|
|
|
37
37
|
const DESCRIPTOR_TEXT = /^[\d.\sxwh]*$/i
|
|
38
38
|
const ATTRIBUTE = /([a-z][\w:-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+))/gi
|
|
39
39
|
const LINK_ATTRIBUTES = ['href']
|
|
40
|
+
// The only class a `code` keeps, whatever attributes the options allow: the language of a fence, so a user's text cannot pick a utility class that covers the page.
|
|
41
|
+
const LANGUAGE_CLASS = /^language-[\w+#.-]+$/
|
|
40
42
|
|
|
41
43
|
// How the URLs of one call are rewritten: `base` is a URL to resolve against, `null` for a `base` that is none, which keeps only the absolute URLs, and `undefined` for no `base`. `leaves` tells a link opening in a new tab, `undefined` for markup that is not opened apart.
|
|
42
44
|
interface Rewrite {
|
|
@@ -176,7 +178,7 @@ function openingTag(tag: string, attributes: string | undefined, allowedNames: r
|
|
|
176
178
|
const attribute = name!.toLowerCase()
|
|
177
179
|
const value = double ?? single ?? bare ?? ''
|
|
178
180
|
|
|
179
|
-
const fit = allowedNames.includes(attribute) && !written.has(attribute) ? keptValue(attribute, value, rewrite) : undefined
|
|
181
|
+
const fit = allowedNames.includes(attribute) && !written.has(attribute) && (tag !== 'code' || attribute !== 'class' || LANGUAGE_CLASS.test(value)) ? keptValue(attribute, value, rewrite) : undefined
|
|
180
182
|
|
|
181
183
|
if (fit !== undefined)
|
|
182
184
|
written.set(attribute, text(fit).replaceAll('"', '"'))
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { t } from 'elysia'
|
|
2
|
+
import type { Schema } from './schema'
|
|
3
|
+
|
|
4
|
+
// A plain record of schemas, which every API taking a schema reads as the object of those fields.
|
|
5
|
+
export type SchemaShape = Record<string, Schema>
|
|
6
|
+
|
|
7
|
+
// The object schema of a record, as `s.object(shape)` types it.
|
|
8
|
+
export type ObjectOf<Shape extends SchemaShape> = ReturnType<typeof t.Object<Shape>>
|
|
9
|
+
|
|
10
|
+
// Either a schema or the record of one.
|
|
11
|
+
export type SchemaInput = Schema | SchemaShape
|
|
12
|
+
|
|
13
|
+
export type SchemaOf<Input extends SchemaInput> = Input extends Schema ? Input : Input extends SchemaShape ? ObjectOf<Input> : never
|
|
14
|
+
|
|
15
|
+
// The symbols TypeBox reads a schema and an optional field by, so this module needs no builder, which would carry Elysia into an island importing it.
|
|
16
|
+
const KIND = Symbol.for('TypeBox.Kind')
|
|
17
|
+
const OPTIONAL = Symbol.for('TypeBox.Optional')
|
|
18
|
+
|
|
19
|
+
export function isSchema(value: unknown): value is Schema {
|
|
20
|
+
return typeof value === 'object' && value !== null && KIND in value
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// A schema as it is, a record as the strict object of its fields: a key the record lacks is refused, as `s.object(shape)` does.
|
|
24
|
+
export function schemaOf<Input extends SchemaInput>(input: Input): SchemaOf<Input> {
|
|
25
|
+
if (isSchema(input))
|
|
26
|
+
return input as SchemaOf<Input>
|
|
27
|
+
|
|
28
|
+
const required = Object.entries(input).filter(([, field]) => !(OPTIONAL in field)).map(([name]) => name)
|
|
29
|
+
|
|
30
|
+
return { [KIND]: 'Object', type: 'object', properties: { ...input }, ...required.length > 0 && { required }, additionalProperties: false } as unknown as SchemaOf<Input>
|
|
31
|
+
}
|
|
@@ -19,9 +19,6 @@ const build = () => schema
|
|
|
19
19
|
// The browser's `s`: a module shared with the server may build its schemas as it loads, and they cost nothing here, as carrying Elysia into every island would weigh about 190 KB.
|
|
20
20
|
export const s = /* @__PURE__ */ new Proxy({}, {
|
|
21
21
|
get: (_, name) => {
|
|
22
|
-
|
|
23
|
-
return undefined
|
|
24
|
-
|
|
25
|
-
return name === 'typebox' ? s : build
|
|
22
|
+
return typeof name === 'symbol' || name === 'then' ? undefined : build
|
|
26
23
|
},
|
|
27
24
|
}) as typeof server
|