-
Notifications
You must be signed in to change notification settings - Fork 17
/
gatsby-node.ts
83 lines (73 loc) · 1.76 KB
/
gatsby-node.ts
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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/node-apis/
*/
import type { GatsbyNode } from "gatsby"
import path from "path"
import { characterInfos, characterKeys } from "./src/constants"
export const sourceNodes: GatsbyNode["sourceNodes"] = async ({
actions,
createNodeId,
createContentDigest,
}) => {
// constantsに記述したcharacterInfoをgraphqlに乗せる
characterKeys.forEach(characterKey => {
const characterInfo = characterInfos[characterKey]
const node = {
key: characterKey,
name: characterInfo.name,
characterId: characterInfo.id,
id: createNodeId(`Character-${characterInfo.id}`),
internal: {
type: "Character",
contentDigest: createContentDigest(characterInfo),
},
}
actions.createNode(node)
})
}
export const createPages: GatsbyNode["createPages"] = async ({
actions,
graphql,
}) => {
const { createPage } = actions
// ニュースページ
const newsPostTemplate = path.resolve("src/templates/newsPost.tsx")
const result = await graphql(`
query NewsPages {
allMarkdownRemark(filter: { fileAbsolutePath: { regex: "/news/" } }) {
edges {
node {
frontmatter {
slug
}
}
}
}
}
`)
if (result.errors) {
throw result.errors
}
const data = result.data as {
allMarkdownRemark: {
edges: {
node: {
frontmatter: {
slug: string
}
}
}[]
}
}
data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: `/news/${node.frontmatter.slug}/`,
component: newsPostTemplate,
context: {
slug: node.frontmatter.slug,
},
})
})
}