mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 16:35:57 +08:00
Merge pull request #29 from jxpeng98/debug-general
General: use node-fetch to post notes
This commit is contained in:
@@ -82,7 +82,8 @@ export async function uploadCommandGeneral(
|
|||||||
const upload = new Upload2NotionGeneral(plugin, dbDetails);
|
const upload = new Upload2NotionGeneral(plugin, dbDetails);
|
||||||
const res = await upload.syncMarkdownToNotionGeneral(basename, cover, tags, markDownData, nowFile, this.app);
|
const res = await upload.syncMarkdownToNotionGeneral(basename, cover, tags, markDownData, nowFile, this.app);
|
||||||
|
|
||||||
if (res.status === 200) {
|
const { response } = res;
|
||||||
|
if (response.status === 200) {
|
||||||
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
||||||
} else {
|
} else {
|
||||||
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
||||||
@@ -120,7 +121,7 @@ export async function uploadCommandCustom(
|
|||||||
if (response.status === 200) {
|
if (response.status === 200) {
|
||||||
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
||||||
} else {
|
} else {
|
||||||
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { App, Notice, requestUrl, TFile } from "obsidian";
|
import { App, Notice, TFile } from "obsidian";
|
||||||
import { Client } from "@notionhq/client";
|
import { Client } from "@notionhq/client";
|
||||||
import { markdownToBlocks } from "@tryfabric/martian";
|
import { markdownToBlocks } from "@tryfabric/martian";
|
||||||
import * as yamlFrontMatter from "yaml-front-matter";
|
import * as yamlFrontMatter from "yaml-front-matter";
|
||||||
@@ -7,6 +7,7 @@ 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";
|
||||||
import { updateYamlInfo } from "../updateYaml";
|
import { updateYamlInfo } from "../updateYaml";
|
||||||
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
export class Upload2NotionGeneral extends UploadBaseGeneral {
|
export class Upload2NotionGeneral extends UploadBaseGeneral {
|
||||||
settings: PluginSettings;
|
settings: PluginSettings;
|
||||||
@@ -101,21 +102,30 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
console.log(bodyString)
|
||||||
return await requestUrl({
|
|
||||||
url: `https://api.notion.com/v1/pages`,
|
const response = await fetch("https://api.notion.com/v1/pages", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
// 'User-Agent': 'obsidian.md',
|
"Authorization": "Bearer " + notionAPI,
|
||||||
Authorization:
|
"Notion-Version": "2022-06-28",
|
||||||
"Bearer " + notionAPI,
|
},
|
||||||
"Notion-Version": "2022-06-28",
|
body: JSON.stringify(bodyString),
|
||||||
},
|
});
|
||||||
body: JSON.stringify(bodyString),
|
|
||||||
});
|
const data: any = await response.json();
|
||||||
} catch (error) {
|
if (!response.ok) {
|
||||||
new Notice(`network error ${error}`);
|
new Notice(`Error ${data.status}: ${data.code} \n Check the console for more information \n opt+cmd+i/ctrl+shift+i`, 5000);
|
||||||
|
console.log(`Error message: \n ${data.message}`);
|
||||||
|
} else {
|
||||||
|
console.log(`Page created: ${data.url}`);
|
||||||
|
console.log(`Page ID: ${data.id}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
response, // for status code
|
||||||
|
data // for id and url
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,11 +164,15 @@ export class Upload2NotionGeneral extends UploadBaseGeneral {
|
|||||||
} else {
|
} else {
|
||||||
res = await this.createPage(title, cover, tags, file2Block);
|
res = await this.createPage(title, cover, tags, file2Block);
|
||||||
}
|
}
|
||||||
if (res.status === 200) {
|
|
||||||
await updateYamlInfo(markdown, nowFile, res, app, this.plugin, this.dbDetails);
|
let {response, data} = res;
|
||||||
} else {
|
|
||||||
new Notice(`${res.text}`);
|
// console.log(response)
|
||||||
|
|
||||||
|
if (response && response.status === 200) {
|
||||||
|
await updateYamlInfo(markdown, nowFile, data, app, this.plugin, this.dbDetails);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user