`@sanity/block-tools` — `<span>`s with only whitespace are removed
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”
@jordanl17 don't edit without commenting why — it's now inaccurate, the original "a<span> </span>b" is correct
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.
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 perfect, makes sense
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.