-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathcollectSlugs.js
More file actions
34 lines (26 loc) · 863 Bytes
/
collectSlugs.js
File metadata and controls
34 lines (26 loc) · 863 Bytes
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
const { opendirSync, readFileSync } = require('node:fs');
const { join } = require('node:path');
function collectSlugs(pathname) {
const dir = opendirSync(pathname);
const res = [];
let direntry;
while ((direntry = dir.readSync()) !== null) {
if (direntry.isFile() && direntry.name.endsWith('.md')) {
const mdContent = readFileSync(join(pathname, direntry.name), { encoding: 'utf-8' });
const slugMatch = mdContent.match(/^slug: (.*)$/m);
if (slugMatch) {
res.push(slugMatch[1]);
}
}
if (direntry.isDirectory()) {
const dirPath = join(pathname, direntry.name);
const dirRes = collectSlugs(dirPath);
res.push(...dirRes);
}
}
dir.closeSync();
return res;
}
module.exports = {
collectSlugs,
};