fix: test the desktop function of custom database

with platform function
This commit is contained in:
Jiaxin Peng
2024-08-31 00:00:09 +01:00
parent bc166d8db1
commit f282dde1b7
3 changed files with 102 additions and 45 deletions

View File

@@ -79,4 +79,5 @@ export const en = {
BlockUploaded: "All blocks uploaded", BlockUploaded: "All blocks uploaded",
ExtraBlockUploaded: "Extra blocks uploaded", ExtraBlockUploaded: "Extra blocks uploaded",
CheckConsole: "Check the console for more information \n opt+cmd+i/ctrl+shift+i", 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",
} }

View File

@@ -111,6 +111,9 @@ export async function uploadCommandCustom(
const {markDownData, nowFile, cover, customValues} = await getNowFileMarkdownContentCustom(app, dbDetails) const {markDownData, nowFile, cover, customValues} = await getNowFileMarkdownContentCustom(app, dbDetails)
new Notice(`Start upload ${nowFile.basename}`);
console.log(`Start upload ${nowFile.basename}`);
if (markDownData) { if (markDownData) {
const {basename} = nowFile; const {basename} = nowFile;

View File

@@ -1,4 +1,4 @@
import {App, Notice, TFile} from "obsidian"; import {App, Notice, Platform, TFile} from "obsidian";
import {markdownToBlocks} from "@jxpeng98/martian"; import {markdownToBlocks} from "@jxpeng98/martian";
import * as yamlFrontMatter from "yaml-front-matter"; import * as yamlFrontMatter from "yaml-front-matter";
// import * as yaml from "yaml" // import * as yaml from "yaml"
@@ -10,6 +10,11 @@ import fetch from 'node-fetch';
import {i18nConfig} from "../../lang/I18n"; import {i18nConfig} from "../../lang/I18n";
interface CreatePageResponse {
response: any;
data: any;
}
export class Upload2NotionCustom extends UploadBaseCustom { export class Upload2NotionCustom extends UploadBaseCustom {
settings: PluginSettings; settings: PluginSettings;
dbDetails: DatabaseDetails; dbDetails: DatabaseDetails;
@@ -46,7 +51,7 @@ export class Upload2NotionCustom extends UploadBaseCustom {
cover: string, cover: string,
customValues: Record<string, string>, customValues: Record<string, string>,
childArr: any, childArr: any,
) { ): Promise<CreatePageResponse> {
const { const {
databaseID, databaseID,
@@ -110,56 +115,81 @@ export class Upload2NotionCustom extends UploadBaseCustom {
} }
console.log(bodyString) console.log(bodyString)
console.log(Platform.isDesktopApp)
const response = await fetch("https://api.notion.com/v1/pages", { let response: any;
method: "POST", let data: any;
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + notionAPI,
"Notion-Version": "2022-06-28",
},
body: JSON.stringify(bodyString),
});
const data: any = await response.json(); if (Platform.isMobileApp) {
if(childArrLength > 100) {
if (!response.ok) { new Notice(i18nConfig["reach-mobile-limit"], 5000);
new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000); } else {
console.log(`Error message: \n ${data.message}`); response = await requestUrl({
} else { url: `https://api.notion.com/v1/pages`,
console.log(`Page created: ${data.url}`); method: "POST",
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: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
"Authorization": "Bearer " + notionAPI, // 'User-Agent': 'obsidian.md',
Authorization:
"Bearer " + notionAPI,
"Notion-Version": "2022-06-28", "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) { data = await response.json();
new Notice(`Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
console.log(`Error message: \n ${extraData.message}`); if (!response.ok) {
} else { new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${data.id}`); console.log(`Error message: \n ${data.message}`);
if (i === pushCount - 1) { } else {
console.log(`${i18nConfig["BlockUploaded"]} to page: ${data.id}`); console.log(`Page created: ${data.url}`);
new Notice(`${i18nConfig["BlockUploaded"]} page: ${data.id}`, 5000); 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) // console.log(response)
if (response && response.status === 200) { if (Platform.isDesktopApp) {
await updateYamlInfo(markdown, nowFile, data, app, this.plugin, this.dbDetails); 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; return res;
} }