@crombolo/core 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crombolo/core",
3
- "version": "0.57.0",
3
+ "version": "0.58.1",
4
4
  "description": "Internal to crombolo, with no compatibility promise: use @crombolo/crombolo. Domain model, entity keys and shared contracts for crombolo. No I/O.",
5
5
  "license": "MIT",
6
6
  "author": "Crombolo",
@@ -29,6 +29,6 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@crombolo/common": "0.57.0"
32
+ "@crombolo/common": "0.58.1"
33
33
  }
34
34
  }
@@ -48,6 +48,20 @@ export type SiteSearchLevel = 'high' | 'normal' | 'low'
48
48
 
49
49
  export const SITE_SEARCH_LEVELS: readonly SiteSearchLevel[] = ['high', 'normal', 'low']
50
50
 
51
+ /**
52
+ * Where a type's pages take their search and sharing values from when the page
53
+ * gives none (story 317). Text may hold `{alias}` and `{#key}`; `image` is a
54
+ * media picker property's alias.
55
+ */
56
+ export interface SeoMapping {
57
+ title?: string
58
+ description?: string
59
+ image?: string
60
+ canonical?: string
61
+ }
62
+
63
+ export const SEO_MAPPING_FIELDS = ['title', 'description', 'image', 'canonical'] as const
64
+
51
65
  export interface PropertyGroupModel {
52
66
  key: string
53
67
  name: string | null
@@ -103,6 +117,8 @@ export interface ContentTypeAggregate {
103
117
  siteSearch?: boolean
104
118
  /** A site's settings (story 058). Absent means unchanged on a save. */
105
119
  siteSettings?: boolean
120
+ /** Its pages' search and sharing values (story 317); null for none. Absent means unchanged on a save. */
121
+ seo?: SeoMapping | null
106
122
  /** The form a Library form type is made from (story 239); null for every other type. Absent means unchanged on a save. */
107
123
  formKey?: string | null
108
124
  }
package/src/index.ts CHANGED
@@ -35,6 +35,7 @@ export * from './return-urls.ts'
35
35
  export * from './search-conformance.ts'
36
36
  export * from './search-providers.ts'
37
37
  export * from './sections.ts'
38
+ export * from './seo-mapping.ts'
38
39
  export * from './uploads.ts'
39
40
  export * from './users.ts'
40
41
  export * from './workflows.ts'
@@ -0,0 +1,44 @@
1
+ /**
2
+ * A search and sharing value as a type or a page sets it (story 317): fixed text,
3
+ * `{alias}` for a property of the page, `{#key}` for a dictionary item, or a mix.
4
+ * `{{` and `}}` write a brace. `docs/seo.md`, "Where a type's values come from".
5
+ */
6
+
7
+ const TOKEN = /\{\{|\}\}|\{(#?)([^{}\s]+)\}/g
8
+
9
+ export type SeoToken = { kind: 'property'; alias: string } | { kind: 'word'; key: string }
10
+
11
+ /** The properties and dictionary items a value names, in order. */
12
+ export function seoTokens(text: string): SeoToken[] {
13
+ const tokens: SeoToken[] = []
14
+ for (const match of text.matchAll(TOKEN)) {
15
+ const name = match[2]
16
+ if (!name) continue
17
+ tokens.push(match[1] ? { kind: 'word', key: name } : { kind: 'property', alias: name })
18
+ }
19
+ return tokens
20
+ }
21
+
22
+ /**
23
+ * The value with its tokens read; undefined when it is empty, or when it names
24
+ * something and everything it names reads empty, so the next source answers.
25
+ */
26
+ export function expandSeoValue(
27
+ text: string | undefined,
28
+ read: { property(alias: string): string; word(key: string): string | undefined },
29
+ ): string | undefined {
30
+ if (!text?.trim()) return undefined
31
+ let named = 0
32
+ let filled = 0
33
+ const out = text.replace(TOKEN, (whole, hash: string, name: string | undefined) => {
34
+ if (whole === '{{') return '{'
35
+ if (whole === '}}') return '}'
36
+ named++
37
+ const value = (hash ? read.word(name as string) : read.property(name as string))?.trim() ?? ''
38
+ if (value) filled++
39
+ return value
40
+ })
41
+ if (named > 0 && filled === 0) return undefined
42
+ const trimmed = out.replace(/\s+/g, ' ').trim()
43
+ return trimmed || undefined
44
+ }