From 43b963fc0182c304564d2f460029609efabcb8c5 Mon Sep 17 00:00:00 2001 From: Espen Hovlandsdal Date: Sat, 2 Nov 2024 11:01:19 -0700 Subject: [PATCH] fix: prevent double spaces in `toPlainText` when span follows non-span (#93) --- src/toPlainText.ts | 2 +- test/toPlainText.test.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/toPlainText.ts b/src/toPlainText.ts index 4fcc4a8..a8615ef 100644 --- a/src/toPlainText.ts +++ b/src/toPlainText.ts @@ -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 diff --git a/test/toPlainText.test.ts b/test/toPlainText.test.ts index 1678910..b2055ba 100644 --- a/test/toPlainText.test.ts +++ b/test/toPlainText.test.ts @@ -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.') +})