-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(richTextBlock): improve type check for rich text preview
- Loading branch information
Showing
4 changed files
with
65 additions
and
20 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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { | ||
PortableTextBlock, | ||
PortableTextObject, | ||
isPortableTextTextBlock, | ||
} from "sanity"; | ||
|
||
export function isRichText(value: unknown): value is PortableTextBlock[] { | ||
return ( | ||
Array.isArray(value) && value.every((item) => isPortableTextBlock(item)) | ||
); | ||
} | ||
|
||
export function isPortableTextBlock( | ||
value: unknown, | ||
): value is PortableTextBlock { | ||
return isPortableTextTextBlock(value) || isPortableTextObject(value); | ||
} | ||
|
||
export function isPortableTextObject( | ||
value: unknown, | ||
): value is PortableTextObject { | ||
return isSanityKeyTypeObject(value); | ||
} | ||
|
||
function isSanityKeyTypeObject(value: unknown): value is { | ||
_key: string; | ||
_type: string; | ||
} { | ||
return ( | ||
typeof value === "object" && | ||
value !== null && | ||
"_key" in value && | ||
typeof value._key === "string" && | ||
"_type" in value && | ||
typeof value._type === "string" | ||
); | ||
} |
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,27 +1,20 @@ | ||
import { PortableTextBlock } from "sanity"; | ||
import { | ||
PortableTextBlock, | ||
isPortableTextSpan, | ||
isPortableTextTextBlock, | ||
} from "sanity"; | ||
|
||
// Convert rich text content to plaintext preview string | ||
// (see https://www.sanity.io/docs/previewing-block-content) | ||
// (inspired by https://www.sanity.io/docs/previewing-block-content) | ||
export function richTextPreview( | ||
richText: PortableTextBlock[], | ||
): string | undefined { | ||
console.log(richText); | ||
const block = richText.find((block) => block._type === "block"); | ||
if ( | ||
block === undefined || | ||
!("children" in block) || | ||
!Array.isArray(block.children) | ||
) { | ||
if (!isPortableTextTextBlock(block)) { | ||
return undefined; | ||
} | ||
return block.children | ||
.filter( | ||
(child: unknown) => | ||
typeof child === "object" && | ||
child !== null && | ||
"_type" in child && | ||
child._type === "span", | ||
) | ||
.filter((child) => isPortableTextSpan(child)) | ||
.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