Skip to content

Commit 6cbf126

Browse files
committed
adding script and tests for checking h2 header start
1 parent 36b28d5 commit 6cbf126

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env node
2+
3+
// [start-readme]
4+
//
5+
// Run this one time script to update headers for accessibility
6+
// Changing H3 to H2, H4 to H3, H5 to H4, and H6 to H5
7+
//
8+
// [end-readme]
9+
10+
const fs = require('fs')
11+
const path = require('path')
12+
const walk = require('walk-sync')
13+
14+
async function updateMdHeaders (dir) {
15+
walk(dir, { includeBasePath: true, directories: false })
16+
.filter(file => !file.endsWith('README.md'))
17+
.forEach(file => {
18+
fs.readFile(file, 'utf8', (err, data) => {
19+
if (err) return console.error(err)
20+
21+
const result = data
22+
.replace(/^### /gm, '## ')
23+
.replace(/^#### /gm, '### ')
24+
.replace(/^##### /gm, '#### ')
25+
.replace(/^###### /gm, '##### ')
26+
27+
fs.writeFile(file, result, 'utf8', function (err) {
28+
if (err) return console.log(err)
29+
})
30+
})
31+
})
32+
}
33+
34+
async function main () {
35+
const mdDirPaths = [
36+
path.join(__dirname, '../../content'),
37+
path.join(__dirname, '../../data/reusables')
38+
]
39+
40+
for (const dir of mdDirPaths) {
41+
await updateMdHeaders(dir)
42+
}
43+
}
44+
45+
main()
46+
.catch(console.error)
47+
.finally(() => console.log('Done'))

tests/content/content-md-header.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const path = require('path')
2+
const walk = require('walk-sync')
3+
const { zip } = require('lodash')
4+
const readFileAsync = require('../../lib/readfile-async')
5+
6+
const rootDir = path.join(__dirname, '../..')
7+
const contentDir = path.join(rootDir, 'content')
8+
const reusablesDir = path.join(rootDir, 'data/reusables')
9+
10+
/*
11+
This will check if the markdown files in the content and data/reusables directorys starts with H3 headers
12+
*/
13+
14+
describe('Check H2 header start in markdown content and reusables files', () => {
15+
describe('must start with h2', () => {
16+
const mdWalkOptions = {
17+
globs: ['**/*.md'],
18+
ignore: ['**/README.md'],
19+
directories: false,
20+
includeBasePath: true
21+
}
22+
const re = /^#.*\n/gm
23+
const contentMarkdownAbsPaths = walk(contentDir, mdWalkOptions).sort()
24+
const contentMarkdownRelPaths = contentMarkdownAbsPaths.map(p => path.relative(rootDir, p))
25+
const contentMarkdownTuples = zip(contentMarkdownRelPaths, contentMarkdownAbsPaths)
26+
27+
const reusableMarkdownAbsPaths = walk(reusablesDir, mdWalkOptions).sort()
28+
const reusableMarkdownRelPaths = reusableMarkdownAbsPaths.map(p => path.relative(rootDir, p))
29+
const reusableMarkdownTuples = zip(reusableMarkdownRelPaths, reusableMarkdownAbsPaths)
30+
31+
test.each([...contentMarkdownTuples, ...reusableMarkdownTuples])(
32+
'in "%s"',
33+
async (markdownRelPath, markdownAbsPath) => {
34+
const fileContents = await readFileAsync(markdownAbsPath, 'utf8')
35+
const match = fileContents.match(re)
36+
const firstHeader = (match) ? match[0].split(' ')[0] : null
37+
const errorMessage = 'Found the wrong header:' + match
38+
expect(['##', null], errorMessage).toContain(firstHeader)
39+
}
40+
)
41+
})
42+
})

0 commit comments

Comments
 (0)