var flattenProps = function (props) { return traverse(props).reduce(function (acc) { // If its an array, it is not empty, and it has only one item. if (Array.isArray(this.node) && this.node.length === 1 && this.level < 3 && !this.node[0].properties && this.parent && this.parent.node) { acc[this.key] = ( (typeof this.node[0] === "string") ? xss(this.node[0].replace(/(<([^>]+)>)|\n|\t/ig,"")) : this.node[0] ); } if (this.level === 3) { var node = _.get(this, 'parent.parent.node'); if (node && node[0] && node[0].type && node[0].type[1]) acc[node.key] = '__ref__' + node[0].type[1]; } return acc; }, {}); }
traverse(mdata).reduce(function (acc) { if (this.key === 'type' && this.notLeaf) { var props = this.parent.node['properties'], size = props.length ? props.length : Object.keys(props).length || 0; if (size) { acc.push({ id: this.node[1], type: this.node[0], props: flattenProps(props), path: this.path, length: size, level: this.level }); } } return acc; }, [])
if (this.key === "type" && this.notLeaf) { index++; this.node.push(index); if (this.key === "content" && this.notLeaf) { if ( this.node[0] && this.node[0].html && typeof this.node[0].html === "string" ) { this.node[0] = xss(this.node[0].html).replace(/\n|\t/g, ""); } else { if ( this.node[0] && this.node[0].value && typeof this.node[0].value === "string" ) { this.node[0] = xss(this.node[0].value).replace(/\n|\t/g, ""); Array.isArray(this.node) && this.node.length === 1 && this.level === 1 && !this.node[0].properties && this.parent && this.parent.node ) { acc[this.key] = typeof this.node[0] === "string" ? xss(this.node[0].replace(/(<([^>]+)>)|\n|\t/gi, ""))