Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
});
}
Expand Down Expand Up @@ -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;
Expand All @@ -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<void> {
console.log(`Uploading file: ${file.basename}`);
const uploadResult = await uploadFile(this, file);
this.displayResult(uploadResult, file.basename);
}
Expand Down Expand Up @@ -193,4 +215,4 @@ export default class Nobsidion extends Plugin {

new Notice(`${this.message["sync-success"]}${pageName}`);
}
}
}
40 changes: 29 additions & 11 deletions service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -117,4 +135,4 @@ export const convertObsidianLinks = async (
}

return updatedMarkdown;
};
};
12 changes: 7 additions & 5 deletions service/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,7 +33,7 @@ const createEmptyPage = async (
let res = null;

const { databaseID, notionAPIToken, allowTags, bannerUrl } = settings;

const bodyString: any = {
parent: { database_id: databaseID },
properties: {
Expand Down Expand Up @@ -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),
});
Expand Down Expand Up @@ -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 }),
});
Expand Down Expand Up @@ -132,7 +134,7 @@ const clearPageContent = async (
method: "DELETE",
headers: {
Authorization: `Bearer ${notionAPIToken}`,
"Notion-Version": "2021-08-16",
"Notion-Version": NOTION_CONFIG.API_VERSION,
},
});
}
Expand Down Expand Up @@ -174,4 +176,4 @@ const notion = {
uploadFileContent,
};

export default notion;
export default notion;