datocms-contentful-to-structured-text
Version:
Convert Contentful Rich Text to a valid DatoCMS Structured Text `dast` document
27 lines (26 loc) • 1.85 kB
TypeScript
import { Node, Root, NodeType, Mark } from 'datocms-structured-text-utils';
import { Block as ContentfulBlock, Inline as ContentfulInline, Paragraph as ContentfulParagraph, Text as ContentfulTextNode, Document as ContentfulDocument, TopLevelBlock as ContentfulRootNode, Quote as ContentfulQuote, Hr as ContentfulHr, OrderedList as ContentfulOrderedList, UnorderedList as ContentfulUnorderedList, ListItem as ContentfulListItem, Hyperlink as ContentfulHyperLink, Heading1, Heading2, Heading3, Heading4, Heading5, Heading6 } from '@contentful/rich-text-types';
export type { Node, Root, NodeType, Mark };
export type { ContentfulInline, ContentfulTextNode, ContentfulRootNode, ContentfulDocument, ContentfulParagraph, ContentfulQuote, ContentfulHr, ContentfulListItem, ContentfulHyperLink, };
export interface Context {
/** The parent `dast` node type. */
parentNodeType: NodeType;
/** The parent Contentful node. */
parentNode: ContentfulNode | null;
/** A reference to the current handlers - merged default + user handlers. */
handlers: Handler[];
/** A reference to the default handlers record (map). */
defaultHandlers: Handler[];
/** Marks for span nodes. */
allowedMarks: Mark[];
/** */
allowedBlocks: NodeType[];
}
export interface Handler {
guard: (node: ContentfulNode) => boolean;
handle: (node: ContentfulNode, context: Context) => Promise<Node | Array<Node> | void>;
}
export declare type ContentfulNode = ContentfulDocument | ContentfulRootNode | ContentfulTextNode | ContentfulBlock | ContentfulInline;
export declare type ContentfulHeading = Heading1 | Heading2 | Heading3 | Heading4 | Heading5 | Heading6;
export declare type ContentfulNodeWithContent = ContentfulBlock | ContentfulInline;
export declare type ContentfulList = ContentfulOrderedList | ContentfulUnorderedList;