diff --git a/functions.js b/functions.js index 611d0b8..8f1fe58 100644 --- a/functions.js +++ b/functions.js @@ -4,6 +4,7 @@ const fs = require('fs') const getPost = require('mediumexporter').getPost const { createClient } = require('webdav') const readline = require('readline') +const { markdown } = require('markdown') const GhostAdminAPI = require('@tryghost/admin-api') 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' }) + let titleSkipped = false; + + let reImage = new RegExp('^!\\[(.*)\\]\\((\\S+?)\\)(.*)') + let reTitle = new RegExp('^#\ .*') + readInterface.on('line', (line) => { console.log('Line: ' + line + '\n') - // TODO: actually process line + // Line to output + // Default is to make it same as input var newLine = line + // Skip the header + if (!titleSkipped && reTitle.exec(line)) return + // check for images - const reImage = new RegExp('^!\\[(.*)\\]\\((\\S+?)\\)(.*)') var m = reImage.exec(line) if (m) { // Get image name @@ -142,16 +151,46 @@ const pushToGhost = (postSlug) => { console.warn('Skipping missing image: ' + imageName) } else { // TODO: upload pic + /* uploadDav(davPath, imagePath) + */ newLine = '![' + imageAlt + '](' + uploadedPath + '/' + imageName + ')' } } outStream.write(newLine + '\n') - }) + }).on('close', () => { - console.log('Processing over. Rest needs to be implemented :P') + 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}) + }) + + 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.') + }) + + }) }; /**