Skip to content

Commit

Permalink
added preview method
Browse files Browse the repository at this point in the history
  • Loading branch information
anemne committed Nov 6, 2024
1 parent dcabe0c commit a01547a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions studio/utils/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
PortableTextBlock,
isPortableTextSpan,
isPortableTextTextBlock,
} from "sanity";

// This help method convert rich text content to plaintext so it could be used as preview in sanity studio
// (inspired by https://www.sanity.io/docs/previewing-block-content)
export function richTextPreview(
richText: PortableTextBlock[],
): string | undefined {
const block = richText.find((block) => block._type === "block");
if (!isPortableTextTextBlock(block)) {
return undefined;
}
return block.children
.filter((child) => isPortableTextSpan(child))
.map((span) => span.text)
.join("");
}

0 comments on commit a01547a

Please sign in to comment.