Compare commits

..

2 Commits
0.0.6 ... 0.0.8

Author SHA1 Message Date
Jiaxin Peng
2434ddd532 fix the notification bug 2023-08-10 23:20:26 +01:00
Jiaxin Peng
55804e491a modify the cover image url setting 2023-08-10 22:50:48 +01:00
4 changed files with 66 additions and 52 deletions

View File

@@ -1,9 +1,10 @@
import { Notice, requestUrl,TFile,normalizePath, App } from "obsidian"; import {App, Notice, requestUrl, TFile} from "obsidian";
import { Client } from "@notionhq/client"; import {Client} from "@notionhq/client";
import { markdownToBlocks, } from "@tryfabric/martian"; import {markdownToBlocks,} from "@tryfabric/martian";
import * as yamlFrontMatter from "yaml-front-matter"; import * as yamlFrontMatter from "yaml-front-matter";
import * as yaml from "yaml" import * as yaml from "yaml"
import MyPlugin from "main"; import MyPlugin from "main";
export class Upload2Notion { export class Upload2Notion {
app: MyPlugin; app: MyPlugin;
notion: Client; notion: Client;
@@ -13,7 +14,7 @@ export class Upload2Notion {
} }
async deletePage(notionID:string){ async deletePage(notionID:string){
const response = await requestUrl({ return await requestUrl({
url: `https://api.notion.com/v1/blocks/${notionID}`, url: `https://api.notion.com/v1/blocks/${notionID}`,
method: 'DELETE', method: 'DELETE',
headers: { headers: {
@@ -23,15 +24,39 @@ export class Upload2Notion {
}, },
body: '' body: ''
}) })
return response; }
async getDataBase(databaseID:string){
const response = await requestUrl({
url: `https://api.notion.com/v1/databases/${databaseID}`,
method: 'GET',
headers: {
'Authorization': 'Bearer ' + this.app.settings.notionAPI,
'Notion-Version': '2022-06-28',
}
}
)
// Check if cover is present in the JSON response and then get the URL
if (response.json.cover && response.json.cover.external) {
return response.json.cover.external.url;
} else {
return null; // or some other default value, if you prefer
}
} }
// 因为需要解析notion的block进行对比非常的麻烦 // 因为需要解析notion的block进行对比非常的麻烦
// 暂时就直接删除新建一个page // 暂时就直接删除新建一个page
async updatePage(notionID:string, title:string, allowTags:boolean, emoji:string, cover:string, tags:string[], type:string, slug:string, stats:string, category:string, summary:string, paword:string, favicon:string, datetime:string, childArr:any) { async updatePage(notionID:string, title:string, allowTags:boolean, emoji:string, cover:string, tags:string[], type:string, slug:string, stats:string, category:string, summary:string, paword:string, favicon:string, datetime:string, childArr:any) {
await this.deletePage(notionID) await this.deletePage(notionID)
const res = await this.createPage(title, allowTags, emoji,cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, childArr)
return res const databasecover = await this.getDataBase(this.app.settings.databaseID)
if (cover == null) {
cover = databasecover
}
return await this.createPage(title, allowTags, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, childArr)
} }
async createPage(title:string, allowTags:boolean, emoji:string, cover:string, tags:string[], type:string, slug:string, stats:string, category:string, summary:string, pawrod:string, favicon:string, datetime:string, childArr: any) { async createPage(title:string, allowTags:boolean, emoji:string, cover:string, tags:string[], type:string, slug:string, stats:string, category:string, summary:string, pawrod:string, favicon:string, datetime:string, childArr: any) {
@@ -42,11 +67,6 @@ export class Upload2Notion {
icon: { icon: {
emoji: emoji || '📜' emoji: emoji || '📜'
}, },
cover: {
external: {
url: cover || ''
}
},
properties: { properties: {
title: { title: {
title: [ title: [
@@ -121,8 +141,16 @@ export class Upload2Notion {
}, },
children: childArr, children: childArr,
} }
if (cover) {
bodyString.cover = {
type: "external",
external: {
url: cover
}
}
}
if(this.app.settings.bannerUrl) { if (!bodyString.cover && this.app.settings.bannerUrl) {
bodyString.cover = { bodyString.cover = {
type: "external", type: "external",
external: { external: {
@@ -132,7 +160,7 @@ export class Upload2Notion {
} }
try { try {
const response = await requestUrl({ return await requestUrl({
url: `https://api.notion.com/v1/pages`, url: `https://api.notion.com/v1/pages`,
method: 'POST', method: 'POST',
headers: { headers: {
@@ -143,7 +171,6 @@ export class Upload2Notion {
}, },
body: JSON.stringify(bodyString), body: JSON.stringify(bodyString),
}) })
return response;
} catch (error) { } catch (error) {
new Notice(`network error ${error}`) new Notice(`network error ${error}`)
} }

29
main.ts
View File

@@ -1,18 +1,7 @@
import { import {App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting} from "obsidian";
App,
Editor,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
normalizePath
} from "obsidian";
import {addIcons} from 'icon'; import {addIcons} from 'icon';
import { Upload2Notion } from "Upload2Notion"; import {Upload2Notion} from "Upload2Notion";
import {NoticeMConfig} from "Message"; import {NoticeMConfig} from "Message";
import { CLIENT_RENEG_LIMIT } from "tls";
// Remember to rename these classes and interfaces! // Remember to rename these classes and interfaces!
@@ -48,7 +37,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
"Share to NotionNext", "Share to NotionNext",
async (evt: MouseEvent) => { async (evt: MouseEvent) => {
// Called when the user clicks the icon. // Called when the user clicks the icon.
this.upload(); await this.upload();
} }
); );
@@ -60,7 +49,7 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
id: "share-to-notionnext", id: "share-to-notionnext",
name: "share to notionnext", name: "share to notionnext",
editorCallback: async (editor: Editor, view: MarkdownView) => { editorCallback: async (editor: Editor, view: MarkdownView) => {
this.upload() await this.upload()
}, },
}); });
@@ -184,15 +173,14 @@ class SampleSettingTab extends PluginSettingTab {
.setName("Notion API Token") .setName("Notion API Token")
.setDesc("It's a secret") .setDesc("It's a secret")
.addText((text) =>{ .addText((text) =>{
let t = text // t.inputEl.type = 'password'
return text
.setPlaceholder("Enter your Notion API Token") .setPlaceholder("Enter your Notion API Token")
.setValue(this.plugin.settings.notionAPI) .setValue(this.plugin.settings.notionAPI)
.onChange(async (value) => { .onChange(async (value) => {
this.plugin.settings.notionAPI = value; this.plugin.settings.notionAPI = value;
await this.plugin.saveSettings(); await this.plugin.saveSettings();
}) })
// t.inputEl.type = 'password'
return t
}); });
@@ -200,15 +188,14 @@ class SampleSettingTab extends PluginSettingTab {
.setName("Database ID") .setName("Database ID")
.setDesc("It's a secret") .setDesc("It's a secret")
.addText((text) => { .addText((text) => {
let t = text // t.inputEl.type = 'password'
return text
.setPlaceholder("Enter your Database ID") .setPlaceholder("Enter your Database ID")
.setValue(this.plugin.settings.databaseID) .setValue(this.plugin.settings.databaseID)
.onChange(async (value) => { .onChange(async (value) => {
this.plugin.settings.databaseID = value; this.plugin.settings.databaseID = value;
await this.plugin.saveSettings(); await this.plugin.saveSettings();
}) })
// t.inputEl.type = 'password'
return t
} }
); );

View File

@@ -1,7 +1,7 @@
{ {
"id": "share-to-notionnext", "id": "share-to-notionnext",
"name": "Share to NotionNext", "name": "Share to NotionNext",
"version": "0.0.6", "version": "0.0.8",
"minAppVersion": "0.0.1", "minAppVersion": "0.0.1",
"description": "This is a plugin for Obsidian. This plugin shares obsidian md file to notion with notion api for NotionNext web deploy.", "description": "This is a plugin for Obsidian. This plugin shares obsidian md file to notion with notion api for NotionNext web deploy.",
"author": "jxpeng98", "author": "jxpeng98",

View File

@@ -1,6 +1,6 @@
{ {
"name": "share-to-notionnext", "name": "share-to-notionnext",
"version": "0.0.6", "version": "0.0.8",
"type": "module", "type": "module",
"description": "This is a plugin for Obsidian. This plugin share obsidian md file to notion with notion api for NotionNext web deploy.", "description": "This is a plugin for Obsidian. This plugin share obsidian md file to notion with notion api for NotionNext web deploy.",
"main": "main.js", "main": "main.js",