Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serializer to block to handle Images from sanity #400

Open
safejace opened this issue May 16, 2022 · 4 comments
Open

Add serializer to block to handle Images from sanity #400

safejace opened this issue May 16, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@safejace
Copy link
Contributor

Describe the solution you'd like to see

The SanityContent component should be able to render images from sanity

Describe alternatives you've considered

Currently i solved it by adding custom serializer

@safejace safejace added the enhancement New feature or request label May 16, 2022
@wojtekpiskorz
Copy link

@safejace would you mind explaining a bit how you were able to do so? I'm trying for SanityContent to work with images in body but I still have only an empty tag

@safejace
Copy link
Contributor Author

hmmm i don't see it in my code right now, but it would look something like this:

CmsContent.vue

<script setup lang="ts">

const serializers = {
  types: {
    myImage: resolveComponent('myImage'),
  },
}
</script>

<template>
  <div v-bind="$attrs" class="cms-content">
      <SanityContent :blocks="blocks" :serializers="serializers" />
  </div>
</template>

MyImage.vue

<script setup lang="ts">
const props = defineProps<{
  src: {
    asset: {
      _ref: string
    }
  }
  alt?: string
  width?: number
  height?: number
}>()
</script>

<template>
  <nuxt-img provider="sanity" :src="props.src.asset._ref" :alt="props.alt" />
</template>

@wojtekpiskorz
Copy link

@safejace Thanks for a quick response :). But the sanitizer like that doesn't work. I'm also not sure exactly how it works. so the sanitizer props should detect the root tag of the provided component and if it's an img it would replace all images inside conted with the provided component?

I do have

  sanity: {
    projectId: 'xxx'
  },
  image: {
    sanity: {
      projectId: 'xxx',
      // Defaults to 'production'
      // dataset: 'development'
    }
  },

In my nuxt.config.ts

@safejace
Copy link
Contributor Author

not sure if it was a typo... but you are saying sanitizer.... it's a serializer

from the module doc: https://sanity.nuxtjs.org/helpers/portable-text#example-with-custom-serializers

the [serializer] props should detect the root tag of the provided component and if it's an img it would replace all images inside conted with the provided component?

that sounds correct.

the <SanityContent> component doesn't know how to render the image. you need to pass in a component that does know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants