diff --git a/src/lang/locale/en.ts b/src/lang/locale/en.ts index 8c7fbef..23eb1b8 100644 --- a/src/lang/locale/en.ts +++ b/src/lang/locale/en.ts @@ -79,4 +79,5 @@ export const en = { BlockUploaded: "All blocks uploaded", ExtraBlockUploaded: "Extra blocks uploaded", CheckConsole: "Check the console for more information \n opt+cmd+i/ctrl+shift+i", + "reach-mobile-limit": "The number of blocks exceeds the limit of 100, please use the desktop plugin", } diff --git a/src/upload/uploadCommand.ts b/src/upload/uploadCommand.ts index d412aa5..cb440f7 100644 --- a/src/upload/uploadCommand.ts +++ b/src/upload/uploadCommand.ts @@ -111,6 +111,9 @@ export async function uploadCommandCustom( const {markDownData, nowFile, cover, customValues} = await getNowFileMarkdownContentCustom(app, dbDetails) + new Notice(`Start upload ${nowFile.basename}`); + console.log(`Start upload ${nowFile.basename}`); + if (markDownData) { const {basename} = nowFile; diff --git a/src/upload/upoload_custom/Upload2NotionCustom.ts b/src/upload/upoload_custom/Upload2NotionCustom.ts index de64711..797704c 100644 --- a/src/upload/upoload_custom/Upload2NotionCustom.ts +++ b/src/upload/upoload_custom/Upload2NotionCustom.ts @@ -1,4 +1,4 @@ -import {App, Notice, TFile} from "obsidian"; +import {App, Notice, Platform, TFile} from "obsidian"; import {markdownToBlocks} from "@jxpeng98/martian"; import * as yamlFrontMatter from "yaml-front-matter"; // import * as yaml from "yaml" @@ -10,6 +10,11 @@ import fetch from 'node-fetch'; import {i18nConfig} from "../../lang/I18n"; +interface CreatePageResponse { + response: any; + data: any; +} + export class Upload2NotionCustom extends UploadBaseCustom { settings: PluginSettings; dbDetails: DatabaseDetails; @@ -46,7 +51,7 @@ export class Upload2NotionCustom extends UploadBaseCustom { cover: string, customValues: Record, childArr: any, - ) { + ): Promise { const { databaseID, @@ -110,56 +115,81 @@ export class Upload2NotionCustom extends UploadBaseCustom { } console.log(bodyString) + console.log(Platform.isDesktopApp) - const response = await fetch("https://api.notion.com/v1/pages", { - method: "POST", - headers: { - "Content-Type": "application/json", - "Authorization": "Bearer " + notionAPI, - "Notion-Version": "2022-06-28", - }, - body: JSON.stringify(bodyString), - }); + let response: any; + let data: any; - const data: any = await response.json(); - - if (!response.ok) { - new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000); - console.log(`Error message: \n ${data.message}`); - } else { - console.log(`Page created: ${data.url}`); - console.log(`Page ID: ${data.id}`); - } - - // upload the rest of the blocks - if (pushCount > 0) { - for (let i = 0; i < pushCount; i++) { - const extraBlocks = { - children: extraArr[i], - }; - - console.log(extraBlocks) - - const extraResponse = await fetch(`https://api.notion.com/v1/blocks/${data.id}/children`, { - method: "PATCH", + if (Platform.isMobileApp) { + if(childArrLength > 100) { + new Notice(i18nConfig["reach-mobile-limit"], 5000); + } else { + response = await requestUrl({ + url: `https://api.notion.com/v1/pages`, + method: "POST", headers: { "Content-Type": "application/json", - "Authorization": "Bearer " + notionAPI, + // 'User-Agent': 'obsidian.md', + Authorization: + "Bearer " + notionAPI, "Notion-Version": "2022-06-28", }, - body: JSON.stringify(extraBlocks), + body: JSON.stringify(bodyString), }); + } + } - const extraData: any = await extraResponse.json(); + if (Platform.isDesktopApp) { + response = await fetch("https://api.notion.com/v1/pages", { + method: "POST", + headers: { + "Content-Type": "application/json", + "Authorization": "Bearer " + notionAPI, + "Notion-Version": "2022-06-28", + }, + body: JSON.stringify(bodyString), + }); - if (!extraResponse.ok) { - new Notice(`Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`, 5000); - console.log(`Error message: \n ${extraData.message}`); - } else { - console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${data.id}`); - if (i === pushCount - 1) { - console.log(`${i18nConfig["BlockUploaded"]} to page: ${data.id}`); - new Notice(`${i18nConfig["BlockUploaded"]} page: ${data.id}`, 5000); + data = await response.json(); + + if (!response.ok) { + new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000); + console.log(`Error message: \n ${data.message}`); + } else { + console.log(`Page created: ${data.url}`); + console.log(`Page ID: ${data.id}`); + } + + // upload the rest of the blocks + if (pushCount > 0) { + for (let i = 0; i < pushCount; i++) { + const extraBlocks = { + children: extraArr[i], + }; + + console.log(extraBlocks) + + const extraResponse = await fetch(`https://api.notion.com/v1/blocks/${data.id}/children`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + "Authorization": "Bearer " + notionAPI, + "Notion-Version": "2022-06-28", + }, + body: JSON.stringify(extraBlocks), + }); + + const extraData: any = await extraResponse.json(); + + if (!extraResponse.ok) { + new Notice(`Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`, 5000); + console.log(`Error message: \n ${extraData.message}`); + } else { + console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${data.id}`); + if (i === pushCount - 1) { + console.log(`${i18nConfig["BlockUploaded"]} to page: ${data.id}`); + new Notice(`${i18nConfig["BlockUploaded"]} page: ${data.id}`, 5000); + } } } } @@ -209,10 +239,33 @@ export class Upload2NotionCustom extends UploadBaseCustom { // console.log(response) - if (response && response.status === 200) { - await updateYamlInfo(markdown, nowFile, data, app, this.plugin, this.dbDetails); + if (Platform.isDesktopApp) { + if (response && response.status === 200) { + await updateYamlInfo( + markdown, + nowFile, + data, + app, + this.plugin, + this.dbDetails + ); + } } + if (Platform.isMobileApp) { + if (response && response.status === 200) { + await updateYamlInfo( + markdown, + nowFile, + response, + app, + this.plugin, + this.dbDetails, + ); + } + } + + return res; }