Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-transformer-remark] Recursively add _PARENT to all sub-objects in frontmatter #2466

Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Recursively add _PARENT to all sub-objects in frontmatter
  • Loading branch information
Seavenly committed Oct 15, 2017
commit b1bf0ae0c8e20f139bb2b68a67fd31acab242d4b
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