2022-04-09 12:28:41 +00:00
|
|
|
import { readPage, writePage } from "plugos-silverbullet-syscall/space";
|
|
|
|
import { json } from "plugos-syscall/fetch";
|
|
|
|
import { invokeFunction } from "plugos-silverbullet-syscall/system";
|
|
|
|
import { getCurrentPage, getText } from "plugos-silverbullet-syscall/editor";
|
2022-04-13 12:46:52 +00:00
|
|
|
import { cleanMarkdown } from "../markdown/util";
|
2022-04-19 14:54:47 +00:00
|
|
|
import { parseMarkdown } from "plugos-silverbullet-syscall/markdown";
|
|
|
|
import { extractMeta } from "../query/data";
|
2022-04-09 12:28:41 +00:00
|
|
|
|
2022-04-09 16:25:42 +00:00
|
|
|
type GhostConfig = {
|
|
|
|
url: string;
|
|
|
|
adminKey: string;
|
|
|
|
postPrefix: string;
|
|
|
|
pagePrefix: string;
|
|
|
|
};
|
|
|
|
|
2022-04-09 12:28:41 +00:00
|
|
|
type Post = {
|
|
|
|
id: string;
|
|
|
|
uuid: string;
|
|
|
|
title: string;
|
|
|
|
slug: string;
|
|
|
|
mobiledoc: string;
|
|
|
|
status: "draft" | "published";
|
|
|
|
visibility: string;
|
|
|
|
created_at: string;
|
|
|
|
upblished_at: string;
|
|
|
|
updated_at: string;
|
|
|
|
tags: Tag[];
|
|
|
|
primary_tag: Tag;
|
|
|
|
url: string;
|
|
|
|
excerpt: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Tag = {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
slug: string;
|
|
|
|
description: string | null;
|
|
|
|
};
|
|
|
|
|
|
|
|
type MobileDoc = {
|
|
|
|
version: string;
|
|
|
|
atoms: any[];
|
|
|
|
cards: Card[];
|
|
|
|
};
|
|
|
|
|
|
|
|
type Card = any[];
|
|
|
|
|
|
|
|
function mobileDocToMarkdown(doc: string): string | null {
|
|
|
|
let mobileDoc = JSON.parse(doc) as MobileDoc;
|
|
|
|
if (mobileDoc.cards.length > 0 && mobileDoc.cards[0][0] === "markdown") {
|
|
|
|
return mobileDoc.cards[0][1].markdown;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function markdownToMobileDoc(text: string): string {
|
|
|
|
return JSON.stringify({
|
|
|
|
version: "0.3.1",
|
|
|
|
atoms: [],
|
|
|
|
cards: [["markdown", { markdown: text }]],
|
|
|
|
markups: [],
|
|
|
|
sections: [
|
|
|
|
[10, 0],
|
|
|
|
[1, "p", []],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class GhostAdmin {
|
|
|
|
private token?: string;
|
|
|
|
|
|
|
|
constructor(private url: string, private key: string) {}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
const [id, secret] = this.key.split(":");
|
|
|
|
|
|
|
|
this.token = await self.syscall(
|
|
|
|
"jwt.jwt",
|
|
|
|
secret,
|
|
|
|
id,
|
|
|
|
"HS256",
|
|
|
|
"5m",
|
|
|
|
"/v3/admin/"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async listPosts(): Promise<Post[]> {
|
|
|
|
let result = await json(
|
|
|
|
`${this.url}/ghost/api/v3/admin/posts?order=published_at+DESC`,
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Authorization: `Ghost ${this.token}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return result.posts;
|
|
|
|
}
|
|
|
|
|
|
|
|
async listMarkdownPosts(): Promise<Post[]> {
|
|
|
|
let markdownPosts: Post[] = [];
|
|
|
|
for (let post of await this.listPosts()) {
|
|
|
|
let mobileDoc = JSON.parse(post.mobiledoc) as MobileDoc;
|
|
|
|
if (mobileDoc.cards.length > 0 && mobileDoc.cards[0][0] === "markdown") {
|
|
|
|
markdownPosts.push(post);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return markdownPosts;
|
|
|
|
}
|
|
|
|
|
2022-04-09 16:25:42 +00:00
|
|
|
publishPost(post: Partial<Post>): Promise<any> {
|
|
|
|
return this.publish("posts", post);
|
|
|
|
}
|
|
|
|
|
|
|
|
publishPage(post: Partial<Post>): Promise<any> {
|
|
|
|
return this.publish("pages", post);
|
2022-04-09 12:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 16:25:42 +00:00
|
|
|
async publish(what: "pages" | "posts", post: Partial<Post>): Promise<any> {
|
|
|
|
let oldPostQuery = await json(
|
|
|
|
`${this.url}/ghost/api/v3/admin/${what}/slug/${post.slug}`,
|
2022-04-09 12:28:41 +00:00
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
Authorization: `Ghost ${this.token}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2022-04-09 16:25:42 +00:00
|
|
|
if (!oldPostQuery[what]) {
|
|
|
|
// New!
|
|
|
|
if (!post.status) {
|
|
|
|
post.status = "draft";
|
|
|
|
}
|
|
|
|
let result = await json(`${this.url}/ghost/api/v3/admin/${what}`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Authorization: `Ghost ${this.token}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
[what]: [post],
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
return result[what][0];
|
|
|
|
} else {
|
|
|
|
let oldPost: Post = oldPostQuery[what][0];
|
|
|
|
post.updated_at = oldPost.updated_at;
|
|
|
|
let result = await json(
|
|
|
|
`${this.url}/ghost/api/v3/admin/${what}/${oldPost.id}`,
|
|
|
|
{
|
|
|
|
method: "PUT",
|
|
|
|
headers: {
|
|
|
|
Authorization: `Ghost ${this.token}`,
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
[what]: [post],
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return result[what][0];
|
|
|
|
}
|
2022-04-09 12:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function postToMarkdown(post: Post): string {
|
|
|
|
let text = mobileDocToMarkdown(post.mobiledoc);
|
2022-04-09 16:25:42 +00:00
|
|
|
return `# ${post.title}\n${text}`;
|
2022-04-09 12:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 16:25:42 +00:00
|
|
|
const postRegex = /#\s*([^\n]+)\n([^$]+)$/;
|
2022-04-09 12:28:41 +00:00
|
|
|
|
2022-04-09 12:57:59 +00:00
|
|
|
async function markdownToPost(text: string): Promise<Partial<Post>> {
|
2022-04-09 16:25:42 +00:00
|
|
|
let match = postRegex.exec(text);
|
2022-04-09 12:28:41 +00:00
|
|
|
if (match) {
|
|
|
|
let [, title, content] = match;
|
|
|
|
return {
|
|
|
|
title,
|
2022-04-09 12:57:59 +00:00
|
|
|
mobiledoc: markdownToMobileDoc(await cleanMarkdown(content)),
|
2022-04-09 12:28:41 +00:00
|
|
|
};
|
|
|
|
}
|
2022-04-09 16:25:42 +00:00
|
|
|
throw Error("Post should stat with a # header");
|
2022-04-09 12:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getConfig(): Promise<GhostConfig> {
|
2022-04-19 14:54:47 +00:00
|
|
|
let { text } = await readPage("ghost-config");
|
|
|
|
let parsedContent = await parseMarkdown(text);
|
|
|
|
let pageMeta = await extractMeta(parsedContent);
|
|
|
|
return pageMeta as GhostConfig;
|
2022-04-09 12:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function downloadAllPostsCommand() {
|
|
|
|
await invokeFunction("server", "downloadAllPosts");
|
|
|
|
}
|
2022-04-09 16:25:42 +00:00
|
|
|
|
2022-04-09 12:28:41 +00:00
|
|
|
export async function downloadAllPosts() {
|
|
|
|
let config = await getConfig();
|
|
|
|
let admin = new GhostAdmin(config.url, config.adminKey);
|
|
|
|
await admin.init();
|
|
|
|
let allPosts = await admin.listMarkdownPosts();
|
|
|
|
for (let post of allPosts) {
|
|
|
|
let text = mobileDocToMarkdown(post.mobiledoc);
|
2022-04-09 16:25:42 +00:00
|
|
|
text = `# ${post.title}\n${text}`;
|
|
|
|
await writePage(`${config.postPrefix}/${post.slug}`, text);
|
2022-04-09 12:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-09 16:25:42 +00:00
|
|
|
export async function publishCommand() {
|
2022-04-09 12:28:41 +00:00
|
|
|
await invokeFunction(
|
|
|
|
"server",
|
2022-04-09 16:25:42 +00:00
|
|
|
"publish",
|
2022-04-09 12:28:41 +00:00
|
|
|
await getCurrentPage(),
|
|
|
|
await getText()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-09 16:25:42 +00:00
|
|
|
export async function publish(name: string, text: string) {
|
2022-04-09 12:28:41 +00:00
|
|
|
let config = await getConfig();
|
|
|
|
let admin = new GhostAdmin(config.url, config.adminKey);
|
|
|
|
await admin.init();
|
2022-04-09 12:57:59 +00:00
|
|
|
let post = await markdownToPost(text);
|
2022-04-09 16:25:42 +00:00
|
|
|
if (name.startsWith(config.postPrefix)) {
|
|
|
|
post.slug = name.substring(config.postPrefix.length + 1);
|
|
|
|
await admin.publishPost(post);
|
|
|
|
console.log("Done!");
|
|
|
|
} else if (name.startsWith(config.pagePrefix)) {
|
|
|
|
post.slug = name.substring(config.pagePrefix.length + 1);
|
|
|
|
await admin.publishPage(post);
|
|
|
|
console.log("Done!");
|
2022-04-09 12:28:41 +00:00
|
|
|
} else {
|
2022-04-09 16:25:42 +00:00
|
|
|
console.error("Not in either the post or page prefix");
|
2022-04-09 12:28:41 +00:00
|
|
|
}
|
|
|
|
}
|