Issue with handling `<img />` tags in `htmlToBlocks` function from `@sanity/block-tools`.

5 replies
Last updated: Nov 3, 2023
Hello all, I'm using
htmlToBlocks
from
@sanity/block-tools
and can't figure out how to handle
<img />
tags that are in the HTML.It's being used for a content migration tool and I have everything in place to download assets from the previous site, upload them into Sanity and refer to it from documents. But when I convert HTML to Portable Text using
htmlToBlock
it ignores
<img />
tags.Can I somehow add support for it or is it an option I have to enable somewhere?
Do I need to try something funky like replace
<img />
in the HTML with
<a href />
with a particular className and then fix it in the resulting JSON so it turns into an image?
Appreciate any input on this!
Oct 24, 2023, 5:47 PM
Have you created a
rule
for handling them? What does your code look like so far?
Oct 25, 2023, 4:31 PM
Now that you point it out, I see the documentation on
rule
!I'm digging into that. Thanks!
Oct 26, 2023, 7:32 AM
That was the missing link. Got it fixed now.Thank you so much for your reply!!
๐Ÿ™
Oct 26, 2023, 9:18 AM
Excellent!
Oct 27, 2023, 12:12 AM
Just leaving this link here because I didn't know what was being talked about and it's interesting! https://www.npmjs.com/package/@sanity/block-tools#rules
Nov 3, 2023, 3:51 PM

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.

Was this answer helpful?