You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
664 B
JavaScript
29 lines
664 B
JavaScript
'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, []);
|
|
}
|