-
Notifications
You must be signed in to change notification settings - Fork 0
/
documents.js
executable file
·63 lines (52 loc) · 1.6 KB
/
documents.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
// Update document jsons
const {
readdirSync
} = require('fs');
var fs = require('fs')
const FOLDER = './src/assets/documents';
var docs = readdirSync(FOLDER);
var topics = {};
var chapters = {};
docs.forEach(process);
write('chapters.json', chapters);
write('topics.json', topics);
console.log('File chapters.json and topics.json updated!')
function process(item, index) {
if (!item.startsWith(".")) {
// Prepare variables
let file = 'assets/documents/' + item;
let title = item.split("-")[1].replace('.html', '');
let chapter = Number(item.split("-")[0].split(".")[0]);
let order = Number(item.split("-")[0].split(".")[1]);
var lesson = item.split("-")[2];
lesson = lesson == null ? 0 : parseInt(lesson[0]);
let fullTitle = lesson == 0 ? title : title + " " + lesson;
let id = fullTitle.toLowerCase().replace(/[^a-z0-9_-]+/gi, '-')
// Build a lesson for a chapter and a lesson for a topic
var chapterLesson = {},
topicLesson = {};
chapterLesson.id = topicLesson.id = id;
chapterLesson.file = topicLesson.file = file;
chapterLesson.title = topicLesson.title = fullTitle;
chapterLesson.lesson = order;
if (lesson != 0) {
topicLesson.lesson = lesson;
}
// Insert the lessons in chapters and topics
insert(chapter, chapterLesson, chapters);
insert(title, topicLesson, topics);
}
}
function insert(id, item, list) {
if (list[id]) {
list[id].push(item)
} else {
list[id] = [item]
}
}
function write(file, list) {
fs.writeFileSync('src/assets/' + file, JSON.stringify(list), {
encoding: 'utf8',
flag: 'w'
})
}