diff --git a/README.md b/README.md index b62bd9a..9354391 100644 --- a/README.md +++ b/README.md @@ -67,7 +67,9 @@ A share link will be automatically generated after successful upload - [ ] support for mobile - [x] support for custom page banner - [ ] update the exsit page -- [ ] transfer the bi-link format like [[]] into the format that Notion supports. +- [ ] transfer the bi-link format like [[]] into the format that Notion supports. +- [ ] support for mult language +- [ ] support for auto copy the share link to clipboard # Buy me a cup of coffee. diff --git a/Upload2Notion.ts b/Upload2Notion.ts index e10949c..da94fad 100644 --- a/Upload2Notion.ts +++ b/Upload2Notion.ts @@ -13,6 +13,30 @@ export class Upload2Notion { constructor(app: MyPlugin) { this.app = app; } + + async deletePage(notionID:string){ + const response = await requestUrl({ + url: `https://api.notion.com/v1/blocks/${notionID}`, + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + this.app.settings.notionAPI, + 'Notion-Version': '2022-02-22', + }, + body: '' + }) + console.log(response) + return response; + } + + // 因为需要解析notion的block进行对比,非常的麻烦, + // 暂时就直接删除,新建一个page + async updatePage(notionID:string, title:string, childArr:any) { + await this.deletePage(notionID) + const res = await this.createPage(title, childArr) + return res + } + async createPage(title:string, childArr: any) { const bodyString:any = { parent: { @@ -56,10 +80,18 @@ export class Upload2Notion { } async syncMarkdownToNotion(title:string, markdown: string, fullPath:string): Promise { + let res:any const yamlObj:any = yamlFrontMatter.loadFront(markdown); + console.log(yamlObj) const __content = yamlObj.__content - const file2md = markdownToBlocks(__content); - const res = await this.createPage(title, file2md); + const file2Block = markdownToBlocks(__content); + const {notionID} = yamlObj; + if(notionID){ + res = await this.updatePage(notionID, title, file2Block); + } else { + res = await this.createPage(title, file2Block); + } + console.log(res,'===') if (res.status === 200) { this.updateYamlInfo(markdown, fullPath, res) } @@ -68,8 +100,9 @@ export class Upload2Notion { async updateYamlInfo(yamlContent: string, fullPath: string, res: any) { const yamlObj:any = yamlFrontMatter.loadFront(yamlContent); - const {url} = res.json + const {url, id} = res.json yamlObj.link = url; + yamlObj.notionID = id; const __content = yamlObj.__content; delete yamlObj.__content const yamlhead = yaml.stringify(yamlObj) diff --git a/main.ts b/main.ts index f1a48bd..d04dc79 100644 --- a/main.ts +++ b/main.ts @@ -45,6 +45,9 @@ export default class MyPlugin extends Plugin { "Share to notion", async (evt: MouseEvent) => { // Called when the user clicks the icon. + console.log(this,window.localStorage.getItem('language')) + + this.upload(); } );