simple-content-site 2.0.1 → 2.1.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.
@@ -1,26 +1,66 @@
1
1
  <script setup lang="ts">
2
- import { defu } from 'defu'
2
+ type ReplaceKey = string | number | symbol | RegExp
3
+ type ReplaceFn = (matches: string[]) => string
4
+ type ReplaceValue = string | number | ReplaceFn
5
+ type ReplaceItem = { match: ReplaceKey, replace: ReplaceValue }
3
6
 
4
7
  const props = withDefaults(defineProps<{
5
8
  parts?: string[]
6
- replacements?: Record<string, string>
9
+ replacements?: ReplaceItem[]
7
10
  }>(), {
8
11
  parts: () => ['Copyright ©', '{fullYear}'],
9
-
10
12
  })
11
13
 
12
- const replaceParams = (text: string, params: Record<string, string>) => text.replace(/\{([^}]+)\}/g, (_, key) => params[key] || '')
14
+ const currentUrl = useRequestURL()
15
+
16
+ // todo: move this logic to a shared space
17
+ const allReplacements = computed<ReplaceItem[]>(() => [
18
+ {
19
+ match: '{fullYear}',
20
+ replace: () => String(new Date().getFullYear()),
21
+ },
22
+ {
23
+ match: '{url}',
24
+ replace: () => currentUrl.toString(),
25
+ },
26
+ ...(props.replacements ?? []),
27
+ ])
13
28
 
14
29
  const rawItems = computed(() => {
15
30
  if (!props.parts || props.parts.length === 0) return ['']
16
-
17
31
  return props.parts
18
32
  })
19
33
 
34
+ const normalizeMatch = (match: ReplaceKey): RegExp => {
35
+ if (match instanceof RegExp) return match
36
+ return new RegExp(String(match), 'g')
37
+ }
38
+
39
+ const applyReplace = (
40
+ text: string,
41
+ matcher: RegExp,
42
+ replacer: ReplaceValue,
43
+ ) => {
44
+ if (typeof replacer === 'function') {
45
+ return text.replace(matcher, (...args) => {
46
+ const matches = args.slice(0, -2)
47
+ return replacer(matches)
48
+ })
49
+ }
50
+
51
+ return text.replace(matcher, String(replacer))
52
+ }
53
+
54
+ const replaceParts = (text: string) => {
55
+ return allReplacements.value.reduce((acc, item) => {
56
+ const matcher = normalizeMatch(item.match)
57
+ return applyReplace(acc, matcher, item.replace)
58
+ }, text)
59
+ }
60
+
20
61
  const text = computed(() => {
21
- return replaceParams([...toValue(rawItems)].join(' '), defu(props.replacements || {}, {
22
- fullYear: new Date().getFullYear(),
23
- }))
62
+ const joined = [...toValue(rawItems)].join(' ')
63
+ return replaceParts(joined)
24
64
  })
25
65
  </script>
26
66
 
package/content.config.ts CHANGED
@@ -84,10 +84,10 @@ const createFooterSchema = () => z.object({
84
84
 
85
85
  let collections: Record<string, DefinedCollection>
86
86
 
87
- if (locales && Array.isArray(locales)) {
87
+ const buildI18nCollections = () => {
88
88
  collections = {}
89
89
  for (const locale of locales) {
90
- const code = typeof locale === 'string' ? locale : locale.code
90
+ const code = (typeof locale === 'string' ? locale : locale.code).replace('-', '_')
91
91
 
92
92
  collections[`landing_${code}`] = defineCollection({
93
93
  type: 'page',
@@ -133,7 +133,8 @@ if (locales && Array.isArray(locales)) {
133
133
  })
134
134
  }
135
135
  }
136
- else {
136
+
137
+ const buildDefaultCollections = () => {
137
138
  collections = {
138
139
  landing: defineCollection({
139
140
  type: 'page',
@@ -174,4 +175,14 @@ else {
174
175
  }
175
176
  }
176
177
 
178
+ if (locales && Array.isArray(locales)) {
179
+ if (locales.length === 0) {
180
+ console.warn('Site: There are 0 locales, building with defaults instead.')
181
+ }
182
+ buildI18nCollections()
183
+ }
184
+ else {
185
+ buildDefaultCollections()
186
+ }
187
+
177
188
  export default defineContentConfig({ collections })
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "simple-content-site",
3
3
  "description": "Nuxt layer for simple website with basic functions.",
4
- "version": "2.0.1",
4
+ "version": "2.1.1",
5
5
  "type": "module",
6
6
  "main": "./nuxt.config.ts",
7
7
  "repository": {
@@ -39,7 +39,7 @@
39
39
  "minimark": "^0.2.0",
40
40
  "motion-v": "^1.7.4",
41
41
  "nuxt-og-image": "^5.1.13",
42
- "nuxt-studio": "^1.0.0",
42
+ "nuxt-studio": "^1.1.0",
43
43
  "pkg-types": "^2.3.0",
44
44
  "scule": "^1.3.0",
45
45
  "tailwindcss": "^4.1.17",