Skip to content

Instantly share code, notes, and snippets.

@destroytoday
Created November 23, 2019 21:25
Show Gist options
  • Save destroytoday/d7d2c2cf17dc3c271fc60c224b727443 to your computer and use it in GitHub Desktop.
Save destroytoday/d7d2c2cf17dc3c271fc60c224b727443 to your computer and use it in GitHub Desktop.
function findAnnotations (field:Block | Inline):Array<Block | Inline> {
const annotations:Array<Block | Inline> = []
// `field.content` returns Block | Inline | Text
// I’d like to ignore Text
// `reduce` is showing a TypeScript error "This expression is not callable" because Text isn’t included
// but if I add it, `node.content` errors because Text doesn’t have `.content`
return field.content.reduce((acc:Array<Block | Inline>, node:Block | Inline) => {
if (node.nodeType === INLINES.EMBEDDED_ENTRY && node.data.target.sys.contentType.sys.id === 'annotation') {
acc.push(node.data.target)
} else if (node.content && node.content.length > 0) {
acc.push(...this.findAnnotations(node))
}
return acc
}, annotations)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment