Skip to content

Commit

Permalink
Recursively add _PARENT to all sub-objects in frontmatter (#2466)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seavenly authored and KyleAMathews committed Oct 15, 2017
1 parent dfa4efb commit 35a6399
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/gatsby-transformer-remark/src/on-node-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@ module.exports = async function onCreateNode({
},
}

// Add _PARENT to sub-object in the frontmatter so we can
// Add _PARENT recursively to sub-objects in the frontmatter so we can
// use this to find the root markdown node when running GraphQL
// queries. Yes this is lame. But it's because in GraphQL child nodes
// can't access their parent nodes so we use this _PARENT convention
// to get around this.
_.each(data.data, (v, k) => {
if (_.isArray(v) && _.isObject(v[0])) {
data.data[k] = v.map(o => {
return { ...o, _PARENT: node.id }
})
}
})
const addParentToSubObjects = data => {
_.each(data, (v, k) => {
if (_.isArray(v) && _.isObject(v[0])) {
_.each(v, o => addParentToSubObjects(o))
} else if (_.isObject(v)) {
addParentToSubObjects(v)
}
})
data._PARENT = node.id
}
addParentToSubObjects(data.data)

markdownNode.frontmatter = {
title: ``, // always include a title
Expand Down

0 comments on commit 35a6399

Please sign in to comment.