version 1 done

This commit is contained in:
Jay Nguyen
2022-04-10 16:45:09 +01:00
parent 3d2042ab98
commit 1648517c08
9 changed files with 204 additions and 158 deletions

45
src/main.ts Normal file
View File

@@ -0,0 +1,45 @@
import { MarkdownView, Plugin } from 'obsidian';
import { DEFAULT_SETTINGS, SettingsProp } from './types/index';
import { SettingTab } from './settingTab'
import { publishPost } from './methods/publishPost';
export default class MyPlugin extends Plugin {
settings: SettingsProp;
async onload() {
// load the settings first
await this.loadSettings();
// 2 ways to publish post:
console.log('this.settings', this.settings)
// 1. Click on the ghost icon on the left
this.addRibbonIcon('ghost', 'Publish Ghost', () => {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
publishPost(view, this.settings)
});
// 2. Run the by command + P
this.addCommand({
id: 'publish',
name: 'Publish current document',
editorCallback: (_, view: MarkdownView) => {
publishPost(view, this.settings)
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SettingTab(this.app, this));
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
}
onunload() { }
async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()) }
async saveSettings() { await this.saveData(this.settings) }
}

View File

@@ -0,0 +1,71 @@
import { Notice, request } from "obsidian";
import { sign } from 'jsonwebtoken';
const MarkdownIt = require("markdown-it")
const md = new MarkdownIt()
export const publishPost = async (view: any, settings: any) => {
const content = {
title: view.file.basename,
data: view.data
}
const version = 'v4'
console.log('content', content)
console.log('settings', settings)
// Admin API key goes here
const key = settings.adminToken;
// Split the key into ID and SECRET
const [id, secret] = key.split(':');
// Create the token (including decoding secret)
const token = sign({}, Buffer.from(secret, 'hex'), {
keyid: id,
algorithm: 'HS256',
expiresIn: '5m',
audience: `/${version}/admin/`
});
const contentPost = (content: any, settings: any) => ({
"posts": [{
"title": content.title,
"html": md.render(content.data),
"status": settings.publishStatus
}]
})
const body = contentPost(content, settings);
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)
if (json?.errors) {
new Notice(`${json.errors[0].type}! ${json.errors[0].message}`)
}
if (json?.posts) {
new Notice(`${json.posts[0].settings.title} has been ${json.posts[0].settings.status} successful!`)
}
return result
}

51
src/settingTab/index.ts Normal file
View File

@@ -0,0 +1,51 @@
import { App, PluginSettingTab, Setting } from 'obsidian';
import MyPlugin from 'src/main';
export class SettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Obsidian Ghost Integration' });
new Setting(containerEl)
.setName('API URL')
.addText(text => text
.setPlaceholder('nguyens.co')
.setValue(this.plugin.settings.url)
.onChange(async (value) => {
console.log('Blog URL: ' + value);
this.plugin.settings.url = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Admin API Key')
.addText(text => text
.setPlaceholder('6251555c94ca6')
.setValue(this.plugin.settings.adminToken)
.onChange(async (value) => {
console.log('admin api key: ' + value);
this.plugin.settings.adminToken = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Post publish status')
.addDropdown(dropdown => dropdown
.addOption('draft', 'Draft')
.addOption('published', 'Publish')
.setValue(this.plugin.settings.publishStatus)
.onChange(async (value) => {
this.plugin.settings.publishStatus = value;
await this.plugin.saveSettings();
}))
}
}

12
src/types/index.ts Normal file
View File

@@ -0,0 +1,12 @@
export interface SettingsProp {
url: string,
adminToken: string,
publishStatus: string
}
export const DEFAULT_SETTINGS: SettingsProp = {
url: '',
adminToken: '',
publishStatus: 'draft'
}