@devite/nuxt-sanity 1.5.2 → 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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@devite/nuxt-sanity",
3
3
  "configKey": "@devite/nuxt-sanity",
4
- "version": "1.5.2",
4
+ "version": "1.5.3",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "unknown"
@@ -5,7 +5,15 @@
5
5
  ref="componentRef"
6
6
  v-bind="{ ...$props, ...$attrs }"
7
7
  >
8
- <slot />
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?: object }>()
25
+ const { data } = defineProps<{ data?: { _type?: string } | Array<{ _type: string }> }>()
18
26
 
19
- const component = computed<Component>(() => {
20
- const type = data?._type
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
- switch (type) {
23
- case 'linkInternal':
24
- return SanityLinkInternal
25
- case 'linkExternal':
26
- return SanityLinkExternal
27
- default:
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 resolveComponent('Sanity' + upperCamelCase)
44
+ return resolvedComponent ? (resolvedComponent as Component) : null
34
45
  }
46
+ }
35
47
  }
36
48
 
37
49
  return null
@@ -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 { data, placeholders = {} as object } = defineProps<{ data: RichText, placeholders?: object }>()
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: object[]) {
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) => placeholders[key] || match)
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
- h(SanityLinkExternal, { data: value }, slots.default),
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.2",
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"