-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
159 lines (137 loc) · 3.54 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// You can delete this file if you're not using it
const path = require("path");
async function getFileNames(graphql) {
const files = await graphql(`
query {
allFile(filter: { relativeDirectory: { glob: "photos/**" } }) {
edges {
node {
relativeDirectory
relativePath
publicURL
}
}
}
}
`).then(result => {
if (result.errors) {
throw result.errors;
}
return result;
});
return files.data.allFile.edges;
}
async function getAlbums(graphql) {
const directories = await graphql(`
query {
allFile(filter: { relativeDirectory: { glob: "photos/*" } }) {
edges {
node {
relativeDirectory
}
}
}
}
`).then(result => {
if (result.errors) {
throw result.errors;
}
return result.data.allFile.edges.map(f => {
const filename = f.node.relativeDirectory.replace("photos/", "");
if (filename.indexOf(" ") !== -1) {
throw Error(`Do not use spaces in album directories: "${filename}"`);
}
return filename;
});
});
return Array.from(new Set(directories));
}
async function getMiniatures(graphql, paths, albums) {
const miniatures = {};
albums.forEach(a => (miniatures[a] = []));
paths.forEach(async path => {
const album = path.split("/", 2)[1];
const miniature = await graphql(
`
query SmallImage($path: String) {
file(relativePath: { eq: $path }) {
childImageSharp {
fluid(maxWidth: 200, quality: 75) {
src
}
}
}
}
`,
{ path }
).then(result => {
if (result.errors) {
throw result.errors;
}
miniatures[album].push(result.data.file.childImageSharp.fluid.src);
});
});
return miniatures;
}
async function getFullSizedImages(graphql, paths, albums) {
const fullSized = {};
albums.forEach(a => (fullSized[a] = []));
paths.forEach(async path => {
const album = path.split("/", 2)[1];
const miniature = await graphql(
`
query SmallImage($path: String) {
file(relativePath: { eq: $path }) {
childImageSharp {
fluid(maxWidth: 2048, quality: 100) {
src
}
}
}
}
`,
{ path }
).then(result => {
if (result.errors) {
throw result.errors;
}
fullSized[album].push(result.data.file.childImageSharp.fluid.src);
});
});
return fullSized;
}
async function createPages({ graphql, actions }) {
const ListTemplate = path.resolve("./src/templates/list.js");
const AlbumTemplate = path.resolve("./src/templates/album.js");
// get all files
// { publicUrl, relativeDirectory }
const files = await getFileNames(graphql);
const paths = files.map(f => f.node.relativePath);
const albums = await getAlbums(graphql);
const miniatures = await getMiniatures(graphql, paths, albums);
const fullSized = await getFullSizedImages(graphql, paths, albums);
actions.createPage({
path: `/`,
component: ListTemplate,
context: {
albums: albums,
allPhotos: miniatures,
},
});
albums.forEach(album => {
actions.createPage({
path: album,
component: AlbumTemplate,
context: {
name: album,
photos: fullSized[album],
},
});
});
}
exports.createPages = createPages;