diff --git a/index.js b/index.js index 8807aa5..955efa8 100644 --- a/index.js +++ b/index.js @@ -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(); diff --git a/src/parser.js b/src/parser.js index e7c0c4b..2c1a691 100644 --- a/src/parser.js +++ b/src/parser.js @@ -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 diff --git a/src/writer.js b/src/writer.js index 5a5b519..2d9732a 100644 --- a/src/writer.js +++ b/src/writer.js @@ -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';