forked from ordercloud-api/oc-documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-create-pages.ts
171 lines (164 loc) · 4.98 KB
/
gatsby-create-pages.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { resolve } from 'path'
import openApiService from './src/services/openapi.service'
import { GatsbyCreatePages } from './src/models/gatsby.models'
import Case from 'case'
export const createPages: GatsbyCreatePages = async ({ graphql, actions }) => {
const { createPage } = actions
const discoverTemplate = resolve(
'src/components/Templates/DiscoverTemplate.tsx'
)
const docTemplate = resolve('src/components/Templates/DocTemplate.tsx')
const knowledgeBaseTemplate = resolve(
'src/components/Templates/KnowledgeBaseTemplate.tsx'
)
const releaseNotesTemplate = resolve(
'src/components/Templates/ReleaseNotes.tsx'
)
const apiReferenceTemplate = resolve(
'src/components/Templates/ApiReferenceTemplate.tsx'
)
const portalReleaseNotesTemplate = resolve(
'src/components/Templates/PortalReleaseNotesTemplate.tsx'
)
const staticPageTemplate = resolve(
'src/components/Templates/StaticPageTemplate.tsx'
)
const staticDocs = graphql(`
query CreatePagesQuery {
allMdx {
edges {
node {
id
fileAbsolutePath
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
result.data.allMdx.edges.forEach(edge => {
let path = edge.node.fileAbsolutePath
.split('/content')[1]
.replace('.mdx', '')
let component
if (path.startsWith('/discover')) {
component = discoverTemplate
} else if (path.startsWith('/learn')) {
component = docTemplate
} else if (path.startsWith('/documents')) {
path = path.replace('documents', 'knowledge-base')
component = knowledgeBaseTemplate
} else if (path.startsWith('/release-notes')) {
component = releaseNotesTemplate
} else if (path.startsWith('/portal-release-notes')) {
component = portalReleaseNotesTemplate
} else if (path.startsWith('/static-pages')) {
component = staticPageTemplate
path = path.replace('/static-pages', '')
} else {
throw new Error(`Can't resolve path ${edge.node.fileAbsolutePath}`)
}
createPage({
path,
component,
context: {
nodeID: edge.node.id,
},
})
})
})
const apiRef = openApiService
.initialize()
.then(result => {
//create doc menu
const docMenu = []
const pages = []
const referencePath = '/api-reference'
pages.push({
path: referencePath,
component: apiReferenceTemplate,
context: {
type: 'reference',
currentPath: referencePath,
},
})
result.sections.forEach(s => {
const sectionPath = `${referencePath}/${Case.kebab(s['x-id'])}`
const docMenuSection = {
name: s.name,
path: sectionPath,
resources: [],
context: {
currentPath: sectionPath,
},
}
pages.push({
path: sectionPath,
component: apiReferenceTemplate,
context: {
currentPath: sectionPath,
section: s,
type: 'reference',
},
})
result.resources
.filter(r => r['x-section-id'] === s['x-id'])
.forEach(r => {
const resourcePath = `${sectionPath}/${Case.kebab(r.name)}`
const docMenuResource = {
name: r.name,
path: resourcePath,
operations: [],
context: {
currentPath: resourcePath,
},
}
pages.push({
path: resourcePath,
component: apiReferenceTemplate,
context: {
currentPath: resourcePath,
section: s,
resource: r,
type: 'reference',
},
})
result.operationsByResource[r.name].forEach(o => {
const operationPath = `${resourcePath}/${Case.kebab(
o.operationId.split('.')[1]
)}`
docMenuResource.operations.push({
name: o.summary,
path: operationPath,
context: {
currentPath: operationPath,
},
})
pages.push({
path: operationPath,
component: apiReferenceTemplate,
context: {
currentPath: operationPath,
section: s,
resource: r,
operation: o,
type: 'reference',
},
})
})
docMenuSection.resources.push(docMenuResource)
})
docMenu.push(docMenuSection)
pages.forEach(p => {
createPage({ ...p, context: { ...p.context, menuData: docMenu } })
})
})
})
.catch(ex => {
console.error('error initializing open API service')
throw ex
})
return Promise.all([staticDocs, apiRef])
}