-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvalidateHeadingIDs.js
More file actions
70 lines (62 loc) · 1.54 KB
/
validateHeadingIDs.js
File metadata and controls
70 lines (62 loc) · 1.54 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const fs = require('fs');
const walk = require('./walk');
/**
* Validate if there is a custom heading id and exit if there isn't a heading
* @param {string} line
* @returns
*/
function validateHeaderId(line) {
if (!line.startsWith('#')) {
return;
}
const match = /\{\/\*(.*?)\*\/}/.exec(line);
const id = match;
if (!id) {
console.error('Run yarn fix-headings to generate headings.');
process.exit(1);
}
}
/**
* Loops through the lines to skip code blocks
* @param {Array<string>} lines
*/
function validateHeaderIds(lines) {
let inCode = false;
const results = [];
lines.forEach((line) => {
// Ignore code blocks
if (line.startsWith('```')) {
inCode = !inCode;
results.push(line);
return;
}
if (inCode) {
results.push(line);
return;
}
validateHeaderId(line);
});
}
/**
* paths are basically array of path for which we have to validate heading IDs
* @param {Array<string>} paths
*/
async function main(paths) {
paths = paths.length === 0 ? ['src/content'] : paths;
const files = paths.map((path) => [...walk(path)]).flat();
files.forEach((file) => {
if (!(file.endsWith('.md') || file.endsWith('.mdx'))) {
return;
}
const content = fs.readFileSync(file, 'utf8');
const lines = content.split('\n');
validateHeaderIds(lines);
});
}
module.exports = main;