UNPKG

html-to-article-json

Version:
60 lines (53 loc) 1.51 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var rowHasContent = function rowHasContent(row) { return row.content && row.content.trim().length > 0; }; var rowHasSize = function rowHasSize(row) { return row.type === 'linebreak' || rowHasContent(row); }; var hasSize = function hasSize(inlineElements) { return inlineElements.some(rowHasSize); }; var flattenTree = function flattenTree(tree) { var result = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; var inlineElements = void 0; tree.forEach(function (child) { if (child.type === 'text' || child.type === 'linebreak') { if (inlineElements) { inlineElements.push(child); } else { inlineElements = [child]; } } else { if (inlineElements && hasSize(inlineElements)) { result.push({ type: 'paragraph', children: inlineElements }); inlineElements = null; } if (child.type === 'blockquote') { result.push({ type: 'blockquote', pullQuote: !!child.pullQuote, children: flattenTree(child.children) }); } else if (child.type === 'block') { flattenTree(child.children, result); } else { result.push(child); } } }); if (inlineElements && hasSize(inlineElements)) { result.push({ type: 'paragraph', children: inlineElements }); } return result; }; exports.default = flattenTree;