Skip to content

@sanity/block-tools<span>s with only whitespace are removed #6848

@silvertech-daniel

Description

@silvertech-daniel

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/block-tools@3.42.1')
const { Schema } = await import ('https://esm.sh/@sanity/schema@3.42.1')
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”

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions