-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsite-to-json.js
53 lines (45 loc) · 1.41 KB
/
site-to-json.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
43
44
45
46
47
48
49
50
51
52
53
const yaml = require('js-yaml');
const fs = require('fs');
const WEBSITE_REPO_FOLDER = 'devfest2024';
function readYaml(path) {
try {
return yaml.load(fs.readFileSync(path, 'utf8'));
} catch (e) {
console.log(e);
}
}
function readJSON(path) {
try {
let rawdata = fs.readFileSync(path);
return JSON.parse(rawdata);
} catch (e) {
console.log(e);
}
}
function writeJSON(filename, data) {
fs.writeFileSync(filename, JSON.stringify(data, null, 2), 'utf8');
}
function main() {
// categories
const categoriesJSON = readJSON(`../${WEBSITE_REPO_FOLDER}/data/categories.json`);
writeJSON('./src/data/categories.json', categoriesJSON.categories);
// slots
const slotsJSON = readJSON(`../${WEBSITE_REPO_FOLDER}/data/slots.json`);
writeJSON('./src/data/slots.json', slotsJSON.slots);
const sessions = walkMarkdownFilesInFolder(`../${WEBSITE_REPO_FOLDER}/data/sessions`);
const speakers = walkMarkdownFilesInFolder(`../${WEBSITE_REPO_FOLDER}/data/speakers`);
writeJSON('./src/data/site.json', { sessions, speakers });
}
function walkMarkdownFilesInFolder(folder) {
const files = fs.readdirSync(folder);
return files.map((file) => {
const filePath = folder + '/' + file;
if (!filePath.startsWith('_') && filePath.endsWith('.yml')) {
const metadata = readYaml(filePath);
console.log(metadata)
return metadata;
}
return null;
}).filter(Boolean);
}
main();