2018-10-14 18:46:24 -04:00
|
|
|
const fs = require('fs');
|
|
|
|
|
const luxon = require('luxon');
|
2018-10-20 10:58:11 -04:00
|
|
|
const path = require('path');
|
2018-10-16 18:47:06 -04:00
|
|
|
const request = require('request');
|
2018-10-14 18:46:24 -04:00
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
const shared = require('./src/shared');
|
|
|
|
|
const wizard = require('./src/wizard');
|
|
|
|
|
const parser = require('./src/parser');
|
2018-10-14 18:46:24 -04:00
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
// global so various functions can access arguments
|
|
|
|
|
let config;
|
2018-10-14 18:46:24 -04:00
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
async function init() {
|
2018-10-14 18:46:24 -04:00
|
|
|
try {
|
2019-12-15 13:44:04 -05:00
|
|
|
config = wizard.getConfig();
|
|
|
|
|
let posts = await parser.parseFilePromise(config)
|
|
|
|
|
writeFiles(posts);
|
2018-10-14 18:46:24 -04:00
|
|
|
} catch (ex) {
|
2019-12-15 13:44:04 -05:00
|
|
|
// appease the UnhandledPromiseRejectionWarning
|
|
|
|
|
console.error(ex);
|
2018-10-24 15:50:35 -04:00
|
|
|
}
|
2018-10-16 17:26:33 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-17 19:08:53 -04:00
|
|
|
function writeFiles(posts) {
|
2018-11-11 07:24:14 -05:00
|
|
|
let delay = 0;
|
2018-10-14 18:46:24 -04:00
|
|
|
posts.forEach(post => {
|
2018-10-20 10:58:11 -04:00
|
|
|
const postDir = getPostDir(post);
|
2018-10-17 19:08:53 -04:00
|
|
|
createDir(postDir);
|
2018-10-18 18:53:43 -04:00
|
|
|
writeMarkdownFile(post, postDir);
|
2018-10-16 18:47:06 -04:00
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
if (config.saveimages && post.meta.imageUrls) {
|
2018-10-16 18:47:06 -04:00
|
|
|
post.meta.imageUrls.forEach(imageUrl => {
|
2018-10-19 16:07:29 -04:00
|
|
|
const imageDir = path.join(postDir, 'images');
|
|
|
|
|
createDir(imageDir);
|
2018-11-11 07:24:14 -05:00
|
|
|
writeImageFile(imageUrl, imageDir, delay);
|
|
|
|
|
delay += 25;
|
2018-10-16 18:47:06 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-10-14 18:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-18 18:53:43 -04:00
|
|
|
function writeMarkdownFile(post, postDir) {
|
2018-10-19 16:07:29 -04:00
|
|
|
const frontmatter = Object.entries(post.frontmatter)
|
|
|
|
|
.reduce((accumulator, pair) => {
|
|
|
|
|
return accumulator + pair[0] + ': "' + pair[1] + '"\n'
|
|
|
|
|
}, '');
|
2018-10-24 10:42:54 -04:00
|
|
|
const data = '---\n' + frontmatter + '---\n\n' + post.content + '\n';
|
2018-10-19 16:07:29 -04:00
|
|
|
|
2018-10-20 10:58:11 -04:00
|
|
|
const postPath = path.join(postDir, getPostFilename(post));
|
2018-10-21 18:09:26 -04:00
|
|
|
fs.writeFile(postPath, data, (err) => {
|
2018-10-18 18:53:43 -04:00
|
|
|
if (err) {
|
|
|
|
|
console.log('Unable to write file.')
|
|
|
|
|
console.log(err);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('Wrote ' + postPath + '.');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 07:24:14 -05:00
|
|
|
function writeImageFile(imageUrl, imageDir, delay) {
|
2019-12-15 13:44:04 -05:00
|
|
|
let imagePath = path.join(imageDir, shared.getFilenameFromUrl(imageUrl));
|
2018-11-11 07:24:14 -05:00
|
|
|
let stream = fs.createWriteStream(imagePath);
|
|
|
|
|
stream.on('finish', () => {
|
|
|
|
|
console.log('Saved ' + imagePath + '.');
|
|
|
|
|
});
|
2018-10-19 16:07:29 -04:00
|
|
|
|
2018-11-11 07:24:14 -05:00
|
|
|
// stagger image requests so we don't piss off hosts
|
|
|
|
|
setTimeout(() => {
|
2018-10-19 16:07:29 -04:00
|
|
|
request
|
|
|
|
|
.get(imageUrl)
|
|
|
|
|
.on('response', response => {
|
|
|
|
|
if (response.statusCode !== 200) {
|
|
|
|
|
console.log('Response status code ' + response.statusCode + ' received for ' + imageUrl + '.');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.on('error', err => {
|
|
|
|
|
console.log('Unable to download image.');
|
|
|
|
|
console.log(err);
|
|
|
|
|
})
|
|
|
|
|
.pipe(stream);
|
2018-11-11 07:24:14 -05:00
|
|
|
}, delay);
|
2018-10-14 18:46:24 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-20 10:58:11 -04:00
|
|
|
function createDir(dir) {
|
|
|
|
|
try {
|
|
|
|
|
fs.accessSync(dir, fs.constants.F_OK);
|
|
|
|
|
} catch (ex) {
|
|
|
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getPostDir(post) {
|
2019-12-15 13:44:04 -05:00
|
|
|
let dir = config.output;
|
2018-10-20 10:58:11 -04:00
|
|
|
let dt = luxon.DateTime.fromISO(post.frontmatter.date);
|
|
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
if (config.yearmonthfolders) {
|
2018-10-20 10:58:11 -04:00
|
|
|
dir = path.join(dir, dt.toFormat('yyyy'), dt.toFormat('LL'));
|
2019-12-15 13:44:04 -05:00
|
|
|
} else if (config.yearfolders) {
|
2018-10-20 10:58:11 -04:00
|
|
|
dir = path.join(dir, dt.toFormat('yyyy'));
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-15 13:44:04 -05:00
|
|
|
if (config.postfolders) {
|
2018-11-02 16:59:36 -04:00
|
|
|
let folder = post.meta.slug;
|
2019-12-15 13:44:04 -05:00
|
|
|
if (config.prefixdate) {
|
2018-10-20 13:56:32 -04:00
|
|
|
folder = dt.toFormat('yyyy-LL-dd') + '-' + folder;
|
|
|
|
|
}
|
|
|
|
|
dir = path.join(dir, folder);
|
2018-10-20 10:58:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getPostFilename(post) {
|
2019-12-15 13:44:04 -05:00
|
|
|
if (config.postfolders) {
|
2018-10-24 10:42:54 -04:00
|
|
|
// the containing folder name will be unique, just use index.md here
|
2018-10-20 13:56:32 -04:00
|
|
|
return 'index.md';
|
|
|
|
|
} else {
|
2018-11-02 16:59:36 -04:00
|
|
|
let filename = post.meta.slug + '.md';
|
2019-12-15 13:44:04 -05:00
|
|
|
if (config.prefixdate) {
|
2018-10-20 13:56:32 -04:00
|
|
|
let dt = luxon.DateTime.fromISO(post.frontmatter.date);
|
|
|
|
|
filename = dt.toFormat('yyyy-LL-dd') + '-' + filename;
|
|
|
|
|
}
|
|
|
|
|
return filename;
|
|
|
|
|
}
|
2018-10-20 10:58:11 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-24 10:42:54 -04:00
|
|
|
// it's go time!
|
2018-10-14 18:46:24 -04:00
|
|
|
init();
|