Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent double spaces in toPlainText when span follows non-span #93

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.')
})