-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (109 loc) · 3.37 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { readFile, writeFile } from 'fs/promises';
import { resolve, join } from 'path';
import { EOL } from 'os';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const section_blacklist = [
'user'
];
const argv = yargs(hideBin(process.argv))
.option('input_file', {
alias: 'i',
type: "string",
default: "data.json"
})
.option('output_directory', {
alias: 'o',
type: "string",
default: "output/"
})
.argv;
function toTitleCase(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function getPharse(term) {
const phrases = {
"gratitude": "I am grateful for",
"greatness": "What will I do to make today great",
"affirmation": "Daily affirmations",
"amazingness": "3 Amazing things that happened today",
"improvements": "How could I have made today even better",
};
if (phrases[term])
return phrases[term];
console.warn(`Undefined phrase, plz add "${term}" to the dictionary.`)
return toTitleCase(term);
}
function renderMarkdownList(items, indent = 0, order = false, cbAfterItem = null) {
let output = "";
const ind = (" ").repeat(indent);
items.forEach((item, i) => {
let startLine = ind;
if (order)
startLine = `${ind}- ${i + 1}. `;
else
startLine = `${ind}- `;
output += `${startLine}${item}${EOL}`;
if (cbAfterItem) {
output += cbAfterItem(i, item);
}
});
return output;
}
async function readJson(path) {
const content = await readFile(path, 'utf8');
return JSON.parse(content);
}
function renderContent(content) {
let output = "";
content.sections.forEach(section => {
if (section_blacklist.indexOf(section.type) !== -1)
return;
const sectionLabel = toTitleCase(section.type);
output += `# ${sectionLabel}${EOL}`;
output += `- [[5 Minute Journal]]${EOL}`;
output += ` - ${sectionLabel}::${EOL}`;
const itemLabels = section.items.map(item => getPharse(item.type) + "::");
const itemContents = section.items.map(item => item.content.text);
output += renderMarkdownList(itemLabels, 2, false, itemIndex => {
return itemContents[itemIndex] ?
renderMarkdownList(itemContents[itemIndex], 3, true) : "";
});
output += EOL;
});
return output;
}
function renderMedia(media) {
let output = " - Photo of the day::" + EOL;
const items = media
.filter(m => m.type == "image")
.map((m, i) => `![fotulka_${i + 1}](${m.file})${EOL}`);
output += renderMarkdownList(items, 3, false);
return output;
}
async function processSingleRecord(record, outputDirectory) {
const outputPath = join(outputDirectory, `${record.date}.md`);
let content = renderContent(record.content);
if (record.media)
content += renderMedia(record.media);
await writeFile(outputPath, content, 'utf8');
console.log(`Write to file ${outputPath}`);
}
async function processData(inputFile, outputDirectory) {
const data = await readJson(inputFile);
const promises = [];
data.records.forEach(record => {
const prom = processSingleRecord(record, outputDirectory);
promises.push(prom);
});
return Promise.all(promises);
}
async function main() {
const input = resolve(argv.input_file);
const output = resolve(argv.output_directory);
console.log(`Read from ${input}`);
console.log(`Save to ${output}`);
await processData(input, output);
console.log("Done");
}
main();