Last active
March 31, 2019 14:20
-
-
Save joshbeckman/c23c1da68a2fec98462aeffcbe99b220 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var child_process = require('child_process'); | |
var main = () => { | |
var excludedFileNames = /\| Hacker News/; | |
var journal = 'worklog'; | |
fs.readdirSync('.') | |
.filter(f => f.match(/\.md$/)) | |
.map(f => { | |
return Object.assign(fs.statSync(f), { | |
name: f, photos: [], | |
}); | |
}) | |
.map(f => { | |
return Object.assign(f, { | |
body: fs.readFileSync(f.name, 'UTF-8'), | |
isoDate: f.birthtime.toISOString().replace(/\.\d*[^Z]/, ''), | |
}) | |
}) | |
.map(f => { | |
(f.body.match(/!\[.*\]\((.*)\)/g) || []) | |
.map(img => { | |
if (img.match(/!\[.*\]\((.*)\)/)[1].match(/^http/)) return f; | |
f.body = f.body.replace(img, '[{photo}]'); | |
f.photos.push( | |
`'${decodeURIComponent(img.match(/!\[.*\]\((.*)\)/)[1])}'` | |
); | |
}) | |
return f; | |
}) | |
.filter(f => !f.name.match(excludedFileNames)) | |
.map(test => { | |
if (test.photos.length) { | |
child_process.execSync( | |
`echo ${escapeShellArg(test.body)} | dayone2 -isoDate=${test.isoDate} -j ${journal} -p ${test.photos.join(' ')} -- new` | |
) | |
} else { | |
child_process.execSync( | |
`echo ${escapeShellArg(test.body)} | dayone2 -isoDate=${test.isoDate} -j ${journal} new` | |
) | |
} | |
fs.unlinkSync(test.name); | |
}) | |
}; | |
function escapeShellArg (arg) { | |
return `'${arg.replace(/'/g, `'\\''`)}'`; | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing! For the other-way round I just created https://github.com/bsingr/dayone2-textbundle