-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-sections.js
61 lines (55 loc) · 1.8 KB
/
extract-sections.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
const fs = require('fs');
const path = require('path');
const readmePath = path.join(__dirname, 'README.adoc');
const outputPath = path.join(__dirname, 'src', 'components', 'sections.json');
fs.readFile(readmePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading README.adoc:', err);
return;
}
const sections = [];
const lines = data.split('\n');
let currentSection = null;
lines.forEach(line => {
if (line.startsWith('== ')) {
if (currentSection) {
sections.push(currentSection);
}
const titles = line.substring(3).split(' / ');
currentSection = {
title: {
en: titles[0].trim(),
ja: titles[1].trim(),
ko: titles[2].trim(),
},
items: [],
};
} else if (line.startsWith('* ')) {
const label = line.substring(2).trim();
currentSection.items.push({
label,
links: [],
});
} else if (line.startsWith('** link:')) {
const urlMatch = line.match(/link:(.*?)\[(.*?)\]/);
if (urlMatch) {
const url = urlMatch[1];
const lang = urlMatch[2];
currentSection.items[currentSection.items.length - 1].links.push({
lang,
url,
});
}
}
});
if (currentSection) {
sections.push(currentSection);
}
fs.writeFile(outputPath, JSON.stringify(sections, null, 2), 'utf8', err => {
if (err) {
console.error('Error writing sections.json:', err);
return;
}
console.log('sections.json has been saved.');
});
});