-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (57 loc) · 1.79 KB
/
index.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
#!/usr/bin/env node
const csv = require('fast-csv');
const fs = require('fs');
const argv = require('yargs')
.version('1.0.1')
.usage('Usage: $0 [csv] -m [md]')
.demandCommand(1, "You must specify the name of the csv to convert to markdown.")
.describe('m', 'The name of the md file to be saved.')
.alias('m', 'markdown')
.default('m', 'notes.md')
.help('h')
.alias('h', 'help')
.epilog('copyright 2017')
.argv;
const stream = fs.createReadStream(argv._[0])
const NOTE_INDEX = 0, COURSE_INDEX = 1, MODULE_INDEX = 2, CLIP_INDEX = 3;
let titles;
let courseName;
let noteContent = [];
const mapNote = (titles, content) => {
var note = {};
note[titles[NOTE_INDEX]] = content[NOTE_INDEX]
note[titles[MODULE_INDEX]] = content[MODULE_INDEX]
note[titles[CLIP_INDEX]] = content[CLIP_INDEX]
return note;
}
const notesStream = csv({
trim: true
})
.on('data', (data)=>{
if(!titles) {
titles = data;
}else {
if(!courseName) {
courseName = data[COURSE_INDEX];
}
noteContent.push(mapNote(titles, data))
}
})
.on('end', ()=>{
let mdContent = `# ${courseName}\n\n`
let lastModule;
let lastClip;
noteContent.forEach(note => {
if(note[titles[MODULE_INDEX]] !== lastModule) {
mdContent += `## ${note[titles[MODULE_INDEX]]}\n\n`;
lastModule = note[titles[MODULE_INDEX]];
}
if(note[titles[CLIP_INDEX]] !== lastClip) {
mdContent += `### ${note[titles[CLIP_INDEX]]}\n\n`;
lastClip = note[titles[CLIP_INDEX]];
}
mdContent += note[titles[NOTE_INDEX]] + '\n\n';
});
fs.writeFileSync(argv.markdown, mdContent);
});
stream.pipe(notesStream);