-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtabs-md.js
42 lines (35 loc) · 1.04 KB
/
tabs-md.js
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
import * as fs from 'fs/promises';
/** @type {{ name: string, tabs: { name: string, url: string }[] }[] }} */
const groups = JSON.parse(await fs.readFile('./tabs.json'));
const existing = new Set();
const out = 'content';
try {
// await fs.mkdir(out, { recursive: true });
const dir = await fs.opendir(out);
for await (const dirent of dir) {
existing.add(dirent.name);
}
} catch (error) {
console.error(error);
}
const files = groups.map(({ name, tabs }) => ({
name:
name
.toLowerCase()
.replace(/[^a-z0-9._-]/g, '-')
.replace(/-+/g, '-') + '.md',
header: `---
title: ${name}
---
# ${name}
`,
content: `\n${tabs
.map(({ name, url }) => `[${name.replace(/[<>{}]|</g, '')}](${url})`)
.join('\n\n')}\n`,
}));
for (const { name, header, content } of files) {
const path = `${out}/${name}`;
const exists = existing.has(name);
const promise = exists ? fs.appendFile(path, content) : fs.writeFile(path, header + content);
promise.then(() => console.log((exists ? '++' : '* ') + name)).catch(console.error);
}