Skip to content

Instantly share code, notes, and snippets.

@lukem512
Created June 22, 2017 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukem512/384113412193cb2941794f844dce35ef to your computer and use it in GitHub Desktop.
Save lukem512/384113412193cb2941794f844dce35ef to your computer and use it in GitHub Desktop.
Combine text elements in AST
let content = node.content;
if (Array.isArray(node.content)) {
// Combine elements of type text
content = node.content.reduce((_content, item) => {
const lastNodeIndex = _content.length - 1;
const lastNode = _content[_content.length - 1];
if (_content.length > 0 && lastNode.type === 'text' && item.type === 'text') {
_content[lastNodeIndex].content = lastNode.content + item.content;
} else {
_content.push(item);
}
return _content;
}, []);
// Unpack arrays with single items
if (content.length === 1) {
content = content[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment