@crombolo/render 0.56.0 → 0.58.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/render",
3
- "version": "0.56.0",
3
+ "version": "0.58.0",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. TSX server rendering and the published-content model for crombolo.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -24,7 +24,8 @@
24
24
  "./jsx-runtime": "./src/jsx-runtime.ts",
25
25
  "./jsx-dev-runtime": "./src/jsx-runtime.ts",
26
26
  "./loose/jsx-runtime": "./src/loose/jsx-runtime.ts",
27
- "./loose/jsx-dev-runtime": "./src/loose/jsx-runtime.ts"
27
+ "./loose/jsx-dev-runtime": "./src/loose/jsx-runtime.ts",
28
+ "./html": "./src/html.ts"
28
29
  },
29
30
  "files": [
30
31
  "src",
@@ -34,8 +35,8 @@
34
35
  "access": "public"
35
36
  },
36
37
  "dependencies": {
37
- "@crombolo/common": "0.56.0",
38
- "@crombolo/core": "0.56.0",
38
+ "@crombolo/common": "0.58.0",
39
+ "@crombolo/core": "0.58.0",
39
40
  "@types/react": "^19.3.0"
40
41
  }
41
42
  }
package/src/context.ts CHANGED
@@ -28,6 +28,8 @@ interface RenderScope {
28
28
  ids: { next: number }
29
29
  /** A block's component by element type alias, from the page's own views generation. */
30
30
  blockComponent?: BlockComponentLoader
31
+ /** An extension's component, `<id>/<name>`, for `<Extension use>` (story 312). */
32
+ extensionComponent?: ExtensionComponentLoader
31
33
  /** A draft is being previewed in the back office. */
32
34
  preview?: boolean
33
35
  /** The signed-in member's anti-forgery token, for `<AntiforgeryMeta />`. */
@@ -65,9 +67,15 @@ export type BlockComponentLoader = (
65
67
  ) => Promise<((props: never) => Child | Promise<Child>) | undefined>
66
68
 
67
69
  /** What a page render carries besides its props: the search, blocks, and whether it is a preview. */
70
+ /** An extension's component as a render function, or undefined when no extension offers it. */
71
+ export type ExtensionComponentLoader = (
72
+ alias: string,
73
+ ) => ((props: object) => Child | Promise<Child>) | undefined
74
+
68
75
  export interface RenderScopeOptions {
69
76
  search?: SiteSearchPort
70
77
  blockComponent?: BlockComponentLoader
78
+ extensionComponent?: ExtensionComponentLoader
71
79
  preview?: boolean
72
80
  antiforgery?: string
73
81
  request?: RequestValues
@@ -208,6 +216,11 @@ export function actionState<T = unknown>(action: unknown): ActionState<T> | unde
208
216
  }
209
217
 
210
218
  /** The signed-in member's anti-forgery token for this render, or undefined for a visitor. */
219
+ /** The loader for an extension's components, while a page renders; undefined outside one. */
220
+ export function currentExtensionComponent(): ExtensionComponentLoader | undefined {
221
+ return store.getStore()?.extensionComponent
222
+ }
223
+
211
224
  export function currentAntiforgery(): string | undefined {
212
225
  return memberAntiforgery(within('<AntiforgeryMeta />'))
213
226
  }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * `<Extension use="brochures/card" … />`: an extension's component in a site's
3
+ * page (story 312). It renders in the extension's own process, given the page's
4
+ * props and these, and its HTML goes here. Children render here first and go as
5
+ * HTML. Nothing renders where no extension offers it, or where it is not granted
6
+ * `templates`, and the site's log says why. docs/writing-components.md.
7
+ */
8
+ import { currentExtensionComponent, usePage } from './context.ts'
9
+ import { RawHtml } from './html.ts'
10
+ import { type Child, renderToString } from './jsx-runtime.ts'
11
+
12
+ export interface ExtensionProps {
13
+ /** `<extension id>/<component>`. */
14
+ use: string
15
+ children?: Child
16
+ [prop: string]: unknown
17
+ }
18
+
19
+ export async function Extension({ use, children, ...props }: ExtensionProps): Promise<RawHtml> {
20
+ const render = currentExtensionComponent()?.(use)
21
+ if (!render) return new RawHtml('')
22
+ const inner = children === undefined ? undefined : new RawHtml(await renderToString(children))
23
+ const rendered = await render({
24
+ ...usePage(),
25
+ ...props,
26
+ ...(inner ? { children: inner } : {}),
27
+ })
28
+ return rendered instanceof RawHtml ? rendered : new RawHtml(await renderToString(rendered))
29
+ }
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export * from './context.ts'
7
7
  export * from './deferred.ts'
8
8
  export * from './email.ts'
9
9
  export * from './excerpt.ts'
10
+ export * from './extension.ts'
10
11
  export * from './form-script.ts'
11
12
  export * from './forms.ts'
12
13
  export * from './gap.ts'
package/src/model.ts CHANGED
@@ -126,6 +126,11 @@ export class PublishedContent<Properties = any> {
126
126
  return !(hidden === true || hidden === 1 || hidden === '1' || hidden === 'true')
127
127
  }
128
128
 
129
+ /** A dictionary item's words in the page's culture; undefined when no item has the name. */
130
+ word(name: string): string | undefined {
131
+ return this.#context?.word?.(name)
132
+ }
133
+
129
134
  /** The culture in scope: the request's for content that varies by culture, else null. */
130
135
  get culture(): string | null {
131
136
  return this.#culture
package/src/renderer.ts CHANGED
@@ -72,6 +72,12 @@ export interface RenderOptions {
72
72
  * with (story 209); by default the generation's own build.
73
73
  */
74
74
  islandsManifest?: (generation: string | undefined) => string | undefined
75
+ /**
76
+ * Components an extension owns (story 312): `<id>/<name>`, or a block's
77
+ * `blocks/<alias>`, where the site has none of its own. Each renders in the
78
+ * extension's process.
79
+ */
80
+ extensionComponents?: { module(alias: string): ComponentModule | undefined }
75
81
  }
76
82
 
77
83
  export const DEFAULT_RENDER_TIMEOUT_MS = 10_000
@@ -228,6 +234,8 @@ export class Renderer {
228
234
  #renderTimeoutMs: number
229
235
  #islandsManifest: (generation: string | undefined) => string | undefined
230
236
 
237
+ #extensionComponents: RenderOptions['extensionComponents']
238
+
231
239
  constructor(options: RenderOptions) {
232
240
  this.#cache = options.cache
233
241
  // Resolved to an absolute path: a dynamic import of a relative specifier
@@ -235,6 +243,7 @@ export class Renderer {
235
243
  this.#componentsDir = resolve(options.componentsDir)
236
244
  this.#snapshots = options.snapshots
237
245
  this.#renderTimeoutMs = options.renderTimeoutMs ?? DEFAULT_RENDER_TIMEOUT_MS
246
+ this.#extensionComponents = options.extensionComponents
238
247
  this.#islandsManifest =
239
248
  options.islandsManifest ??
240
249
  ((generation) => (generation ? `${ISLANDS_PATH}/${generation}/manifest.json` : undefined))
@@ -577,6 +586,10 @@ export class Renderer {
577
586
  preview: request.preview,
578
587
  antiforgery: request.antiforgery,
579
588
  blockComponent: (blockAlias) => this.blockComponent(blockAlias, used),
589
+ extensionComponent: (use) =>
590
+ this.#extensionComponents?.module(use)?.default as
591
+ | ((props: object) => Child | Promise<Child>)
592
+ | undefined,
580
593
  request: { headers: shared ? new Headers() : (request.headers ?? new Headers()) },
581
594
  site: request.site,
582
595
  personal: shared ? { value: false } : personal,
@@ -662,7 +675,8 @@ export class Renderer {
662
675
 
663
676
  async #load(alias: string, used?: Set<string>): Promise<ComponentModule | undefined> {
664
677
  const file = this.componentPath(alias)
665
- if (!file) return undefined
678
+ // The site's own component wins; an extension's renders in its own process.
679
+ if (!file) return this.#extensionComponents?.module(alias)
666
680
  used?.add(file)
667
681
  // The path carries the generation, so one entry per view per generation and
668
682
  // a swap never answers from the generation before it.
package/src/seo.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  * editor's preview both call it, so what an editor is shown is what is served.
7
7
  */
8
8
 
9
+ import { expandSeoValue, type SeoMapping } from '@crombolo/core'
9
10
  import { currentMetadata } from './context.ts'
10
11
  import { RawHtml } from './html.ts'
11
12
  import { element } from './jsx-runtime.ts'
@@ -29,6 +30,11 @@ export interface SeoSiteDefaults {
29
30
  defaultImage?: string
30
31
  /** False on a site search engines should stay out of entirely, such as staging. */
31
32
  indexable: boolean
33
+ /**
34
+ * Each content type's `[seo]` values, by alias (story 317). A fixed image is
35
+ * already an address; any other `image` is a media picker property's alias.
36
+ */
37
+ types?: Readonly<Record<string, SeoMapping>>
32
38
  }
33
39
 
34
40
  /** What a template supplies when the page itself says nothing. */
@@ -60,12 +66,14 @@ export interface Metadata {
60
66
  openGraph?: { images?: string | { url: string } | Array<string | { url: string }> }
61
67
  }
62
68
 
63
- /** Where a value came from: the page's own field, the template, the site, or the page name. */
64
- export type MetaSource = 'page' | 'metadata' | 'template' | 'site' | 'name' | 'none'
69
+ /** Where a value came from: the page's own field, its type's setting, the template, the site, or the page name. */
70
+ export type MetaSource = 'page' | 'metadata' | 'type' | 'template' | 'site' | 'name' | 'none'
65
71
 
66
72
  export interface MetaValue<T> {
67
73
  value: T
68
74
  source: MetaSource
75
+ /** What `generateMetadata` replaced, when that was the type's setting (story 317). */
76
+ over?: MetaSource
69
77
  }
70
78
 
71
79
  export interface Alternate {
@@ -134,18 +142,28 @@ function firstImage(images: NonNullable<Metadata['openGraph']>['images']): strin
134
142
  /** A page's values with what its `generateMetadata` returned laid over them. Pure. */
135
143
  function withMetadata(meta: EffectiveMeta, metadata: Metadata, origin: string): EffectiveMeta {
136
144
  const out = { ...meta }
145
+ const over = (was: MetaValue<unknown>) => (was.source === 'type' ? { over: 'type' as const } : {})
137
146
  if (filled(metadata.title)) {
138
147
  const suffix = meta.fullTitle.slice(meta.title.value.length)
139
- out.title = { value: metadata.title, source: 'metadata' }
148
+ out.title = { value: metadata.title, source: 'metadata', ...over(meta.title) }
140
149
  out.fullTitle = `${metadata.title}${suffix}`
141
150
  }
142
151
  if (filled(metadata.description))
143
- out.description = { value: metadata.description, source: 'metadata' }
152
+ out.description = {
153
+ value: metadata.description,
154
+ source: 'metadata',
155
+ ...over(meta.description),
156
+ }
144
157
  const canonical = metadata.alternates?.canonical
145
158
  if (filled(canonical))
146
- out.canonical = { value: absoluteUrl(canonical, origin), source: 'metadata' }
159
+ out.canonical = {
160
+ value: absoluteUrl(canonical, origin),
161
+ source: 'metadata',
162
+ ...over(meta.canonical),
163
+ }
147
164
  const image = firstImage(metadata.openGraph?.images)
148
- if (filled(image)) out.image = { value: absoluteUrl(image, origin), source: 'metadata' }
165
+ if (filled(image))
166
+ out.image = { value: absoluteUrl(image, origin), source: 'metadata', ...over(meta.image) }
149
167
  const robots = metadata.robots
150
168
  const noindex = typeof robots === 'string' ? /\bnoindex\b/i.test(robots) : robots?.index === false
151
169
  if (noindex && !meta.noindex.value) out.noindex = { value: true, source: 'metadata' }
@@ -160,42 +178,69 @@ function withMetadata(meta: EffectiveMeta, metadata: Metadata, origin: string):
160
178
  return out
161
179
  }
162
180
 
181
+ /** A type's sharing image: an address as given, or the first pick of the media picker it names. */
182
+ async function mappedImage(
183
+ content: PublishedContent,
184
+ image: string | undefined,
185
+ ): Promise<string | undefined> {
186
+ if (!image) return undefined
187
+ if (/^(https?:)?\/\//i.test(image) || image.startsWith('/')) return image
188
+ return (await content.media(image))[0]?.url
189
+ }
190
+
163
191
  async function pageMeta(
164
192
  content: PublishedContent,
165
193
  context: MetaContext,
166
194
  overrides: SeoOverrides,
167
195
  ): Promise<EffectiveMeta> {
168
196
  const { origin, site } = context
169
- const own = (alias: string) => content.text(alias).trim()
197
+ // A value may name the page's properties and dictionary items (story 317).
198
+ const reader = {
199
+ property: (alias: string) => content.text(alias),
200
+ word: (key: string) => content.word(key),
201
+ }
202
+ const expanded = (text: string | undefined) => expandSeoValue(text, reader) ?? ''
203
+ const own = (alias: string) => expanded(content.text(alias))
204
+ const mapping = site.types?.[content.contentType.alias] ?? {}
205
+ const typed = (field: 'title' | 'description' | 'canonical') => expanded(mapping[field])
170
206
 
171
207
  const title: MetaValue<string> = filled(own(SEO_ALIASES.title))
172
208
  ? { value: own(SEO_ALIASES.title), source: 'page' }
173
- : filled(overrides.title)
174
- ? { value: overrides.title, source: 'template' }
175
- : { value: content.name, source: 'name' }
209
+ : filled(typed('title'))
210
+ ? { value: typed('title'), source: 'type' }
211
+ : filled(overrides.title)
212
+ ? { value: overrides.title, source: 'template' }
213
+ : { value: content.name, source: 'name' }
176
214
  const suffix =
177
215
  overrides.titleSuffix === null ? '' : (overrides.titleSuffix ?? site.titleSuffix ?? '')
178
216
 
179
217
  const description: MetaValue<string | undefined> = filled(own(SEO_ALIASES.description))
180
218
  ? { value: own(SEO_ALIASES.description), source: 'page' }
181
- : filled(overrides.description)
182
- ? { value: overrides.description, source: 'template' }
183
- : { value: undefined, source: 'none' }
219
+ : filled(typed('description'))
220
+ ? { value: typed('description'), source: 'type' }
221
+ : filled(overrides.description)
222
+ ? { value: overrides.description, source: 'template' }
223
+ : { value: undefined, source: 'none' }
184
224
 
185
225
  const picked = (await content.media(SEO_ALIASES.image))[0]?.url
226
+ const typeImage = await mappedImage(content, mapping.image)
186
227
  const image: MetaValue<string | undefined> = filled(picked)
187
228
  ? { value: absoluteUrl(picked, origin), source: 'page' }
188
- : filled(overrides.image)
189
- ? { value: absoluteUrl(overrides.image, origin), source: 'template' }
190
- : filled(site.defaultImage)
191
- ? { value: absoluteUrl(site.defaultImage, origin), source: 'site' }
192
- : { value: undefined, source: 'none' }
229
+ : filled(typeImage)
230
+ ? { value: absoluteUrl(typeImage, origin), source: 'type' }
231
+ : filled(overrides.image)
232
+ ? { value: absoluteUrl(overrides.image, origin), source: 'template' }
233
+ : filled(site.defaultImage)
234
+ ? { value: absoluteUrl(site.defaultImage, origin), source: 'site' }
235
+ : { value: undefined, source: 'none' }
193
236
 
194
237
  const canonical: MetaValue<string | undefined> = filled(own(SEO_ALIASES.canonical))
195
238
  ? { value: absoluteUrl(own(SEO_ALIASES.canonical), origin), source: 'page' }
196
- : filled(overrides.canonical)
197
- ? { value: absoluteUrl(overrides.canonical, origin), source: 'template' }
198
- : { value: absoluteUrl(content.url, origin), source: 'name' }
239
+ : filled(typed('canonical'))
240
+ ? { value: absoluteUrl(typed('canonical'), origin), source: 'type' }
241
+ : filled(overrides.canonical)
242
+ ? { value: absoluteUrl(overrides.canonical, origin), source: 'template' }
243
+ : { value: absoluteUrl(content.url, origin), source: 'name' }
199
244
 
200
245
  const noindex: MetaValue<boolean> = !site.indexable
201
246
  ? { value: true, source: 'site' }