forked from MarquezProject/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.ts
85 lines (77 loc) · 2.66 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
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
}
}
}
limitPost: site {
siteMetadata {
blogItemsPerPage
}
}
}
`).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,
},
})
})
})
}