-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.ts
105 lines (89 loc) · 2.38 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { GatsbyNode } from 'gatsby'
import path from 'path'
import { extendObjectByPath } from './src/utilities/objectPathTools'
interface DocsPathQuery {
allMarkdownRemark: {
nodes: {
fileAbsolutePath: string
id: string
frontmatter: {
title: string
order: string
}
}[]
}
}
export interface PageInfo {
id: string
title: string
path: string
order: string
}
export interface SiteMap {
[key: string]: Record<string, SiteMap> | PageInfo
}
export const createPages: GatsbyNode['createPages'] = async ({
actions,
graphql,
}) => {
const DefaultDocsPage = path.resolve(
'./src/templates/documentation/default.tsx'
)
const docsPathsQuery = await graphql<DocsPathQuery>(`
{
allMarkdownRemark {
nodes {
id
fileAbsolutePath
frontmatter {
title
order
}
}
}
}
`)
if (!docsPathsQuery.data) throw new Error('No data readme pages')
// construct sitemap of all the file paths
const pages: { id: string; path: string; title: string; order: string }[] = []
const siteMap: SiteMap = {}
for (const pathNode of docsPathsQuery.data.allMarkdownRemark.nodes) {
// absolute path of the pharos-documentation repo
const basePath = `${__dirname}/src/pharos-documentation`
// strip off the basePath and the extension
let relativePath = pathNode.fileAbsolutePath
.replace(basePath, '')
.replace('.md', '')
// skip the readme in the root of the repository
if (relativePath === '/README') continue
// if it's a README not in the root
// directory, make it an index page.
if (relativePath.split('/').at(-1) === 'README')
relativePath = relativePath.replace('README', '')
else relativePath += '/'
const steps = relativePath.split(/(?=\/)/g)
extendObjectByPath({
obj: siteMap,
path: steps,
valueObj: {
id: pathNode.id,
path: relativePath,
title: pathNode.frontmatter.title,
order: pathNode.frontmatter.order,
},
})
pages.push({
id: pathNode.id,
path: relativePath,
title: pathNode.frontmatter.title,
order: pathNode.frontmatter.order,
})
}
for (const page of pages) {
actions.createPage({
path: page.path,
component: DefaultDocsPage,
context: { id: page.id, siteMap },
})
}
}