-
Notifications
You must be signed in to change notification settings - Fork 66.7k
Expand file tree
/
Copy pathgenerate.ts
More file actions
105 lines (84 loc) · 3.41 KB
/
generate.ts
File metadata and controls
105 lines (84 loc) · 3.41 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
import fs from 'fs'
import path from 'path'
import { execSync } from 'child_process'
import { program } from 'commander'
import type { NextFunction, Response } from 'express'
import type { ExtendedRequest } from '@/types'
import fpt from '#src/versions/lib/non-enterprise-default-version.js'
import { allVersionKeys } from '#src/versions/lib/all-versions.js'
import { liquid } from '#src/content-render/index.js'
import contextualize from '#src/frame/middleware/context/context.js'
interface CommandOptions {
openSections?: string | string[]
}
const layoutFilename = path.posix.join(process.cwd(), 'src/dev-toc/layout.html')
const layout = fs.readFileSync(layoutFilename, 'utf8')
const staticDirName = 'src/dev-toc/static'
const staticDir = path.posix.join(process.cwd(), staticDirName)
if (!fs.existsSync(staticDir)) fs.mkdirSync(staticDir)
program
.description('Generate a local TOC of the docs website and open it in your browser')
.option(
'-o, --openSections [product-ids...]',
'open sections for one or more product IDs by default (e.g., "-o codespaces pull-requests")',
)
.parse(process.argv)
const options = program.opts<CommandOptions>()
const openSections = options.openSections || ''
const defaultOpenSections = Array.isArray(openSections) ? openSections : [openSections]
main()
async function main(): Promise<void> {
const next = (() => {}) as NextFunction
const res = {} as Response
const req = {
language: 'en',
cookies: {},
headers: {},
query: {},
path: '',
method: 'GET',
get: () => '',
header: () => '',
accepts: () => false,
context: {} as any,
} as unknown as ExtendedRequest
async function recurse(tree: any): Promise<void> {
const { page } = tree
tree.renderedFullTitle = page.rawTitle.includes('{')
? await liquid.parseAndRender(page.rawTitle, req.context)
: page.rawTitle
for (const node of tree.childPages || []) {
await recurse(node)
}
}
for (const version of allVersionKeys) {
req.pagePath = version === fpt ? '/' : `/${version}`
// Create a subdir for the version if one doesn't exist yet.
const versionStaticDir = path.posix.join(staticDir, version)
if (!fs.existsSync(versionStaticDir)) fs.mkdirSync(versionStaticDir)
// Create a versioned filename.
const filename = path.posix.join(versionStaticDir, 'index.html')
// Create a minimal context object.
await contextualize(req, res, next)
// Add the tree to the req.context.
if (req.context && req.context.siteTree && req.context.currentVersion) {
req.context.currentEnglishTree = req.context.siteTree.en[req.context.currentVersion]
}
if (req.context && req.context.currentEnglishTree) {
await recurse(req.context.currentEnglishTree)
}
// Add any defaultOpenSections to the context.
if (req.context) {
req.context.defaultOpenSections = defaultOpenSections
}
// Parse the layout in src/dev-toc/layout.html with the context we created above.
const outputHtml = await liquid.parseAndRender(layout, Object.assign({}, req.context))
// Write a static file for each version.
fs.writeFileSync(filename, outputHtml)
}
// Default to FPT for the file to open.
const fptFile = path.posix.join(staticDirName, fpt, 'index.html')
execSync(`open ${fptFile}`)
console.log(`\nCreated the TOC! If it doesn't open automatically, open the following file in your browser to view it:\n
${fptFile}`)
}