'use strict'; module.exports = function splitTextComponents(treeList) { function flatten(treeList, tagStack) { return treeList.reduce((all, item) => { if (item.type === "text") { let splitItems = item.text.split("\n").reduce((items, line) => { return items.concat([{ type: "text", text: line, tags: tagStack }, { type: "newline" }]); }, []); /* Remove the last newline, because it's not delimiting anything. */ splitItems.pop(); return all.concat(splitItems); } else { return all.concat(flatten(item.contents, tagStack.concat([item]))); } }, []); } return flatten(treeList, []); }