-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
127 lines (116 loc) · 3.39 KB
/
gatsby-node.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const path = require(`path`)
const fs = require(`fs`).promises
const ExifReader = require(`exifreader`)
const childProcess = require(`child_process`)
const svgToMiniDataURI = require(`mini-svg-data-uri`)
const SVGO = require(`svgo`)
const pageTemplate = path.resolve(`./src/templates/page.js`)
const postTemplate = path.resolve(`./src/templates/post.js`)
const query = `
{
pages: allMdx(
filter: { fileAbsolutePath: { regex: "/pages/" } }
) {
nodes {
frontmatter {
slug
}
}
}
posts: allMdx(
filter: { fileAbsolutePath: { regex: "/posts/" } }
sort: { fields: frontmatter___date, order: DESC }
) {
nodes {
frontmatter {
slug
}
}
}
}
`
exports.createPages = async ({ graphql, actions }) => {
const response = await graphql(query)
if (response.errors) throw new Error(response.errors)
const { pages, posts } = response.data
pages.nodes.forEach(page => {
const { slug } = page.frontmatter
if (slug) {
actions.createPage({
path: slug,
component: pageTemplate,
context: { slug },
})
}
})
posts.nodes.forEach((post, index, arr) => {
const nextSlug = arr[index - 1]?.frontmatter.slug || ``
const prevSlug = arr[index + 1]?.frontmatter.slug || ``
const { slug } = post.frontmatter
actions.createPage({
path: slug,
component: postTemplate,
context: { slug, nextSlug, prevSlug },
})
})
}
exports.onCreateNode = async ({ node, actions }) => {
if (node.dir && node.dir.includes(`content/photos`) && node.ext === `.jpg`) {
const buffer = await fs.readFile(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 })
}
if (
node.internal.type === `Mdx` &&
node.fileAbsolutePath.includes(`content/posts`)
) {
node.frontmatter.slug = `/blog` + node.frontmatter.slug
}
}
// Enable absolute imports from `src`.
// See https://gatsbyjs.org/docs/add-custom-webpack-config#absolute-imports.
exports.onCreateWebpackConfig = ({ actions }) => {
actions.setWebpackConfig({
resolve: {
modules: [path.resolve(__dirname, `src`), `node_modules`],
},
})
}
// Works on macOS only. Should do nothing on other platforms.
const notify = (title, text) =>
`osascript -e 'display notification "${text}" ` +
`with title "${title}" sound name "default"'`
exports.onCreateDevServer = () => {
const cmd = notify(`Done!`, `gatsby developed finished`)
childProcess.exec(cmd)
}
exports.onPostBuild = () => {
const cmd = notify(`Done!`, `gatsby build finished`)
childProcess.exec(cmd)
}
// https://github.com/gatsbyjs/gatsby/issues/25193
exports.createResolvers = ({ createResolvers }) => {
const svgo = new SVGO()
const resolvers = {
File: {
dataURI: {
type: `String`,
// full resolve args: parent, args, context, info
async resolve(parent) {
if (parent.extension === `svg` && parent.size < 15000) {
const svg = await fs.readFile(parent.absolutePath, `utf8`)
const { data } = await svgo.optimize(svg)
return svgToMiniDataURI(data)
}
return null
},
},
},
}
createResolvers(resolvers)
}