Skip to content

Instantly share code, notes, and snippets.

@joshbeckman
Last active March 31, 2019 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbeckman/c23c1da68a2fec98462aeffcbe99b220 to your computer and use it in GitHub Desktop.
Save joshbeckman/c23c1da68a2fec98462aeffcbe99b220 to your computer and use it in GitHub Desktop.
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();
@bsingr
Copy link

bsingr commented Mar 31, 2019

Thanks for sharing! For the other-way round I just created https://github.com/bsingr/dayone2-textbundle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment