-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgatsby-node.js
127 lines (119 loc) · 3.08 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
123
124
125
126
127
/**
* Implement Gatsby's Node APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/node-apis/
*/
const path = require('path')
const fs = require('fs')
const fetch = require('node-fetch')
const yaml = require('js-yaml')
exports.onCreateWebpackConfig = ({ stage: _stage, actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
'../../theme.config$': path.join(__dirname, 'src/semantic/theme.config')
}
}
})
}
exports.onPreBootstrap = async () => {
async function createPageFromRemoteMd(url, pagePath, frontmatter, options) {
const res = await fetch(url)
const md = await res.text()
const fm = yaml.safeDump(frontmatter)
const { header } = options || {}
fs.writeFileSync(
path.resolve(pagePath),
`---\n${fm}---\n\n${header || ''}\n${md}`
)
}
await createPageFromRemoteMd(
`https://raw.githubusercontent.com/inkdropapp/inkdrop-model/master/docs/schema.md`,
`src/pages/reference/data-models.md`,
{
index: 20,
category: 'data',
path: '/reference/data-models',
title: 'Data Models'
},
{
header: `* [Book](#resource-book)\n* [File](#resource-file)\n* [Note](#resource-note)\n* [Tag](#resource-tag)\n`
}
)
}
const categories = [
'usage',
'hacking',
'appendix',
'info',
'data',
'classes',
'flux-state',
'flux-action',
'data-access',
'component'
]
exports.createPages = async ({ actions, graphql }) => {
const { createPage } = actions
const manualTemplate = path.resolve(`src/templates/manual-template.js`)
const referenceTemplate = path.resolve(`src/templates/reference-template.js`)
const infoTemplate = path.resolve(`src/templates/info-template.js`)
function getTemplateForCategory(category) {
switch (category) {
case 'info':
return infoTemplate
case 'data':
case 'classes':
case 'flux-state':
case 'flux-action':
case 'data-access':
case 'component':
return referenceTemplate
default:
return manualTemplate
}
}
/*
* Generate pages from Markdown
*/
for (category of categories) {
const result = await graphql(`
{
allMarkdownRemark(
sort: { order: ASC, fields: [frontmatter___index] }
limit: 1000
filter: { frontmatter: { category: { eq: "${category}" } } }
) {
edges {
node {
frontmatter {
path
title
category
}
}
}
}
}
`)
if (result.errors) {
throw result.errors
}
const posts = result.data.allMarkdownRemark.edges
posts.forEach(({ node }, index) => {
const { path, category } = node.frontmatter
const prev = index === 0 ? false : posts[index - 1].node
const next = index === posts.length - 1 ? false : posts[index + 1].node
if (path && category) {
createPage({
path,
component: getTemplateForCategory(category),
context: {
prev,
next
}
})
}
})
}
}