Upload and set featured image while pushing to Ghost

NOTE: mediumexporter also needs to be updated to download the
feature image in the first place!
This commit is contained in:
Hippo 2020-01-01 21:37:36 +05:30
parent 38b5a8ec5d
commit b6a8fe4e63
1 changed files with 19 additions and 18 deletions

View File

@ -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
}
}
}