mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 16:35:57 +08:00
Fixed pull requests and Added api error alert
This commit is contained in:
@@ -88,7 +88,7 @@ if you don't want to use banner, leave it blank
|
||||
|
||||
[GitHub - obsidianmd/obsidian-api](https://github.com/obsidianmd/obsidian-api)
|
||||
|
||||
[GitHub - Easychris/obsidian-to-notion: Obsidian Weread Plugin is an plugin to sync Weread(微信读书) hightlights and annotations into your Obsidian Vault.](https://github.com/Easychris/obsidian-to-notion)
|
||||
[GitHub - Easychris/obsidian-to-notion: Obsidian Weread Plugin is an plugin to sync Weread(微信读书) hightlights and annotations into your Obsidian Vault.](https://github.dev/zhaohongxuan/obsidian-weread-plugin)
|
||||
|
||||
[GitHub - Quorafind/Obsidian-Memos: A quick capture plugin for Obsidian, all data from your notes.](https://github.com/Quorafind/Obsidian-Memos)
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { requestUrl } from "obsidian";
|
||||
import { Notice, requestUrl,TFile,normalizePath, App } from "obsidian";
|
||||
import { Client } from "@notionhq/client";
|
||||
import { markdownToBlocks, } from "@tryfabric/martian";
|
||||
import * as fs from "fs";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
import * as yaml from "yaml"
|
||||
import MyPlugin from "main";
|
||||
import { join } from "path";
|
||||
import { CLIENT_RENEG_LIMIT } from "tls";
|
||||
|
||||
export class Upload2Notion {
|
||||
app: MyPlugin;
|
||||
@@ -25,7 +27,6 @@ export class Upload2Notion {
|
||||
},
|
||||
body: ''
|
||||
})
|
||||
console.log(response)
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -65,44 +66,52 @@ export class Upload2Notion {
|
||||
}
|
||||
}
|
||||
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/pages`,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
// 'User-Agent': 'obsidian.md',
|
||||
'Authorization': 'Bearer ' + this.app.settings.notionAPI,
|
||||
'Notion-Version': '2021-08-16',
|
||||
},
|
||||
body: JSON.stringify(bodyString),
|
||||
})
|
||||
return response;
|
||||
try {
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/pages`,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
// 'User-Agent': 'obsidian.md',
|
||||
'Authorization': 'Bearer ' + this.app.settings.notionAPI,
|
||||
'Notion-Version': '2021-08-16',
|
||||
},
|
||||
body: JSON.stringify(bodyString),
|
||||
})
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
async syncMarkdownToNotion(title:string, markdown: string, fullPath:string): Promise<any> {
|
||||
async syncMarkdownToNotion(title:string, markdown: string, nowFile: TFile, app:App): Promise<any> {
|
||||
let res:any
|
||||
const yamlObj:any = yamlFrontMatter.loadFront(markdown);
|
||||
console.log(yamlObj)
|
||||
const __content = yamlObj.__content
|
||||
const file2Block = markdownToBlocks(__content);
|
||||
const {notionID} = yamlObj;
|
||||
const file2Block = markdownToBlocks(markdown);
|
||||
const frontmasster =await app.metadataCache.getFileCache(nowFile)?.frontmatter
|
||||
const notionID = frontmasster ? frontmasster.notionId : null
|
||||
|
||||
if(notionID){
|
||||
res = await this.updatePage(notionID, title, file2Block);
|
||||
} else {
|
||||
res = await this.createPage(title, file2Block);
|
||||
}
|
||||
console.log(res,'===')
|
||||
if (res.status === 200) {
|
||||
await this.updateYamlInfo(markdown, fullPath, res)
|
||||
await this.updateYamlInfo(markdown, nowFile, res, app)
|
||||
} else {
|
||||
new Notice(`${res.text}`)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
async updateYamlInfo(yamlContent: string, fullPath: string, res: any) {
|
||||
async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any,app:App) {
|
||||
const yamlObj:any = yamlFrontMatter.loadFront(yamlContent);
|
||||
const {url, id} = res.json
|
||||
yamlObj.link = url;
|
||||
await navigator.clipboard.writeText(url)
|
||||
try {
|
||||
await navigator.clipboard.writeText(url)
|
||||
} catch (error) {
|
||||
new Notice(`复制链接失败,请手动复制${error}`)
|
||||
}
|
||||
yamlObj.notionID = id;
|
||||
const __content = yamlObj.__content;
|
||||
delete yamlObj.__content
|
||||
@@ -112,9 +121,17 @@ export class Upload2Notion {
|
||||
// if __content have start \n remove it
|
||||
const __content_remove_n = __content.replace(/^\n/, '')
|
||||
const content = '---\n' +yamlhead_remove_n +'\n---\n' + __content_remove_n;
|
||||
|
||||
const fullPath = this.getFilePath(nowFile);
|
||||
//write content fo file
|
||||
fs.writeFileSync(fullPath, content);
|
||||
return res
|
||||
console.log(res)
|
||||
}
|
||||
|
||||
|
||||
getFilePath(file: TFile): string {
|
||||
const basePath: string = file.vault.adapter.getBasePath();
|
||||
const filePath: string = file.path;
|
||||
const fullPath = normalizePath(join(basePath, filePath));
|
||||
return fullPath;
|
||||
}
|
||||
}
|
||||
|
||||
61
main.ts
61
main.ts
@@ -18,7 +18,7 @@ import {NoticeMConfig} from "Message";
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
interface MyPluginSettings {
|
||||
interface PluginSettings {
|
||||
notionAPI: string;
|
||||
databaseID: string;
|
||||
bannerUrl: string;
|
||||
@@ -26,7 +26,7 @@ interface MyPluginSettings {
|
||||
langConfig: any;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: MyPluginSettings = {
|
||||
const DEFAULT_SETTINGS: PluginSettings = {
|
||||
notionAPI: "",
|
||||
databaseID: "",
|
||||
bannerUrl: "",
|
||||
@@ -34,8 +34,8 @@ const DEFAULT_SETTINGS: MyPluginSettings = {
|
||||
langConfig: NoticeMConfig( window.localStorage.getItem('language') || 'en')
|
||||
};
|
||||
|
||||
export default class MyPlugin extends Plugin {
|
||||
settings: MyPluginSettings;
|
||||
export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
settings: PluginSettings;
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
addIcons();
|
||||
@@ -48,8 +48,6 @@ export default class MyPlugin extends Plugin {
|
||||
this.upload();
|
||||
}
|
||||
);
|
||||
// Perform additional things with the ribbon
|
||||
ribbonIconEl.addClass("my-plugin-ribbon-class");
|
||||
|
||||
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
|
||||
const statusBarItemEl = this.addStatusBarItem();
|
||||
@@ -79,12 +77,14 @@ export default class MyPlugin extends Plugin {
|
||||
);
|
||||
return;
|
||||
}
|
||||
const { nowFile, fileData,fullPath } =
|
||||
await this.getNowFileMarkdwonContent(this.app);
|
||||
const { basename } = nowFile;
|
||||
if (fileData) {
|
||||
const { markDownData, nowFile } =
|
||||
await this.getNowFileMarkdownContent(this.app);
|
||||
|
||||
|
||||
if (markDownData) {
|
||||
const { basename } = nowFile;
|
||||
const upload = new Upload2Notion(this);
|
||||
const res = await upload.syncMarkdownToNotion(basename,fileData, fullPath)
|
||||
const res = await upload.syncMarkdownToNotion(basename, markDownData,nowFile, this.app)
|
||||
console.log(res)
|
||||
if(res.status === 200){
|
||||
new Notice(`${this.settings.langConfig["sync-success"]}${basename}`)
|
||||
@@ -94,19 +94,13 @@ export default class MyPlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
async getNowFileMarkdwonContent(app: App) {
|
||||
async getNowFileMarkdownContent(app: App) {
|
||||
const nowFile = app.workspace.getActiveFile();
|
||||
if (nowFile) {
|
||||
const filePath: string = nowFile.path;
|
||||
// @ts-ignore
|
||||
const basePath: string = nowFile.vault.adapter.basePath;
|
||||
const fullPath = normalizePath(join(basePath, filePath));
|
||||
console.log("fullpath", fullPath);
|
||||
const fileData = fs.readFileSync(fullPath, "utf8");
|
||||
const markDownData = await nowFile.vault.read(nowFile);
|
||||
return {
|
||||
fileData,
|
||||
markDownData,
|
||||
nowFile,
|
||||
fullPath,
|
||||
};
|
||||
} else {
|
||||
new Notice(this.settings.langConfig["open-file"]);
|
||||
@@ -127,26 +121,10 @@ export default class MyPlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
class SampleModal extends Modal {
|
||||
constructor(app: App) {
|
||||
super(app);
|
||||
}
|
||||
|
||||
onOpen() {
|
||||
const { contentEl } = this;
|
||||
contentEl.setText("Woah!");
|
||||
}
|
||||
|
||||
onClose() {
|
||||
const { contentEl } = this;
|
||||
contentEl.empty();
|
||||
}
|
||||
}
|
||||
|
||||
class SampleSettingTab extends PluginSettingTab {
|
||||
plugin: MyPlugin;
|
||||
plugin: ObsidianSyncNotionPlugin;
|
||||
|
||||
constructor(app: App, plugin: MyPlugin) {
|
||||
constructor(app: App, plugin: ObsidianSyncNotionPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
@@ -160,7 +138,7 @@ class SampleSettingTab extends PluginSettingTab {
|
||||
text: "Settings for obsidian to notion plugin.",
|
||||
});
|
||||
|
||||
new Setting(containerEl)
|
||||
const notionApiKye = new Setting(containerEl)
|
||||
.setName("Notion API Token")
|
||||
.setDesc("It's a secret")
|
||||
.addText((text) =>
|
||||
@@ -172,8 +150,9 @@ class SampleSettingTab extends PluginSettingTab {
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
notionApiKye.controlEl.querySelector('input').type='password'
|
||||
|
||||
new Setting(containerEl)
|
||||
const notionDatabaseID = new Setting(containerEl)
|
||||
.setName("Database ID")
|
||||
.setDesc("It's a secret")
|
||||
.addText((text) =>
|
||||
@@ -186,6 +165,8 @@ class SampleSettingTab extends PluginSettingTab {
|
||||
})
|
||||
);
|
||||
|
||||
notionDatabaseID.controlEl.querySelector('input').type='password'
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName("Banner url(optional)")
|
||||
.setDesc("page banner url(optional), default is empty, if you want to show a banner, please enter the url(like:https://raw.githubusercontent.com/EasyChris/obsidian-to-notion/ae7a9ac6cf427f3ca338a409ce6967ced9506f12/doc/2.png)")
|
||||
|
||||
Reference in New Issue
Block a user