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..9a3b1af 100644 --- a/service/index.ts +++ b/service/index.ts @@ -41,34 +41,52 @@ export const initializeNotionPage = async ( 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 +135,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..cc5f5a6 100644 --- a/service/notion.ts +++ b/service/notion.ts @@ -22,6 +22,8 @@ import { requestUrl } from "obsidian"; import { markdownToBlocks } from "@tryfabric/martian"; import { PluginSettings, ServiceResult } from "./types"; +import { NOTION_CONFIG } from "notionConfig"; + const createEmptyPage = async ( settings: PluginSettings, @@ -31,7 +33,7 @@ const createEmptyPage = async ( let res = null; const { databaseID, notionAPIToken, allowTags, bannerUrl } = settings; - + const bodyString: any = { parent: { database_id: databaseID }, properties: { @@ -59,7 +61,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 +93,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 +134,7 @@ const clearPageContent = async ( method: "DELETE", headers: { Authorization: `Bearer ${notionAPIToken}`, - "Notion-Version": "2021-08-16", + "Notion-Version": NOTION_CONFIG.API_VERSION, }, }); } @@ -174,4 +176,4 @@ const notion = { uploadFileContent, }; -export default notion; +export default notion; \ No newline at end of file