forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-early-access-tocs.js
executable file
·43 lines (33 loc) · 1.18 KB
/
add-early-access-tocs.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
#!/usr/bin/env node
import fs from 'fs'
import path from 'path'
import readFrontmatter from '../../lib/read-frontmatter.js'
import { sentenceCase } from 'change-case'
const earlyAccessDir = path.posix.join(process.cwd(), 'content', 'early-access')
updateOrCreateToc(earlyAccessDir)
console.log('Updated Early Access TOCs!')
function updateOrCreateToc(directory) {
const children = fs.readdirSync(directory).filter((subpath) => !subpath.endsWith('index.md'))
if (!children.length) return
const tocFile = path.posix.join(directory, 'index.md')
let content, data
if (fs.existsSync(tocFile)) {
const matter = readFrontmatter(fs.readFileSync(tocFile, 'utf8'))
content = matter.content
data = matter.data
} else {
content = ''
data = {
title: sentenceCase(path.basename(directory)),
versions: '*',
hidden: true,
}
}
data.children = children.map((child) => `/${child.replace('.md', '')}`)
const newContents = readFrontmatter.stringify(content, data, { lineWidth: 10000 })
fs.writeFileSync(tocFile, newContents)
children.forEach((child) => {
if (child.endsWith('.md')) return
updateOrCreateToc(path.posix.join(directory, child))
})
}