forked from mazipan/talks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toJson.js
127 lines (117 loc) · 3.82 KB
/
toJson.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
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
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
const MarkdownIt = require('markdown-it');
const md = new MarkdownIt();
const writeFile = (pathFile, contentString) => {
fs.writeFile(path.resolve(pathFile), contentString, function (err) {
if (err) {
return console.log(`❌ Error write file ${pathFile}`, err);
}
console.log(`✅ Success write file ${pathFile}`);
});
};
const main = async () => {
try {
const readmeContent = await fs.readFileSync(path.resolve('./README.md'), {
encoding: 'utf-8',
});
const html = md.render(readmeContent);
const $ = cheerio.load(html);
let objResult = {};
$('h3')
.map((i, el) => {
const year = $(el).text();
const data = $(el)
.nextUntil('h3', 'h4')
.map((_, h4) => {
const title = $(h4).text();
const data = $(h4)
.next('ul')
.children()
.map((_, li) => {
const text = $(li).text();
if (text.includes('Event Link')) {
if (text.includes('here')) {
const link = $(li)
.children('a')
.map((_, a) => {
return $(a).attr('href');
})
.get();
return `Link: ${link}`;
}
return `Link: NOT_AVAILABLE`;
} else if (text.includes('Slide')) {
if (text.includes('here')) {
const link = $(li)
.children('a')
.map((_, a) => {
return $(a).attr('href');
})
.get();
return `Slide: ${link}`;
}
return `Slide: NOT_AVAILABLE`;
}
return text;
})
.get();
const formatteddata = {};
data.forEach((item) => {
const itemSplit = item.split(':');
const t = itemSplit[0].trim().toLowerCase().replace(/\s/g, '_');
if (t === 'link') {
const v = item.replace('Link:', '').trim();
const vArray = v.split(',');
formatteddata[t] = vArray;
} else if (t === 'slide') {
const v = item.replace('Slide:', '').trim();
formatteddata[t] = v;
} else if (t === 'location') {
const v = itemSplit[1].trim();
formatteddata['place'] = v;
} else {
const v = itemSplit[1].trim();
formatteddata[t] = v;
}
});
const id = `${title
.toLowerCase()
.replace(/\./g, '')
.replace(/[^\w ]/, '')
.replace(/\s/g, '')}`;
if (id.includes('topic:')) {
return null;
}
return {
id: `${title
.toLowerCase()
.replace(/\./g, '')
.replace(/[^\w ]/, '')
.replace(/\s/g, '')}`,
event: title,
...formatteddata,
};
})
.get()
.filter(Boolean);
objResult = {
...objResult,
...{
[year]: data,
},
};
return {
[year]: data,
};
})
.get();
writeFile('./all-talks.json', JSON.stringify(objResult, null, 2));
writeFile('./all-talks.js', `export default ${JSON.stringify(objResult, null, 2)}`);
writeFile('./all-talks-node.js', `module.exports = ${JSON.stringify(objResult, null, 2)}`);
} catch (error) {
console.error('❌ Error read file README.md', error);
}
};
main();