Skip to content

Instantly share code, notes, and snippets.

@kalebheitzman
Last active January 14, 2021 06:07
Show Gist options
  • Save kalebheitzman/5a2186db31b453446c7be5375df5b8c8 to your computer and use it in GitHub Desktop.
Save kalebheitzman/5a2186db31b453446c7be5375df5b8c8 to your computer and use it in GitHub Desktop.
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/node-apis/
*/
// import libs
const path = require("path")
const chalk = require("chalk")
exports.onCreateNode = ({ node, getNodesByType, actions }) => {
if (node.internal.type !== `MarkdownRemark`) return
const { createNodeField } = actions
// get nodes
const nodes = getNodesByType("MarkdownRemark")
/**
* Process Tour Nodes
*/
if (node.frontmatter.templateKey === `tour`) {
console.log(
`${chalk.bgGreen(chalk.black(" Processing Tour "))} ${
node.frontmatter.title
}`
)
let routeIds = []
const aircraft = nodes.find(
aircraftNode =>
aircraftNode.frontmatter.templateKey === "aircraft" &&
aircraftNode.frontmatter.icao === node.frontmatter.aircraftIcao
)
node.frontmatter.flights.forEach(flight => {
const route = nodes.find(
routeNode => routeNode.frontmatter.routeCode === flight
)
// add tour information to flight
console.log(
` -> adding ${node.frontmatter.code} to ${route.frontmatter.routeCode}`
)
createNodeField({
node: route,
name: `tour`,
value: node,
})
routeIds.push(route.id)
})
// adding routes to tour
console.log(` -> adding routes to ${node.frontmatter.title}`)
createNodeField({
node: node,
name: `flights`,
value: routeIds,
})
// adding aircraft to tour
console.log(
` -> adding ${aircraft.frontmatter.icao} aircraft to ${node.frontmatter.title}`
)
createNodeField({
node: node,
name: `aircraft`,
value: aircraft.id,
})
}
/**
* Process Flight Nodes
*/
if (node.frontmatter.templateKey === `flight`) {
const {
frontmatter: {
title,
flightPlan: {
departureIcao,
arrivalIcao,
departureName,
arrivalName,
flightPlanJson,
},
},
} = node
console.log(
`${chalk.bgGreen(
chalk.black(" Processing Flight ")
)} ${title} (${departureIcao}-${arrivalIcao})`
)
// get the route and parse out info
const route = JSON.parse(flightPlanJson)
// add departure airport to flight
console.log(` -> adding ${departureIcao} departure airport`)
createNodeField({
node: node,
name: `departure`,
value: route.nodes.filter(item => {
return item.ident === departureIcao
})[0],
})
// add arrival airport to flight
console.log(` -> adding ${arrivalIcao} arrival airport`)
createNodeField({
node: node,
name: `arrival`,
value: route.nodes.filter(item => {
return item.ident === arrivalIcao
})[0],
})
// add parsed route to flight
console.log(` -> adding route`)
createNodeField({
node: node,
name: `route`,
value: route.nodes,
})
// get the aircraft node that matches
const aircraft = nodes.find(
node3 =>
node3.frontmatter.templateKey === "aircraft" &&
node3.frontmatter.icao === node.frontmatter.flightPlan.aircraftIcao
)
// add aircraft to route
console.log(` -> adding aircraft ${aircraft.frontmatter.icao} to route`)
createNodeField({
node: node,
name: `aircraft`,
value: aircraft.id,
})
// prettify the output
console.log("\n")
}
}
// create pages
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
return graphql(`
query {
flights: allMarkdownRemark(
filter: { frontmatter: { templateKey: { eq: "flight" } } }
) {
edges {
node {
id
frontmatter {
title
slug
routeCode
flightPlan {
flightPlan
aircraftIcao
arrivalIcao
departureIcao
flightPlanId
}
}
fields {
tour {
id
frontmatter {
code
}
}
}
}
}
}
tours: allMarkdownRemark(
filter: { frontmatter: { templateKey: { eq: "tour" } } }
) {
edges {
node {
id
frontmatter {
slug
code
}
}
}
}
}
`).then(result => {
// create flights
console.log(`${chalk.bgGreen(chalk.black(" Adding Flight Pages "))}`)
result.data.flights.edges.forEach(flight => {
// get vars to build url
const {
frontmatter: {
routeCode,
flightPlan: { flightPlan, arrivalIcao, departureIcao, aircraftIcao },
},
fields: { tour },
} = flight.node
let flightPlanFile = flightPlan ? flightPlan.split("assets/")[1] : null
if (tour) {
// build individual route
const flightTemplate = path.resolve(`src/templates/flight.tsx`)
const routeUrl = `/tours/${tour.frontmatter.code}/${routeCode}/${aircraftIcao}/${departureIcao}/${arrivalIcao}`.toLowerCase()
console.log(` -> create route ${routeUrl}`)
createPage({
path: routeUrl,
component: flightTemplate,
context: {
id: flight.node.id,
flightPlanFile: flightPlanFile,
tour: tour.id,
},
})
}
})
// create tours
console.log(`${chalk.bgGreen(chalk.black(" Adding Tour Pages "))}`)
result.data.tours.edges.forEach(tour => {
const tourTemplate = path.resolve(`src/templates/tour.tsx`)
const tourPath = `/tours/${tour.node.frontmatter.code}/${tour.node.frontmatter.slug}`.toLowerCase()
console.log(` -> create tour ${tourPath}`)
createPage({
path: tourPath,
component: tourTemplate,
context: {
id: tour.node.id,
},
})
})
})
}
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === "build-html") {
actions.setWebpackConfig({
module: {
rules: [
{
test: /react-leaflet|leaflet/,
use: loaders.null(),
},
],
},
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment