/
index.ts
46 lines (43 loc) · 1.66 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type {ArraySchemaType, PortableTextTextBlock} from '@sanity/types'
import blockContentTypeFeatures from './util/blockContentTypeFeatures'
import HtmlDeserializer from './HtmlDeserializer'
import {normalizeBlock} from './util/normalizeBlock'
import {BlockContentFeatures, HtmlDeserializerOptions, TypedObject} from './types'
/**
* 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
*/
export function htmlToBlocks(
html: string,
blockContentType: ArraySchemaType,
options: HtmlDeserializerOptions = {}
): (TypedObject | PortableTextTextBlock)[] {
const deserializer = new HtmlDeserializer(blockContentType, options)
return deserializer.deserialize(html).map((block) => normalizeBlock(block))
}
/**
* 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
*/
export function getBlockContentFeatures(blockContentType: ArraySchemaType): BlockContentFeatures {
return blockContentTypeFeatures(blockContentType)
}
export {normalizeBlock}
export {randomKey} from './util/randomKey'
export type {TypedObject, HtmlDeserializerOptions, BlockContentFeatures}
export type {
ResolvedAnnotationType,
DeserializerRule,
HtmlParser,
ArbitraryTypedObject,
BlockEditorSchemaProps,
} from './types'
export type {BlockNormalizationOptions} from './util/normalizeBlock'