Include "Edit on Ghost" button when post is pushed

Convenient, isn't it? ;)
This commit is contained in:
Hippo 2021-03-29 22:12:06 +05:30
parent 1edee70807
commit 1d80daba64
3 changed files with 60 additions and 23 deletions

View File

@ -325,7 +325,7 @@ class Seance {
loglevel: 'info'
})
this.ghostAdmin.posts.add({
let res = await this.ghostAdmin.posts.add({
title: postMeta.title,
custom_excerpt: postMeta.subtitle || null,
tags: postMeta.tags,
@ -333,21 +333,33 @@ class Seance {
html: markdown.toHTML(await fs.promises.readFile(postOutput, 'utf-8')),
feature_image: featuredImagePath
}, {source: 'html'})
.then((res) => {
// Check if user was added
if (res.primary_author.id == 1) {
this.emit('notification', {
message: `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.`,
})
}
this.emit('update', {
status: 'progress',
progress: 100, // we don't know the percentage
message: 'Post conveyed successfully',
loglevel: 'info'
// Check if user was added
if (res.primary_author.id == 1) {
this.emit('notification', {
message: `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.`,
})
}
this.emit('update', {
status: 'progress',
progress: 100, // we don't know the percentage
message: 'Post conveyed successfully',
loglevel: 'info'
})
console.log(res)
return {
slug: res.slug,
id: res.id,
uuid: res.uuid,
preview_url: res.url,
primary_author: res.primary_author,
title: res.title,
subtitle: res.custom_excerpt,
status: res.status,
}
};

View File

@ -193,7 +193,7 @@ app.ws('/ws/push-ghost', (ws, req) => {
command = msg.split(' ')
if (command.length == 3 && command[0] == 'push') {
const postSlug = command[1]
let postSlug = command[1]
// check password
if (command[2] != process.env.SEANCE_PW) {
@ -204,6 +204,9 @@ app.ws('/ws/push-ghost', (ws, req) => {
// Start the Seance session
seance = new Seance()
// catch data
let postId
// set up handlers
seance.on('update', (e) => {
ws.send(`update: ${e.message}`)
@ -217,13 +220,19 @@ app.ws('/ws/push-ghost', (ws, req) => {
ws.send(`error: ${e.message}`)
})
seance.pushToGhost(postSlug)
.then(() => {
console.info(`"${postSlug}" pushed successfully.`)
// run seance and wait till it finishes
let res = await seance.pushToGhost(postSlug)
console.info(`"${postSlug}" pushed successfully.`)
// send 'done' message
let ghostSite = await seance.ghostAdmin.site.read()
let postEditUrl
if (!!res.id) postEditUrl = `${ghostSite.url}ghost/#/editor/post/${res.id}`
if (!!res.slug) postSlug = res.slug
ws.send(`done: ${postSlug} ${postEditUrl}`)
// send 'done' message
ws.send(`done: ${postSlug}`)
})
} else {
ws.send('error: Malformed message')
return

View File

@ -96,7 +96,7 @@ function mediumFetch(e) {
function ghostPush(e) {
if(e) { e.preventDefault() }
const postSlug = document.querySelector('[data-post-slug]').dataset.postSlug
let postSlug = document.querySelector('[data-post-slug]').dataset.postSlug
var password = document.querySelector('input[name=password]').value
console.log('Pushing ' + postSlug)
const socketProtocol = (window.location.protocol === 'https:' ? 'wss:' : 'ws:')
@ -147,7 +147,23 @@ function ghostPush(e) {
addNotification(commandData, className='is-danger')
} else {
// show alert
addNotification(`${postSlug} has been pushed to Ghost. Buffer share link: <a href="https://www.snipettemag.com/${postSlug}">https://www.snipettemag.com/${postSlug}</a>`, 'is-success')
// TODO: don't hard-code destination URL
// (instead, fetch from server's config and display)
// first, unpack info
let postEditUrl
commandData = commandData.split(' ')
if (!!commandData[0]) postSlug = commandData[0]
if (!!commandData[1]) postEditUrl = commandData[1]
let msg = `${postSlug} has been pushed to Ghost. Buffer share link: <a href="https://www.snipettemag.com/${postSlug}" target="_blank">https://www.snipettemag.com/${postSlug}</a>`
if (!!postEditUrl) {
msg = msg + `. <a href="${postEditUrl}" class="button" target="_blank">Edit on Ghost</a>`
}
addNotification(msg, 'is-success')
}
// since it's done, clean up