mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 08:08:34 +08:00
fix: test the desktop function for next and general
This commit is contained in:
@@ -1,8 +1,6 @@
|
|||||||
import {App, Notice, TFile} from "obsidian";
|
import {App, Notice, TFile, Platform, requestUrl} from "obsidian";
|
||||||
import {Client} from "@notionhq/client";
|
|
||||||
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 MyPlugin from "src/main";
|
import MyPlugin from "src/main";
|
||||||
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
||||||
import {UploadBaseGeneral} from "./BaseUpload2NotionGeneral";
|
import {UploadBaseGeneral} from "./BaseUpload2NotionGeneral";
|
||||||
@@ -10,6 +8,11 @@ import {updateYamlInfo} from "../updateYaml";
|
|||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import {i18nConfig} from "../../lang/I18n";
|
import {i18nConfig} from "../../lang/I18n";
|
||||||
|
|
||||||
|
interface CreatePageResponse {
|
||||||
|
response: any;
|
||||||
|
data: any;
|
||||||
|
}
|
||||||
|
|
||||||
export class Upload2NotionGeneral extends UploadBaseGeneral {
|
export class Upload2NotionGeneral extends UploadBaseGeneral {
|
||||||
settings: PluginSettings;
|
settings: PluginSettings;
|
||||||
dbDetails: DatabaseDetails;
|
dbDetails: DatabaseDetails;
|
||||||
@@ -48,7 +51,7 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
|
|||||||
cover: string,
|
cover: string,
|
||||||
tags: string[],
|
tags: string[],
|
||||||
childArr: any,
|
childArr: any,
|
||||||
) {
|
): Promise<CreatePageResponse> {
|
||||||
|
|
||||||
const {
|
const {
|
||||||
databaseID,
|
databaseID,
|
||||||
@@ -139,8 +142,32 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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",
|
||||||
@@ -150,10 +177,11 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
|
|||||||
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);
|
||||||
|
console.log(`Error message: \n ${data.message}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`Page created: ${data.url}`);
|
console.log(`Page created: ${data.url}`);
|
||||||
console.log(`Page ID: ${data.id}`);
|
console.log(`Page ID: ${data.id}`);
|
||||||
@@ -192,10 +220,11 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
response, // for status code
|
response,
|
||||||
data // for id and url
|
data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,11 +245,11 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
|
|||||||
const yamlContent: any = yamlFrontMatter.loadFront(markdown);
|
const yamlContent: any = yamlFrontMatter.loadFront(markdown);
|
||||||
const __content = yamlContent.__content;
|
const __content = yamlContent.__content;
|
||||||
const file2Block = markdownToBlocks(__content, options);
|
const file2Block = markdownToBlocks(__content, options);
|
||||||
const frontmasster =
|
const frontMatter =
|
||||||
app.metadataCache.getFileCache(nowFile)?.frontmatter;
|
app.metadataCache.getFileCache(nowFile)?.frontmatter;
|
||||||
const {abName} = this.dbDetails
|
const {abName} = this.dbDetails
|
||||||
const notionIDKey = `NotionID-${abName}`;
|
const notionIDKey = `NotionID-${abName}`;
|
||||||
const notionID = frontmasster ? frontmasster[notionIDKey] : null;
|
const notionID = frontMatter ? frontMatter[notionIDKey] : null;
|
||||||
|
|
||||||
|
|
||||||
if (notionID) {
|
if (notionID) {
|
||||||
@@ -237,10 +266,30 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
|
|||||||
|
|
||||||
let {response, data} = res;
|
let {response, data} = res;
|
||||||
|
|
||||||
// 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;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import {UploadBaseNext} from "./BaseUpload2NotionNext";
|
import {UploadBaseNext} from "./BaseUpload2NotionNext";
|
||||||
import {App, Notice, TFile} from "obsidian";
|
import {App, Notice, TFile, Platform, requestUrl} from "obsidian";
|
||||||
import {Client} from "@notionhq/client";
|
import {Client} from "@notionhq/client";
|
||||||
import {markdownToBlocks} from "@jxpeng98/martian";
|
import {markdownToBlocks} from "@jxpeng98/martian";
|
||||||
import * as yamlFrontMatter from "yaml-front-matter";
|
import * as yamlFrontMatter from "yaml-front-matter";
|
||||||
@@ -8,9 +8,14 @@ import MyPlugin from "src/main";
|
|||||||
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
||||||
import {updateYamlInfo} from "../updateYaml";
|
import {updateYamlInfo} from "../updateYaml";
|
||||||
import {LIMITS, paragraph} from "@jxpeng98/martian/src/notion";
|
import {LIMITS, paragraph} from "@jxpeng98/martian/src/notion";
|
||||||
import fetch from "node-fetch";
|
import fetch from 'node-fetch';
|
||||||
import {i18nConfig} from "../../lang/I18n";
|
import {i18nConfig} from "../../lang/I18n";
|
||||||
|
|
||||||
|
interface CreatePageResponse {
|
||||||
|
response: any;
|
||||||
|
data: any;
|
||||||
|
}
|
||||||
|
|
||||||
export class Upload2NotionNext extends UploadBaseNext {
|
export class Upload2NotionNext extends UploadBaseNext {
|
||||||
settings: PluginSettings;
|
settings: PluginSettings;
|
||||||
dbDetails: DatabaseDetails;
|
dbDetails: DatabaseDetails;
|
||||||
@@ -79,7 +84,7 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
favicon: string,
|
favicon: string,
|
||||||
datetime: string,
|
datetime: string,
|
||||||
childArr: any,
|
childArr: any,
|
||||||
) {
|
): Promise<CreatePageResponse> {
|
||||||
const {databaseID, notionAPI} = this.dbDetails;
|
const {databaseID, notionAPI} = this.dbDetails;
|
||||||
|
|
||||||
// remove the annotations from the childArr if type is code block
|
// remove the annotations from the childArr if type is code block
|
||||||
@@ -241,19 +246,41 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
|
|
||||||
console.log(bodyString);
|
console.log(bodyString);
|
||||||
|
|
||||||
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",
|
method: "POST",
|
||||||
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",
|
||||||
|
},
|
||||||
|
body: JSON.stringify(bodyString),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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",
|
"Notion-Version": "2022-06-28",
|
||||||
},
|
},
|
||||||
body: JSON.stringify(bodyString),
|
body: JSON.stringify(bodyString),
|
||||||
});
|
});
|
||||||
|
|
||||||
const data: any = await response.json();
|
data = await response.json();
|
||||||
|
|
||||||
// can use response.ok or response.status === 200
|
|
||||||
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);
|
||||||
console.log(`Error message: \n ${data.message}`);
|
console.log(`Error message: \n ${data.message}`);
|
||||||
@@ -262,18 +289,16 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
console.log(`Page ID: ${data.id}`);
|
console.log(`Page ID: ${data.id}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the childArr is over 100, patch the rest of the blocks append to the page
|
// upload the rest of the blocks
|
||||||
if (pushCount > 0) {
|
if (pushCount > 0) {
|
||||||
for (let i = 0; i < pushCount; i++) {
|
for (let i = 0; i < pushCount; i++) {
|
||||||
const extraBlocks = {
|
const extraBlocks = {
|
||||||
children: extraArr[i]
|
children: extraArr[i],
|
||||||
}
|
};
|
||||||
|
|
||||||
console.log(extraBlocks)
|
console.log(extraBlocks)
|
||||||
|
|
||||||
const extraResponse = await fetch(
|
const extraResponse = await fetch(`https://api.notion.com/v1/blocks/${data.id}/children`, {
|
||||||
`https://api.notion.com/v1/blocks/${data.id}/children`,
|
|
||||||
{
|
|
||||||
method: "PATCH",
|
method: "PATCH",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@@ -286,10 +311,7 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
const extraData: any = await extraResponse.json();
|
const extraData: any = await extraResponse.json();
|
||||||
|
|
||||||
if (!extraResponse.ok) {
|
if (!extraResponse.ok) {
|
||||||
new Notice(
|
new Notice(`Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||||
`Extra Block: Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`,
|
|
||||||
5000,
|
|
||||||
);
|
|
||||||
console.log(`Error message: \n ${extraData.message}`);
|
console.log(`Error message: \n ${extraData.message}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${data.id}`);
|
console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${data.id}`);
|
||||||
@@ -300,10 +322,11 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
response, // for status code
|
response,
|
||||||
data, // for id and url
|
data
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -395,6 +418,7 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
|
|
||||||
let {response, data} = res;
|
let {response, data} = res;
|
||||||
|
|
||||||
|
if (Platform.isDesktopApp) {
|
||||||
if (response && response.status === 200) {
|
if (response && response.status === 200) {
|
||||||
await updateYamlInfo(
|
await updateYamlInfo(
|
||||||
markdown,
|
markdown,
|
||||||
@@ -402,9 +426,23 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
data,
|
data,
|
||||||
app,
|
app,
|
||||||
this.plugin,
|
this.plugin,
|
||||||
|
this.dbDetails
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Platform.isMobileApp) {
|
||||||
|
if (response && response.status === 200) {
|
||||||
|
await updateYamlInfo(
|
||||||
|
markdown,
|
||||||
|
nowFile,
|
||||||
|
response,
|
||||||
|
app,
|
||||||
|
this.plugin,
|
||||||
this.dbDetails,
|
this.dbDetails,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user