-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevernoteToDayOne.js
62 lines (51 loc) · 1.81 KB
/
evernoteToDayOne.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
const fs = require('fs');
const TurndownService = require('turndown');
const {JSDOM} = require('jsdom');
const { execSync } = require('child_process');
const noteFolder = './My\ Notes/';
const turndownService = new TurndownService();
// ensures title doesn't stick to first paragraph
turndownService.addRule('title', {
filter: 'title',
replacement: function(content) {
return content + ' \n';
}
})
// dayone2 will insert photos into each `[{photo}]` by order found in --photos
turndownService.addRule('img', {
filter: 'img',
replacement: function(content) {
return '[{photo}]';
}
})
fs.readdirSync(noteFolder).forEach(fileName => {
// Skip directories which don't end in `.html` & `index.html` listing
if (fileName.indexOf('.html') !== fileName.length - 5 || fileName === 'index.html') {
return;
}
console.log("Importing " + fileName);
var contents = fs.readFileSync(noteFolder + fileName, 'utf8');
var dom = new JSDOM(contents);
let createdDate = '';
const metaTags = dom.window.document.getElementsByTagName('meta');
for (let i = 0; i < metaTags.length; i++) {
if (metaTags[i].name === 'created') {
createdDate = metaTags[i].content;
break;
}
}
let imgUrls = [];
const imgTags = dom.window.document.getElementsByTagName('img');
for (let i = 0; i < imgTags.length; i++) {
imgUrls.push('"' + noteFolder + decodeURIComponent(imgTags[i].src) + '"');
}
const markdown = turndownService.turndown(contents).replace(/"/g, '\\"');
const urls = imgUrls.join(' ');
const isoDate = new Date(createdDate).toISOString().replace('.000', '');
let execCommand = `dayone2 new "${markdown}"`;
if (urls.length > 0) {
execCommand += ` --photos ${urls}`;
}
execCommand += ` --isoDate "${isoDate}" --tags "EvernoteImport"`;
execSync(execCommand, console.log);
});