diff --git a/Upload2Notion.ts b/Upload2Notion.ts index c7e22d6..a151154 100644 --- a/Upload2Notion.ts +++ b/Upload2Notion.ts @@ -85,25 +85,19 @@ export class Upload2Notion { } } - async syncMarkdownToNotion(title:string, allowTags:boolean, tags:string[], markdown: string, nowFile: TFile, app:App, settings:any): Promise { + async syncMarkdownToNotion(title:string, allowTags:boolean, tags:string[], markdown: string,pageId:string): Promise { let res:any const yamlObj:any = yamlFrontMatter.loadFront(markdown); const __content = yamlObj.__content const file2Block = markdownToBlocks(__content); - const frontmasster =await app.metadataCache.getFileCache(nowFile)?.frontmatter - const notionID = frontmasster ? frontmasster.notionID : null - - if(notionID){ - res = await this.updatePage(notionID, title, allowTags, tags, file2Block); - } else { - res = await this.createPage(title, allowTags, tags, file2Block); - } - if (res.status === 200) { - await this.updateYamlInfo(markdown, nowFile, res, app, settings) - } else { - new Notice(`${res.text}`) + // 如果是第一块chunk,就直接新增 + if( pageId){ + res = await this.appendPage(pageId,file2Block) + }else{ + res = await this.createPage(title, allowTags, tags, file2Block); } return res + } async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any,app:App, settings:any) { @@ -136,4 +130,56 @@ export class Upload2Notion { new Notice(`write file error ${error}`) } } + + splitLongString(str: string) { + if (str.length <= 4000) { + return [str]; + } + + const chunks = []; + let startIndex = 0; + let endIndex = 4000; + + while (endIndex < str.length) { + if (str[endIndex] !== '\n') { + while (endIndex > startIndex && str[endIndex] !== '\n') { + endIndex--; + } + } + + chunks.push(str.substring(startIndex, endIndex)); + startIndex = endIndex + 1; + endIndex = startIndex + 4000; + } + + chunks.push(str.substring(startIndex)); + + return chunks; + } + + async appendPage(pageId:string, childArr: any){ + const bodyString:any = { + children: childArr, + } + if (pageId === undefined){ + return + } + try { + const response = await requestUrl({ + url: `https://api.notion.com/v1/blocks/${pageId}/children`, + method: 'PATCH', + 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) { + new Notice(`network error ${error}`) + } + } + } diff --git a/main.ts b/main.ts index 421a50d..cbc7bcf 100644 --- a/main.ts +++ b/main.ts @@ -82,17 +82,38 @@ export default class ObsidianSyncNotionPlugin extends Plugin { } const { markDownData, nowFile, tags } =await this.getNowFileMarkdownContent(this.app); - - if (markDownData) { - const { basename } = nowFile; - const upload = new Upload2Notion(this); - const res = await upload.syncMarkdownToNotion(basename, allowTags, tags, markDownData, nowFile, this.app, this.settings) - if(res.status === 200){ - new Notice(`${langConfig["sync-success"]}${basename}`) - }else { - new Notice(`${langConfig["sync-fail"]}${basename}`, 5000) + const upload = new Upload2Notion(this); + const frontmasster = app.metadataCache.getFileCache(nowFile)?.frontmatter + const notionID = frontmasster ? frontmasster.notionID : null + let chunks = upload.splitLongString(markDownData) + // 存在就先删除 + if (notionID) { + await upload.deletePage(notionID) + } + try { + if (chunks.length >0) { + const {basename} = nowFile; + // create page + const res = await upload.syncMarkdownToNotion(basename, allowTags, tags, chunks[0], null) + let {id} = res.json + // append page + for (let i = 1; i < chunks.length; i++) { + const response = await upload.syncMarkdownToNotion(basename, allowTags, tags, chunks[i], id) + if (response.status != 200) { + new Notice(`${langConfig["sync-fail"]}${basename},please retry!`, 5000) + break } } + // update YamlInfo + await upload.updateYamlInfo(markDownData, nowFile, res, app, this.settings) + new Notice(`${langConfig["sync-success"]}${basename}`) + + } else { + new Notice('sync-fail,please check your file in obsidian') + } + } catch (Exception) { + new Notice('sync-fail,please retry') + } } async getNowFileMarkdownContent(app: App) {