Skip to content

Commit 43b963f

Browse files
authored
fix: prevent double spaces in toPlainText when span follows non-span (#93)
1 parent 203ae7c commit 43b963f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/toPlainText.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {ArbitraryTypedObject, PortableTextBlock} from '@portabletext/types'
33
import {isPortableTextBlock, isPortableTextSpan} from './asserters'
44

55
const leadingSpace = /^\s/
6-
const trailingSpace = /^\s/
6+
const trailingSpace = /\s$/
77

88
/**
99
* Takes a Portable Text block (or an array of them) and returns the text value

test/toPlainText.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,16 @@ test('toPlainText: does not add leading whitespace on span-hugging non-span', ()
134134
}),
135135
).toEqual('Now that is an image.')
136136
})
137+
138+
test('toPlainText: does not add leading whitespace on span-hugging non-span (trailing)', () => {
139+
expect(
140+
toPlainText({
141+
_type: 'block',
142+
children: [
143+
{_type: 'span', text: 'Now that is a '},
144+
{_type: 'image', src: '/some/image.png'},
145+
{_type: 'span', text: 'beautiful image.'},
146+
],
147+
}),
148+
).toEqual('Now that is a beautiful image.')
149+
})

0 commit comments

Comments
 (0)