@portabletext/block-tools
Version:
Sanity-flavored HTML to Portable Text conversion (wraps @portabletext/html)
93 lines • 1.86 kB
TypeScript
import { PortableTextObject, PortableTextSpan, Schema } from "@portabletext/schema";
/**
* @public
*/
interface TypedObject {
_type: string;
_key?: string;
}
/**
* @public
*/
interface ArbitraryTypedObject extends TypedObject {
[key: string]: unknown;
}
/**
* @public
*/
interface DeserializerRule {
deserialize: (el: Node, next: (elements: Node | Node[] | NodeList) => TypedObject | TypedObject[] | undefined, createBlock: (props: ArbitraryTypedObject) => {
_type: string;
block: ArbitraryTypedObject;
}) => TypedObject | TypedObject[] | undefined;
}
/**
* An opinionated `DeserializerRule` that flattens tables in a way that repeats
* the header row for each cell in the row.
*
* @example
* ```html
* <table>
* <thead>
* <tr>
* <th>Header 1</th>
* <th>Header 2</th>
* </tr>
* </thead>
* <tbody>
* <tr>
* <td>Cell 1</td>
* <td>Cell 2</td>
* </tr>
* </tbody>
* </table>
* ```
* Turns into
* ```json
* [
* {
* _type: 'block',
* children: [
* {
* _type: 'text',
* text: 'Header 1'
* },
* {
* _type: 'text',
* text: 'Cell 1'
* }
* ]
* },
* {
* _type: 'block',
* children: [
* {
* _type: 'text',
* text: 'Header 2'
* },
* {
* _type: 'text',
* text: 'Cell 2'
* }
* ]
* }
* ]
* ```
*
* Use the `separator` option to control if a child element should separate
* headers and cells.
*
* @beta
*/
declare function createFlattenTableRule({
schema,
separator
}: {
schema: Schema;
separator?: () => (Omit<PortableTextSpan, '_key'> & {
_key?: string;
}) | (Omit<PortableTextObject, '_key'> & {
_key?: string;
}) | undefined;
}): DeserializerRule;
export { createFlattenTableRule };