(
textContent: string | null | undefined,
{ annotations, link, preserveWhitespace }: RichTextOptions = {},
)
| 3 | import type { RichText, RichTextOptions, RichTextText } from '../notion-types'; |
| 4 | |
| 5 | export function buildRichText( |
| 6 | textContent: string | null | undefined, |
| 7 | { annotations, link, preserveWhitespace }: RichTextOptions = {}, |
| 8 | ): RichText { |
| 9 | if (!textContent?.length) return []; |
| 10 | |
| 11 | const text = preserveWhitespace |
| 12 | ? textContent |
| 13 | : collapseWhitespace(textContent); |
| 14 | |
| 15 | const hasAnnotations = Boolean( |
| 16 | annotations && Object.keys(annotations).length, |
| 17 | ); |
| 18 | |
| 19 | return chunkString(text, LIMITS.TEXT_CONTENT_CHARACTERS).map((content) => { |
| 20 | const richText: RichTextText = { text: { content } }; |
| 21 | if (hasAnnotations) richText.annotations = annotations; |
| 22 | if (link) richText.text.link = link; |
| 23 | return richText; |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | function collapseWhitespace(text: string): string { |
| 28 | return text.replace(/[\s\n]+/g, ' '); |
no test coverage detected