-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate-docs.js
104 lines (87 loc) · 2.76 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
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
'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 fixCrossLinks = markdowns => {
const dashify = title =>
title
.toLowerCase()
.replaceAll(/\W|_/g, '-')
.replaceAll(/-+/g, '-')
.replaceAll(/^-|-$/g, '')
const titleLinks = {}
for (const [filename, md] of Object.entries(markdowns)) {
const url = `/docs/${filename.replace(/.md$/, '')}`
titleLinks[dashify(filename)] = url
for (const title of md.matchAll(/^#+\s*(.+)$/gm)) {
titleLinks[dashify(title[1])] = url + '#' + dashify(title[1])
}
}
for (const [filename, md] of Object.entries(markdowns)) {
markdowns[filename] = md.replaceAll(/\(#(.+?)\)/g, (_, $1) => {
const fixedLink = titleLinks[$1]
if (!fixedLink) throw new Error('broken link at ' + filename + ': ' + $1)
return `(${fixedLink})`
})
}
return markdowns
}
const writeMd = (docFile, text, header) => {
const writePath = path.join(__dirname, 'docs', docFile)
const docHeader = [
'---',
...Object.entries(header).map(pair => pair.join(': ')),
'---',
''
].join('\n')
fs.writeFileSync(writePath, docHeader + text)
}
let markdowns = {
'api-reference': readDemarcated('API_REFERENCE'),
'cli-usage': readDemarcated('CLI_USAGE'),
'options': readDemarcated('OPTIONS'),
'reporting-issues': readDemarcated('REPORTING_ISSUES'),
'miscellaneous': readDemarcated('MISCELLANEOUS'),
}
markdowns = fixCrossLinks(markdowns)
writeMd('api-reference.md', markdowns['api-reference'], {
id: 'api-reference',
title: 'API Reference',
sidebar_label: 'API Reference'
})
writeMd('cli-usage.md', markdowns['cli-usage'], {
id: 'cli-usage',
title: 'CLI Usage',
sidebar_label: 'CLI Usage'
})
writeMd('options.md', markdowns['options'], {
id: 'options',
title: 'Options',
sidebar_label: 'Options'
})
writeMd('reporting-issues.md', markdowns['reporting-issues'], {
id: 'reporting-issues',
title: 'Reporting Issues',
sidebar_label: 'Reporting Issues'
})
writeMd('miscellaneous.md', markdowns['miscellaneous'], {
id: 'miscellaneous',
title: 'Miscellaneous',
sidebar_label: 'Miscellaneous'
})