-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-node.js
134 lines (127 loc) · 3.2 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require(`path`)
const _ = require(`lodash`)
function createLinkedPages(createPage, edges) {
const listTemplate = path.resolve(`src/templates/list.jsx`)
const tagPosts = {}
const destPosts = {}
edges.forEach(({ node }) => {
if (node.frontmatter.tags) {
node.frontmatter.tags.forEach(tag => {
if (!tagPosts[tag]) {
tagPosts[tag] = []
}
tagPosts[tag].push(node)
})
}
if (node.frontmatter.country) {
if (!destPosts[node.frontmatter.country]) {
destPosts[node.frontmatter.country] = []
}
destPosts[node.frontmatter.country].push(node)
}
})
Object.keys(destPosts).forEach(dest => {
createPage({
path: `/destination/${dest}`,
component: listTemplate,
context: {
posts: destPosts[dest],
title: dest,
type: `destination`,
},
})
})
Object.keys(tagPosts).forEach(tagName => {
createPage({
path: `/tag/${tagName.toLowerCase()}`,
component: listTemplate,
context: {
posts: tagPosts[tagName],
title: tagName,
type: `tag`,
},
})
})
return { tagPosts, destPosts }
}
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
const blogPostTemplate = path.resolve(`src/templates/blogTemplate.jsx`)
return graphql(`
{
allMarkdownRemark(
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
excerpt(pruneLength: 250)
html
id
timeToRead
frontmatter {
date
path
tags
title
country
type
featured
itinerary
km
duration
coordinates {
coordinates
country
}
cover {
childImageSharp {
fluid(maxHeight: 280, maxWidth: 320, quality: 100) {
base64
aspectRatio
src
srcSet
sizes
}
}
}
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
const { destPosts, tagPosts } = createLinkedPages(
createPage,
result.data.allMarkdownRemark.edges
)
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
if ([`article`, `photo`].includes(node.frontmatter.type)) {
createPage({
path: node.frontmatter.path,
component: blogPostTemplate,
context: {
similar: _.uniqBy(
_.flatten(
_.concat(
destPosts[node.frontmatter.country],
node.frontmatter.tags.map(tag => tagPosts[tag])
)
),
`id`
),
}, // additional data can be passed via context
})
}
})
return Promise.resolve()
})
}