mirror of
https://github.com/LukeHagar/unicorn-utterances.git
synced 2025-12-09 21:07:49 +00:00
Added programmatic user author detection
This commit is contained in:
@@ -1,9 +1,52 @@
|
||||
const path = require(`path`)
|
||||
const fs = require('fs')
|
||||
const fs = require("fs")
|
||||
const { createFilePath } = require(`gatsby-source-filesystem`)
|
||||
|
||||
exports.onCreateNode = ({ node, actions, getNode }) => {
|
||||
const { createNodeField } = actions
|
||||
|
||||
if (node.internal.type === `MarkdownRemark`) {
|
||||
const value = createFilePath({
|
||||
node,
|
||||
getNode,
|
||||
})
|
||||
createNodeField({
|
||||
name: `slug`,
|
||||
node,
|
||||
value,
|
||||
})
|
||||
}
|
||||
|
||||
if (node.internal.type === `UsersJson`) {
|
||||
const value = createFilePath({
|
||||
node,
|
||||
getNode,
|
||||
})
|
||||
createNodeField({
|
||||
name: `slug`,
|
||||
node,
|
||||
value,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
exports.sourceNodes = ({ getNodesByType, actions: { createNodeField } }) => {
|
||||
const postNodes = getNodesByType(`MarkdownRemark`)
|
||||
const userNodes = getNodesByType(`UsersJson`)
|
||||
|
||||
userNodes.forEach(userNode => {
|
||||
const isAuthor = postNodes.some(post => post.frontmatter.author === userNode.id)
|
||||
|
||||
createNodeField({
|
||||
name: `isAuthor`,
|
||||
node: userNode,
|
||||
value: isAuthor,
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exports.createPages = ({ graphql, actions }) => {
|
||||
const { createPage } = actions
|
||||
const { createPage, createNodeField } = actions
|
||||
|
||||
const blogPost = path.resolve(`./src/templates/blog-post.js`)
|
||||
const blogAuthor = path.resolve(`./src/templates/blog-author.js`)
|
||||
@@ -22,6 +65,9 @@ exports.createPages = ({ graphql, actions }) => {
|
||||
}
|
||||
frontmatter {
|
||||
title
|
||||
author {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,7 +80,7 @@ exports.createPages = ({ graphql, actions }) => {
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
`,
|
||||
).then(result => {
|
||||
if (result.errors) {
|
||||
throw result.errors
|
||||
@@ -50,16 +96,16 @@ exports.createPages = ({ graphql, actions }) => {
|
||||
|
||||
const postInfo = post.node.frontmatter
|
||||
if (postInfo.attached && postInfo.attached.length > 0) {
|
||||
postInfo.attached.forEach(({file: fileStr}) => {
|
||||
postInfo.attached.forEach(({ file: fileStr }) => {
|
||||
const postPath = post.node.fields.slug
|
||||
const relFilePath = path.join(__dirname, 'static', 'posts', postPath, fileStr)
|
||||
const relFilePath = path.join(__dirname, "static", "posts", postPath, fileStr)
|
||||
const fileExists = fs.existsSync(path.resolve(relFilePath))
|
||||
if (!fileExists) {
|
||||
console.error(`Could not find file to attach in the static folder: ${postPath}${fileStr}`)
|
||||
console.error(`To fix this problem, attach the file to the static folder's expected path above, or remove it from the post frontmatter definition`)
|
||||
process.exit(1)
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
createPage({
|
||||
@@ -78,7 +124,7 @@ exports.createPages = ({ graphql, actions }) => {
|
||||
path: `authors/${author.node.id}`,
|
||||
component: blogAuthor,
|
||||
context: {
|
||||
slug: author.node.id
|
||||
slug: author.node.id,
|
||||
},
|
||||
})
|
||||
})
|
||||
@@ -86,16 +132,3 @@ exports.createPages = ({ graphql, actions }) => {
|
||||
return null
|
||||
})
|
||||
}
|
||||
|
||||
exports.onCreateNode = ({ node, actions, getNode }) => {
|
||||
const { createNodeField } = actions
|
||||
|
||||
if (node.internal.type === `MarkdownRemark`) {
|
||||
const value = createFilePath({ node, getNode })
|
||||
createNodeField({
|
||||
name: `slug`,
|
||||
node,
|
||||
value,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user