forked from cloudflare/workers-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
122 lines (117 loc) Β· 3.24 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
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
// You can delete this file if you're not using it
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions
const galleryTemplate = path.resolve(`src/templates/gallery.tsx`)
const markdownTemplate = path.resolve(`src/templates/markdownTemplate.tsx`)
// Create a custom page for the Template Gallery that's NOT based on markdown, just TSX
createPage({
path: `/workers/templates/`,
component: galleryTemplate,
})
result = await graphql(`
{
allMdx(limit: 1000) {
edges {
node {
fields {
pathToServe
}
frontmatter {
alwaysopen
weight
}
fileAbsolutePath
}
}
}
}
`)
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`)
return
}
result.data.allMdx.edges.forEach(({ node }) => {
return createPage({
path: node.fields.pathToServe,
component: markdownTemplate,
context: {
parent: node.fields.parent,
weight: node.frontmatter.weight,
}, // additional data can be passed via context, can use as variable on query
})
})
const templatePage = path.resolve(`src/templates/templatePage.tsx`)
templates = await graphql(`
{
allRestApiTemplates {
nodes {
endpointId
}
}
}
`)
templates.data.allRestApiTemplates.nodes.forEach(({ endpointId }) => {
createPage({
path: `/workers/templates/pages/${endpointId}/`,
component: templatePage,
context: {
id: endpointId,
},
})
})
}
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
// Ensures we are processing only markdown files
if (node.internal.type === 'Mdx') {
// Use `createFilePath` to turn markdown files in our `content` directory into `/workers/`pathToServe
const originalPath = node.fileAbsolutePath.replace(
node.fileAbsolutePath.match(/.*content/)[0],
''
)
let pathToServe = createFilePath({
node,
getNode,
basePath: 'content/',
})
let parentDir = path.dirname(pathToServe)
if (pathToServe.includes('index')) {
pathToServe = parentDir
parentDir = path.dirname(parentDir) // "/" dirname will = "/"
}
pathToServe = pathToServe.replace(/\/+$/, '/') // always end the path with a slash
const templateId = pathToServe.match(/^\/templates\/pages.*/)
? pathToServe.replace(/^\/templates\/pages\//, '').replace('/', '')
: ''
// Creates new query'able field with name of 'pathToServe', 'parent'..
// for allMdx edge nodes
createNodeField({
node,
name: 'pathToServe',
value: `/workers${pathToServe}`,
})
createNodeField({
node,
name: 'parent',
value: parentDir,
})
createNodeField({
node,
name: 'filePath',
value: originalPath,
})
createNodeField({
node,
name: 'templateId',
value: templateId,
})
}
}