datocms-html-to-structured-text
Version:
Convert HTML (or a `hast` syntax tree) to a valid DatoCMS Structured Text `dast` document
32 lines • 1.08 kB
JavaScript
import visitChildren from './visit-children.js';
// visitNode() is for visiting a single node
const visitNode = async function visitNode(createNode, node, context) {
const handlers = context.handlers;
let handler;
if (node.type === 'element') {
if (typeof node.tagName === 'string' &&
typeof handlers[node.tagName] === 'function') {
handler = handlers[node.tagName];
}
else {
handler = unknownHandler;
}
}
else if (node.type === 'root') {
handler = handlers.root;
}
else if (node.type === 'text') {
handler = handlers.text;
}
if (typeof handler !== 'function') {
return undefined;
}
return await handler(createNode, node, context);
};
export default visitNode;
// This is a default handler for unknown nodes.
// It skips the current node and processes its children.
const unknownHandler = async function unknownHandler(createNode, node, context) {
return visitChildren(createNode, node, context);
};
//# sourceMappingURL=visit-node.js.map