UNPKG

datocms-contentful-to-structured-text

Version:

Convert Contentful Rich Text to a valid DatoCMS Structured Text `dast` document

28 lines (21 loc) 650 B
import { helpers } from '@contentful/rich-text-types'; import { Node } from 'datocms-structured-text-utils'; import { ContentfulNode, Context } from '../types'; import visitChildren from './visit-children'; const visitNode = async ( node: ContentfulNode | null, context: Context, ): Promise<Node | Array<Node> | void> => { if (!node) { return; } const matchingHandler = context.handlers.find((h) => h.guard(node)); if (matchingHandler) { return await matchingHandler.handle(node, context); } if (helpers.isText(node)) { return undefined; } return await visitChildren(node, context); }; export default visitNode;