Work on file upload to ghost.

This commit is contained in:
Laur Ivan 2022-06-29 23:12:22 +02:00
parent c0964709e1
commit c7a0e57c42
2 changed files with 53 additions and 13 deletions

View File

@ -4,22 +4,22 @@
import { Convertor } from "./convertor/Processors"; import { Convertor } from "./convertor/Processors";
const GhostAdminAPI = require("@tryghost/admin-api"); const GhostAdminAPI = require("@tryghost/admin-api");
// Configure the client
let convertor = new Convertor("./src/data/markdown-it-example.md"); const api = new GhostAdminAPI({
let mdoc: string = convertor.process();
let creation = true;
if (creation) {
// Configure the client
const api = new GhostAdminAPI({
url: "http://localhost:2368", url: "http://localhost:2368",
// Admin API key goes here // Admin API key goes here
key: "62ac6f5ab1479d0001082bd4:73341f843a5be78647c6f7e47d43d6cb09ec323df6d5706915df4685b3d46ce7", key: "62ac6f5ab1479d0001082bd4:73341f843a5be78647c6f7e47d43d6cb09ec323df6d5706915df4685b3d46ce7",
version: "v4.0", version: "v4.0",
}); });
let convertor = new Convertor("./src/data/markdown-it-example.md");
convertor.processImages(api, "./");
let mdoc: string = ""; //convertor.process();
let creation = false;
let imageUpload = true;
if (creation) {
api.posts api.posts
.add(JSON.parse(mdoc)) .add(JSON.parse(mdoc))
.then((response: any) => console.log(JSON.stringify(response))) .then((response: any) => console.log(JSON.stringify(response)))

View File

@ -3,6 +3,8 @@ import fs from "fs";
import MarkdownIt from "markdown-it"; import MarkdownIt from "markdown-it";
import Token from "markdown-it/lib/token"; import Token from "markdown-it/lib/token";
import metadataParser from "markdown-yaml-metadata-parser"; import metadataParser from "markdown-yaml-metadata-parser";
import { downloadImagefromURL, uploadToGhost } from "./images";
import path from "node:path";
interface Stack { interface Stack {
tag: string; tag: string;
@ -135,7 +137,7 @@ export class Convertor {
this.sections.push(this.processHeader(stack, value)); this.sections.push(this.processHeader(stack, value));
break; break;
default: 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) { private processInline(stack: Stack[], t: Token) {
console.log("*********************************************"); 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) { private processBlock(t: Token) {
@ -193,4 +203,34 @@ export class Convertor {
return result; 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;
}
} }