From c7a0e57c42c089b089c01e7862bdb4ec400f6cd0 Mon Sep 17 00:00:00 2001 From: Laur Ivan Date: Wed, 29 Jun 2022 23:12:22 +0200 Subject: [PATCH] Work on file upload to ghost. --- src/Markdown.ts | 22 +++++++++---------- src/convertor/Processors.ts | 44 +++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/Markdown.ts b/src/Markdown.ts index a1000ee..d9ef82a 100644 --- a/src/Markdown.ts +++ b/src/Markdown.ts @@ -4,22 +4,22 @@ import { Convertor } from "./convertor/Processors"; const GhostAdminAPI = require("@tryghost/admin-api"); - +// Configure the client +const api = new GhostAdminAPI({ + url: "http://localhost:2368", + // Admin API key goes here + key: "62ac6f5ab1479d0001082bd4:73341f843a5be78647c6f7e47d43d6cb09ec323df6d5706915df4685b3d46ce7", + version: "v4.0", +}); let convertor = new Convertor("./src/data/markdown-it-example.md"); +convertor.processImages(api, "./"); -let mdoc: string = convertor.process(); +let mdoc: string = ""; //convertor.process(); -let creation = true; +let creation = false; +let imageUpload = true; if (creation) { - // Configure the client - const api = new GhostAdminAPI({ - url: "http://localhost:2368", - // Admin API key goes here - key: "62ac6f5ab1479d0001082bd4:73341f843a5be78647c6f7e47d43d6cb09ec323df6d5706915df4685b3d46ce7", - version: "v4.0", - }); - api.posts .add(JSON.parse(mdoc)) .then((response: any) => console.log(JSON.stringify(response))) diff --git a/src/convertor/Processors.ts b/src/convertor/Processors.ts index 0bf440f..6d23b50 100644 --- a/src/convertor/Processors.ts +++ b/src/convertor/Processors.ts @@ -3,6 +3,8 @@ import fs from "fs"; import MarkdownIt from "markdown-it"; import Token from "markdown-it/lib/token"; import metadataParser from "markdown-yaml-metadata-parser"; +import { downloadImagefromURL, uploadToGhost } from "./images"; +import path from "node:path"; interface Stack { tag: string; @@ -135,7 +137,7 @@ export class Convertor { this.sections.push(this.processHeader(stack, value)); break; default: - this.processInline(stack, value); + this.sections.push(this.processInline(stack, value)); } } }); @@ -156,7 +158,15 @@ export class Convertor { private processInline(stack: Stack[], t: Token) { console.log("*********************************************"); - console.log(JSON.stringify(t), stack.length); + console.log(stack.length, JSON.stringify(stack[0])); + // build the header as a markdown card + let tbuf: string[] = []; + if (t.map != null) + for (let i = t.map[0]; i < t.map[1]; i++) tbuf.push(this.lines[i]); + let result = this.builder.createCardSection("markdown", { + markdown: tbuf.join("\n"), + }); + return result; } private processBlock(t: Token) { @@ -193,4 +203,34 @@ export class Convertor { return result; } + + /** + * Identifies images and tries to process them + * + * @returns this + */ + public processImages(api: any, basepath: string): Convertor { + // collect images in an array + const imageRegex = /!\[[^\]]*\]\((.*?)\s*("(?:.*[^"])")?\s*\)/g; + let images: string[] = []; + let m: any; + while ((m = imageRegex.exec(this.content))) { + if (images.indexOf(m[1]) < 0) images.push(m[1]); + } + console.log("Images: ", JSON.stringify(images)); + + // download image locally if in an url + images.forEach(async (url) => { + let localpath = url; + if (url.toLowerCase().startsWith("http")) { + // Download the image locally + let localPath = path.join(basepath, path.basename(url)); + await downloadImagefromURL(url, localPath); + if (api) { + await uploadToGhost(api, localPath); + } + } + }); + return this; + } }