Repository navigation
Expand file tree
/
Copy pathcheck.mjs
More file actions
54 lines (45 loc) · 1.44 KB
/
Copy pathcheck.mjs
File metadata and controls
54 lines (45 loc) · 1.44 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
/**
* A lot of these docs are copy-pasted and modified, and I may miss things as I write them.
* This script is designed to catch these errors.
*/
import fs from 'fs'
import { micromark } from 'micromark'
import { mdx } from 'micromark-extension-mdx'
import { parse } from 'node-html-parser'
import path from 'path'
function findFiles(dir) {
const files = fs.readdirSync(dir)
let results = []
for (const file of files) {
const abs = path.join(dir, file)
if (fs.statSync(abs).isDirectory()) {
results.push(...findFiles(abs))
} else if (abs.endsWith('.mdx')) {
results.push(abs)
}
}
return results
}
const mdxs = findFiles('./src')
const whitelist = [
'export const metadata',
'Not required for this example.',
"The above ACL isn't required to access resources, only if you want to access user-restricted or internal objects.",
]
const overlaps = {}
for (const mdxFile of mdxs) {
const contents = fs.readFileSync(mdxFile, 'utf-8')
if (!contents) continue
const md = micromark(contents, { extensions: [mdx()] })
const html = parse(md)
const strings = [...html.getElementsByTagName('p')]
for (const ele of strings) {
const str = ele.innerText
const whitelisted = whitelist.find((e) => str.includes(e))
if (whitelisted) continue
if (overlaps[str])
throw `Overlap detected between ${mdxFile} and ${overlaps[str]}: ${str}`
overlaps[str] = mdxFile
}
}
console.log('All checks passed!')