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 - 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)
|
[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 { Client } from "@notionhq/client";
|
||||||
import { markdownToBlocks, } from "@tryfabric/martian";
|
import { markdownToBlocks, } from "@tryfabric/martian";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
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";
|
||||||
|
import { join } from "path";
|
||||||
|
import { CLIENT_RENEG_LIMIT } from "tls";
|
||||||
|
|
||||||
export class Upload2Notion {
|
export class Upload2Notion {
|
||||||
app: MyPlugin;
|
app: MyPlugin;
|
||||||
@@ -25,7 +27,6 @@ export class Upload2Notion {
|
|||||||
},
|
},
|
||||||
body: ''
|
body: ''
|
||||||
})
|
})
|
||||||
console.log(response)
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +66,7 @@ export class Upload2Notion {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const response = await requestUrl({
|
const response = await requestUrl({
|
||||||
url: `https://api.notion.com/v1/pages`,
|
url: `https://api.notion.com/v1/pages`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@@ -77,32 +79,39 @@ export class Upload2Notion {
|
|||||||
body: JSON.stringify(bodyString),
|
body: JSON.stringify(bodyString),
|
||||||
})
|
})
|
||||||
return response;
|
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
|
let res:any
|
||||||
const yamlObj:any = yamlFrontMatter.loadFront(markdown);
|
const file2Block = markdownToBlocks(markdown);
|
||||||
console.log(yamlObj)
|
const frontmasster =await app.metadataCache.getFileCache(nowFile)?.frontmatter
|
||||||
const __content = yamlObj.__content
|
const notionID = frontmasster ? frontmasster.notionId : null
|
||||||
const file2Block = markdownToBlocks(__content);
|
|
||||||
const {notionID} = yamlObj;
|
|
||||||
if(notionID){
|
if(notionID){
|
||||||
res = await this.updatePage(notionID, title, file2Block);
|
res = await this.updatePage(notionID, title, file2Block);
|
||||||
} else {
|
} else {
|
||||||
res = await this.createPage(title, file2Block);
|
res = await this.createPage(title, file2Block);
|
||||||
}
|
}
|
||||||
console.log(res,'===')
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
await this.updateYamlInfo(markdown, fullPath, res)
|
await this.updateYamlInfo(markdown, nowFile, res, app)
|
||||||
|
} else {
|
||||||
|
new Notice(`${res.text}`)
|
||||||
}
|
}
|
||||||
return res
|
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 yamlObj:any = yamlFrontMatter.loadFront(yamlContent);
|
||||||
const {url, id} = res.json
|
const {url, id} = res.json
|
||||||
yamlObj.link = url;
|
yamlObj.link = url;
|
||||||
|
try {
|
||||||
await navigator.clipboard.writeText(url)
|
await navigator.clipboard.writeText(url)
|
||||||
|
} catch (error) {
|
||||||
|
new Notice(`复制链接失败,请手动复制${error}`)
|
||||||
|
}
|
||||||
yamlObj.notionID = id;
|
yamlObj.notionID = id;
|
||||||
const __content = yamlObj.__content;
|
const __content = yamlObj.__content;
|
||||||
delete yamlObj.__content
|
delete yamlObj.__content
|
||||||
@@ -112,9 +121,17 @@ export class Upload2Notion {
|
|||||||
// if __content have start \n remove it
|
// if __content have start \n remove it
|
||||||
const __content_remove_n = __content.replace(/^\n/, '')
|
const __content_remove_n = __content.replace(/^\n/, '')
|
||||||
const content = '---\n' +yamlhead_remove_n +'\n---\n' + __content_remove_n;
|
const content = '---\n' +yamlhead_remove_n +'\n---\n' + __content_remove_n;
|
||||||
|
const fullPath = this.getFilePath(nowFile);
|
||||||
//write content fo file
|
//write content fo file
|
||||||
fs.writeFileSync(fullPath, content);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
59
main.ts
59
main.ts
@@ -18,7 +18,7 @@ import {NoticeMConfig} from "Message";
|
|||||||
|
|
||||||
// Remember to rename these classes and interfaces!
|
// Remember to rename these classes and interfaces!
|
||||||
|
|
||||||
interface MyPluginSettings {
|
interface PluginSettings {
|
||||||
notionAPI: string;
|
notionAPI: string;
|
||||||
databaseID: string;
|
databaseID: string;
|
||||||
bannerUrl: string;
|
bannerUrl: string;
|
||||||
@@ -26,7 +26,7 @@ interface MyPluginSettings {
|
|||||||
langConfig: any;
|
langConfig: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_SETTINGS: MyPluginSettings = {
|
const DEFAULT_SETTINGS: PluginSettings = {
|
||||||
notionAPI: "",
|
notionAPI: "",
|
||||||
databaseID: "",
|
databaseID: "",
|
||||||
bannerUrl: "",
|
bannerUrl: "",
|
||||||
@@ -34,8 +34,8 @@ const DEFAULT_SETTINGS: MyPluginSettings = {
|
|||||||
langConfig: NoticeMConfig( window.localStorage.getItem('language') || 'en')
|
langConfig: NoticeMConfig( window.localStorage.getItem('language') || 'en')
|
||||||
};
|
};
|
||||||
|
|
||||||
export default class MyPlugin extends Plugin {
|
export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||||
settings: MyPluginSettings;
|
settings: PluginSettings;
|
||||||
async onload() {
|
async onload() {
|
||||||
await this.loadSettings();
|
await this.loadSettings();
|
||||||
addIcons();
|
addIcons();
|
||||||
@@ -48,8 +48,6 @@ export default class MyPlugin extends Plugin {
|
|||||||
this.upload();
|
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.
|
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
|
||||||
const statusBarItemEl = this.addStatusBarItem();
|
const statusBarItemEl = this.addStatusBarItem();
|
||||||
@@ -79,12 +77,14 @@ export default class MyPlugin extends Plugin {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { nowFile, fileData,fullPath } =
|
const { markDownData, nowFile } =
|
||||||
await this.getNowFileMarkdwonContent(this.app);
|
await this.getNowFileMarkdownContent(this.app);
|
||||||
|
|
||||||
|
|
||||||
|
if (markDownData) {
|
||||||
const { basename } = nowFile;
|
const { basename } = nowFile;
|
||||||
if (fileData) {
|
|
||||||
const upload = new Upload2Notion(this);
|
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)
|
console.log(res)
|
||||||
if(res.status === 200){
|
if(res.status === 200){
|
||||||
new Notice(`${this.settings.langConfig["sync-success"]}${basename}`)
|
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();
|
const nowFile = app.workspace.getActiveFile();
|
||||||
if (nowFile) {
|
if (nowFile) {
|
||||||
const filePath: string = nowFile.path;
|
const markDownData = await nowFile.vault.read(nowFile);
|
||||||
// @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");
|
|
||||||
return {
|
return {
|
||||||
fileData,
|
markDownData,
|
||||||
nowFile,
|
nowFile,
|
||||||
fullPath,
|
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
new Notice(this.settings.langConfig["open-file"]);
|
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 {
|
class SampleSettingTab extends PluginSettingTab {
|
||||||
plugin: MyPlugin;
|
plugin: ObsidianSyncNotionPlugin;
|
||||||
|
|
||||||
constructor(app: App, plugin: MyPlugin) {
|
constructor(app: App, plugin: ObsidianSyncNotionPlugin) {
|
||||||
super(app, plugin);
|
super(app, plugin);
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
}
|
}
|
||||||
@@ -160,7 +138,7 @@ class SampleSettingTab extends PluginSettingTab {
|
|||||||
text: "Settings for obsidian to notion plugin.",
|
text: "Settings for obsidian to notion plugin.",
|
||||||
});
|
});
|
||||||
|
|
||||||
new Setting(containerEl)
|
const notionApiKye = new Setting(containerEl)
|
||||||
.setName("Notion API Token")
|
.setName("Notion API Token")
|
||||||
.setDesc("It's a secret")
|
.setDesc("It's a secret")
|
||||||
.addText((text) =>
|
.addText((text) =>
|
||||||
@@ -172,8 +150,9 @@ class SampleSettingTab extends PluginSettingTab {
|
|||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
notionApiKye.controlEl.querySelector('input').type='password'
|
||||||
|
|
||||||
new Setting(containerEl)
|
const notionDatabaseID = new Setting(containerEl)
|
||||||
.setName("Database ID")
|
.setName("Database ID")
|
||||||
.setDesc("It's a secret")
|
.setDesc("It's a secret")
|
||||||
.addText((text) =>
|
.addText((text) =>
|
||||||
@@ -186,6 +165,8 @@ class SampleSettingTab extends PluginSettingTab {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
notionDatabaseID.controlEl.querySelector('input').type='password'
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName("Banner url(optional)")
|
.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)")
|
.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