forked from wesbos/wesbos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagify.js
32 lines (26 loc) · 933 Bytes
/
tagify.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
const glob = require('glob');
const { promises: fs } = require('fs');
async function go() {
glob('./src/tips/**/*.mdx', async (err, files) => {
for (const path of files) {
const [, , , folderName, fileName] = path.split('/');
const [slug, tagStart] = folderName.split('[');
const tags = tagStart.replace(']', '').split(',');
const content = await fs.readFile(path, 'utf-8');
console.log({ folderName, fileName, slug, tags });
const formattedTags = `tags:
${tags.map(tag => ` - ${tag}`).join('\n')}`;
let updatedContent = content.replace(
'---',
`---
${formattedTags}`
);
const [existingSlug] = content.match(/slug: .*/);
updatedContent = updatedContent.replace(existingSlug, `slug: ${slug}`);
// console.log(existingSlug);
// console.log(updatedContent);
await fs.writeFile(path, updatedContent, 'utf-8');
}
});
}
go();