Created
June 22, 2017 10:54
-
-
Save lukem512/384113412193cb2941794f844dce35ef to your computer and use it in GitHub Desktop.
Combine text elements in AST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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