-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgatsby-node.js
141 lines (124 loc) ยท 3.93 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
const path = require("path");
const readingTime = require(`reading-time`);
const PostPageTemplate = path.resolve(`./src/templates/PostPage.tsx`);
const TagPageTemplate = path.resolve(`./src/templates/TagPage.tsx`);
const PortfolioPageTemplate = path.resolve(`./src/templates/PortfolioPage.tsx`);
const AllPostPageTemplate = path.resolve(`./src/templates/AllPostPage.tsx`);
const WORDS_PER_MINUTE = 500;
exports.onCreateWebpackConfig = ({ actions, plugins, reporter }) => {
actions.setWebpackConfig({
plugins: [
plugins.provide({
React: "react",
}),
],
});
reporter.info(`Provided React in all files`);
};
exports.createPages = async ({ graphql, actions: { createPage } }) => {
const result = await graphql(`
query {
allPosts: allMdx(filter: { frontmatter: { title: { nin: "์ ํ์ ํฌํธํด๋ฆฌ์ค" } } }) {
nodes {
id
body
frontmatter {
slug
tags
locale
}
internal {
contentFilePath
}
}
}
allTags: allMdx {
group(field: { frontmatter: { tags: SELECT } }) {
tag: fieldValue
nodes {
id
}
}
}
portfolio: mdx(frontmatter: { title: { eq: "์ ํ์ ํฌํธํด๋ฆฌ์ค" } }) {
id
body
frontmatter {
slug
}
internal {
contentFilePath
}
}
}
`);
// TODO: ํ์ฌ ๋ฌธ์ ์
// ๋ฉ์ธ(pages/index.tsx) ํ์ด์ง๋ ํ๊ทธ ํ
ํ๋ฆฟ ํ์ด์ง๊ฐ ์ญํ ์ด ๊ฒน์นจ
// tags/all-posts๋ก ๋ง๋ค๊ณ
// ๋ฉ์ธ ํ์ด์ง๋ ์ง์ง ๋ฉ์ธ ๋๋๋๋๋ก ๋ฐ๋ก ๋ง๋ค๊น ๊ณ ๋ฏผ์ค
const POST_PER_PAGE = 10;
// ALL POSTS ํ์ด์ง๋ค์ด์
์์ฑ
const posts = result.data.allPosts.nodes;
// ko๋ก ์์ฑ๋ ํน์ locale์ด ์๋ ํฌ์คํธ๋ง ๋ฝ์์ ํ์ด์ง๋ค์ด์
ํด์ค์ผ ํจ
const koPosts = posts.filter((post) => !post.frontmatter.locale);
const allPostsNumPages = Math.ceil(koPosts.length / POST_PER_PAGE);
Array.from({ length: allPostsNumPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/` : `/${i + 1}`,
component: AllPostPageTemplate,
context: {
limit: POST_PER_PAGE,
skip: i * POST_PER_PAGE,
numPages: allPostsNumPages,
currentPage: i + 1,
},
});
});
// Tags ํ์ด์ง๋ค์ด์
์์ฑ
const tags = result.data.allTags.group;
tags.forEach(({ tag, nodes }) => {
const allTagsNumPages = Math.ceil(nodes.length / POST_PER_PAGE);
// ๊ฐ ํ๊ทธ๋ณ๋ก ํ์ด์ง๋ค์ด์
ํด์ค์ผ ํจ
Array.from({ length: allTagsNumPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/tags/${tag}` : `/tags/${tag}/${i + 1}`,
component: TagPageTemplate,
context: {
limit: POST_PER_PAGE,
skip: i * POST_PER_PAGE,
numPages: allTagsNumPages,
currentPage: i + 1,
tag,
},
});
});
});
// ๋ชจ๋ ํฌ์คํธ ํ์ด์ง ์์ฑ
result.data.allPosts.nodes.forEach((node) => {
const locale = node.frontmatter.locale;
const path = !locale
? `/posts/${node.frontmatter.slug}`
: `/${locale}/posts/${node.frontmatter.slug}`;
createPage({
path,
component: `${PostPageTemplate}?__contentFilePath=${node.internal.contentFilePath}`,
context: {
tags: node.frontmatter.tags,
slug: node.frontmatter.slug,
id: node.id,
readingTime: readingTime(node.body, { wordsPerMinute: WORDS_PER_MINUTE }),
},
});
});
// ํฌํธํด๋ฆฌ์ค ํ์ด์ง ์์ฑ
const portfolio = result.data.portfolio;
createPage({
path: `/portfolio`,
component: `${PortfolioPageTemplate}?__contentFilePath=${portfolio.internal.contentFilePath}`,
context: {
slug: portfolio.frontmatter.slug,
id: portfolio.id,
readingTime: readingTime(result.data.portfolio.body, { wordsPerMinute: WORDS_PER_MINUTE }),
},
});
};