Importing block content with images into Sanity and resolving issues.
5 replies
Last updated: Aug 9, 2023
H
Hey there everyone,
I'm curious if anyone can offer some guidance on importing block content that contains images. I have already used the
I have already been using the
I'm curious if anyone can offer some guidance on importing block content that contains images. I have already used the
@sanity/block-toolsto import the content, but would like all images to also be imported into Sanity as well.
I have already been using the
_sanityAssethelper elsewhere, but I'm not certain how to format the rules so that Sanity gets the structure correct. Some pointers would be very appreciated!
Jun 12, 2023, 10:17 PM
H
After some experimenting, this does the trick:
'rules': [ { deserialize(el, next, block) { if (el.tagName?.toLowerCase() === 'img') { return block({ _type: 'image', _sanityAsset: `image@${el.src}`, nodes: next(el.childNodes), }) } } } ]
Jun 12, 2023, 10:32 PM
J
user S
When you did this, did you load the images to Sanity beforehand?Jul 29, 2023, 8:25 PM
J
user S
actually I'm curious of your whole blockContent structure. Really struggling to get images to load.Jul 31, 2023, 5:34 PM
H
user S
The schema for the block content is something like this:{ type: 'array', name: 'content', title: 'Content', description: 'The content of the blog post.', of: [ { type: 'block', }, { type: 'image', fields: [ { type: 'string', name: 'alt', title: 'Alt', description: 'The alt text of the image', }, ], }, ], },
content[] { ..., _type == "image" => { "src": asset->url, alt, }, },
Note that the `_sanityAsset: `image@${el.src}`` line basically tells sanity to load whatever is at
el.srcas an image during the import.
Would be helpful to know if: you're still seeing issues, and if you are, where specifically you're seeing them. ie, importing them? querying them? Displaying them?
Aug 9, 2023, 12:50 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.