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,8 +115,32 @@ 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;
let data: any;
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",
// 'User-Agent': 'obsidian.md',
Authorization:
"Bearer " + notionAPI,
"Notion-Version": "2022-06-28",
},
body: JSON.stringify(bodyString),
});
}
}
if (Platform.isDesktopApp) {
response = await fetch("https://api.notion.com/v1/pages", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -121,7 +150,7 @@ export class Upload2NotionCustom extends UploadBaseCustom {
body: JSON.stringify(bodyString), body: JSON.stringify(bodyString),
}); });
const data: any = await response.json(); data = await response.json();
if (!response.ok) { if (!response.ok) {
new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000); new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
@@ -164,6 +193,7 @@ export class Upload2NotionCustom extends UploadBaseCustom {
} }
} }
} }
}
return { return {
response, // for status code response, // for status code
@@ -209,9 +239,32 @@ export class Upload2NotionCustom extends UploadBaseCustom {
// console.log(response) // console.log(response)
if (Platform.isDesktopApp) {
if (response && response.status === 200) { if (response && response.status === 200) {
await updateYamlInfo(markdown, nowFile, data, app, this.plugin, this.dbDetails); 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;
} }