sanity-io / block-content-to-html

Deprecated in favor of @portabletext/to-html
MIT License
20 stars 2 forks source link

Import in an ESM environment returns a require error #8

Closed flayks closed 3 years ago

flayks commented 3 years ago

I'm trying to use this plugin to convert my HTML blocks from Sanity to my SvelteKit project which is based on Vite and ESM, and since it doesn't support requires, I get this error:

require is not defined

ReferenceError: require is not defined
    at ../src/blocksToHtml.js:1:36

My (super simple) implementation:

<script>
    import blocksToHtml from '@sanity/block-content-to-html'
</script>

<div class="text">
    {blocksToHtml({ blocks: content.text })}
</div>
stordahl commented 3 years ago

I dont think this will fix your issue, but you want to be passing the 'content' array to blocksToHtml.blocks and use Svelte's {@html } expression like so...

<script>
    import blocksToHtml from '@sanity/block-content-to-html'
</script>

<div class="text">
    {@html blocksToHtml({ blocks: content })}
</div>
flayks commented 3 years ago

I dont think this will fix your issue, but you want to be passing the 'content' array to blocksToHtml.blocks and use Svelte's {@html } expression like so...

<script>
    import blocksToHtml from '@sanity/block-content-to-html'
</script>

<div class="text">
    {@html blocksToHtml({ blocks: content })}
</div>

Indeed, I'm not really sure that will fix anything for now as the problem is more when importing the library. The result content type from Sanity right now is an object if I'm correct so for now it just returns [object Object] as it is not interpreted yet. But if that were to work, yes I'd have to use {@html} :)

flayks commented 3 years ago

Fixed with v2.0.0 apparently