Image uplaod and replace works.
This commit is contained in:
parent
8cf8c12d54
commit
be2467d2c8
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,4 +1,4 @@
|
||||
.idea
|
||||
node_modules
|
||||
dist
|
||||
|
||||
temp
|
||||
|
@ -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
|
||||
|
@ -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<string, any> = {};
|
||||
markers: Record<string, any> = {};
|
||||
images: Record<string, StackedImage> = {};
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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<GhostReference> {
|
||||
//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;
|
||||
}
|
||||
|
@ -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"]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user