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:
parent
38b5a8ec5d
commit
b6a8fe4e63
1 changed files with 19 additions and 18 deletions
37
functions.js
37
functions.js
|
@ -137,6 +137,8 @@ const pushToGhost = async (postSlug) => {
|
||||||
let reImage = new RegExp('^!\\[(.*)\\]\\((\\S+?)\\)(.*)')
|
let reImage = new RegExp('^!\\[(.*)\\]\\((\\S+?)\\)(.*)')
|
||||||
let reTitle = new RegExp('^#\ .*')
|
let reTitle = new RegExp('^#\ .*')
|
||||||
|
|
||||||
|
// Note down uploaded images
|
||||||
|
var uploadedImages = []
|
||||||
|
|
||||||
for await (const line of readInterface) {
|
for await (const line of readInterface) {
|
||||||
// Line to output
|
// Line to output
|
||||||
|
@ -177,26 +179,25 @@ const pushToGhost = async (postSlug) => {
|
||||||
outStream.write(newLine + '\n')
|
outStream.write(newLine + '\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
postMetaFile = path.join(postFolder, 'metadata.json')
|
// Upload feature_image, if required
|
||||||
let postMeta = JSON.parse(fs.readFileSync(postMetaFile))
|
var featuredImagePath
|
||||||
|
if (!!postMeta.featuredImage) {
|
||||||
|
var imageName = postMeta.featuredImage.replace('*', '')
|
||||||
|
|
||||||
// calculate users
|
// if the image is listed in postMeta.images, it would have
|
||||||
let users = []
|
// already been uploaded
|
||||||
postMeta.authors.forEach((user) => {
|
if (uploadedImages.indexOf(imageName) != -1) {
|
||||||
users.push({slug: user.username})
|
console.log(`Skipping feature image ${imageName}: already listed for upload`)
|
||||||
})
|
} else {
|
||||||
|
var imagePath = path.join(postFolder, 'images', imageName)
|
||||||
|
|
||||||
ghostAdmin.posts.add({
|
// We can only upload if the file exists!
|
||||||
title: postMeta.title,
|
if (!fs.existsSync(imagePath)) {
|
||||||
custom_excerpt: postMeta.subtitle || null,
|
console.warn(`Skipping feature image "${imageName}": file not found`)
|
||||||
tags: postMeta.tags,
|
} else {
|
||||||
authors: users,
|
console.log(`Uploading feature image: ${imageName}`)
|
||||||
html: markdown.toHTML(fs.readFileSync(postOutput, mode='utf-8'))
|
uploadDav(davPath, imagePath)
|
||||||
}, {source: 'html'})
|
featuredImagePath = uploadedPath + '/' + imageName
|
||||||
.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.`)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue