-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-api-docs.mjs
More file actions
66 lines (56 loc) · 1.86 KB
/
build-api-docs.mjs
File metadata and controls
66 lines (56 loc) · 1.86 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
// @ts-check
import fs from 'fs'
import path from 'path'
import {r, escape} from 'regexr'
import {CommentAnalyzer, MarkdownRenderer} from 'readem'
/** Check if a file exists.
* @param {string} filePath
* @returns {Promise<boolean>}
*/
async function fileExists(filePath) {
try {
await fs.promises.access(filePath, fs.constants.F_OK)
return true
} catch (e) {
return false
}
}
async function main() {
const docsMeta = await new CommentAnalyzer().analyze('./node_modules/lume/src', file => {
// Ignore lib files that we copied into the repo.
return !file.startsWith('./src/lib/')
})
const renderer = new MarkdownRenderer()
await renderer.render(docsMeta, './api/')
const navMarkdown = renderer
.renderNav(docsMeta, {linePadStart: ' ', basePath: path.sep + 'api'})
.split('\n')
// Unindent the nav markdown.
.map(line => ' ' + line.trimLeft())
// If Windows, replace backslash with slash (move into readem?)
.map(line => line.replaceAll('\\', '/'))
.join('\n')
const sidebarFile = path.resolve('.', '_sidebar.src.md')
/** @type {string} */
const startMarker = escape('<!-- __API_AUTOGENERATED_BEGIN__ -->')
/** @type {string} */
const endMarker = escape('<!-- __API_AUTOGENERATED_END__ -->')
let content = (await fileExists(sidebarFile))
? await fs.promises.readFile(sidebarFile, {encoding: 'utf8'})
: startMarker + endMarker
content = content.replace(
r`${startMarker}(\s|\S)*${endMarker}`,
`${startMarker}
${navMarkdown}
${endMarker}`,
)
await fs.promises.writeFile(sidebarFile.replace('.src.md', '.md'), content, {encoding: 'utf8'})
}
main().catch(e => {
// Make sure the process exits with a non-zero exit code on unhandle promise
// rejections. This won't be necessary in an upcoming release of Node.js, in
// which case it'll exit non-zero automatically. Time of writing this comment:
// Node 13.8.
console.error(e)
process.exit(1)
})