Skip to content

feat: add long text support #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 59 additions & 13 deletions Upload2Notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,19 @@ export class Upload2Notion {
}
}

async syncMarkdownToNotion(title:string, allowTags:boolean, tags:string[], markdown: string, nowFile: TFile, app:App, settings:any): Promise<any> {
async syncMarkdownToNotion(title:string, allowTags:boolean, tags:string[], markdown: string,pageId:string): Promise<any> {
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) {
Expand Down Expand Up @@ -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}`)
}
}

}
39 changes: 30 additions & 9 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down