-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
gatsby-node.ts
112 lines (101 loc) · 3.69 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
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
import { createFilePath } from 'gatsby-source-filesystem';
import { GatsbyNode } from 'gatsby';
import path from 'path';
export const onCreateNode: GatsbyNode['onCreateNode'] = async ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `Mdx`) {
const slug = createFilePath({ node, getNode })
const sourceName = getNode(node.parent).sourceInstanceName
const prefix = sourceName === "basepages" ? '' : '/'+sourceName;
createNodeField({
node,
name: `slug`,
value: `${prefix}${slug}`,
})
createNodeField({
node,
name: `sourceName`,
value: sourceName,
})
}
}
export const createPages: GatsbyNode['createPages'] = async ({ graphql, actions }) => {
const { createPage } = actions
return graphql<any>(`
query GatsbyNodeQuery {
all: allMdx {
edges {
node {
fields {
slug
sourceName
}
}
}
}
blog: allMdx(filter: { fields: { sourceName: { eq: "blog" } } }) {
edges {
node {
id
}
}
}
portfolio: allMdx(filter: { fields: { sourceName: { eq: "portfolio" } } }) {
edges {
node {
id
}
}
}
limitPost: site {
siteMetadata {
blogItemsPerPage
portfolioItemsPerPage
}
}
}
`).then(result => {
result.data.all.edges.forEach(({ node }) => {
let template = node.fields.sourceName
createPage({
path: node.fields.slug,
component: path.resolve("./src/templates/" + template + ".tsx"),
context: {
slug: node.fields.slug,
},
})
})
const blogPosts = result.data.blog.edges
const blogPostsPerPage =
result.data.limitPost.siteMetadata.blogItemsPerPage
const numBlogPages = Math.ceil(blogPosts.length / blogPostsPerPage)
Array.from({ length: numBlogPages }).forEach((_, i) => {
createPage({
path: i === 0 ? `/blog` : `/blog/${i + 1}`,
component: path.resolve("./src/templates/blog-list.tsx"),
context: {
limit: blogPostsPerPage,
skip: i * blogPostsPerPage,
numPages: numBlogPages,
currentPage: i + 1,
},
})
})
const portfolioItems = result.data.portfolio.edges
const portfolioItemsPerPage =
result.data.limitPost.siteMetadata.portfolioItemsPerPage
const numPortfolioItems = Math.ceil(portfolioItems.length / portfolioItemsPerPage)
Array.from({ length: numPortfolioItems }).forEach((_, i) => {
createPage({
path: i === 0 ? `/portfolio` : `/portfolio/${i + 1}`,
component: path.resolve("./src/templates/portfolio-list.tsx"),
context: {
limit: portfolioItemsPerPage,
skip: i * portfolioItemsPerPage,
numPages: numPortfolioItems,
currentPage: i + 1,
},
})
})
})
}