@devite/nuxt-sanity 1.5.1 → 1.5.3
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/dist/module.json
CHANGED
|
@@ -5,7 +5,15 @@
|
|
|
5
5
|
ref="componentRef"
|
|
6
6
|
v-bind="{ ...$props, ...$attrs }"
|
|
7
7
|
>
|
|
8
|
-
<
|
|
8
|
+
<template
|
|
9
|
+
v-for="(_, slotName) in $slots"
|
|
10
|
+
#[slotName]="slotProps"
|
|
11
|
+
>
|
|
12
|
+
<slot
|
|
13
|
+
:name="slotName"
|
|
14
|
+
v-bind="slotProps ?? {}"
|
|
15
|
+
/>
|
|
16
|
+
</template>
|
|
9
17
|
</component>
|
|
10
18
|
</template>
|
|
11
19
|
|
|
@@ -14,24 +22,28 @@ import type { Component } from '@nuxt/schema'
|
|
|
14
22
|
import { computed, ref, resolveComponent } from '#imports'
|
|
15
23
|
import { SanityLinkExternal, SanityLinkInternal, SanityRichText } from '#components'
|
|
16
24
|
|
|
17
|
-
const { data } = defineProps<{ data?:
|
|
25
|
+
const { data } = defineProps<{ data?: { _type?: string } | Array<{ _type: string }> }>()
|
|
18
26
|
|
|
19
|
-
const component = computed<Component>(() => {
|
|
20
|
-
|
|
27
|
+
const component = computed<Component | null>(() => {
|
|
28
|
+
if (Array.isArray(data) && data.every((item) => item._type === 'block')) {
|
|
29
|
+
return SanityRichText
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (data && typeof data === 'object' && '_type' in data) {
|
|
33
|
+
const type = data._type as string
|
|
21
34
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (data?.constructor.name === 'Array' && data.every((item) => item._type === 'block'))
|
|
29
|
-
return SanityRichText
|
|
30
|
-
else if (type) {
|
|
35
|
+
switch (type) {
|
|
36
|
+
case 'linkInternal':
|
|
37
|
+
return SanityLinkInternal
|
|
38
|
+
case 'linkExternal':
|
|
39
|
+
return SanityLinkExternal
|
|
40
|
+
default: {
|
|
31
41
|
const upperCamelCase = type.charAt(0).toUpperCase() + type.slice(1)
|
|
42
|
+
const resolvedComponent = resolveComponent('Sanity' + upperCamelCase)
|
|
32
43
|
|
|
33
|
-
return
|
|
44
|
+
return resolvedComponent ? (resolvedComponent as Component) : null
|
|
34
45
|
}
|
|
46
|
+
}
|
|
35
47
|
}
|
|
36
48
|
|
|
37
49
|
return null
|
|
@@ -14,8 +14,11 @@
|
|
|
14
14
|
</template>
|
|
15
15
|
|
|
16
16
|
<script setup lang="ts">
|
|
17
|
-
import type { ImageAsset } from '@sanity/types'
|
|
17
|
+
import type { ImageAsset, Reference } from '@sanity/types'
|
|
18
|
+
import { type Ref, resolveImageAssetById } from '#imports'
|
|
18
19
|
|
|
19
|
-
const { asset, loading = 'lazy' } = defineProps<{ asset?: ImageAsset, loading?: 'lazy' | 'eager' }>()
|
|
20
|
-
const imageAsset
|
|
20
|
+
const { asset, loading = 'lazy' } = defineProps<{ asset?: ImageAsset | Reference, loading?: 'lazy' | 'eager' }>()
|
|
21
|
+
const imageAsset: Ref<ImageAsset | undefined> | ImageAsset | undefined = asset?._ref
|
|
22
|
+
? await resolveImageAssetById((asset as Reference)._ref)
|
|
23
|
+
: asset as (ImageAsset | undefined)
|
|
21
24
|
</script>
|
|
@@ -9,8 +9,11 @@
|
|
|
9
9
|
|
|
10
10
|
<script setup lang="ts">
|
|
11
11
|
import type { LinkInternal } from '@devite/nuxt-sanity'
|
|
12
|
-
import type {
|
|
12
|
+
import type { Reference } from '@sanity/types'
|
|
13
|
+
import { type Ref, resolveInternalLink } from '#imports'
|
|
13
14
|
|
|
14
|
-
const { data } = defineProps<{ data: LinkInternal }>()
|
|
15
|
-
const resolvedLink: Ref<LinkInternal | undefined> |
|
|
15
|
+
const { data } = defineProps<{ data: LinkInternal | { reference: Reference } }>()
|
|
16
|
+
const resolvedLink: Ref<LinkInternal | undefined> | LinkInternal = 'reference' in data
|
|
17
|
+
? await resolveInternalLink(data.reference)
|
|
18
|
+
: data as LinkInternal
|
|
16
19
|
</script>
|
|
@@ -8,32 +8,26 @@
|
|
|
8
8
|
<script setup lang="ts">
|
|
9
9
|
import { PortableText, type PortableTextComponents } from '@portabletext/vue'
|
|
10
10
|
import type { RichText } from '@devite/nuxt-sanity'
|
|
11
|
+
import type { PortableTextChild, PortableTextSpan } from '@sanity/types'
|
|
11
12
|
import { computed, h } from '#imports'
|
|
12
13
|
import { SanityLinkExternal, SanityLinkInternal } from '#components'
|
|
13
14
|
|
|
14
|
-
const
|
|
15
|
+
const props = defineProps<{ data: RichText, placeholders?: Record<string, string> }>()
|
|
15
16
|
const currentData = computed(() => {
|
|
16
|
-
return data.map((block) => {
|
|
17
|
+
return props.data.map((block) => {
|
|
17
18
|
return {
|
|
18
19
|
...block,
|
|
19
|
-
children: replaceChildren(block.children),
|
|
20
|
+
children: replaceChildren(block.children as PortableTextChild[]),
|
|
20
21
|
}
|
|
21
22
|
})
|
|
22
23
|
})
|
|
23
24
|
|
|
24
|
-
function replaceChildren(children:
|
|
25
|
+
function replaceChildren(children: PortableTextChild[]): PortableTextChild[] {
|
|
25
26
|
return children.map((child) => {
|
|
26
27
|
if (child._type === 'span') {
|
|
27
28
|
return {
|
|
28
29
|
...child,
|
|
29
|
-
text: replacePlaceholders(child.text),
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (child.children) {
|
|
34
|
-
return {
|
|
35
|
-
...child,
|
|
36
|
-
children: replaceChildren(child.children),
|
|
30
|
+
text: replacePlaceholders((child as PortableTextSpan).text),
|
|
37
31
|
}
|
|
38
32
|
}
|
|
39
33
|
|
|
@@ -42,15 +36,17 @@ function replaceChildren(children: object[]) {
|
|
|
42
36
|
}
|
|
43
37
|
|
|
44
38
|
function replacePlaceholders(text: string) {
|
|
45
|
-
return text.replace(/\{\{(.*?)\}\}/g, (match, key) =>
|
|
39
|
+
return text.replace(/\{\{(.*?)\}\}/g, (match, key) => {
|
|
40
|
+
if (!props.placeholders || !Object.prototype.hasOwnProperty.call(props.placeholders, key)) return match
|
|
41
|
+
|
|
42
|
+
return props.placeholders[key]
|
|
43
|
+
})
|
|
46
44
|
}
|
|
47
45
|
|
|
48
46
|
const richTextSerializer: PortableTextComponents = {
|
|
49
47
|
marks: {
|
|
50
|
-
linkExternal: ({ value }, { slots }) =>
|
|
51
|
-
|
|
52
|
-
linkInternal: ({ value }, { slots }) =>
|
|
53
|
-
h(SanityLinkInternal, { data: value }, slots.default),
|
|
48
|
+
linkExternal: ({ value }, { slots }) => h(SanityLinkExternal, { data: value }, slots.default),
|
|
49
|
+
linkInternal: ({ value }, { slots }) => h(SanityLinkInternal, { data: value }, slots.default),
|
|
54
50
|
},
|
|
55
51
|
}
|
|
56
52
|
</script>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devite/nuxt-sanity",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.3",
|
|
4
4
|
"description": "Provides additional helper components and utilities for the Nuxt.js Sanity module",
|
|
5
5
|
"repository": "devite-io/nuxt-sanity",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"changelogen": "^0.5.7",
|
|
40
40
|
"eslint": "^9.12.0",
|
|
41
41
|
"nuxt": "^3.13.2",
|
|
42
|
+
"prettier": "^3.4.2",
|
|
42
43
|
"typescript": "^5.7.2",
|
|
43
44
|
"vitest": "^2.1.8",
|
|
44
45
|
"vue-tsc": "^2.1.10"
|