-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest-frontmatter.js
More file actions
156 lines (134 loc) · 3.94 KB
/
test-frontmatter.js
File metadata and controls
156 lines (134 loc) · 3.94 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import fs from 'fs';
import path from 'path';
import matter from 'gray-matter';
function findFilesRecursively(directory) {
let dir = [];
let files = [];
try {
dir = fs.readdirSync(directory);
} catch {
dir = [];
}
dir.forEach((item) => {
const fullPath = path.join(directory, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
files = files.concat(findFilesRecursively(fullPath));
} else if (item.endsWith('.md') || item.endsWith('.mdx') || item.endsWith('.json')) {
files.push(fullPath);
}
});
return files;
}
const validate = {
namespace: {
duplicated: function (contentDir) {
const files = findFilesRecursively(contentDir);
const namespaceMap = new Map();
for (const filePath of files) {
const fileContent = fs.readFileSync(filePath, 'utf-8');
let namespace;
if (filePath.endsWith('.json')) {
try {
const jsonData = JSON.parse(fileContent);
namespace = jsonData.namespace;
} catch (error) {
console.error(`Error parsing JSON in file ${filePath}:`, error);
continue;
}
} else {
const { data } = matter(fileContent);
namespace = data.namespace;
}
if (!namespace) {
console.warn(`Warning: The file ${filePath} is missing a 'namespace' field.`);
process.exit(1);
}
if (namespaceMap.has(namespace)) {
console.error(
`Duplicate namespace found: '${namespace}' in file ${filePath} (also in ${namespaceMap.get(
namespace
)})`
);
process.exit(1);
} else {
namespaceMap.set(namespace, filePath);
}
}
},
},
permalink: {
format: function (contentDir) {
const slugRegex = /^[a-z-0-9-|\/]+$/;
const files = findFilesRecursively(contentDir);
for (const filePath of files) {
const fileContent = fs.readFileSync(filePath, 'utf-8');
let permalink;
if (filePath.endsWith('.json')) {
try {
const jsonData = JSON.parse(fileContent);
permalink = jsonData.permalink;
} catch (error) {
console.error(`Error parsing JSON in file ${filePath}:`, error);
process.exit(1);
}
} else {
const { data } = matter(fileContent);
permalink = data.permalink;
}
if (!permalink) {
console.warn(`Warning: The file ${filePath} is missing a 'permalink' field.`);
process.exit(1);
}
if (!slugRegex.test(permalink)) {
console.error(
`Invalid characters found in permalink '${permalink}' in file ${filePath}. Only lower letters, numbers, and hyphens are allowed.`
);
process.exit(1);
}
}
},
duplicated: function (contentDir) {
const files = findFilesRecursively(contentDir);
const permalinkMap = new Map();
for (const filePath of files) {
const fileContent = fs.readFileSync(filePath, 'utf-8');
let permalink;
if (filePath.endsWith('.json')) {
try {
const jsonData = JSON.parse(fileContent);
permalink = jsonData.permalink;
} catch (error) {
console.error(`Error parsing JSON in file ${filePath}:`, error);
continue;
}
} else {
const { data } = matter(fileContent);
permalink = data.permalink;
}
if (!permalink) {
console.warn(`Warning: The file ${filePath} is missing a 'permalink' field.`);
process.exit(1);
}
if (permalinkMap.has(permalink)) {
console.error(
`Duplicate permalink found: '${permalink}' in file ${filePath} (also in ${permalinkMap.get(
permalink
)})`
);
process.exit(1);
} else {
permalinkMap.set(permalink, filePath);
}
}
},
},
};
function pathjoin(folder, dir) {
return path.join(process.cwd(), folder, dir);
}
validate.permalink.format(pathjoin('src', 'content'));
validate.permalink.duplicated(pathjoin('src', 'content/docs/en'));
validate.permalink.duplicated(pathjoin('src', 'content/docs/pt-br'));
validate.namespace.duplicated(pathjoin('src', 'content/docs/en'));
validate.namespace.duplicated(pathjoin('src', 'content/docs/pt-br'));