1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-11-23 02:10:01 +01:00
blog/gatsby-node.js

118 lines
2.5 KiB
JavaScript
Raw Normal View History

2018-11-18 23:52:57 +01:00
const { createMarkdownFields } = require('./gatsby/createMarkdownFields')
const { createExif } = require('./gatsby/createExif')
2018-10-14 02:25:24 +02:00
const {
generatePostPages,
generateTagPages,
generateRedirectPages
} = require('./gatsby/createPages')
2019-04-13 22:52:58 +02:00
const { generateJsonFeed } = require('./gatsby/feeds')
2018-09-06 22:28:28 +02:00
exports.onCreateNode = ({ node, actions, getNode, createNodeId }) => {
2018-09-13 00:46:40 +02:00
// Markdown files
2018-07-17 23:33:55 +02:00
if (node.internal.type === 'MarkdownRemark') {
createMarkdownFields(node, actions, getNode)
2018-09-13 00:46:40 +02:00
}
// Image files
if (node.internal.mediaType === 'image/jpeg') {
createExif(node, actions, createNodeId)
2018-09-12 21:03:35 +02:00
}
2018-09-09 21:20:19 +02:00
}
2019-01-04 18:51:03 +01:00
exports.createPages = async ({ graphql, actions }) => {
2018-09-24 23:50:48 +02:00
const { createPage, createRedirect } = actions
2018-07-17 23:33:55 +02:00
2019-01-04 18:51:03 +01:00
const result = await graphql(`
{
2019-11-11 01:00:26 +01:00
posts: allMarkdownRemark(sort: { order: DESC, fields: [fields___date] }) {
2019-01-04 18:51:03 +01:00
edges {
2019-11-11 01:00:26 +01:00
next {
fields {
slug
}
frontmatter {
title
}
}
2019-01-04 18:51:03 +01:00
node {
fields {
slug
}
frontmatter {
tags
2018-07-17 23:33:55 +02:00
}
}
2019-11-11 01:00:26 +01:00
previous {
fields {
slug
}
frontmatter {
title
}
}
2018-07-17 23:33:55 +02:00
}
2019-01-04 18:51:03 +01:00
}
2019-11-06 20:44:33 +01:00
tags: allMarkdownRemark {
group(field: frontmatter___tags) {
tag: fieldValue
totalCount
}
}
2019-01-04 18:51:03 +01:00
}
`)
2018-07-17 23:33:55 +02:00
2019-01-04 18:51:03 +01:00
if (result.errors) throw result.errors
2018-07-19 00:22:06 +02:00
2019-11-06 20:44:33 +01:00
const posts = result.data.posts.edges
const tags = result.data.tags.group
2018-07-19 23:04:41 +02:00
2019-01-04 18:51:03 +01:00
// Generate posts & posts index
2019-11-06 20:44:33 +01:00
generatePostPages(createPage, posts)
2018-07-19 23:04:41 +02:00
2019-04-13 22:52:58 +02:00
// Generate tag pages
2019-11-06 20:44:33 +01:00
generateTagPages(createPage, tags)
2018-09-24 23:50:48 +02:00
2019-04-13 22:52:58 +02:00
// Create manual redirects
2019-01-04 18:51:03 +01:00
generateRedirectPages(createRedirect)
2018-07-17 23:33:55 +02:00
}
2019-01-08 21:59:55 +01:00
2019-04-13 22:52:58 +02:00
exports.onPostBuild = async ({ graphql }) => {
// JSON Feed query
const result = await graphql(`
{
2019-11-11 01:00:26 +01:00
allMarkdownRemark {
2019-04-13 22:52:58 +02:00
edges {
node {
html
fields {
slug
date
}
excerpt
frontmatter {
title
tags
updated
image {
childImageSharp {
resize(width: 940, quality: 85) {
src
}
}
}
}
}
}
}
}
`)
if (result.errors) throw result.errors
// Generate json feed
await generateJsonFeed(result.data.allMarkdownRemark.edges)
return Promise.resolve()
}