@crombolo/render 0.57.0 → 0.58.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +3 -3
  2. package/src/model.ts +5 -0
  3. package/src/seo.ts +66 -21
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/render",
3
- "version": "0.57.0",
3
+ "version": "0.58.1",
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",
@@ -35,8 +35,8 @@
35
35
  "access": "public"
36
36
  },
37
37
  "dependencies": {
38
- "@crombolo/common": "0.57.0",
39
- "@crombolo/core": "0.57.0",
38
+ "@crombolo/common": "0.58.1",
39
+ "@crombolo/core": "0.58.1",
40
40
  "@types/react": "^19.3.0"
41
41
  }
42
42
  }
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/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' }