forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_mini_toc_frontmatter.js
executable file
·51 lines (45 loc) · 1.25 KB
/
add_mini_toc_frontmatter.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
#!/usr/bin/env node
// [start-readme]
//
// Run this one time script to add max mini toc
// to rest reference documentation
//
// [end-readme]
import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs'
import walk from 'walk-sync'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const re = /^---\n/gm
async function updateMdHeaders(dir) {
walk(dir, { includeBasePath: true, directories: false })
.filter(
(file) =>
!file.endsWith('README.md') &&
!file.endsWith('index.md') &&
file.includes('content/rest/reference')
)
.forEach((file) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) return console.error(err)
const matchHeader = data.match(re)[1]
let result
let t = 0
if (matchHeader) {
result = data.replace(re, function (match) {
t++
return t === 2 ? 'miniTocMaxHeadingLevel: 3\n---\n' : match
})
}
fs.writeFile(file, result, 'utf8', function (err) {
if (err) return console.log(err)
})
})
})
}
async function main() {
await updateMdHeaders(path.join(__dirname, '../../content'))
}
main()
.catch(console.error)
.finally(() => console.log('Done'))