diff --git a/functions.js b/functions.js index 611c460..33dc1b4 100644 --- a/functions.js +++ b/functions.js @@ -137,6 +137,8 @@ const pushToGhost = async (postSlug) => { let reImage = new RegExp('^!\\[(.*)\\]\\((\\S+?)\\)(.*)') let reTitle = new RegExp('^#\ .*') + // Note down uploaded images + var uploadedImages = [] for await (const line of readInterface) { // Line to output @@ -177,26 +179,25 @@ const pushToGhost = async (postSlug) => { outStream.write(newLine + '\n') } - postMetaFile = path.join(postFolder, 'metadata.json') - let postMeta = JSON.parse(fs.readFileSync(postMetaFile)) + // Upload feature_image, if required + var featuredImagePath + if (!!postMeta.featuredImage) { + var imageName = postMeta.featuredImage.replace('*', '') - // calculate users - let users = [] - postMeta.authors.forEach((user) => { - users.push({slug: user.username}) - }) + // if the image is listed in postMeta.images, it would have + // already been uploaded + if (uploadedImages.indexOf(imageName) != -1) { + console.log(`Skipping feature image ${imageName}: already listed for upload`) + } else { + var imagePath = path.join(postFolder, 'images', imageName) - ghostAdmin.posts.add({ - title: postMeta.title, - custom_excerpt: postMeta.subtitle || null, - tags: postMeta.tags, - authors: users, - html: markdown.toHTML(fs.readFileSync(postOutput, mode='utf-8')) - }, {source: 'html'}) - .then((res) => { - // Check if user was added - if (res.primary_author.id == 1) { - console.warn(`WARNING: The admin editor, "${res.primary_author.name}", is set as author for this post. If this is incorrect, there was some problem matching usernames. Please check and set it manually.`) + // We can only upload if the file exists! + if (!fs.existsSync(imagePath)) { + console.warn(`Skipping feature image "${imageName}": file not found`) + } else { + console.log(`Uploading feature image: ${imageName}`) + uploadDav(davPath, imagePath) + featuredImagePath = uploadedPath + '/' + imageName } } }