mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 08:08:34 +08:00
Next: use node-fetch to post notes for a better debug
This commit is contained in:
@@ -43,13 +43,15 @@ export async function uploadCommandNext(
|
|||||||
|
|
||||||
if (markDownData) {
|
if (markDownData) {
|
||||||
const { basename } = nowFile;
|
const { basename } = nowFile;
|
||||||
|
|
||||||
const upload = new Upload2NotionNext(plugin, dbDetails);
|
const upload = new Upload2NotionNext(plugin, dbDetails);
|
||||||
const res = await upload.syncMarkdownToNotionNext(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, markDownData, nowFile, this.app);
|
const res = await upload.syncMarkdownToNotionNext(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -83,7 +85,7 @@ export async function uploadCommandGeneral(
|
|||||||
if (res.status === 200) {
|
if (res.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,5 +1,5 @@
|
|||||||
import { UploadBaseNext } from "./BaseUpload2NotionNext";
|
import { UploadBaseNext } from "./BaseUpload2NotionNext";
|
||||||
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";
|
||||||
@@ -8,6 +8,7 @@ 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 "@tryfabric/martian/src/notion";
|
import { LIMITS, paragraph } from "@tryfabric/martian/src/notion";
|
||||||
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
export class Upload2NotionNext extends UploadBaseNext {
|
export class Upload2NotionNext extends UploadBaseNext {
|
||||||
settings: PluginSettings;
|
settings: PluginSettings;
|
||||||
@@ -204,21 +205,33 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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': 'Bearer ' + notionAPI,
|
"Notion-Version": "2022-06-28",
|
||||||
'Notion-Version': '2022-06-28',
|
},
|
||||||
},
|
body: JSON.stringify(bodyString),
|
||||||
body: JSON.stringify(bodyString),
|
});
|
||||||
})
|
|
||||||
} catch (error) {
|
const data: any = await response.json();
|
||||||
new Notice(`network error ${error}`)
|
|
||||||
}
|
// can use response.ok or response.status === 200
|
||||||
|
if (!response.ok) {
|
||||||
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async syncMarkdownToNotionNext(
|
async syncMarkdownToNotionNext(
|
||||||
@@ -309,12 +322,13 @@ export class Upload2NotionNext extends UploadBaseNext {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (res.status === 200) {
|
let {response, data} = res;
|
||||||
await updateYamlInfo(markdown, nowFile, res, app, this.plugin, this.dbDetails)
|
|
||||||
} else {
|
if (response && response.status === 200) {
|
||||||
new Notice(`${res.text}`)
|
await updateYamlInfo(markdown, nowFile, data, app, this.plugin, this.dbDetails);
|
||||||
}
|
}
|
||||||
return res
|
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {App, Notice, requestUrl, TFile} from "obsidian";
|
import {App, Notice, TFile} from "obsidian";
|
||||||
import {markdownToBlocks} from "@tryfabric/martian";
|
import {markdownToBlocks} from "@tryfabric/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"
|
||||||
@@ -88,7 +88,7 @@ export class Upload2NotionCustom extends UploadBaseCustom {
|
|||||||
const data: any = await response.json();
|
const data: any = await response.json();
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
new Notice(`Error ${data.status}: ${data.code} \n Check the console for more information \n opt+cmd+i/ctrl+shift+i`, 5000);
|
new Notice(`Error ${data.status}: ${data.code} \n Check the console for more information \n opt+cmd+i/ctrl+shift+i`, 5000);
|
||||||
console.log(data.message);
|
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}`);
|
||||||
@@ -140,8 +140,6 @@ export class Upload2NotionCustom extends UploadBaseCustom {
|
|||||||
|
|
||||||
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);
|
||||||
} else {
|
|
||||||
new Notice(`${res.text}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
Reference in New Issue
Block a user