sanity icon indicating copy to clipboard operation
sanity copied to clipboard

`@sanity/block-tools` — `<span>`s with only whitespace are removed

Open silvertech-daniel opened this issue 1 year ago • 1 comments

When I have a text block that contains spans with only whitespace inside of them, that whitespace does not get carried over into the portable text output of htmlToBlocks().

To Reproduce

In your browser console:

const { htmlToBlocks } = await import('https://esm.sh/@sanity/[email protected]')
const { Schema } = await import ('https://esm.sh/@sanity/[email protected]')
const blockType = Schema.compile({
  types: [
    {
      type: 'array',
      of: [{type: 'block'}]
    }
  ]
}).get()
htmlToBlocks("a b", blockType) // portable text “ab”

Expected behavior

The whitespace should be preserved. The workaround I'm using is:

htmlToBlocks("a<span> </span>b", blockType, {
    rules: [
        {
            deserialize: (e, next, block) => {
                if (e.localName === 'span' && e.textContent && !e.textContent.trim())
                    return { _type: 'span', marks: [], text: ' ' }
            }
        }
    ]
}) // portable text “a b”

silvertech-daniel avatar Jun 04 '24 17:06 silvertech-daniel

@jordanl17 don't edit without commenting why — it's now inaccurate, the original "a<span> </span>b" is correct

wlib avatar Jun 22 '24 19:06 wlib

I don't see why this is unplanned, it's a straightforward thing to fix that I hit several times. a<span> </span>b turning into ab instead of a b is clearly not desirable.

silvertech-daniel avatar Feb 21 '25 14:02 silvertech-daniel

Hi @silvertech-daniel . Sorry about that. block-tools is now published under @portabletext and has a new home over here: https://github.com/portabletext/editor/tree/main/packages/block-tools

I care a lot about the correctness of this tool, so we should definitely look into this case.

I'm on my phone right now, but managed to open a draft PR with a failing test case so I remember to look into this: https://github.com/portabletext/editor/pull/827

christianhg avatar Feb 22 '25 13:02 christianhg

@christianhg perfect, makes sense

silvertech-daniel avatar Feb 24 '25 13:02 silvertech-daniel

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

github-actions[bot] avatar May 26 '25 00:05 github-actions[bot]