-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgatsby-node.js
More file actions
81 lines (73 loc) · 2.06 KB
/
gatsby-node.js
File metadata and controls
81 lines (73 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
const sharp = require('sharp')
sharp.simd(false)
sharp.cache(false)
const path = require(`path`)
const fs = require(`fs`)
const ExifReader = require(`exifreader`)
const pageTemplate = path.resolve(`./src/templates/page.js`)
const postTemplate = path.resolve(`./src/templates/post.js`)
const query = `
{
pages: allMarkdownRemark(
filter: { frontmatter: { purpose: { eq: "page" } } }
) {
edges {
node {
frontmatter {
slug
}
}
}
}
posts: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/posts/" } }
sort: { fields: frontmatter___date, order: DESC }
) {
edges {
node {
frontmatter {
slug
}
}
}
}
}
`
exports.createPages = async ({ graphql, actions: { createPage } }) => {
const response = await graphql(query)
if (response.errors) throw new Error(response.errors)
const { pages, posts } = response.data
pages.edges.forEach(({ node }) => {
const { slug } = node.frontmatter
createPage({
path: slug,
component: pageTemplate,
context: { slug },
})
})
posts.edges.forEach(({ node }, index, arr) => {
const nextSlug = index === 0 ? `` : arr[index - 1].node.frontmatter.slug
const prevSlug =
index === arr.length - 1 ? `` : arr[index + 1].node.frontmatter.slug
const slug = node.frontmatter.slug
if (!slug.startsWith(`/`))
throw Error(`Post slugs must start with a forward slash!`)
createPage({
path: `/blog` + slug,
component: postTemplate,
context: { slug, nextSlug, prevSlug },
})
})
}
exports.onCreateNode = ({ node, actions }) => {
if (node.dir && node.dir.includes(`content/photos`) && node.ext === `.jpg`) {
const buffer = fs.readFileSync(node.absolutePath)
const tags = ExifReader.load(buffer)
const meta = {
//lat: tags.GPSLatitude.description,
//lng: tags.GPSLongitude.description,
//caption: tags.Headline.description,
}
actions.createNodeField({ node, name: `meta`, value: meta })
}
}