diff --git a/.gitignore b/.gitignore index 66cf8ea..b61bc19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ .idea node_modules dist - +temp diff --git a/src/Markdown.ts b/src/Markdown.ts index d9ef82a..e2f70e1 100644 --- a/src/Markdown.ts +++ b/src/Markdown.ts @@ -12,12 +12,18 @@ const api = new GhostAdminAPI({ version: "v4.0", }); let convertor = new Convertor("./src/data/markdown-it-example.md"); -convertor.processImages(api, "./"); +convertor + .processImages(api, "./temp") + .then(() => { + console.log("ok"); + }) + .catch((r) => { + console.log(r); + }); let mdoc: string = ""; //convertor.process(); let creation = false; -let imageUpload = true; if (creation) { api.posts diff --git a/src/convertor/Processors.ts b/src/convertor/Processors.ts index 6d23b50..fef5b8f 100644 --- a/src/convertor/Processors.ts +++ b/src/convertor/Processors.ts @@ -11,6 +11,10 @@ interface Stack { meta: any; token: Token; } +interface StackedImage { + path: string; + url: string; +} const renderer: any = Renderer; @@ -21,6 +25,7 @@ const headerAsMarkdown = true; export class Convertor { markups: Record = {}; markers: Record = {}; + images: Record = {}; sections: any[] = []; builder: any = new PostNodeBuilder(); @@ -209,28 +214,50 @@ export class Convertor { * * @returns this */ - public processImages(api: any, basepath: string): Convertor { + public async processImages(api: any, basepath: string) { // 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)); + //let images: string[] = []; + //let m: any; + [...this.content.matchAll(imageRegex)].forEach(async (item) => { + console.log(JSON.stringify(item)); + if (item[1] in this.images) { + console.log("Skip cached image for URL: ", item[1]); + } else { + let url = item[1]; + // Add the image to the cache of processed images + this.images[url] = { + path: url, + url: url, + }; - // 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); + // Download images locally if not already + if (url.toLowerCase().startsWith("http")) { + // Download the image locally + let localpath = path.join(basepath, path.basename(url)); + await downloadImagefromURL(url, localpath); + + // Upload the image to ghost + if (api) { + let reference = await uploadToGhost(api, localpath); + this.images[url].url = reference.url; + } } + + // replace the image string with the new URL + console.log( + "replace", + this.images[url].path, + " with ", + this.images[url].url + ); + this.content = this.content.replaceAll( + this.images[url].path, + this.images[url].url + ); } + + console.log(this.content); }); - return this; } } diff --git a/src/convertor/images.ts b/src/convertor/images.ts index c637320..ab43386 100644 --- a/src/convertor/images.ts +++ b/src/convertor/images.ts @@ -2,9 +2,9 @@ import fs from "fs"; import http from "http"; import https from "https"; import path from "path"; +import { Blob } from "buffer"; import stream from "stream"; -import { Z_FIXED } from "zlib"; const Stream = stream.Transform; @@ -35,18 +35,26 @@ export async function downloadImagefromURL(url: string, filename: string) { } interface Image { - file: Blob | File | undefined; + file: Blob | File | string; purpose: "image" | "profile_image" | "icon"; ref?: string; } -export async function uploadToGhost(api: any, filename: string) { - const data: Buffer = fs.readFileSync(filename); +export interface GhostReference { + url: string; + ref: string; +} + +export async function uploadToGhost( + api: any, + filename: string +): Promise { + //const data: Buffer = fs.readFileSync(filename); let image: Image = { ref: path.basename(filename), - file: new Blob([data]), + file: filename, purpose: "image", }; let result = api.images.upload(image); - console.log(JSON.stringify(result)); + return result; } diff --git a/tsconfig.json b/tsconfig.json index b9fb561..24a914e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,7 @@ "forceConsistentCasingInFileNames": true, "module": "CommonJS", "moduleResolution": "Node", - "target": "ES2015", + "target": "ES2021", "outDir": "./dist", "declaration": true, "declarationDir": "./dist/__types__", @@ -31,7 +31,5 @@ "strictNullChecks": true, "strictPropertyInitialization": true }, - "include": [ - "./src" - ] + "include": ["./src"] }