-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild-docs.js
executable file
·66 lines (60 loc) · 1.96 KB
/
build-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
#!/usr/bin/env node
import {createReadStream, createWriteStream} from 'node:fs'
import {copyFile} from 'node:fs/promises'
import _technicalDocsCli from '@derhuerst/technical-docs-cli'
const {
createMarkdownRenderer,
determineSyntaxStylesheetPath,
} = _technicalDocsCli
const GOATCOUNTER_SITE_CODE = process.env.GOATCOUNTER_SITE_CODE || null
const HOSTNAME = process.env.HOSTNAME || null
if (GOATCOUNTER_SITE_CODE && !HOSTNAME) {
console.error('missing/empty $HOSTNAME env var')
process.exit(1)
}
const SYNTAX_STYLESHEET_URL = '/syntax.css'
const SYNTAX_STYLESHEET_SRC = determineSyntaxStylesheetPath('github')
const SYNTAX_STYLESHEET_DEST = 'docs/syntax.css'
const markdownRenderingCfg = {
inlineHtml: true,
syntaxStylesheetUrl: SYNTAX_STYLESHEET_URL,
additionalHeadChildren: (h) => {
if (!GOATCOUNTER_SITE_CODE) return []
return [
h('script', `
if (window.location.host !== ${JSON.stringify(HOSTNAME)}) {
window.goatcounter = {no_onload: true}
}
`),
h('script', {
src: '//gc.zgo.at/count.js',
async: true,
'data-goatcounter': `https://${GOATCOUNTER_SITE_CODE}.goatcounter.com/help/start`,
}),
]
},
}
{
const src = 'docs/readme.md'
const dest = 'docs/index.html'
console.info(`rendering Markdown file ${src} to HTML file ${dest}`)
const srcPath = new URL(src, import.meta.url).pathname
const destPath = new URL(dest, import.meta.url).pathname
// unfortunately, we can't use stream.pipeline right now
// see https://github.com/unifiedjs/unified-stream/issues/1
await new Promise((resolve, reject) => {
createReadStream(srcPath)
.once('error', reject)
.pipe(createMarkdownRenderer(markdownRenderingCfg))
.once('error', reject)
.pipe(createWriteStream(destPath))
.once('error', reject)
.once('finish', resolve)
})
}
{
const srcPath = SYNTAX_STYLESHEET_SRC
const destPath = new URL(SYNTAX_STYLESHEET_DEST, import.meta.url).pathname
console.info(`copying syntax stylesheet from ${srcPath} to ${destPath}`)
await copyFile(srcPath, destPath)
}