mirror of
https://github.com/d0zingcat/obsidian-send-to-ghost.git
synced 2026-06-06 23:26:44 +00:00
use frontmatter as a setting
This commit is contained in:
@@ -1,67 +1,78 @@
|
||||
import { SettingsProp, ViewProp, ContentProp } from './../types/index';
|
||||
import { SettingsProp, ViewProp, ContentProp } from "./../types/index";
|
||||
import { Notice, request } from "obsidian";
|
||||
|
||||
import { sign } from 'jsonwebtoken';
|
||||
const MarkdownIt = require("markdown-it")
|
||||
import { sign } from "jsonwebtoken";
|
||||
|
||||
const md = new MarkdownIt()
|
||||
const matter = require("gray-matter");
|
||||
const MarkdownIt = require("markdown-it");
|
||||
|
||||
const md = new MarkdownIt();
|
||||
|
||||
export const publishPost = async (view: ViewProp, settings: SettingsProp) => {
|
||||
const version = "v4";
|
||||
|
||||
const content = {
|
||||
title: view.file.basename,
|
||||
data: view.data
|
||||
}
|
||||
// Admin API key goes here
|
||||
const key = settings.adminToken;
|
||||
|
||||
const version = 'v4'
|
||||
// Split the key into ID and SECRET
|
||||
const [id, secret] = key.split(":");
|
||||
|
||||
// Admin API key goes here
|
||||
const key = settings.adminToken;
|
||||
// Create the token (including decoding secret)
|
||||
const token = sign({}, Buffer.from(secret, "hex"), {
|
||||
keyid: id,
|
||||
algorithm: "HS256",
|
||||
expiresIn: "5m",
|
||||
audience: `/${version}/admin/`,
|
||||
});
|
||||
|
||||
// Split the key into ID and SECRET
|
||||
const [id, secret] = key.split(':');
|
||||
// get frontmatter
|
||||
const m = matter(`${view.data}`);
|
||||
|
||||
// Create the token (including decoding secret)
|
||||
const token = sign({}, Buffer.from(secret, 'hex'), {
|
||||
keyid: id,
|
||||
algorithm: 'HS256',
|
||||
expiresIn: '5m',
|
||||
audience: `/${version}/admin/`
|
||||
});
|
||||
const frontmatter = {
|
||||
title: m.data.title || view.file.basename,
|
||||
tags: m.data.tags || undefined,
|
||||
featured: m.data.featured || false,
|
||||
status: m.data.status || "draft",
|
||||
excerpt: m.data.excerpt || undefined,
|
||||
feature_image: m.data.feature_image || undefined,
|
||||
};
|
||||
|
||||
const contentPost = (content: ContentProp, settings: SettingsProp) => ({
|
||||
"posts": [{
|
||||
"title": content.title,
|
||||
"html": md.render(content.data),
|
||||
"status": settings.publishStatus
|
||||
}]
|
||||
})
|
||||
const contentPost = (frontmatter: ContentProp) => ({
|
||||
posts: [
|
||||
{
|
||||
...frontmatter,
|
||||
html: md.render(m.content),
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const body = contentPost(content, settings);
|
||||
const body = contentPost(frontmatter);
|
||||
|
||||
const result = await request({
|
||||
url: `${settings.url}/ghost/api/${version}/admin/posts/?source=html`,
|
||||
method: "POST",
|
||||
contentType: "application/json",
|
||||
headers: {
|
||||
"Access-Control-Allow-Origin": "app://obsidian.md",
|
||||
"Access-Control-Allow-Methods": "POST",
|
||||
"Content-Type": "application/json;charset=utf-8",
|
||||
Authorization: `Ghost ${token}`,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
const result = await request({
|
||||
url: `${settings.url}/ghost/api/${version}/admin/posts/?source=html`,
|
||||
method: "POST",
|
||||
contentType: "application/json",
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': 'app://obsidian.md',
|
||||
'Access-Control-Allow-Methods': 'POST',
|
||||
'Content-Type': 'application/json;charset=utf-8',
|
||||
'Authorization': `Ghost ${token}`
|
||||
},
|
||||
body: JSON.stringify(body)
|
||||
})
|
||||
const json = JSON.parse(result);
|
||||
|
||||
const json = JSON.parse(result)
|
||||
if (json?.posts) {
|
||||
new Notice(
|
||||
`"${json?.posts?.[0]?.title}" has been ${json?.posts?.[0]?.status} successful!`
|
||||
);
|
||||
} else {
|
||||
new Notice(`${json.errors[0].context || json.errors[0].message}`);
|
||||
new Notice(
|
||||
`${json.errors[0]?.details[0].message} - ${json.errors[0]?.details[0].params.allowedValues}`
|
||||
);
|
||||
}
|
||||
|
||||
if (json?.errors) {
|
||||
new Notice(`${json.errors[0].type}! ${json.errors[0].message}`)
|
||||
}
|
||||
|
||||
if (json?.posts) {
|
||||
new Notice(`"${json?.posts?.[0]?.title}" has been ${json?.posts?.[0]?.status} successful!`)
|
||||
}
|
||||
|
||||
return json
|
||||
}
|
||||
return json;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user