-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.js
More file actions
124 lines (109 loc) · 2.61 KB
/
build.js
File metadata and controls
124 lines (109 loc) · 2.61 KB
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
const fs = require('fs')
const _ = require('lodash')
const prettier = require('prettier')
const prettierConfig = require('./.prettierrc.json')
const buildPage = fileName => `
/*
*
*
*
* WARNING:
* Auto-generated from ${fileName} by yarn build:content
*
*
*
*/
import Content, { meta } from '../content/${fileName}'
import Post from '../components/Post'
export default () => <Post Content={Content} meta={meta} />
`
const buildStructure = fileNames => {
const importLines = []
const mappingLines = []
fileNames.forEach(fileName => {
const camelName = _.camelCase(fileName)
const baseName = fileName.replace(/.mdx$/, '')
importLines.push(`import { meta as ${camelName} } from './${fileName}'`)
mappingLines.push(`'/${baseName}': ${camelName},`)
})
return `
/*
*
*
*
* WARNING:
* Auto-generated from content/*.mdx by yarn build:content
*
*
*
*/
${importLines.join('\n')}
export default {
${mappingLines.join('\n')}
}
`
}
const buildTopic = (topic, topicType) => `
/*
*
*
*
* WARNING:
* Auto-generated from posts tagged ${topic} by yarn build:content
*
*
*
*/
import Topic from '../components/Topic'
export default () => <Topic topic="${topic}" type="${topicType}" />
`
fileNames = fs.readdirSync('./content')
contentFileNames = []
allTags = []
allSeries = []
fileNames.forEach(fileName => {
if (fileName.endsWith('mdx')) {
console.log(`Compiling ${fileName}`)
const pageName = fileName.replace(/mdx$/, 'jsx')
const fileData = fs.readFileSync(`./content/${fileName}`, 'utf8')
const tagsMatch = fileData.match(/tags..\[([^\]]*)\]/)
const tags = tagsMatch[1].replace(/'| /g, '').split(',')
const seriesMatch = fileData.match(/series..'([^']*)'/)
const series = seriesMatch && seriesMatch[1]
allTags = _.compact(_.uniq(allTags.concat(tags)))
allSeries = _.compact(_.uniq(allSeries.concat([series])))
fs.writeFileSync(
`./pages/${pageName}`,
prettier.format(
buildPage(fileName),
_.merge({ parser: 'babel' }, prettierConfig)
)
)
contentFileNames.push(fileName)
}
})
allTags.forEach(tag =>
fs.writeFileSync(
`./pages/${tag}.jsx`,
prettier.format(
buildTopic(tag, 'tag'),
_.merge({ parser: 'babel' }, prettierConfig)
)
)
)
allSeries.forEach(series =>
fs.writeFileSync(
`./pages/${series}.jsx`,
prettier.format(
buildTopic(series, 'series'),
_.merge({ parser: 'babel' }, prettierConfig)
)
)
)
fs.writeFileSync(
'./content/structure.js',
prettier.format(
buildStructure(contentFileNames),
_.merge({ parser: 'babel' }, prettierConfig)
)
)