use node-fetch to post notes for a better debug

This commit is contained in:
2024-07-27 22:28:21 +01:00
parent 60d002677b
commit b5ac42e6e3
3 changed files with 64 additions and 58 deletions

View File

@@ -41,6 +41,7 @@ const ctx = await esbuild.context({
"@codemirror/view", "@codemirror/view",
...builtins, ...builtins,
], ],
platform: "node",
format: "cjs", format: "cjs",
target: "es2016", target: "es2016",
logLevel: "info", logLevel: "info",

View File

@@ -1,34 +1,35 @@
{ {
"name": "share-to-notionnext", "name": "share-to-notionnext",
"version": "2.3.3", "version": "2.3.3",
"type": "module", "type": "module",
"description": "Shares obsidian md file to notion with notion api for NotionNext web deploy, originally created by EasyChris/obsidian-to-notion.", "description": "Shares obsidian md file to notion with notion api for NotionNext web deploy, originally created by EasyChris/obsidian-to-notion.",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
"dev": "node esbuild.config.mjs", "dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production", "build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"version": "node version-bump.mjs && git add manifest.json versions.json" "version": "node version-bump.mjs && git add manifest.json versions.json"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "GNU GPLv3", "license": "GNU GPLv3",
"devDependencies": { "devDependencies": {
"@types/node": "^20.5.7", "@types/node": "^20.5.7",
"@types/yaml-front-matter": "^4.1.3", "@types/yaml-front-matter": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^6.16.0", "@typescript-eslint/eslint-plugin": "^6.16.0",
"@typescript-eslint/parser": "^6.16.0", "@typescript-eslint/parser": "^6.16.0",
"builtin-modules": "^3.3.0", "builtin-modules": "^3.3.0",
"esbuild": "0.19.5", "esbuild": "0.19.5",
"obsidian": "latest", "obsidian": "^1.6.6",
"tslib": "2.6.2", "tslib": "2.6.2",
"typescript": "5.2.2" "typescript": "5.2.2"
}, },
"dependencies": { "dependencies": {
"@tryfabric/martian": "^1.2.4", "@tryfabric/martian": "^1.2.4",
"https-proxy-agent": "^7.0.2", "https-proxy-agent": "^7.0.2",
"process": "^0.11.10", "node-fetch": "^3.3.2",
"remark-math": "^6.0.0", "process": "^0.11.10",
"yaml": "^2.3.4", "remark-math": "^6.0.0",
"yaml-front-matter": "^4.1.1" "yaml": "^2.3.4",
} "yaml-front-matter": "^4.1.1"
} }
}

View File

@@ -1,12 +1,13 @@
import {App, Notice, requestUrl, TFile} from "obsidian"; import {App, Notice, requestUrl, 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"
import MyPlugin from "src/main"; 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 { UploadBaseCustom } from "./BaseUpload2NotionCustom"; import {UploadBaseCustom} from "./BaseUpload2NotionCustom";
import axios from 'axios'; import fetch from 'node-fetch';
export class Upload2NotionCustom extends UploadBaseCustom { export class Upload2NotionCustom extends UploadBaseCustom {
settings: PluginSettings; settings: PluginSettings;
@@ -27,7 +28,7 @@ export class Upload2NotionCustom extends UploadBaseCustom {
) { ) {
await this.deletePage(notionID); await this.deletePage(notionID);
const { databaseID } = this.dbDetails; const {databaseID} = this.dbDetails;
const databaseCover = await this.getDataBase( const databaseCover = await this.getDataBase(
databaseID databaseID
@@ -74,20 +75,23 @@ export class Upload2NotionCustom extends UploadBaseCustom {
console.log(bodyString) console.log(bodyString)
try { const response = await fetch("https://api.notion.com/v1/pages", {
await fetch("https://api.notion.com/v1/pages", { method: "POST",
method: "POST", headers: {
headers: { "Content-Type": "application/json",
"Content-Type": "application/json", "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 (e) {
console.log(JSON.stringify(e))
}
if (!response.ok) {
const data: any = await response.json();
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);
} else {
return response;
}
} }
async syncMarkdownToNotionCustom( async syncMarkdownToNotionCustom(
@@ -109,7 +113,7 @@ export class Upload2NotionCustom extends UploadBaseCustom {
const file2Block = markdownToBlocks(__content, options); const file2Block = markdownToBlocks(__content, options);
const frontmasster = const frontmasster =
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 = frontmasster ? frontmasster[notionIDKey] : null;
@@ -208,7 +212,7 @@ export class Upload2NotionCustom extends UploadBaseCustom {
}; };
case "multi_select": case "multi_select":
return { return {
multi_select: Array.isArray(value) ? value.map(item => ({ name: item })) : [{ name: value }], multi_select: Array.isArray(value) ? value.map(item => ({name: item})) : [{name: value}],
}; };
} }
} }
@@ -222,11 +226,11 @@ export class Upload2NotionCustom extends UploadBaseCustom {
const properties: { [key: string]: any } = {}; const properties: { [key: string]: any } = {};
// Only include custom properties that have values // Only include custom properties that have values
customProperties.forEach(({ customName, customType }) => { customProperties.forEach(({customName, customType}) => {
if (customValues[customName] !== undefined) { if (customValues[customName] !== undefined) {
properties[customName] = this.buildPropertyObject(customName, customType, customValues); properties[customName] = this.buildPropertyObject(customName, customType, customValues);
}
} }
}
); );
// console.log(properties) // console.log(properties)