-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
74 lines (71 loc) · 1.74 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
const path = require('path')
const {
createFilePath
} = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({
node,
getNode,
actions
}) => {
const {
createNodeField
} = actions
console.log(node)
// 在 GraphQL 数据中创建的节点类型是 MarkdownRemark
if (node.internal.type === `MarkdownRemark`) {
// 根据文件名创建 slug
let slug = createFilePath({
node,
getNode,
basePath: `posts`
})
// 通过 createNodefield 在当前字段的 fields 下创建一个数据字段
createNodeField({
node,
name: `slug`,
value: slug,
})
}
}
// 创建页面,这个名称是固定的
exports.createPages = async ({
graphql,
actions
}) => {
// 获取到 createPage 方法
const {
createPage
} = actions
// 查询所有 md 文件数据
const result = await graphql(`
query {
allMarkdownRemark {
edges {
node {
fields {
slug
}
frontmatter {
path
}
}
}
}
}
`)
// 逐个创建相应的页面
result.data.allMarkdownRemark.edges.forEach(({
node
}) => {
let path = node.frontmatter.path || node.fields.slug; // 可以使用自定义路径
createPage({
path,
// 创建页面需要模板组件
component: path.resolve(`./src/templates/post.js`),
context: {
// 传递给模板组件中在查询时, 接收的变量值
path
},
})
})
}