UNPKG

@prismicio/mock

Version:

Generate mock Prismic documents, fields, Slices, and models for development and testing environments

43 lines (35 loc) 988 B
import * as prismic from "@prismicio/client" import { sentenceCase } from "../../lib/changeCase" import { createFaker } from "../../lib/createFaker" import type { MockRichTextValueConfig } from "../../types" const patterns = { short: { sentenceCount: 2, }, medium: { sentenceCount: 6, }, long: { sentenceCount: 12, }, } as const type MockRichTextParagraphValueConfig = { pattern?: keyof typeof patterns } & MockRichTextValueConfig export const paragraph = ( config: MockRichTextParagraphValueConfig, ): prismic.RTParagraphNode | undefined => { const faker = config.faker || createFaker(config.seed) const patternKey = config.pattern || faker.randomElement(Object.keys(patterns) as (keyof typeof patterns)[]) const pattern = patterns[patternKey] const text = Array.from( { length: pattern.sentenceCount }, () => sentenceCase(faker.words(faker.range(5, 15))) + ".", ).join(" ") return { type: prismic.RichTextNodeType.paragraph, text, spans: [], } }