1
0
silverbullet/packages/plugs/ghost/ghost.ts

221 lines
5.4 KiB
TypeScript
Raw Normal View History

2022-06-28 12:14:15 +00:00
import { readPage } from "@silverbulletmd/plugos-silverbullet-syscall/space";
2022-04-25 09:24:13 +00:00
import { invokeFunction } from "@silverbulletmd/plugos-silverbullet-syscall/system";
2022-04-25 08:33:38 +00:00
import {
getCurrentPage,
getText,
2022-04-25 09:24:13 +00:00
} from "@silverbulletmd/plugos-silverbullet-syscall/editor";
import { cleanMarkdown } from "../markdown/util";
2022-04-25 09:24:13 +00:00
import { parseMarkdown } from "@silverbulletmd/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 fetch(
2022-04-09 12:28:41 +00:00
`${this.url}/ghost/api/v3/admin/posts?order=published_at+DESC`,
{
headers: {
Authorization: `Ghost ${this.token}`,
},
}
);
return (await result.json()).posts;
2022-04-09 12:28:41 +00:00
}
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 oldPostQueryR = await fetch(
2022-04-09 16:25:42 +00:00
`${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",
},
}
);
let oldPostQuery = await oldPostQueryR.json();
2022-04-09 16:25:42 +00:00
if (!oldPostQuery[what]) {
// New!
if (!post.status) {
post.status = "draft";
}
let result = await fetch(`${this.url}/ghost/api/v3/admin/${what}`, {
2022-04-09 16:25:42 +00:00
method: "POST",
headers: {
Authorization: `Ghost ${this.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
[what]: [post],
}),
});
return (await result.json())[what][0];
2022-04-09 16:25:42 +00:00
} else {
let oldPost: Post = oldPostQuery[what][0];
post.updated_at = oldPost.updated_at;
let result = await fetch(
2022-04-09 16:25:42 +00:00
`${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 (await result.json())[what][0];
2022-04-09 16:25:42 +00:00
}
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> {
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
}
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
}
}