simple-content-site 2.0.1 → 2.1.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.
|
@@ -1,26 +1,66 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
|
|
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?:
|
|
9
|
+
replacements?: ReplaceItem[]
|
|
7
10
|
}>(), {
|
|
8
11
|
parts: () => ['Copyright ©', '{fullYear}'],
|
|
9
|
-
|
|
10
12
|
})
|
|
11
13
|
|
|
12
|
-
const
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
}))
|
|
62
|
+
const joined = [...toValue(rawItems)].join(' ')
|
|
63
|
+
return replaceParts(joined)
|
|
24
64
|
})
|
|
25
65
|
</script>
|
|
26
66
|
|