-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathlint-frontmatter.js
More file actions
70 lines (58 loc) · 1.68 KB
/
lint-frontmatter.js
File metadata and controls
70 lines (58 loc) · 1.68 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
/* eslint-disable no-console */
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const ROOT = path.resolve(__dirname, '..');
const IGNORED_DIRS = new Set([
'.git',
'build',
'dist',
'node_modules',
]);
const MARKDOWN_EXTENSIONS = new Set(['.md', '.mdx']);
function walk(dir, files = []) {
fs.readdirSync(dir, { withFileTypes: true }).forEach((entry) => {
if (entry.isDirectory()) {
if (!IGNORED_DIRS.has(entry.name)) {
walk(path.join(dir, entry.name), files);
}
return;
}
if (entry.isFile() && MARKDOWN_EXTENSIONS.has(path.extname(entry.name))) {
files.push(path.join(dir, entry.name));
}
});
return files;
}
function extractFrontmatter(content) {
if (!content.startsWith('---\n') && !content.startsWith('---\r\n')) {
return null;
}
const lines = content.split(/\r?\n/);
const closingIndex = lines.slice(1).findIndex((line) => line.trim() === '---');
if (closingIndex >= 0) {
return lines.slice(1, closingIndex + 1).join('\n');
}
throw new Error('Missing closing frontmatter delimiter');
}
function main() {
const failures = [];
walk(ROOT).forEach((file) => {
const relativePath = path.relative(ROOT, file);
const content = fs.readFileSync(file, 'utf8');
try {
const frontmatter = extractFrontmatter(content);
if (frontmatter !== null) {
yaml.load(frontmatter);
}
} catch (error) {
failures.push(`${relativePath}: ${error.message}`);
}
});
if (failures.length > 0) {
console.error('Invalid Markdown frontmatter found:\n');
failures.forEach((failure) => console.error(`- ${failure}`));
process.exit(1);
}
}
main();