UNPKG

@portabletext/block-tools

Version:

Can format HTML, Slate JSON or Sanity block array into any other format.

144 lines (143 loc) 4.28 kB
import { ArraySchemaType, I18nTitledListValue, ObjectSchemaType, PortableTextSpan, PortableTextTextBlock, SpanSchemaType, TitledListValue } from "@sanity/types"; import { ComponentType } from "react"; /** * @public */ interface BlockContentFeatures { styles: TitledListValue<string>[]; decorators: TitledListValue<string>[]; annotations: ResolvedAnnotationType[]; lists: I18nTitledListValue<string>[]; types: { block: ArraySchemaType; span: SpanSchemaType; inlineObjects: ObjectSchemaType[]; blockObjects: ObjectSchemaType[]; }; } /** * @beta */ interface BlockEditorSchemaProps { icon?: string | ComponentType; render?: ComponentType; } /** * @public */ interface ResolvedAnnotationType { blockEditor?: BlockEditorSchemaProps; title: string | undefined; value: string; type: ObjectSchemaType; icon: ComponentType | undefined; } /** * @public */ interface TypedObject { _type: string; _key?: string; } /** * @public */ interface ArbitraryTypedObject extends TypedObject { [key: string]: unknown; } /** * @public */ type HtmlParser = (html: string) => Document; /** * @public */ type WhiteSpacePasteMode = 'preserve' | 'remove' | 'normalize'; /** * @public */ interface HtmlDeserializerOptions { keyGenerator?: () => string; rules?: DeserializerRule[]; parseHtml?: HtmlParser; unstable_whitespaceOnPasteMode?: WhiteSpacePasteMode; } /** * @public */ /** * @public */ interface DeserializerRule { deserialize: (el: Node, next: (elements: Node | Node[] | NodeList) => TypedObject | TypedObject[] | undefined, createBlock: (props: ArbitraryTypedObject) => { _type: string; block: ArbitraryTypedObject; }) => TypedObject | TypedObject[] | undefined; } /** * @public */ /** * Block normalization options * * @public */ interface BlockNormalizationOptions { /** * Decorator names that are allowed within portable text blocks, eg `em`, `strong` */ allowedDecorators?: string[]; /** * Name of the portable text block type, if not `block` */ blockTypeName?: string; /** * Custom key generator function */ keyGenerator?: () => string; } /** * Normalizes a block by ensuring it has a `_key` property. If the block is a * portable text block, additional normalization is applied: * * - Ensures it has `children` and `markDefs` properties * - Ensures it has at least one child (adds an empty span if empty) * - Joins sibling spans that has the same marks * - Removes decorators that are not allowed according to the schema * - Removes marks that have no annotation definition * * @param node - The block to normalize * @param options - Options for normalization process. See {@link BlockNormalizationOptions} * @returns Normalized block * @public */ declare function normalizeBlock(node: TypedObject, options?: BlockNormalizationOptions): Omit<TypedObject | PortableTextTextBlock<TypedObject | PortableTextSpan>, '_key'> & { _key: string; }; /** * Generate a random key of the given length * * @param length - Length of string to generate * @returns A string of the given length * @public */ declare function randomKey(length: number): string; /** * Convert HTML to blocks respecting the block content type's schema * * @param html - The HTML to convert to blocks * @param blockContentType - A compiled version of the schema type for the block content * @param options - Options for deserializing HTML to blocks * @returns Array of blocks * @public */ declare function htmlToBlocks(html: string, blockContentType: ArraySchemaType, options?: HtmlDeserializerOptions): (TypedObject | PortableTextTextBlock)[]; /** * Normalize and extract features of an schema type containing a block type * * @param blockContentType - Schema type for the block type * @returns Returns the featureset of a compiled block content type. * @public */ declare function getBlockContentFeatures(blockContentType: ArraySchemaType): BlockContentFeatures; export { type ArbitraryTypedObject, type BlockContentFeatures, type BlockEditorSchemaProps, type BlockNormalizationOptions, type DeserializerRule, type HtmlDeserializerOptions, type HtmlParser, type ResolvedAnnotationType, type TypedObject, getBlockContentFeatures, htmlToBlocks, normalizeBlock, randomKey };