Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core/preview): use new types in portable-text previews
- Loading branch information
1 parent
b2aa1f2
commit 3fd22cc
Showing
2 changed files
with
8 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import {Block, isBlock, isSpan} from '@sanity/types' | ||
import {isPortableTextTextBlock, isPortableTextSpan, PortableTextBlock} from '@sanity/types' | ||
|
||
export function isPortableTextArray(blocks: unknown): blocks is Block[] { | ||
return Array.isArray(blocks) && (blocks.length === 0 || blocks.some(isBlock)) | ||
export function isPortableTextPreviewValue(value: unknown): value is PortableTextBlock[] { | ||
return Array.isArray(value) && (value.length === 0 || value.some(isPortableTextTextBlock)) | ||
} | ||
|
||
export function extractTextFromBlocks(blocks: Block[]): string { | ||
const firstBlock = blocks.find(isBlock) | ||
export function extractTextFromBlocks(blocks: unknown): string { | ||
const firstBlock = Array.isArray(blocks) && blocks.find(isPortableTextTextBlock) | ||
if (!firstBlock || !firstBlock.children) { | ||
return '' | ||
} | ||
|
||
return firstBlock.children | ||
.filter(isSpan) | ||
.filter(isPortableTextSpan) | ||
.map((span) => span.text) | ||
.join('') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters