Skip to content

Instantly share code, notes, and snippets.

@AndreasJacobsen
Last active July 2, 2020 11:32
Show Gist options
  • Save AndreasJacobsen/371faf073a1337b6879e4fd6b860b26f to your computer and use it in GitHub Desktop.
Save AndreasJacobsen/371faf073a1337b6879e4fd6b860b26f to your computer and use it in GitHub Desktop.
// Create pages for docs
exports.createPages = ({ actions, graphql }) => {
const path = require(`path`);
const { createPage } = actions;
const projects = path.resolve("src/templates/projects.js");
const defaultPage = path.resolve("src/templates/defaultPage.js");
const projectsSubPages = path.resolve("src/templates/projectsSubPages.js");
return graphql(`
{
allSanityDefaultPage {
edges {
node {
slug
}
}
}
allSanityProjects {
edges {
node {
slug
childPages {
slug
childPages {
slug
childPages {
slug
childPages {
slug
}
}
}
}
}
}
}
}
`).then((result) => {
if (result.errors) {
reporter.panic("failed to create pages ", result.errors);
}
result.data.allSanityDefaultPage.edges.forEach(({ node }) => {
createPage({
path: node.slug,
component: defaultPage,
context: {
slug: node.slug,
},
});
});
result.data.allSanityProjects.edges.forEach(({ node }) => {
createPage({
path: node.slug,
component: projects,
context: {
slug: node.slug,
},
});
});
result.data.allSanityProjects.edges.forEach(({ node }) => {
console.log(node);
if (node && node.childPages.length > 0) {
node.childPages.map((childPage) => {
createPage({
path: node.slug + "/" + childPage.slug,
component: projectsSubPages,
context: {
slug: childPage.slug,
parentSlug: node.slug,
},
});
// TODO: Can be optimized by moving this snippet into the node.childPages.map above, so that it does not run twice
if (childPage.childPages.length > 0) {
node.childPages.map((childPage) => {
childPage.childPages.map((subChildPage) => {
createPage({
path: node.slug + "/" + subChildPage.slug,
component: projectsSubPages,
context: {
slug: subChildPage.slug,
parentSlug: node.slug,
},
});
if (subChildPage.childPages.length > 0) {
createPage({
path: node.slug + "/" + subChildPage.childPages.slug,
component: projectsSubPages,
context: {
slug: subChildPage.childPages.slug,
parentSlug: node.slug,
},
});
}
});
});
}
});
}
});
});
};
{
"data": {
"allSanityProjects": {
"edges": [
{
"node": {
"childPages": [],
"_key": null
}
},
{
"node": {
"childPages": [
{
"_rawBlockContent": [
{
"_key": "198132f6c5fc",
"_type": "block",
"children": [
{
"_key": "198132f6c5fc0",
"_type": "span",
"marks": [],
"text": "Project 1 level 1, number 1"
}
],
"markDefs": [],
"style": "normal"
}
],
"title": "Melk",
"slug": "melk",
"childPages": [
{
"slug": "akjdslfkajsdf",
"title": "camambert",
"childPages": [
{
"title": "corn flakes",
"slug": "faasdfasdf",
"childPages": [],
"_key": "bdaa7339fdc0"
}
],
"_key": "f98b0182d3e3"
},
{
"slug": "briouch",
"title": "briouch",
"childPages": [],
"_key": "ddd596843b79"
}
],
"_key": "314959ade166"
},
{
"_rawBlockContent": [
{
"_key": "5a5d97c7fdcf",
"_type": "block",
"children": [
{
"_key": "5a5d97c7fdcf0",
"_type": "span",
"marks": [],
"text": "asdfasdfasdfasdf"
}
],
"markDefs": [],
"style": "normal"
}
],
"title": "Brød",
"slug": "bread",
"childPages": [
{
"slug": "asdfasdf",
"title": "asdfasdf",
"childPages": [],
"_key": "22ec6cd560ad"
}
],
"_key": "36a6d3a14071"
},
{
"_rawBlockContent": null,
"title": "Sodastream Trademark ",
"slug": "sodastream",
"childPages": [],
"_key": "e7deb1870019"
}
],
"_key": null
}
},
{
"node": {
"childPages": [
{
"_rawBlockContent": null,
"title": "Lik",
"slug": "lik",
"childPages": [],
"_key": "3643126ff628"
},
{
"_rawBlockContent": null,
"title": "Fryser",
"slug": "fryser",
"childPages": [],
"_key": "85197ee7d8a2"
},
{
"_rawBlockContent": null,
"title": "Tesla model S",
"slug": "wannebe",
"childPages": [],
"_key": "4b6f03429a69"
}
],
"_key": null
}
}
]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment