forked from terser/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-docs.js
50 lines (40 loc) · 1.27 KB
/
generate-docs.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
'use strict'
/**
* Generates docs MD files from the terser README.md
**/
const fs = require('fs')
const path = require('path')
if (!process.argv[2]) {
throw new Error('Usage: ./generate-docs.js path/to/terser/README.md')
}
const doc = fs.readFileSync(process.argv[2], 'utf-8')
const readDemarcated = marker => {
const markerRegExp = new RegExp(String.raw`<!--\s*${marker}:START\s*-->`)
const markerEndRegExp = new RegExp(String.raw`<!--\s*${marker}:END\s*-->`)
const matchStart = doc.match(markerRegExp)
const matchEnd = doc.match(markerEndRegExp)
if (!(matchStart && matchEnd)) {
throw new Error('Marker ' + marker + ' not found')
}
return doc.slice(matchStart.index + matchStart[0].length, matchEnd.index)
}
const writeMd = (docFile, text, header) => {
const writePath = path.join(__dirname, 'website/docs', docFile)
const docHeader = [
'---',
...Object.entries(header).map(pair => pair.join(': ')),
'---',
''
].join('\n')
fs.writeFileSync(writePath, docHeader + text)
}
writeMd('api-reference.md', readDemarcated('API_REFERENCE'), {
id: 'api-reference',
title: 'API Reference',
sidebar_label: 'API Reference'
})
writeMd('cli-usage.md', readDemarcated('CLI_USAGE'), {
id: 'cli-usage',
title: 'CLI Usage',
sidebar_label: 'CLI Usage'
})