Remove global config variable

This commit is contained in:
Will Boyd
2019-12-17 14:03:58 -05:00
parent f4ae769f13
commit e0f7764554
3 changed files with 13 additions and 29 deletions
-4
View File
@@ -2,9 +2,6 @@ const wizard = require('./src/wizard');
const parser = require('./src/parser');
const writer = require('./src/writer');
// global so various functions can access arguments
let config;
async function init() {
try {
config = wizard.getConfig();
@@ -16,5 +13,4 @@ async function init() {
}
}
// it's go time!
init();
+6 -14
View File
@@ -5,28 +5,20 @@ const xml2js = require('xml2js');
const shared = require('./shared');
const translator = require('./translator');
let config;
async function parseFilePromise(configIn) {
const content = fs.readFileSync(configIn.input, 'utf8');
async function parseFilePromise(config) {
const content = fs.readFileSync(config.input, 'utf8');
const processors = { tagNameProcessors: [xml2js.processors.stripPrefix] };
const data = await xml2js.parseStringPromise(content, processors);
config = configIn;
let images = collectImages(data, config);
let posts = collectPosts(data);
mergeImagesIntoPosts(images, posts);
let posts = processData(data);
return Promise.resolve(posts);
}
function processData(data) {
let images = collectImages(data);
let posts = collectPosts(data);
mergeImagesIntoPosts(images, posts);
return posts;
}
function collectImages(data) {
function collectImages(data, config) {
// start by collecting all attachment images
let images = getItemsOfType(data, 'attachment')
// filter to certain image file types
+7 -11
View File
@@ -5,16 +5,12 @@ const request = require('request');
const shared = require('./shared');
let config;
function writeFiles(posts, configIn) {
config = configIn;
function writeFiles(posts, config) {
let delay = 0;
posts.forEach(post => {
const postDir = getPostDir(post);
const postDir = getPostDir(post, config);
createDir(postDir);
writeMarkdownFile(post, postDir);
writeMarkdownFile(post, postDir, config);
if (config.saveimages && post.meta.imageUrls) {
post.meta.imageUrls.forEach(imageUrl => {
@@ -27,14 +23,14 @@ function writeFiles(posts, configIn) {
});
}
function writeMarkdownFile(post, postDir) {
function writeMarkdownFile(post, postDir, config) {
const frontmatter = Object.entries(post.frontmatter)
.reduce((accumulator, pair) => {
return accumulator + pair[0] + ': "' + pair[1] + '"\n'
}, '');
const data = '---\n' + frontmatter + '---\n\n' + post.content + '\n';
const postPath = path.join(postDir, getPostFilename(post));
const postPath = path.join(postDir, getPostFilename(post, config));
fs.writeFile(postPath, data, (err) => {
if (err) {
console.log('Unable to write file.')
@@ -77,7 +73,7 @@ function createDir(dir) {
}
}
function getPostDir(post) {
function getPostDir(post, config) {
let dir = config.output;
let dt = luxon.DateTime.fromISO(post.frontmatter.date);
@@ -98,7 +94,7 @@ function getPostDir(post) {
return dir;
}
function getPostFilename(post) {
function getPostFilename(post, config) {
if (config.postfolders) {
// the containing folder name will be unique, just use index.md here
return 'index.md';