Actually upload the post to Ghost on ghost-push!
This commit is contained in:
parent
e01e510ed3
commit
8d6380069f
1 changed files with 43 additions and 4 deletions
45
functions.js
45
functions.js
|
@ -4,6 +4,7 @@ const fs = require('fs')
|
||||||
const getPost = require('mediumexporter').getPost
|
const getPost = require('mediumexporter').getPost
|
||||||
const { createClient } = require('webdav')
|
const { createClient } = require('webdav')
|
||||||
const readline = require('readline')
|
const readline = require('readline')
|
||||||
|
const { markdown } = require('markdown')
|
||||||
const GhostAdminAPI = require('@tryghost/admin-api')
|
const GhostAdminAPI = require('@tryghost/admin-api')
|
||||||
|
|
||||||
const MEDIUM_IMG_CDN = 'https://miro.medium.com/fit/c/'
|
const MEDIUM_IMG_CDN = 'https://miro.medium.com/fit/c/'
|
||||||
|
@ -123,14 +124,22 @@ const pushToGhost = (postSlug) => {
|
||||||
|
|
||||||
const outStream = fs.createWriteStream(postOutput, { encoding: 'utf-8' })
|
const outStream = fs.createWriteStream(postOutput, { encoding: 'utf-8' })
|
||||||
|
|
||||||
|
let titleSkipped = false;
|
||||||
|
|
||||||
|
let reImage = new RegExp('^!\\[(.*)\\]\\((\\S+?)\\)(.*)')
|
||||||
|
let reTitle = new RegExp('^#\ .*')
|
||||||
|
|
||||||
readInterface.on('line', (line) => {
|
readInterface.on('line', (line) => {
|
||||||
console.log('Line: ' + line + '\n')
|
console.log('Line: ' + line + '\n')
|
||||||
|
|
||||||
// TODO: actually process line
|
// Line to output
|
||||||
|
// Default is to make it same as input
|
||||||
var newLine = line
|
var newLine = line
|
||||||
|
|
||||||
|
// Skip the header
|
||||||
|
if (!titleSkipped && reTitle.exec(line)) return
|
||||||
|
|
||||||
// check for images
|
// check for images
|
||||||
const reImage = new RegExp('^!\\[(.*)\\]\\((\\S+?)\\)(.*)')
|
|
||||||
var m = reImage.exec(line)
|
var m = reImage.exec(line)
|
||||||
if (m) {
|
if (m) {
|
||||||
// Get image name
|
// Get image name
|
||||||
|
@ -142,16 +151,46 @@ const pushToGhost = (postSlug) => {
|
||||||
console.warn('Skipping missing image: ' + imageName)
|
console.warn('Skipping missing image: ' + imageName)
|
||||||
} else {
|
} else {
|
||||||
// TODO: upload pic
|
// TODO: upload pic
|
||||||
|
/*
|
||||||
uploadDav(davPath, imagePath)
|
uploadDav(davPath, imagePath)
|
||||||
|
*/
|
||||||
|
|
||||||
newLine = '![' + imageAlt + '](' + uploadedPath + '/' + imageName + ')'
|
newLine = '![' + imageAlt + '](' + uploadedPath + '/' + imageName + ')'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
outStream.write(newLine + '\n')
|
outStream.write(newLine + '\n')
|
||||||
|
}).on('close', () => {
|
||||||
|
|
||||||
|
console.log('Processing over. Pushing to Ghost')
|
||||||
|
|
||||||
|
// load metadata file
|
||||||
|
|
||||||
|
postMetaFile = path.join(postFolder, 'metadata.json')
|
||||||
|
let postMeta = JSON.parse(fs.readFileSync(postMetaFile))
|
||||||
|
|
||||||
|
// calculate users
|
||||||
|
let users = []
|
||||||
|
postMeta.authors.forEach((user) => {
|
||||||
|
users.push({slug: user.username})
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('Processing over. Rest needs to be implemented :P')
|
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.`)
|
||||||
|
}
|
||||||
|
console.log('Post conveyed successfully.')
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue