forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-headers.js
executable file
·94 lines (87 loc) · 2.79 KB
/
update-headers.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
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
#!/usr/bin/env node
// [start-readme]
//
// Run this one time script to update headers for accessibility
// Changing H3 to H2, H4 to H3, H5 to H4, and H6 to H5
//
// [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.includes('content/rest/reference'))
.forEach((file) => {
fs.readFile(file, 'utf8', (err, data) => {
if (err) return console.error(err)
const matchHeader = data.match(re)
let firstHeader = matchHeader ? matchHeader[0].split(' ')[0] : null
if (firstHeader) {
for (let index = 1; index < matchHeader.length; index++) {
const nextHeader = matchHeader[index].split(' ')[0]
if (nextHeader.length < firstHeader.length && nextHeader.length >= 3) {
console.log(file)
break
}
}
}
if (file.includes('data/reusables/')) {
if (
!file.endsWith('data/reusables/actions/actions-group-concurrency.md') &&
!file.endsWith('data/reusables/github-actions/actions-on-examples.md')
) {
firstHeader = 'reusable-' + firstHeader
}
}
let result
switch (firstHeader) {
case '#':
return
case '##':
return
case '###':
result = data
.replace(/^### /gm, '## ')
.replace(/^#### /gm, '### ')
.replace(/^##### /gm, '#### ')
.replace(/^###### /gm, '##### ')
break
case '####':
result = data
.replace(/^#### /gm, '## ')
.replace(/^##### /gm, '### ')
.replace(/^###### /gm, '#### ')
break
case 'reusable-####':
result = data.replace(/^#### /gm, '### ').replace(/^##### /gm, '#### ')
break
case 'reusable-#####':
result = data.replace(/^##### /gm, '#### ')
break
case '#####':
result = data.replace(/^##### /gm, '### ').replace(/^###### /gm, '#### ')
break
default:
return
}
fs.writeFile(file, result, 'utf8', function (err) {
if (err) return console.log(err)
})
})
})
}
async function main() {
const mdDirPaths = [
path.join(__dirname, '../../content'),
path.join(__dirname, '../../data/reusables'),
]
for (const dir of mdDirPaths) {
await updateMdHeaders(dir)
}
}
main()
.catch(console.error)
.finally(() => console.log('Done'))