-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
128 lines (115 loc) · 3.33 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
const path = require("path")
const { isEmpty } = require("lodash")
const createTagPage = (createPage, allMarkdownByLang) => {
const AllTagsIndexTemplate = path.resolve("src/templates/AllTagsIndex.js")
const SingleTagIndexTemplate = path.resolve("src/templates/SingleTagIndex.js")
const postByTags = {}
allMarkdownByLang.forEach(({ edges }) => {
edges.forEach(({ node }, index) => {
const { frontmatter } = node
if (frontmatter.tags) {
frontmatter.tags.forEach(tag => {
if (!postByTags[tag]) {
postByTags[tag] = []
}
postByTags[tag].push(node)
})
}
})
})
const tags = Object.keys(postByTags)
createPage({
path: "tags",
component: AllTagsIndexTemplate,
context: { tags: tags.sort() },
})
tags.forEach(tagName => {
const postsByTagName = postByTags[tagName]
createPage({
path: `tags/${tagName}`,
component: SingleTagIndexTemplate,
context: { posts: postsByTagName, tagName },
})
})
}
const createRepoPage = (createPage, allMarkdownByLang, allGithubRepo, site) => {
const blogPostTemplate = path.resolve("src/templates/blogPost.js")
const siteUrl = site.siteMetadata.siteUrl
allMarkdownByLang.forEach(({ edges }) => {
const repoEdges = edges.filter(({ node }) =>
node.frontmatter.tags.includes("repo")
)
repoEdges.forEach(({ node }, index) => {
const { lang, path, modifier } = node.frontmatter
const filtreName = repo => {
const pathName = `/${repo.name}`
return pathName === path
}
const repoInfo = allGithubRepo.nodes.filter(filtreName)
createPage({
path: `${lang}${path}`,
component: blogPostTemplate,
classModifier: modifier,
context: {
classModifier: modifier,
pathSlug: path,
lang,
siteUrl,
prev: index === 0 ? null : repoEdges[index - 1].node,
next:
index === repoEdges.length - 1 ? null : repoEdges[index + 1].node,
repoInfo: !isEmpty(repoInfo) ? repoInfo.shift() : repoInfo,
},
})
})
})
}
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
return new Promise((resolve, reject) => {
resolve(
graphql(`
query {
allMarkdownRemark(sort: { order: ASC, fields: frontmatter___date }) {
group(field: frontmatter___lang) {
edges {
node {
id
frontmatter {
title
tags
path
lang
modifier
}
}
}
}
}
site {
siteMetadata {
siteUrl
}
}
allGithubRepo {
nodes {
id
name
description
stargazers_count
topics
html_url
tags
}
}
}
`).then(({ data }) => {
const { allMarkdownRemark, allGithubRepo, site } = data
const allMarkdownByLang = allMarkdownRemark.group
createTagPage(createPage, allMarkdownByLang)
createRepoPage(createPage, allMarkdownByLang, allGithubRepo, site)
resolve()
})
)
})
}