generated from lizhongyue248/gatsby-template-zyue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
119 lines (114 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
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'Asciidoc') {
/** @type {{modified: number}} */
const { pageAttributes } = node
const slug = createFilePath({ node, getNode, basePath: 'pages' })
createNodeField({ node, name: 'slug', value: node.pageAttributes.href || `/post${slug}` })
/** @type {{birthTime: string, modifiedTime: string}} */
const parent = getNode(node.parent)
const birthTime = new Date(Number(pageAttributes.created) || pageAttributes.created || parent.birthTime)
const modifiedTime = new Date(Number(pageAttributes.modified) || pageAttributes.modified || parent.modifiedTime)
createNodeField({ node, name: 'year', value: birthTime.getFullYear() })
createNodeField({ node, name: 'birthTime', value: birthTime })
createNodeField({ node, name: 'modifiedTime', value: modifiedTime })
}
}
const createPostPage = (posts, createPage) => {
posts.forEach(({ node, next, previous }) => {
createPage({
path: node.fields.slug,
component: path.resolve('./src/templates/post.tsx'),
context: {
// Data passed to context is available
// in page queries as GraphQL variables.
slug: node.fields.slug,
next: previous,
previous: next
}
})
})
}
const createBlogPage = (posts, createPage) => {
const postsPerPage = 10
const numPages = Math.ceil(posts.length / postsPerPage)
const createBlogPageUrl = (url, index) => {
createPage({
path: url,
component: path.resolve('./src/templates/post-list.tsx'),
context: {
limit: postsPerPage,
skip: index * postsPerPage,
numPages,
currentPage: index + 1
}
})
}
Array.from({ length: numPages }).forEach((_, i) => {
createBlogPageUrl(`/blog/${i + 1}`, i)
if (i === 0) {
createBlogPageUrl('/', i)
createBlogPageUrl('/blog', i)
}
})
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
/** @type {{data: {allAsciidoc}}} result */
const result = await graphql(`
query {
allAsciidoc(
filter: {pageAttributes: {exclude: {ne: "true"}}},
sort: {order: [ASC, DESC], fields: [pageAttributes___sort, fields___birthTime]}
) {
edges {
node {
fields {
slug
}
}
next {
document {
title
}
fields {
slug
}
}
previous {
document {
title
}
fields {
slug
}
}
}
}
}
`)
const posts = result.data.allAsciidoc.edges
createPostPage(posts, createPage)
createBlogPage(posts, createPage)
}
exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
if (stage === 'build-html' || stage === 'develop-html') {
actions.setWebpackConfig({
module: {
rules: [
{
test: /prismjs/,
use: loaders.null()
},
{
test: /styled-components/,
use: loaders.null()
}
]
}
})
}
}