Skip to content

Commit

Permalink
fix: prevent double spaces in toPlainText when span follows non-span (
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored Nov 2, 2024
1 parent 203ae7c commit 43b963f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/toPlainText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {ArbitraryTypedObject, PortableTextBlock} from '@portabletext/types'
import {isPortableTextBlock, isPortableTextSpan} from './asserters'

const leadingSpace = /^\s/
const trailingSpace = /^\s/
const trailingSpace = /\s$/

/**
* Takes a Portable Text block (or an array of them) and returns the text value
Expand Down
13 changes: 13 additions & 0 deletions test/toPlainText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,16 @@ test('toPlainText: does not add leading whitespace on span-hugging non-span', ()
}),
).toEqual('Now that is an image.')
})

test('toPlainText: does not add leading whitespace on span-hugging non-span (trailing)', () => {
expect(
toPlainText({
_type: 'block',
children: [
{_type: 'span', text: 'Now that is a '},
{_type: 'image', src: '/some/image.png'},
{_type: 'span', text: 'beautiful image.'},
],
}),
).toEqual('Now that is a beautiful image.')
})

0 comments on commit 43b963f

Please sign in to comment.