From 0c869c8e55c842111539e834961454f762f5a957 Mon Sep 17 00:00:00 2001 From: zedeff19 Date: Fri, 30 May 2025 01:40:57 +0530 Subject: [PATCH 1/2] notionConfig file for universal API Version --- main.ts | 26 ++++++++++++++++++++++++-- service/index.ts | 42 +++++++++++++++++++++++++++++++----------- service/notion.ts | 16 +++++++++++----- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/main.ts b/main.ts index ac237c8..a60682d 100644 --- a/main.ts +++ b/main.ts @@ -94,7 +94,7 @@ export default class Nobsidion extends Plugin { id: "bulk-share-to-notion", name: "Upload entire vault to Notion", callback: async () => { - this.bulkUpload(); + this.bulkUploadParallel(); }, }); } @@ -143,6 +143,7 @@ export default class Nobsidion extends Plugin { } async bulkUpload() { + console.log("Starting bulkUpload function..."); if (!this.hasValidNotionCredentials()) { new Notice(this.message["config-settings"]); return; @@ -156,12 +157,33 @@ export default class Nobsidion extends Plugin { new Notice(this.message["all-sync-success"]); } + async bulkUploadParallel() { + console.log("Starting bulk upload..."); + if (!this.hasValidNotionCredentials()) { + new Notice(this.message["config-settings"]); + return; + } + + const markdownFiles = this.app.vault.getMarkdownFiles(); + const uploadPromises = markdownFiles.map(file => + this.uploadFile(file).catch(error => { + console.error(`Failed to upload ${file.name}:`, error); + return null; // Prevent Promise.all rejection on single failure + }) + ); + + await Promise.all(uploadPromises); + new Notice(this.message["all-sync-success"]); +} + + hasValidNotionCredentials() { const { notionAPIToken, databaseID } = this.settings; return notionAPIToken !== "" && databaseID !== ""; } async uploadFile(file: TFile): Promise { + console.log(`Uploading file: ${file.basename}`); const uploadResult = await uploadFile(this, file); this.displayResult(uploadResult, file.basename); } @@ -193,4 +215,4 @@ export default class Nobsidion extends Plugin { new Notice(`${this.message["sync-success"]}${pageName}`); } -} +} \ No newline at end of file diff --git a/service/index.ts b/service/index.ts index 4350167..9ebdc92 100644 --- a/service/index.ts +++ b/service/index.ts @@ -13,6 +13,7 @@ export const uploadFile = async ( nobsidion: Nobsidion, file: TFile ): Promise => { + console.log("Uploading file to Notion(index.ts/uploadFile):", file.basename); const contentWithFrontMatter = await initializeNotionPage(nobsidion, file); const content = await convertObsidianLinks( @@ -37,38 +38,57 @@ export const initializeNotionPage = async ( nobsidion: Nobsidion, file: TFile ): Promise => { + console.log("Initializing Notion page for file(initializeNotionPage):", file.basename); const contentWithFrontMatter = await nobsidion.getContent(file); const settings = nobsidion.settings; const notionWorkspaceID = settings.notionWorkspaceID; - console.log(file.basename); - console.log(contentWithFrontMatter); - if (!contentWithFrontMatter.notionPageId) { + // Add error handling for the API call + console.log("Creating empty Notion page for file:", file.basename); const { data, error } = await notion.createEmptyPage( settings, file.basename ); + + // Check for API errors first + if (error || !data) { + console.error("Failed to create Notion page:", error); + throw new Error(`Page creation failed: ${error?.message || "Unknown error"}`); + } + + // Add null checks before destructuring + if (!data.url || !data.id) { + throw new Error("Invalid response from Notion API - missing required fields"); + } + const { url: rawNotionPageUrl, id: notionPageId } = data; + // Ensure workspace ID is valid + if (!notionWorkspaceID) { + throw new Error("Notion Workspace ID is not configured"); + } + const notionPageUrl = updateNotionPageUrlWithWorkspaceId( rawNotionPageUrl, notionWorkspaceID ); - contentWithFrontMatter.notionPageId = notionPageId; - contentWithFrontMatter.notionPageUrl = notionPageUrl; - - const processedMarkdown = fromYamlFrontMatterToMarkdown( - contentWithFrontMatter - ); + // Update front matter + const processedMarkdown = fromYamlFrontMatterToMarkdown({ + ...contentWithFrontMatter, + notionPageId, + notionPageUrl + }); - nobsidion.updateMarkdownFile(file, processedMarkdown); + // Save changes + await nobsidion.updateMarkdownFile(file, processedMarkdown); } return contentWithFrontMatter; }; + /** * Convert Obsidian wiki-link into hyperlink. * @@ -117,4 +137,4 @@ export const convertObsidianLinks = async ( } return updatedMarkdown; -}; +}; \ No newline at end of file diff --git a/service/notion.ts b/service/notion.ts index bb3ae53..ab858ad 100644 --- a/service/notion.ts +++ b/service/notion.ts @@ -22,16 +22,22 @@ import { requestUrl } from "obsidian"; import { markdownToBlocks } from "@tryfabric/martian"; import { PluginSettings, ServiceResult } from "./types"; +import { NOTION_CONFIG } from "notionConfig"; + const createEmptyPage = async ( settings: PluginSettings, title: string, tags: string[] = [] ): Promise => { + console.log("Creating empty Notion page(notion.ts/createEnptyPage function)..."); let res = null; const { databaseID, notionAPIToken, allowTags, bannerUrl } = settings; - + console.log(databaseID); + console.log(notionAPIToken); + console.log(allowTags); + console.log(bannerUrl); const bodyString: any = { parent: { database_id: databaseID }, properties: { @@ -59,7 +65,7 @@ const createEmptyPage = async ( headers: { "Content-Type": "application/json", Authorization: `Bearer ${notionAPIToken}`, - "Notion-Version": "2021-08-16", + "Notion-Version": NOTION_CONFIG.API_VERSION, }, body: JSON.stringify(bodyString), }); @@ -91,7 +97,7 @@ const addContentToPage = async ( headers: { "Content-Type": "application/json", Authorization: `Bearer ${notionAPIToken}`, - "Notion-Version": "2021-08-16", + "Notion-Version": NOTION_CONFIG.API_VERSION, }, body: JSON.stringify({ children: blocks }), }); @@ -132,7 +138,7 @@ const clearPageContent = async ( method: "DELETE", headers: { Authorization: `Bearer ${notionAPIToken}`, - "Notion-Version": "2021-08-16", + "Notion-Version": NOTION_CONFIG.API_VERSION, }, }); } @@ -174,4 +180,4 @@ const notion = { uploadFileContent, }; -export default notion; +export default notion; \ No newline at end of file From d88feeaebc0ce307738a37815c00dc4e74f583e4 Mon Sep 17 00:00:00 2001 From: zedeff19 Date: Fri, 30 May 2025 01:51:30 +0530 Subject: [PATCH 2/2] notionConfig file for universal API Version --- service/index.ts | 2 -- service/notion.ts | 6 +----- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/service/index.ts b/service/index.ts index 9ebdc92..9a3b1af 100644 --- a/service/index.ts +++ b/service/index.ts @@ -13,7 +13,6 @@ export const uploadFile = async ( nobsidion: Nobsidion, file: TFile ): Promise => { - console.log("Uploading file to Notion(index.ts/uploadFile):", file.basename); const contentWithFrontMatter = await initializeNotionPage(nobsidion, file); const content = await convertObsidianLinks( @@ -38,7 +37,6 @@ export const initializeNotionPage = async ( nobsidion: Nobsidion, file: TFile ): Promise => { - console.log("Initializing Notion page for file(initializeNotionPage):", file.basename); const contentWithFrontMatter = await nobsidion.getContent(file); const settings = nobsidion.settings; const notionWorkspaceID = settings.notionWorkspaceID; diff --git a/service/notion.ts b/service/notion.ts index ab858ad..cc5f5a6 100644 --- a/service/notion.ts +++ b/service/notion.ts @@ -30,14 +30,10 @@ const createEmptyPage = async ( title: string, tags: string[] = [] ): Promise => { - console.log("Creating empty Notion page(notion.ts/createEnptyPage function)..."); let res = null; const { databaseID, notionAPIToken, allowTags, bannerUrl } = settings; - console.log(databaseID); - console.log(notionAPIToken); - console.log(allowTags); - console.log(bannerUrl); + const bodyString: any = { parent: { database_id: databaseID }, properties: {