mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 16:35:57 +08:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
da72846903 | ||
|
|
8e08c4325d | ||
|
|
ebb7a290e8 | ||
|
|
24e68fc798 | ||
|
|
78d2a29c8a | ||
|
|
1156bd3809 | ||
|
|
b46e24f1ed | ||
|
|
a950e2e301 | ||
|
|
aac7bc154a | ||
|
|
a573ef728d |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -24,3 +24,7 @@ data.json
|
||||
|
||||
# local data
|
||||
local-data
|
||||
|
||||
# vitepress
|
||||
doc/.vitepress/dist
|
||||
doc/.vitepress/cache
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
## Fix
|
||||
## Feature
|
||||
|
||||
- Fix a bug that the plugin cannot read the title from the markdown front matter if using custom database.
|
||||
- 修复当使用自定义数据库时,插件无法从markdown front matter中读取标题的问题。
|
||||
- Unify and combine the upload logic for three types of database.
|
||||
- Better debugging information for upload failures.
|
||||
- 统一和合并三种数据库的上传逻辑。
|
||||
- 优化控制台输出的调试信息。
|
||||
18
README-zh.md
18
README-zh.md
@@ -44,21 +44,21 @@ tags:
|
||||
|
||||
- 这个版本开始,你可以**任意修改第一列表头的名字 (标题列,默认:'title')**。 (**注意:你必须保证你的Notion数据库中有'tags‘这一列的存在,并且在markdown文件中把`tags:`添加到你的YAML表头中。否则会返回`400 错误`。但是你可以`tags:`设置为空。**)
|
||||
|
||||

|
||||

|
||||
例如这样:
|
||||

|
||||

|
||||
- 增加了一个控制按钮来控制是否开启当前数据库同步命令的功能。如果你关闭了这个按钮,你在上传命令列表中就不会出现当前数据库的选项。
|
||||
|
||||

|
||||

|
||||
|
||||
### 0.2.6
|
||||
|
||||
- 增加一个按钮用来控制是否在名列列表中显示如下数据库。
|
||||
|
||||

|
||||

|
||||
如果你关闭普通数据库的按钮,你在上传命令列表中就不会出现普通数据库的选项。如果你关闭NotionNext数据库的按钮,你在上传命令列表中就不会出现NotionNext数据库的选项。
|
||||
|
||||

|
||||

|
||||
|
||||
### 0.2.3
|
||||
|
||||
@@ -70,9 +70,9 @@ tags:
|
||||
- 你可以有一个NotionNext和一个普通Notion数据库。
|
||||
- 普通Notion数据库的同步方式和原作者的一样,只同步`title`和`tags`。**如果使用普通数据库功能,你需要保证你的数据库中有`title`和`tags`这两个表头。必须使用小写。**
|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||
### 0.2.1
|
||||
|
||||
@@ -181,10 +181,10 @@ Contents Below
|
||||
```
|
||||
|
||||
模板使用如下:
|
||||

|
||||

|
||||
|
||||
**插件预览如下**
|
||||

|
||||

|
||||
|
||||
---
|
||||
|
||||
|
||||
48
README.md
48
README.md
@@ -40,38 +40,38 @@ If you want to use this plugin, you need to follow the following steps to set up
|
||||
|
||||
1). Go to [Notion API](https://www.notion.com/my-integrations) to create a new integration, and **copy the token**.
|
||||
|
||||

|
||||

|
||||
|
||||
2). Create a database in your Notion workspace.
|
||||
|
||||
Open Notion, click top-left `Create` a new page` -> You can give a title for the page and press "/" Select 'Database - Full page'.
|
||||
|
||||

|
||||

|
||||
|
||||
Rename the database properties to `title` and `tags`.
|
||||
**⚠️⚠️`title` is the title column! `tags` is lower case⚠️⚠️**
|
||||
|
||||

|
||||

|
||||
|
||||
3). Add the Notion API integration.
|
||||
|
||||
Click the right-top '...' -> connections -> connect to -> find the integration you created and connect it.
|
||||
|
||||

|
||||

|
||||
|
||||
4). Publish the database to the public.
|
||||
Click the 'Share' -> 'Publish' -> 'Publish' -> **copy the database id from the URL**.
|
||||
|
||||

|
||||

|
||||
|
||||
### 2. Setting up the plugin in Obsidian
|
||||
|
||||
1). Go to Obsidian settings -> community plugins -> search 'NotionNext' -> install it.
|
||||

|
||||

|
||||
|
||||
2). Open the plugin settings and create a new database.
|
||||
|
||||

|
||||

|
||||
|
||||
### 3. Create a database
|
||||
|
||||
@@ -81,7 +81,7 @@ Click the 'Share' -> 'Publish' -> 'Publish' -> **copy the database id from the U
|
||||
|
||||
General database is the default database, which only has the `title` and `tags` columns. **⚠️⚠️`title` and `tags` are lowercase⚠️⚠️**
|
||||
|
||||

|
||||

|
||||
|
||||
The general is as follows:
|
||||
|
||||
@@ -94,17 +94,17 @@ The general is as follows:
|
||||
|
||||
After setting, it should look like this:
|
||||
|
||||

|
||||

|
||||
|
||||
Once you click `save`, the new database will be created in the plugin settings and display in the detabase list.
|
||||
|
||||

|
||||

|
||||
|
||||
#### 2️⃣ NotionNext database
|
||||
|
||||
If you use [NotionNext](https://github.com/tangly1024/NotionNext) to set up your website, you can use the NotionNext database. (**All the properties are in lowercase, and you do not need to change the properties**)
|
||||
|
||||

|
||||

|
||||
|
||||
Like the general database, you need to fill in the following information:
|
||||
|
||||
@@ -117,7 +117,7 @@ Like the general database, you need to fill in the following information:
|
||||
|
||||
The custom database is purely customised, and you can customise the properties you want to sync. (**All the properties are in lowercase, and you do not need to change the properties**)
|
||||
|
||||

|
||||

|
||||
|
||||
The basic information is identical to the general database and NotionNext database. You need to fill in the following information:
|
||||
|
||||
@@ -128,15 +128,15 @@ The basic information is identical to the general database and NotionNext databa
|
||||
|
||||
After adding the information, You can click `Add New Property` to add the properties you want to sync. There is no limit to the number of properties you can add.
|
||||
|
||||

|
||||

|
||||
|
||||
**⚠️⚠️⚠️**: **`Title column` is the main property in the Notion database, which has the open button to open the underlying page.** You can select the type of all other properties. **⚠️⚠️⚠️**
|
||||
|
||||

|
||||

|
||||
|
||||
According to the previously shown database, you can use `title` as the `Title column`, and `Tags` as the property 1.
|
||||
|
||||

|
||||

|
||||
|
||||
**Until now, you have set up the plugin in Obsidian. You can start syncing your notes to Notion.**
|
||||
|
||||
@@ -160,7 +160,7 @@ This is a test file.
|
||||
|
||||
After creating the note, you can click the `Share to NotionNext` button on the left sidebar to sync the note to Notion.
|
||||
|
||||

|
||||

|
||||
|
||||
#### 2️⃣ note for NotionNext database
|
||||
|
||||
@@ -188,7 +188,7 @@ NotionID-pengjiaxin: 8ba573de-8fdf-4681-b063-c39d26e7860e
|
||||
this is a test file
|
||||
```
|
||||
|
||||

|
||||

|
||||
|
||||
#### 3️⃣ note for custom database
|
||||
|
||||
@@ -196,7 +196,7 @@ this is a test file
|
||||
|
||||
Use the previously created general database and rename `title` to `Name` and `tags` to `Tags` in Notion.
|
||||
|
||||

|
||||

|
||||
|
||||
The front matter for the custom database is as follows:
|
||||
|
||||
@@ -211,23 +211,23 @@ Tags:
|
||||
This is a test file.
|
||||
```
|
||||
|
||||

|
||||

|
||||
|
||||
##### sync more properties
|
||||
|
||||
Add more property to the custom database. For example, we can add a `summary` and `date` property to the custom database.
|
||||
|
||||
1). Click `Edit` in the plugin settings.
|
||||

|
||||

|
||||
2). Click `Add` and add the `summary` and `date` property.
|
||||
|
||||

|
||||

|
||||
|
||||
3). After adding the properties, you can see the properties in the plugin settings.
|
||||

|
||||

|
||||
4). Add the `summary` and `date` in Notion
|
||||
|
||||

|
||||

|
||||
|
||||
5). Create a new note with the `summary` and `date` property.
|
||||
|
||||
@@ -246,7 +246,7 @@ Add more property to the custom database. For example, we can add a `summary` an
|
||||
|
||||
6). Sync the note to Notion.
|
||||
|
||||

|
||||

|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -41,7 +41,6 @@ const ctx = await esbuild.context({
|
||||
"@codemirror/view",
|
||||
...builtins,
|
||||
],
|
||||
platform: "node",
|
||||
format: "cjs",
|
||||
target: "es2016",
|
||||
logLevel: "info",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"id": "share-to-notionnext",
|
||||
"name": "Share to NotionNext",
|
||||
"version": "2.4.3",
|
||||
"version": "2.6.0",
|
||||
"minAppVersion": "0.0.1",
|
||||
"description": "Shares obsidian md file to notion with notion api for NotionNext web deploy, originally created by EasyChris/obsidian-to-notion.",
|
||||
"author": "EasyChris, jxpeng98",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "share-to-notionnext",
|
||||
"version": "2.4.3",
|
||||
"version": "2.6.0",
|
||||
"type": "module",
|
||||
"description": "Share files to any Notion database using the Notion API, originally created by EasyChris/obsidian-to-notion.",
|
||||
"main": "main.js",
|
||||
@@ -26,7 +26,6 @@
|
||||
"dependencies": {
|
||||
"@jxpeng98/martian": "^1.2.7",
|
||||
"https-proxy-agent": "^7.0.2",
|
||||
"node-fetch": "^3.3.2",
|
||||
"remark-math": "^6.0.0",
|
||||
"yaml": "^2.3.4",
|
||||
"yaml-front-matter": "^4.1.1"
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
|
||||
import { addIcons } from 'src/ui/icon';
|
||||
import { Upload2NotionGeneral } from "src/upload/upload_general/Upload2NotionGeneral";
|
||||
import { Upload2NotionNext } from "src/upload/upload_next/Upload2NotionNext";
|
||||
import { i18nConfig } from "src/lang/I18n";
|
||||
import ribbonCommands from "src/commands/NotionCommands";
|
||||
import { ObsidianSettingTab, PluginSettings, DEFAULT_SETTINGS, DatabaseDetails } from "src/ui/settingTabs";
|
||||
@@ -86,4 +84,3 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
582
src/upload/Upload2Notion.ts
Normal file
582
src/upload/Upload2Notion.ts
Normal file
@@ -0,0 +1,582 @@
|
||||
import { App, TFile } from "obsidian";
|
||||
import { markdownToBlocks } from "@jxpeng98/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
import { LIMITS, paragraph } from "@jxpeng98/martian/src/notion";
|
||||
import MyPlugin from "src/main";
|
||||
import { DatabaseDetails } from "../ui/settingTabs";
|
||||
import { updateYamlInfo } from "./updateYaml";
|
||||
import { UploadBase, NotionPageResponse } from "./common/UploadBase";
|
||||
|
||||
export type DatasetType = "general" | "next" | "custom";
|
||||
|
||||
interface BaseSyncRequest {
|
||||
dataset: DatasetType;
|
||||
markdown: string;
|
||||
nowFile: TFile;
|
||||
app: App;
|
||||
}
|
||||
|
||||
interface GeneralSyncRequest extends BaseSyncRequest {
|
||||
dataset: "general";
|
||||
title: string;
|
||||
cover: string;
|
||||
tags: string[];
|
||||
}
|
||||
|
||||
interface NextSyncRequest extends BaseSyncRequest {
|
||||
dataset: "next";
|
||||
title: string;
|
||||
emoji: string;
|
||||
cover: string;
|
||||
tags: string[];
|
||||
type: string;
|
||||
slug: string;
|
||||
stats: string;
|
||||
category: string;
|
||||
summary: string;
|
||||
password: string;
|
||||
favicon: string;
|
||||
datetime: string;
|
||||
}
|
||||
|
||||
interface CustomSyncRequest extends BaseSyncRequest {
|
||||
dataset: "custom";
|
||||
cover: string;
|
||||
customValues: Record<string, string>;
|
||||
}
|
||||
|
||||
export type SyncRequest =
|
||||
| GeneralSyncRequest
|
||||
| NextSyncRequest
|
||||
| CustomSyncRequest;
|
||||
|
||||
interface GeneralParams {
|
||||
title: string;
|
||||
cover?: string;
|
||||
tags: string[];
|
||||
childArr: any[];
|
||||
notionId?: string;
|
||||
}
|
||||
|
||||
interface NextParams {
|
||||
title: string;
|
||||
emoji: string;
|
||||
cover?: string;
|
||||
tags: string[];
|
||||
type: string;
|
||||
slug: string;
|
||||
stats: string;
|
||||
category: string;
|
||||
summary: string;
|
||||
password: string;
|
||||
favicon: string;
|
||||
datetime: string;
|
||||
childArr: any[];
|
||||
notionId?: string;
|
||||
}
|
||||
|
||||
interface CustomParams {
|
||||
cover?: string;
|
||||
customValues: Record<string, string>;
|
||||
childArr: any[];
|
||||
notionId?: string;
|
||||
}
|
||||
|
||||
export class Upload2Notion extends UploadBase {
|
||||
|
||||
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||
super(plugin, dbDetails);
|
||||
}
|
||||
|
||||
async sync(request: SyncRequest): Promise<NotionPageResponse> {
|
||||
console.log(`[Upload2Notion] Start sync`, {
|
||||
dataset: request.dataset,
|
||||
file: request.nowFile.path,
|
||||
database: this.dbDetails.databaseID,
|
||||
});
|
||||
|
||||
let response: NotionPageResponse;
|
||||
|
||||
switch (request.dataset) {
|
||||
case "general":
|
||||
response = await this.handleGeneral(request);
|
||||
break;
|
||||
case "next":
|
||||
response = await this.handleNext(request);
|
||||
break;
|
||||
case "custom":
|
||||
response = await this.handleCustom(request);
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unsupported dataset type: ${(request as any).dataset}`);
|
||||
}
|
||||
|
||||
console.log(`[Upload2Notion] Notion response status`, response.response?.status);
|
||||
|
||||
if (response.response && response.response.status === 200) {
|
||||
console.log(`[Upload2Notion] Sync success`, {
|
||||
notionPageId: response.data?.id,
|
||||
notionPageUrl: response.data?.url,
|
||||
});
|
||||
await updateYamlInfo(
|
||||
request.markdown,
|
||||
request.nowFile,
|
||||
response.data,
|
||||
request.app,
|
||||
this.plugin,
|
||||
this.dbDetails,
|
||||
);
|
||||
} else {
|
||||
console.log(`[Upload2Notion] Sync failed`, response.data);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
private async handleGeneral(request: GeneralSyncRequest): Promise<NotionPageResponse> {
|
||||
console.log(`[Upload2Notion] Handling general dataset`, {
|
||||
cover: request.cover,
|
||||
tags: request.tags,
|
||||
});
|
||||
const blocks = this.buildBlocks(request.markdown, {
|
||||
notionLimits: {truncate: false},
|
||||
});
|
||||
const notionId = this.getNotionId(request.app, request.nowFile);
|
||||
|
||||
return this.upsertGeneral({
|
||||
title: request.title,
|
||||
cover: request.cover,
|
||||
tags: request.tags || [],
|
||||
childArr: blocks,
|
||||
notionId,
|
||||
});
|
||||
}
|
||||
|
||||
private async handleNext(request: NextSyncRequest): Promise<NotionPageResponse> {
|
||||
console.log(`[Upload2Notion] Handling next dataset`, {
|
||||
type: request.type,
|
||||
slug: request.slug,
|
||||
category: request.category,
|
||||
});
|
||||
const blocks = this.buildBlocks(request.markdown, {
|
||||
notionLimits: {truncate: false},
|
||||
});
|
||||
this.splitRichTextParagraphs(blocks);
|
||||
const notionId = this.getNotionId(request.app, request.nowFile);
|
||||
|
||||
return this.upsertNext({
|
||||
title: request.title,
|
||||
emoji: request.emoji,
|
||||
cover: request.cover,
|
||||
tags: request.tags || [],
|
||||
type: request.type,
|
||||
slug: request.slug,
|
||||
stats: request.stats,
|
||||
category: request.category,
|
||||
summary: request.summary,
|
||||
password: request.password,
|
||||
favicon: request.favicon,
|
||||
datetime: request.datetime,
|
||||
childArr: blocks,
|
||||
notionId,
|
||||
});
|
||||
}
|
||||
|
||||
private async handleCustom(request: CustomSyncRequest): Promise<NotionPageResponse> {
|
||||
console.log(`[Upload2Notion] Handling custom dataset`, {
|
||||
customKeys: Object.keys(request.customValues || {}),
|
||||
});
|
||||
const blocks = this.buildBlocks(request.markdown, {
|
||||
strictImageUrls: true,
|
||||
notionLimits: {truncate: false},
|
||||
});
|
||||
const notionId = this.getNotionId(request.app, request.nowFile);
|
||||
|
||||
return this.upsertCustom({
|
||||
cover: request.cover,
|
||||
customValues: request.customValues,
|
||||
childArr: blocks,
|
||||
notionId,
|
||||
});
|
||||
}
|
||||
|
||||
private buildBlocks(markdown: string, options: Record<string, unknown>): any[] {
|
||||
const yamlContent: any = yamlFrontMatter.loadFront(markdown);
|
||||
const content = yamlContent.__content;
|
||||
return markdownToBlocks(content, options);
|
||||
}
|
||||
|
||||
private getNotionId(app: App, nowFile: TFile): string | undefined {
|
||||
const frontMatter = app.metadataCache.getFileCache(nowFile)?.frontmatter;
|
||||
if (!frontMatter) {
|
||||
return undefined;
|
||||
}
|
||||
const {abName} = this.dbDetails;
|
||||
const notionIDKey = `NotionID-${abName}`;
|
||||
const notionId = frontMatter[notionIDKey];
|
||||
return notionId ? String(notionId) : undefined;
|
||||
}
|
||||
|
||||
private splitRichTextParagraphs(blocks: any[]): void {
|
||||
blocks.forEach((block, index) => {
|
||||
if (
|
||||
block.type === "paragraph" &&
|
||||
block.paragraph.rich_text.length > LIMITS.RICH_TEXT_ARRAYS
|
||||
) {
|
||||
console.log(`[Upload2Notion] Splitting rich text paragraph`, {
|
||||
index,
|
||||
length: block.paragraph.rich_text.length,
|
||||
});
|
||||
const paragraphChunks = this.chunkArray<any>(block.paragraph.rich_text, 100);
|
||||
const newParagraphBlocks = paragraphChunks.map((chunk) => paragraph(chunk));
|
||||
blocks.splice(index, 1, ...newParagraphBlocks);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private chunkArray<T>(items: T[], size: number): T[][] {
|
||||
const result: T[][] = [];
|
||||
for (let i = 0; i < items.length; i += size) {
|
||||
result.push(items.slice(i, i + size));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private async upsertGeneral(params: GeneralParams): Promise<NotionPageResponse> {
|
||||
const {firstChunk, extraChunks} = this.prepareBlocks(params.childArr);
|
||||
const cover = params.notionId
|
||||
? await this.resolveCoverForUpdate(params.cover)
|
||||
: params.cover;
|
||||
|
||||
if (params.notionId) {
|
||||
await this.deletePage(params.notionId);
|
||||
}
|
||||
|
||||
const body = this.buildGeneralBody({
|
||||
title: params.title,
|
||||
tags: params.tags,
|
||||
firstChunk,
|
||||
});
|
||||
this.applyCover(body, cover);
|
||||
console.log(body);
|
||||
return this.submitPage(body, extraChunks);
|
||||
}
|
||||
|
||||
private async upsertNext(params: NextParams): Promise<NotionPageResponse> {
|
||||
const {firstChunk, extraChunks} = this.prepareBlocks(params.childArr);
|
||||
const cover = params.notionId
|
||||
? await this.resolveCoverForUpdate(params.cover)
|
||||
: params.cover;
|
||||
|
||||
if (params.notionId) {
|
||||
await this.deletePage(params.notionId);
|
||||
}
|
||||
|
||||
const body = this.buildNextBody({
|
||||
firstChunk,
|
||||
title: params.title,
|
||||
emoji: params.emoji,
|
||||
tags: params.tags,
|
||||
type: params.type,
|
||||
slug: params.slug,
|
||||
stats: params.stats,
|
||||
category: params.category,
|
||||
summary: params.summary,
|
||||
password: params.password,
|
||||
favicon: params.favicon,
|
||||
datetime: params.datetime,
|
||||
});
|
||||
this.applyCover(body, cover);
|
||||
console.log(body);
|
||||
return this.submitPage(body, extraChunks);
|
||||
}
|
||||
|
||||
private async upsertCustom(params: CustomParams): Promise<NotionPageResponse> {
|
||||
const {firstChunk, extraChunks} = this.prepareBlocks(params.childArr);
|
||||
const cover = params.notionId
|
||||
? await this.resolveCoverForUpdate(params.cover)
|
||||
: params.cover;
|
||||
|
||||
if (params.notionId) {
|
||||
await this.deletePage(params.notionId);
|
||||
}
|
||||
|
||||
const body = this.buildCustomBody({
|
||||
customValues: params.customValues,
|
||||
firstChunk,
|
||||
});
|
||||
this.applyCover(body, cover);
|
||||
console.log(body);
|
||||
return this.submitPage(body, extraChunks);
|
||||
}
|
||||
|
||||
private buildGeneralBody(params: {title: string; tags: string[]; firstChunk: any[]}): any {
|
||||
const {
|
||||
databaseID,
|
||||
customTitleButton,
|
||||
customTitleName,
|
||||
tagButton,
|
||||
} = this.dbDetails;
|
||||
|
||||
return {
|
||||
parent: {
|
||||
database_id: databaseID,
|
||||
},
|
||||
properties: {
|
||||
[customTitleButton ? customTitleName : "title"]: {
|
||||
title: [
|
||||
{
|
||||
text: {
|
||||
content: params.title,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
...(tagButton
|
||||
? {
|
||||
tags: {
|
||||
multi_select:
|
||||
params.tags && params.tags.length > 0
|
||||
? params.tags.map((tag) => ({name: tag}))
|
||||
: [],
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
children: params.firstChunk,
|
||||
};
|
||||
}
|
||||
|
||||
private buildNextBody(params: {
|
||||
firstChunk: any[];
|
||||
title: string;
|
||||
emoji: string;
|
||||
tags: string[];
|
||||
type: string;
|
||||
slug: string;
|
||||
stats: string;
|
||||
category: string;
|
||||
summary: string;
|
||||
password: string;
|
||||
favicon: string;
|
||||
datetime: string;
|
||||
}): any {
|
||||
const {databaseID} = this.dbDetails;
|
||||
|
||||
const pageProperties: any = {
|
||||
parent: {
|
||||
database_id: databaseID,
|
||||
},
|
||||
properties: {
|
||||
title: {
|
||||
title: [
|
||||
{
|
||||
text: {
|
||||
content: params.title,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
type: {
|
||||
select: {
|
||||
name: params.type || "Post",
|
||||
},
|
||||
},
|
||||
status: {
|
||||
select: {
|
||||
name: params.stats || "Draft",
|
||||
},
|
||||
},
|
||||
category: {
|
||||
select: {
|
||||
name: params.category || "Obsidian",
|
||||
},
|
||||
},
|
||||
password: {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: params.password || "",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
icon: {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: params.favicon || "",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
date: {
|
||||
date: {
|
||||
start: params.datetime || new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (params.tags && params.tags.length > 0) {
|
||||
pageProperties.properties.tags = {
|
||||
multi_select: params.tags.map((tag) => ({name: tag})),
|
||||
};
|
||||
}
|
||||
|
||||
if (params.emoji) {
|
||||
pageProperties.icon = {
|
||||
emoji: params.emoji,
|
||||
};
|
||||
}
|
||||
|
||||
if (params.slug) {
|
||||
pageProperties.properties.slug = {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: params.slug,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
if (params.summary) {
|
||||
pageProperties.properties.summary = {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: params.summary,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...pageProperties,
|
||||
children: params.firstChunk,
|
||||
};
|
||||
}
|
||||
|
||||
private buildCustomBody(params: {
|
||||
customValues: Record<string, string>;
|
||||
firstChunk: any[];
|
||||
}): any {
|
||||
const {customProperties, databaseID} = this.dbDetails;
|
||||
const properties: { [key: string]: any } = {};
|
||||
|
||||
if (customProperties) {
|
||||
customProperties.forEach(({customName, customType}) => {
|
||||
if (params.customValues[customName] !== undefined) {
|
||||
const propertyValue = this.buildCustomProperty(customName, customType, params.customValues);
|
||||
if (propertyValue !== undefined) {
|
||||
properties[customName] = propertyValue;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
parent: {
|
||||
database_id: databaseID,
|
||||
},
|
||||
properties,
|
||||
children: params.firstChunk,
|
||||
};
|
||||
}
|
||||
|
||||
private buildCustomProperty(customName: string, customType: string, customValues: Record<string, any>) {
|
||||
const value = customValues[customName] || "";
|
||||
|
||||
switch (customType) {
|
||||
case "title":
|
||||
return {
|
||||
title: [
|
||||
{
|
||||
text: {
|
||||
content: value,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
case "rich_text":
|
||||
return {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: value || "",
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
case "date":
|
||||
return {
|
||||
date: {
|
||||
start: value || new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
case "number":
|
||||
return {
|
||||
number: Number(value),
|
||||
};
|
||||
case "phone_number":
|
||||
return {
|
||||
phone_number: value,
|
||||
};
|
||||
case "email":
|
||||
return {
|
||||
email: value,
|
||||
};
|
||||
case "url":
|
||||
return {
|
||||
url: value,
|
||||
};
|
||||
case "files":
|
||||
return {
|
||||
files: Array.isArray(value)
|
||||
? value.map((url: string) => ({
|
||||
name: url,
|
||||
type: "external",
|
||||
external: {
|
||||
url,
|
||||
},
|
||||
}))
|
||||
: [
|
||||
{
|
||||
name: value,
|
||||
type: "external",
|
||||
external: {
|
||||
url: value,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
case "checkbox":
|
||||
return {
|
||||
checkbox: Boolean(value) || false,
|
||||
};
|
||||
case "select":
|
||||
return {
|
||||
select: {
|
||||
name: value,
|
||||
},
|
||||
};
|
||||
case "multi_select":
|
||||
return {
|
||||
multi_select: Array.isArray(value)
|
||||
? value.map((item: string) => ({name: item}))
|
||||
: [{name: value}],
|
||||
};
|
||||
case "relation":
|
||||
return {
|
||||
relation: Array.isArray(value)
|
||||
? value.map((item: string) => ({id: item}))
|
||||
: [{id: value}],
|
||||
};
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
186
src/upload/common/UploadBase.ts
Normal file
186
src/upload/common/UploadBase.ts
Normal file
@@ -0,0 +1,186 @@
|
||||
import { Notice, requestUrl } from "obsidian";
|
||||
import MyPlugin from "src/main";
|
||||
import { DatabaseDetails } from "../../ui/settingTabs";
|
||||
import { i18nConfig } from "../../lang/I18n";
|
||||
|
||||
export interface NotionPageResponse {
|
||||
response: any;
|
||||
data: any;
|
||||
}
|
||||
|
||||
interface PreparedBlocks {
|
||||
firstChunk: any[];
|
||||
extraChunks: any[][];
|
||||
}
|
||||
|
||||
export abstract class UploadBase {
|
||||
protected plugin: MyPlugin;
|
||||
protected dbDetails: DatabaseDetails;
|
||||
|
||||
protected constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||
this.plugin = plugin;
|
||||
this.dbDetails = dbDetails;
|
||||
}
|
||||
|
||||
async deletePage(notionID: string) {
|
||||
const {notionAPI} = this.dbDetails;
|
||||
return requestUrl({
|
||||
url: `https://api.notion.com/v1/blocks/${notionID}`,
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: "",
|
||||
});
|
||||
}
|
||||
|
||||
protected prepareBlocks(childArr: any[]): PreparedBlocks {
|
||||
this.stripCodeAnnotations(childArr);
|
||||
|
||||
const childArrLength = childArr.length;
|
||||
console.log(`Page includes ${childArrLength} blocks`);
|
||||
|
||||
if (childArrLength <= 100) {
|
||||
return {
|
||||
firstChunk: childArr,
|
||||
extraChunks: [],
|
||||
};
|
||||
}
|
||||
|
||||
const extraChunks: any[][] = [];
|
||||
for (let i = 100; i < childArr.length; i += 100) {
|
||||
extraChunks.push(childArr.slice(i, i + 100));
|
||||
}
|
||||
|
||||
return {
|
||||
firstChunk: childArr.slice(0, 100),
|
||||
extraChunks,
|
||||
};
|
||||
}
|
||||
|
||||
protected applyCover(body: any, cover?: string) {
|
||||
if (cover) {
|
||||
body.cover = {
|
||||
type: "external",
|
||||
external: {
|
||||
url: cover,
|
||||
},
|
||||
};
|
||||
} else if (!body.cover && this.plugin.settings.bannerUrl) {
|
||||
body.cover = {
|
||||
type: "external",
|
||||
external: {
|
||||
url: this.plugin.settings.bannerUrl,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
protected async resolveCoverForUpdate(cover?: string): Promise<string | undefined> {
|
||||
if (cover) {
|
||||
return cover;
|
||||
}
|
||||
const databaseCover = await this.fetchDatabaseCover();
|
||||
return databaseCover ?? undefined;
|
||||
}
|
||||
|
||||
protected async submitPage(body: any, extraChunks: any[][]): Promise<NotionPageResponse> {
|
||||
const {notionAPI} = this.dbDetails;
|
||||
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/pages`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
throw: false,
|
||||
});
|
||||
|
||||
const data = await response.json;
|
||||
|
||||
if (response.status !== 200) {
|
||||
new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||
console.log(`Error message: \n ${data.message}`);
|
||||
} else {
|
||||
console.log(`Page created: ${data.url}`);
|
||||
console.log(`Page ID: ${data.id}`);
|
||||
if (extraChunks.length > 0) {
|
||||
await this.appendExtraBlocks(data.id, extraChunks);
|
||||
}
|
||||
}
|
||||
|
||||
return {response, data};
|
||||
}
|
||||
|
||||
private stripCodeAnnotations(childArr: any[]) {
|
||||
childArr.forEach((block: any) => {
|
||||
if (block.type === "code") {
|
||||
block.code.rich_text.forEach((item: any) => {
|
||||
if (item.type === "text" && item.annotations) {
|
||||
delete item.annotations;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private async appendExtraBlocks(pageId: string, extraChunks: any[][]) {
|
||||
const {notionAPI} = this.dbDetails;
|
||||
|
||||
for (let i = 0; i < extraChunks.length; i++) {
|
||||
const chunk = extraChunks[i];
|
||||
const extraBlocks = {
|
||||
children: chunk,
|
||||
};
|
||||
|
||||
console.log(extraBlocks);
|
||||
|
||||
const extraResponse = await requestUrl({
|
||||
url: `https://api.notion.com/v1/blocks/${pageId}/children`,
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(extraBlocks),
|
||||
});
|
||||
|
||||
const extraData: any = await extraResponse.json;
|
||||
|
||||
if (extraResponse.status !== 200) {
|
||||
new Notice(`Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||
console.log(`Error message: \n ${extraData.message}`);
|
||||
} else {
|
||||
console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${pageId}`);
|
||||
if (i === extraChunks.length - 1) {
|
||||
console.log(`${i18nConfig["BlockUploaded"]} to page: ${pageId}`);
|
||||
new Notice(`${i18nConfig["BlockUploaded"]} page: ${pageId}`, 5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async fetchDatabaseCover(): Promise<string | null> {
|
||||
const {notionAPI, databaseID} = this.dbDetails;
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/databases/${databaseID}`,
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
});
|
||||
|
||||
if (response.json.cover && response.json.cover.external) {
|
||||
return response.json.cover.external.url;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { App, Notice } from "obsidian";
|
||||
import { i18nConfig } from "../../lang/I18n";
|
||||
import { DatabaseDetails } from "../../ui/settingTabs";
|
||||
import {App, Notice} from "obsidian";
|
||||
import {i18nConfig} from "../../lang/I18n";
|
||||
import {DatabaseDetails} from "../../ui/settingTabs";
|
||||
|
||||
export async function getNowFileMarkdownContentCustom(
|
||||
app: App,
|
||||
@@ -24,6 +24,7 @@ export async function getNowFileMarkdownContentCustom(
|
||||
.map(property => property.customName);
|
||||
|
||||
// Extract custom values from the front matter based on the names
|
||||
// Only collect data 'Relation' should be handled separately in the function buildBodyString
|
||||
customPropertyNames.forEach(propertyName => {
|
||||
if (FileCache.frontmatter && FileCache.frontmatter[propertyName] !== undefined) {
|
||||
customValues[propertyName] = FileCache.frontmatter[propertyName];
|
||||
@@ -31,7 +32,8 @@ export async function getNowFileMarkdownContentCustom(
|
||||
});
|
||||
|
||||
// Check if any of the customProperties has a customType of 'title'
|
||||
const titleProperty = dbDetails.customProperties.find(property => property.customType === 'title');
|
||||
const titleProperty = dbDetails.customProperties
|
||||
.find(property => property.customType === 'title');
|
||||
|
||||
// If a 'title' type property exists, use the file's basename as its value
|
||||
if (titleProperty) {
|
||||
@@ -1,13 +1,11 @@
|
||||
import {i18nConfig} from "../lang/I18n";
|
||||
import {App, Notice} from "obsidian";
|
||||
import {Upload2NotionNext} from "./upload_next/Upload2NotionNext";
|
||||
import {Upload2NotionGeneral} from "./upload_general/Upload2NotionGeneral";
|
||||
import {Upload2NotionCustom} from "./upoload_custom/Upload2NotionCustom";
|
||||
import {Upload2Notion} from "./Upload2Notion";
|
||||
import {DatabaseDetails, PluginSettings} from "../ui/settingTabs";
|
||||
import ObsidianSyncNotionPlugin from "../main";
|
||||
import {getNowFileMarkdownContentNext} from "./upload_next/getMarkdownNext";
|
||||
import {getNowFileMarkdownContentGeneral} from "./upload_general/getMarkdownGeneral";
|
||||
import {getNowFileMarkdownContentCustom} from "./upoload_custom/getMarkdownCustom";
|
||||
import {getNowFileMarkdownContentNext} from "./common/getMarkdownNext";
|
||||
import {getNowFileMarkdownContentGeneral} from "./common/getMarkdownGeneral";
|
||||
import {getNowFileMarkdownContentCustom} from "./common/getMarkdownCustom";
|
||||
|
||||
export async function uploadCommandNext(
|
||||
plugin: ObsidianSyncNotionPlugin,
|
||||
@@ -17,6 +15,7 @@ export async function uploadCommandNext(
|
||||
) {
|
||||
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
console.log(`[uploadCommandNext] ${new Date().toISOString()} Triggered for file`, app.workspace.getActiveFile()?.path);
|
||||
|
||||
// Check if the user has set up the Notion API and database ID
|
||||
if (notionAPI === "" || databaseID === "") {
|
||||
@@ -44,14 +43,33 @@ export async function uploadCommandNext(
|
||||
if (markDownData) {
|
||||
const {basename} = nowFile;
|
||||
|
||||
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 upload = new Upload2Notion(plugin, dbDetails);
|
||||
const res = await upload.sync({
|
||||
dataset: "next",
|
||||
title: basename,
|
||||
emoji: emoji || "",
|
||||
cover: cover || "",
|
||||
tags: tags || [],
|
||||
type: type || "",
|
||||
slug: slug || "",
|
||||
stats: stats || "",
|
||||
category: category || "",
|
||||
summary: summary || "",
|
||||
password: paword || "",
|
||||
favicon: favicon || "",
|
||||
datetime: datetime || "",
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
|
||||
const {response} = res;
|
||||
if (response.status === 200) {
|
||||
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
||||
console.log(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`);
|
||||
} else {
|
||||
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
||||
console.log(`${i18nConfig["sync-fail"]} ${basename}`);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -66,6 +84,7 @@ export async function uploadCommandGeneral(
|
||||
) {
|
||||
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
console.log(`[uploadCommandGeneral] ${new Date().toISOString()} Triggered for file`, app.workspace.getActiveFile()?.path);
|
||||
|
||||
// Check if the user has set up the Notion API and database ID
|
||||
if (notionAPI === "" || databaseID === "") {
|
||||
@@ -82,14 +101,24 @@ export async function uploadCommandGeneral(
|
||||
if (markDownData) {
|
||||
const {basename} = nowFile;
|
||||
|
||||
const upload = new Upload2NotionGeneral(plugin, dbDetails);
|
||||
const res = await upload.syncMarkdownToNotionGeneral(basename, cover, tags, markDownData, nowFile, this.app);
|
||||
const upload = new Upload2Notion(plugin, dbDetails);
|
||||
const res = await upload.sync({
|
||||
dataset: "general",
|
||||
title: basename,
|
||||
cover: cover || "",
|
||||
tags: tags || [],
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
|
||||
const {response} = res;
|
||||
if (response.status === 200) {
|
||||
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
||||
console.log(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`);
|
||||
} else {
|
||||
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
||||
console.log(`${i18nConfig["sync-fail"]} ${basename}`);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -103,7 +132,8 @@ export async function uploadCommandCustom(
|
||||
app: App,
|
||||
) {
|
||||
|
||||
const {notionAPI, databaseID} = settings;
|
||||
const {notionAPI, databaseID} = dbDetails;
|
||||
console.log(`[uploadCommandCustom] ${new Date().toISOString()} Triggered for file`, app.workspace.getActiveFile()?.path);
|
||||
|
||||
// Check if the user has set up the Notion API and database ID
|
||||
if (notionAPI === "" || databaseID === "") {
|
||||
@@ -120,14 +150,24 @@ export async function uploadCommandCustom(
|
||||
if (markDownData) {
|
||||
const {basename} = nowFile;
|
||||
|
||||
const upload = new Upload2NotionCustom(plugin, dbDetails);
|
||||
const res = await upload.syncMarkdownToNotionCustom(cover, customValues, markDownData, nowFile, this.app);
|
||||
const upload = new Upload2Notion(plugin, dbDetails);
|
||||
const res = await upload.sync({
|
||||
dataset: "custom",
|
||||
cover: cover || "",
|
||||
customValues,
|
||||
markdown: markDownData,
|
||||
nowFile,
|
||||
app,
|
||||
});
|
||||
|
||||
const {response} = res;
|
||||
|
||||
if (response.status === 200) {
|
||||
new Notice(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`).noticeEl.style.color = "green";
|
||||
console.log(`${i18nConfig["sync-preffix"]} ${basename} ${i18nConfig["sync-success"]}`);
|
||||
} else {
|
||||
new Notice(`${i18nConfig["sync-fail"]} ${basename}`, 5000);
|
||||
console.log(`${i18nConfig["sync-fail"]} ${basename}`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,96 +0,0 @@
|
||||
import { App, Notice, requestUrl, TFile } from "obsidian";
|
||||
import { Client } from '@notionhq/client';
|
||||
import { markdownToBlocks, } from "@jxpeng98/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
// import * as yaml from "yaml"
|
||||
import MyPlugin from "src/main";
|
||||
import { DatabaseDetails } from "../../ui/settingTabs";
|
||||
|
||||
export class UploadBaseGeneral {
|
||||
plugin: MyPlugin;
|
||||
notion: Client;
|
||||
agent: any;
|
||||
dbDetails: DatabaseDetails
|
||||
|
||||
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||
this.plugin = plugin;
|
||||
this.dbDetails = dbDetails
|
||||
}
|
||||
|
||||
async deletePage(notionID: string) {
|
||||
|
||||
const { notionAPI } = this.dbDetails
|
||||
return requestUrl({
|
||||
url: `https://api.notion.com/v1/blocks/${notionID}`,
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
},
|
||||
body: ''
|
||||
});
|
||||
}
|
||||
|
||||
async getDataBase(databaseID: string) {
|
||||
const { notionAPI } = this.dbDetails
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/databases/${databaseID}`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Check if cover is present in the JSON response and then get the URL
|
||||
if (response.json.cover && response.json.cover.external) {
|
||||
return response.json.cover.external.url;
|
||||
} else {
|
||||
return null; // or some other default value, if you prefer
|
||||
}
|
||||
}
|
||||
|
||||
// async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any, app: App, settings: any) {
|
||||
// let {url, id} = res.json
|
||||
// // replace www to notionID
|
||||
// const {notionUser} = this.plugin.settings;
|
||||
//
|
||||
// if (notionUser !== "") {
|
||||
// // replace url str "www" to notionID
|
||||
// url = url.replace("www.notion.so", `${notionUser}.notion.site`)
|
||||
// }
|
||||
//
|
||||
// await app.fileManager.processFrontMatter(nowFile, yamlContent => {
|
||||
// if (yamlContent['notionID']) {
|
||||
// delete yamlContent['notionID']
|
||||
// }
|
||||
// if (yamlContent['link']) {
|
||||
// delete yamlContent['link']
|
||||
// }
|
||||
// // add new notionID and link
|
||||
// yamlContent.notionID = id;
|
||||
// yamlContent.link = url;
|
||||
// });
|
||||
//
|
||||
// try {
|
||||
// await navigator.clipboard.writeText(url)
|
||||
// } catch (error) {
|
||||
// new Notice(`复制链接失败,请手动复制${error}`)
|
||||
// }
|
||||
// // const __content = yamlContent.__content;
|
||||
// // delete yamlContent.__content
|
||||
// // const yamlhead = yaml.stringify(yamlContent)
|
||||
// // // if yamlhead hava last \n remove it
|
||||
// // const yamlhead_remove_n = yamlhead.replace(/\n$/, '')
|
||||
// // // if __content have start \n remove it
|
||||
// // const __content_remove_n = __content.replace(/^\n/, '')
|
||||
// // const content = '---\n' +yamlhead_remove_n +'\n---\n' + __content_remove_n;
|
||||
// // try {
|
||||
// // await nowFile.vault.modify(nowFile, content)
|
||||
// // } catch (error) {
|
||||
// // new Notice(`write file error ${error}`)
|
||||
// // }
|
||||
// }
|
||||
}
|
||||
@@ -1,297 +0,0 @@
|
||||
import {App, Notice, TFile, Platform, requestUrl} from "obsidian";
|
||||
import {markdownToBlocks} from "@jxpeng98/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
import MyPlugin from "src/main";
|
||||
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
||||
import {UploadBaseGeneral} from "./BaseUpload2NotionGeneral";
|
||||
import {updateYamlInfo} from "../updateYaml";
|
||||
import fetch from 'node-fetch';
|
||||
import {i18nConfig} from "../../lang/I18n";
|
||||
|
||||
interface CreatePageResponse {
|
||||
response: any;
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Upload2NotionGeneral extends UploadBaseGeneral {
|
||||
settings: PluginSettings;
|
||||
dbDetails: DatabaseDetails;
|
||||
|
||||
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||
super(plugin, dbDetails);
|
||||
this.dbDetails = dbDetails;
|
||||
}
|
||||
|
||||
// 因为需要解析notion的block进行对比,非常的麻烦,
|
||||
// 暂时就直接删除,新建一个page
|
||||
async updatePage(
|
||||
notionID: string,
|
||||
title: string,
|
||||
cover: string,
|
||||
tags: string[],
|
||||
childArr: any,
|
||||
) {
|
||||
await this.deletePage(notionID);
|
||||
|
||||
const {databaseID} = this.dbDetails;
|
||||
|
||||
const databaseCover = await this.getDataBase(
|
||||
databaseID,
|
||||
);
|
||||
|
||||
if (cover == null) {
|
||||
cover = databaseCover;
|
||||
}
|
||||
|
||||
return await this.createPage(title, cover, tags, childArr);
|
||||
}
|
||||
|
||||
async createPage(
|
||||
title: string,
|
||||
cover: string,
|
||||
tags: string[],
|
||||
childArr: any,
|
||||
): Promise<CreatePageResponse> {
|
||||
|
||||
const {
|
||||
databaseID,
|
||||
customTitleButton,
|
||||
customTitleName,
|
||||
tagButton,
|
||||
notionAPI
|
||||
} = this.dbDetails;
|
||||
|
||||
// remove the annotations from the childArr if type is code block
|
||||
childArr.forEach((block: any) => {
|
||||
if (block.type === "code") {
|
||||
block.code.rich_text.forEach((item: any) => {
|
||||
if (item.type === "text" && item.annotations) {
|
||||
delete item.annotations;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// check the length of the childArr and split it into chunks of 100
|
||||
const childArrLength = childArr.length;
|
||||
let extraArr: any[] = [];
|
||||
let firstArr: any;
|
||||
let pushCount = 0;
|
||||
|
||||
console.log(`Page includes ${childArrLength} blocks`)
|
||||
|
||||
if (childArrLength > 100) {
|
||||
for (let i = 0; i < childArr.length; i += 100) {
|
||||
if (i == 0) {
|
||||
firstArr = childArr.slice(0, 100);
|
||||
} else {
|
||||
const chunk = childArr.slice(i, i + 100);
|
||||
extraArr.push(chunk);
|
||||
pushCount++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
firstArr = childArr;
|
||||
}
|
||||
|
||||
const bodyString: any = {
|
||||
parent: {
|
||||
database_id: databaseID,
|
||||
},
|
||||
properties: {
|
||||
[customTitleButton
|
||||
? customTitleName
|
||||
: "title"]: {
|
||||
title: [
|
||||
{
|
||||
text: {
|
||||
content: title,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
...(tagButton
|
||||
? {
|
||||
tags: {
|
||||
multi_select: tags && true ? tags.map((tag) => ({name: tag})) : [],
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
children: firstArr,
|
||||
};
|
||||
|
||||
if (cover) {
|
||||
bodyString.cover = {
|
||||
type: "external",
|
||||
external: {
|
||||
url: cover,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (!bodyString.cover && this.plugin.settings.bannerUrl) {
|
||||
bodyString.cover = {
|
||||
type: "external",
|
||||
external: {
|
||||
url: this.plugin.settings.bannerUrl,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
console.log(bodyString)
|
||||
console.log(Platform.isDesktopApp)
|
||||
|
||||
let response: any;
|
||||
let data: any;
|
||||
|
||||
if (Platform.isMobileApp) {
|
||||
if(childArrLength > 100) {
|
||||
new Notice(i18nConfig["reach-mobile-limit"], 5000);
|
||||
} else {
|
||||
response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/pages`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
// 'User-Agent': 'obsidian.md',
|
||||
Authorization:
|
||||
"Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(bodyString),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (Platform.isDesktopApp) {
|
||||
response = await fetch("https://api.notion.com/v1/pages", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(bodyString),
|
||||
});
|
||||
|
||||
data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||
console.log(`Error message: \n ${data.message}`);
|
||||
} else {
|
||||
console.log(`Page created: ${data.url}`);
|
||||
console.log(`Page ID: ${data.id}`);
|
||||
}
|
||||
|
||||
// upload the rest of the blocks
|
||||
if (pushCount > 0) {
|
||||
for (let i = 0; i < pushCount; i++) {
|
||||
const extraBlocks = {
|
||||
children: extraArr[i],
|
||||
};
|
||||
|
||||
console.log(extraBlocks)
|
||||
|
||||
const extraResponse = await fetch(`https://api.notion.com/v1/blocks/${data.id}/children`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(extraBlocks),
|
||||
});
|
||||
|
||||
const extraData: any = await extraResponse.json();
|
||||
|
||||
if (!extraResponse.ok) {
|
||||
new Notice(`Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||
console.log(`Error message: \n ${extraData.message}`);
|
||||
} else {
|
||||
console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${data.id}`);
|
||||
if (i === pushCount - 1) {
|
||||
console.log(`${i18nConfig["BlockUploaded"]} to page: ${data.id}`);
|
||||
new Notice(`${i18nConfig["BlockUploaded"]} page: ${data.id}`, 5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
response,
|
||||
data
|
||||
}
|
||||
}
|
||||
|
||||
async syncMarkdownToNotionGeneral(
|
||||
title: string,
|
||||
cover: string,
|
||||
tags: string[],
|
||||
markdown: string,
|
||||
nowFile: TFile,
|
||||
app: App,
|
||||
): Promise<any> {
|
||||
const options = {
|
||||
notionLimits: {
|
||||
truncate: false,
|
||||
}
|
||||
}
|
||||
let res: any;
|
||||
const yamlContent: any = yamlFrontMatter.loadFront(markdown);
|
||||
const __content = yamlContent.__content;
|
||||
const file2Block = markdownToBlocks(__content, options);
|
||||
const frontMatter =
|
||||
app.metadataCache.getFileCache(nowFile)?.frontmatter;
|
||||
const {abName} = this.dbDetails
|
||||
const notionIDKey = `NotionID-${abName}`;
|
||||
const notionID = frontMatter ? frontMatter[notionIDKey] : null;
|
||||
|
||||
|
||||
if (notionID) {
|
||||
res = await this.updatePage(
|
||||
notionID,
|
||||
title,
|
||||
cover,
|
||||
tags,
|
||||
file2Block,
|
||||
);
|
||||
} else {
|
||||
res = await this.createPage(title, cover, tags, file2Block);
|
||||
}
|
||||
|
||||
let {response, data} = res;
|
||||
|
||||
if (Platform.isDesktopApp) {
|
||||
if (response && response.status === 200) {
|
||||
await updateYamlInfo(
|
||||
markdown,
|
||||
nowFile,
|
||||
data,
|
||||
app,
|
||||
this.plugin,
|
||||
this.dbDetails
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (Platform.isMobileApp) {
|
||||
if (response && response.status === 200) {
|
||||
await updateYamlInfo(
|
||||
markdown,
|
||||
nowFile,
|
||||
response,
|
||||
app,
|
||||
this.plugin,
|
||||
this.dbDetails,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
import { App, Notice, requestUrl, TFile } from "obsidian";
|
||||
import { Client } from '@notionhq/client';
|
||||
import { markdownToBlocks, } from "@jxpeng98/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
// import * as yaml from "yaml"
|
||||
import MyPlugin from "src/main";
|
||||
import { DatabaseDetails } from "../../ui/settingTabs";
|
||||
|
||||
export class UploadBaseNext {
|
||||
plugin: MyPlugin;
|
||||
notion: Client;
|
||||
agent: any;
|
||||
dbDetails: DatabaseDetails
|
||||
|
||||
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||
this.plugin = plugin;
|
||||
this.dbDetails = dbDetails
|
||||
}
|
||||
|
||||
async deletePage(notionID: string) {
|
||||
const { notionAPI } = this.dbDetails
|
||||
return requestUrl({
|
||||
url: `https://api.notion.com/v1/blocks/${notionID}`,
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
},
|
||||
body: ''
|
||||
});
|
||||
}
|
||||
|
||||
async getDataBase(databaseID: string) {
|
||||
const { notionAPI } = this.dbDetails
|
||||
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/databases/${databaseID}`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Check if cover is present in the JSON response and then get the URL
|
||||
if (response.json.cover && response.json.cover.external) {
|
||||
return response.json.cover.external.url;
|
||||
} else {
|
||||
return null; // or some other default value, if you prefer
|
||||
}
|
||||
}
|
||||
|
||||
// async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any, app: App, settings: any) {
|
||||
// let {url, id} = res.json
|
||||
// // replace www to notionID
|
||||
// const { notionUser} = this.plugin.settings;
|
||||
//
|
||||
// if (notionUser !== "") {
|
||||
// // replace url str "www" to notionID
|
||||
// url = url.replace("www.notion.so", `${notionUser}.notion.site`)
|
||||
// }
|
||||
//
|
||||
// await app.fileManager.processFrontMatter(nowFile, yamlContent => {
|
||||
// if (yamlContent['notionID']) {
|
||||
// delete yamlContent['notionID']
|
||||
// }
|
||||
// if (yamlContent['link']) {
|
||||
// delete yamlContent['link']
|
||||
// }
|
||||
// // add new notionID and link
|
||||
// yamlContent.notionID = id;
|
||||
// yamlContent.link = url;
|
||||
// });
|
||||
//
|
||||
// try {
|
||||
// await navigator.clipboard.writeText(url)
|
||||
// } catch (error) {
|
||||
// new Notice(`复制链接失败,请手动复制${error}`)
|
||||
// }
|
||||
// // const __content = yamlContent.__content;
|
||||
// // delete yamlContent.__content
|
||||
// // const yamlhead = yaml.stringify(yamlContent)
|
||||
// // // if yamlhead hava last \n remove it
|
||||
// // const yamlhead_remove_n = yamlhead.replace(/\n$/, '')
|
||||
// // // if __content have start \n remove it
|
||||
// // const __content_remove_n = __content.replace(/^\n/, '')
|
||||
// // const content = '---\n' +yamlhead_remove_n +'\n---\n' + __content_remove_n;
|
||||
// // try {
|
||||
// // await nowFile.vault.modify(nowFile, content)
|
||||
// // } catch (error) {
|
||||
// // new Notice(`write file error ${error}`)
|
||||
// // }
|
||||
// }
|
||||
}
|
||||
@@ -1,447 +0,0 @@
|
||||
import {UploadBaseNext} from "./BaseUpload2NotionNext";
|
||||
import {App, Notice, TFile, Platform, requestUrl} from "obsidian";
|
||||
import {markdownToBlocks} from "@jxpeng98/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
import MyPlugin from "src/main";
|
||||
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
||||
import {updateYamlInfo} from "../updateYaml";
|
||||
import {LIMITS, paragraph} from "@jxpeng98/martian/src/notion";
|
||||
import fetch from 'node-fetch';
|
||||
import {i18nConfig} from "../../lang/I18n";
|
||||
|
||||
interface CreatePageResponse {
|
||||
response: any;
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Upload2NotionNext extends UploadBaseNext {
|
||||
settings: PluginSettings;
|
||||
dbDetails: DatabaseDetails;
|
||||
|
||||
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||
super(plugin, dbDetails);
|
||||
this.dbDetails = dbDetails;
|
||||
}
|
||||
|
||||
// 因为需要解析notion的block进行对比,非常的麻烦,
|
||||
// 暂时就直接删除,新建一个page
|
||||
async updatePage(
|
||||
notionID: string,
|
||||
title: string,
|
||||
emoji: string,
|
||||
cover: string,
|
||||
tags: string[],
|
||||
type: string,
|
||||
slug: string,
|
||||
stats: string,
|
||||
category: string,
|
||||
summary: string,
|
||||
paword: string,
|
||||
favicon: string,
|
||||
datetime: string,
|
||||
childArr: any,
|
||||
) {
|
||||
await this.deletePage(notionID);
|
||||
|
||||
const {databaseID} = this.dbDetails;
|
||||
|
||||
const databaseCover = await this.getDataBase(databaseID);
|
||||
|
||||
if (cover == null) {
|
||||
cover = databaseCover;
|
||||
}
|
||||
|
||||
return await this.createPage(
|
||||
title,
|
||||
emoji,
|
||||
cover,
|
||||
tags,
|
||||
type,
|
||||
slug,
|
||||
stats,
|
||||
category,
|
||||
summary,
|
||||
paword,
|
||||
favicon,
|
||||
datetime,
|
||||
childArr,
|
||||
);
|
||||
}
|
||||
|
||||
async createPage(
|
||||
title: string,
|
||||
emoji: string,
|
||||
cover: string,
|
||||
tags: string[],
|
||||
type: string,
|
||||
slug: string,
|
||||
stats: string,
|
||||
category: string,
|
||||
summary: string,
|
||||
pawrod: string,
|
||||
favicon: string,
|
||||
datetime: string,
|
||||
childArr: any,
|
||||
): Promise<CreatePageResponse> {
|
||||
const {databaseID, notionAPI} = this.dbDetails;
|
||||
|
||||
// remove the annotations from the childArr if type is code block
|
||||
childArr.forEach((block: any) => {
|
||||
if (block.type === "code") {
|
||||
block.code.rich_text.forEach((item: any) => {
|
||||
if (item.type === "text" && item.annotations) {
|
||||
delete item.annotations;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// check the length of the childArr
|
||||
// if it is too long, split it into multiple pages
|
||||
const childArrLength = childArr.length;
|
||||
let extraArr: any[] = [];
|
||||
let firstArr: any;
|
||||
let pushCount = 0;
|
||||
|
||||
console.log(`Page includes ${childArrLength} blocks`)
|
||||
|
||||
if (childArrLength > 100) {
|
||||
for (let i = 0; i < childArr.length; i += 100) {
|
||||
if (i == 0) {
|
||||
firstArr = childArr.slice(0, 100);
|
||||
} else {
|
||||
const chunk = childArr.slice(i, i + 100);
|
||||
extraArr.push(chunk);
|
||||
pushCount++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
firstArr = childArr;
|
||||
}
|
||||
|
||||
const pageProperties: any = {
|
||||
parent: {
|
||||
database_id: databaseID,
|
||||
},
|
||||
properties: {
|
||||
title: {
|
||||
title: [
|
||||
{
|
||||
text: {
|
||||
content: title,
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
type: {
|
||||
select: {
|
||||
name: type || "Post",
|
||||
},
|
||||
},
|
||||
status: {
|
||||
select: {
|
||||
name: stats || "Draft",
|
||||
},
|
||||
},
|
||||
category: {
|
||||
select: {
|
||||
name: category || "Obsidian",
|
||||
},
|
||||
},
|
||||
|
||||
password: {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: pawrod || "",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
icon: {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: favicon || "",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
date: {
|
||||
date: {
|
||||
start: datetime || new Date().toISOString(),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
// add tags
|
||||
if (tags) {
|
||||
pageProperties.properties.tags = {
|
||||
multi_select: tags.map((tag) => {
|
||||
return {name: tag};
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
// add title icon
|
||||
if (emoji) {
|
||||
pageProperties.icon = {
|
||||
emoji: emoji,
|
||||
};
|
||||
}
|
||||
|
||||
// add slug
|
||||
if (slug) {
|
||||
pageProperties.properties.slug = {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: slug,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
// check if summary is available
|
||||
if (summary) {
|
||||
pageProperties.properties.summary = {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: summary,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
if (cover) {
|
||||
pageProperties.cover = {
|
||||
type: "external",
|
||||
external: {
|
||||
url: cover,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (!pageProperties.cover && this.plugin.settings.bannerUrl) {
|
||||
pageProperties.cover = {
|
||||
type: "external",
|
||||
external: {
|
||||
url: this.plugin.settings.bannerUrl,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const bodyString: any = {
|
||||
...pageProperties,
|
||||
children: firstArr,
|
||||
};
|
||||
|
||||
console.log(bodyString);
|
||||
|
||||
let response: any;
|
||||
let data: any;
|
||||
|
||||
if (Platform.isMobileApp) {
|
||||
if(childArrLength > 100) {
|
||||
new Notice(i18nConfig["reach-mobile-limit"], 5000);
|
||||
} else {
|
||||
response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/pages`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
// 'User-Agent': 'obsidian.md',
|
||||
Authorization:
|
||||
"Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(bodyString),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (Platform.isDesktopApp) {
|
||||
response = await fetch("https://api.notion.com/v1/pages", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(bodyString),
|
||||
});
|
||||
|
||||
data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||
console.log(`Error message: \n ${data.message}`);
|
||||
} else {
|
||||
console.log(`Page created: ${data.url}`);
|
||||
console.log(`Page ID: ${data.id}`);
|
||||
}
|
||||
|
||||
// upload the rest of the blocks
|
||||
if (pushCount > 0) {
|
||||
for (let i = 0; i < pushCount; i++) {
|
||||
const extraBlocks = {
|
||||
children: extraArr[i],
|
||||
};
|
||||
|
||||
console.log(extraBlocks)
|
||||
|
||||
const extraResponse = await fetch(`https://api.notion.com/v1/blocks/${data.id}/children`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(extraBlocks),
|
||||
});
|
||||
|
||||
const extraData: any = await extraResponse.json();
|
||||
|
||||
if (!extraResponse.ok) {
|
||||
new Notice(`Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||
console.log(`Error message: \n ${extraData.message}`);
|
||||
} else {
|
||||
console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${data.id}`);
|
||||
if (i === pushCount - 1) {
|
||||
console.log(`${i18nConfig["BlockUploaded"]} to page: ${data.id}`);
|
||||
new Notice(`${i18nConfig["BlockUploaded"]} page: ${data.id}`, 5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
response,
|
||||
data
|
||||
};
|
||||
}
|
||||
|
||||
async syncMarkdownToNotionNext(
|
||||
title: string,
|
||||
emoji: string,
|
||||
cover: string,
|
||||
tags: string[],
|
||||
type: string,
|
||||
slug: string,
|
||||
stats: string,
|
||||
category: string,
|
||||
summary: string,
|
||||
paword: string,
|
||||
favicon: string,
|
||||
datetime: string,
|
||||
markdown: string,
|
||||
nowFile: TFile,
|
||||
app: App,
|
||||
): Promise<any> {
|
||||
const options = {
|
||||
notionLimits: {
|
||||
truncate: false,
|
||||
},
|
||||
};
|
||||
let res: any;
|
||||
const yamlContent: any = yamlFrontMatter.loadFront(markdown);
|
||||
const __content = yamlContent.__content;
|
||||
const file2Block = markdownToBlocks(__content, options);
|
||||
const frontmatter =
|
||||
app.metadataCache.getFileCache(nowFile)?.frontmatter;
|
||||
const {abName} = this.dbDetails;
|
||||
const notionIDKey = `NotionID-${abName}`;
|
||||
const notionID = frontmatter ? frontmatter[notionIDKey] : null;
|
||||
|
||||
// increase the limits
|
||||
// Motivated by https://github.com/tryfabric/martian/issues/51
|
||||
file2Block.forEach((block, index) => {
|
||||
if (
|
||||
block.type === "paragraph" &&
|
||||
block.paragraph.rich_text.length > LIMITS.RICH_TEXT_ARRAYS
|
||||
) {
|
||||
const newParagraphBlocks: any[] = [];
|
||||
const chunk: any = [];
|
||||
const richTextChunks = chunk(block.paragraph.rich_text, 100);
|
||||
|
||||
richTextChunks.forEach((chunk: any) => {
|
||||
newParagraphBlocks.push(paragraph(chunk));
|
||||
});
|
||||
|
||||
file2Block.splice(index, 1, ...newParagraphBlocks);
|
||||
}
|
||||
});
|
||||
|
||||
if (notionID) {
|
||||
res = await this.updatePage(
|
||||
notionID,
|
||||
title,
|
||||
emoji,
|
||||
cover,
|
||||
tags,
|
||||
type,
|
||||
slug,
|
||||
stats,
|
||||
category,
|
||||
summary,
|
||||
paword,
|
||||
favicon,
|
||||
datetime,
|
||||
file2Block,
|
||||
);
|
||||
} else {
|
||||
res = await this.createPage(
|
||||
title,
|
||||
emoji,
|
||||
cover,
|
||||
tags,
|
||||
type,
|
||||
slug,
|
||||
stats,
|
||||
category,
|
||||
summary,
|
||||
paword,
|
||||
favicon,
|
||||
datetime,
|
||||
file2Block,
|
||||
);
|
||||
}
|
||||
|
||||
let {response, data} = res;
|
||||
|
||||
if (Platform.isDesktopApp) {
|
||||
if (response && response.status === 200) {
|
||||
await updateYamlInfo(
|
||||
markdown,
|
||||
nowFile,
|
||||
data,
|
||||
app,
|
||||
this.plugin,
|
||||
this.dbDetails
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (Platform.isMobileApp) {
|
||||
if (response && response.status === 200) {
|
||||
await updateYamlInfo(
|
||||
markdown,
|
||||
nowFile,
|
||||
response,
|
||||
app,
|
||||
this.plugin,
|
||||
this.dbDetails,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
import { App, Notice, requestUrl, TFile } from "obsidian";
|
||||
import { Client } from '@notionhq/client';
|
||||
import { markdownToBlocks, } from "@jxpeng98/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
// import * as yaml from "yaml"
|
||||
import MyPlugin from "src/main";
|
||||
import { DatabaseDetails } from "../../ui/settingTabs";
|
||||
|
||||
export class UploadBaseCustom {
|
||||
plugin: MyPlugin;
|
||||
notion: Client;
|
||||
agent: any;
|
||||
dbDetails: DatabaseDetails
|
||||
|
||||
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||
this.plugin = plugin;
|
||||
this.dbDetails = dbDetails
|
||||
}
|
||||
|
||||
async deletePage(notionID: string) {
|
||||
const { notionAPI } = this.dbDetails
|
||||
return requestUrl({
|
||||
url: `https://api.notion.com/v1/blocks/${notionID}`,
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
},
|
||||
body: ''
|
||||
});
|
||||
}
|
||||
|
||||
async getDataBase(databaseID: string) {
|
||||
const { notionAPI } = this.dbDetails
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/databases/${databaseID}`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
// Check if cover is present in the JSON response and then get the URL
|
||||
if (response.json.cover && response.json.cover.external) {
|
||||
return response.json.cover.external.url;
|
||||
} else {
|
||||
return null; // or some other default value, if you prefer
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,378 +0,0 @@
|
||||
import {App, Notice, Platform, TFile, requestUrl} from "obsidian";
|
||||
import {markdownToBlocks} from "@jxpeng98/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
import MyPlugin from "src/main";
|
||||
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
||||
import {updateYamlInfo} from "../updateYaml";
|
||||
import {UploadBaseCustom} from "./BaseUpload2NotionCustom";
|
||||
import fetch from 'node-fetch';
|
||||
import {i18nConfig} from "../../lang/I18n";
|
||||
|
||||
|
||||
interface CreatePageResponse {
|
||||
response: any;
|
||||
data: any;
|
||||
}
|
||||
|
||||
export class Upload2NotionCustom extends UploadBaseCustom {
|
||||
settings: PluginSettings;
|
||||
dbDetails: DatabaseDetails;
|
||||
|
||||
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||
super(plugin, dbDetails);
|
||||
this.dbDetails = dbDetails;
|
||||
}
|
||||
|
||||
// 因为需要解析notion的block进行对比,非常的麻烦,
|
||||
// 暂时就直接删除,新建一个page
|
||||
async updatePage(
|
||||
notionID: string,
|
||||
cover: string,
|
||||
customValues: Record<string, string>,
|
||||
childArr: any,
|
||||
) {
|
||||
await this.deletePage(notionID);
|
||||
|
||||
const {databaseID} = this.dbDetails;
|
||||
|
||||
const databaseCover = await this.getDataBase(
|
||||
databaseID
|
||||
);
|
||||
|
||||
if (cover == null) {
|
||||
cover = databaseCover;
|
||||
}
|
||||
|
||||
return await this.createPage(cover, customValues, childArr);
|
||||
}
|
||||
|
||||
async createPage(
|
||||
cover: string,
|
||||
customValues: Record<string, string>,
|
||||
childArr: any,
|
||||
): Promise<CreatePageResponse> {
|
||||
|
||||
const {
|
||||
databaseID,
|
||||
customProperties,
|
||||
notionAPI
|
||||
} = this.dbDetails;
|
||||
|
||||
// remove the annotations from the childArr if type is code block
|
||||
childArr.forEach((block: any) => {
|
||||
if (block.type === "code") {
|
||||
block.code.rich_text.forEach((item: any) => {
|
||||
if (item.type === "text" && item.annotations) {
|
||||
delete item.annotations;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// check the length of the childArr and split it into chunks of 100
|
||||
const childArrLength = childArr.length;
|
||||
let extraArr: any[] = [];
|
||||
let firstArr: any;
|
||||
let pushCount = 0;
|
||||
|
||||
console.log(`Page includes ${childArrLength} blocks`)
|
||||
|
||||
if (childArrLength > 100) {
|
||||
for (let i = 0; i < childArr.length; i += 100) {
|
||||
if (i == 0) {
|
||||
firstArr = childArr.slice(0, 100);
|
||||
} else {
|
||||
const chunk = childArr.slice(i, i + 100);
|
||||
extraArr.push(chunk);
|
||||
pushCount++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
firstArr = childArr;
|
||||
}
|
||||
|
||||
const bodyString: any = this.buildBodyString(customProperties, customValues, firstArr);
|
||||
|
||||
if (cover) {
|
||||
bodyString.cover = {
|
||||
type: "external",
|
||||
external: {
|
||||
url: cover,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (!bodyString.cover && this.plugin.settings.bannerUrl) {
|
||||
bodyString.cover = {
|
||||
type: "external",
|
||||
external: {
|
||||
url: this.plugin.settings.bannerUrl,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
console.log(bodyString)
|
||||
console.log(Platform.isDesktopApp)
|
||||
|
||||
let response: any;
|
||||
let data: any;
|
||||
|
||||
if (Platform.isMobileApp) {
|
||||
if(childArrLength > 100) {
|
||||
new Notice(i18nConfig["reach-mobile-limit"], 5000);
|
||||
} else {
|
||||
response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/pages`,
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
// 'User-Agent': 'obsidian.md',
|
||||
Authorization:
|
||||
"Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(bodyString),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (Platform.isDesktopApp) {
|
||||
response = await fetch("https://api.notion.com/v1/pages", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(bodyString),
|
||||
});
|
||||
|
||||
data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
new Notice(`Error ${data.status}: ${data.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||
console.log(`Error message: \n ${data.message}`);
|
||||
} else {
|
||||
console.log(`Page created: ${data.url}`);
|
||||
console.log(`Page ID: ${data.id}`);
|
||||
}
|
||||
|
||||
// upload the rest of the blocks
|
||||
if (pushCount > 0) {
|
||||
for (let i = 0; i < pushCount; i++) {
|
||||
const extraBlocks = {
|
||||
children: extraArr[i],
|
||||
};
|
||||
|
||||
console.log(extraBlocks)
|
||||
|
||||
const extraResponse = await fetch(`https://api.notion.com/v1/blocks/${data.id}/children`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer " + notionAPI,
|
||||
"Notion-Version": "2022-06-28",
|
||||
},
|
||||
body: JSON.stringify(extraBlocks),
|
||||
});
|
||||
|
||||
const extraData: any = await extraResponse.json();
|
||||
|
||||
if (!extraResponse.ok) {
|
||||
new Notice(`Error ${extraData.status}: ${extraData.code} \n ${i18nConfig["CheckConsole"]}`, 5000);
|
||||
console.log(`Error message: \n ${extraData.message}`);
|
||||
} else {
|
||||
console.log(`${i18nConfig["ExtraBlockUploaded"]} to page: ${data.id}`);
|
||||
if (i === pushCount - 1) {
|
||||
console.log(`${i18nConfig["BlockUploaded"]} to page: ${data.id}`);
|
||||
new Notice(`${i18nConfig["BlockUploaded"]} page: ${data.id}`, 5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
response, // for status code
|
||||
data // for id and url
|
||||
}
|
||||
}
|
||||
|
||||
async syncMarkdownToNotionCustom(
|
||||
cover: string,
|
||||
customValues: Record<string, string>,
|
||||
markdown: string,
|
||||
nowFile: TFile,
|
||||
app: App,
|
||||
): Promise<any> {
|
||||
const options = {
|
||||
strictImageUrls: true,
|
||||
notionLimits: {
|
||||
truncate: false,
|
||||
}
|
||||
}
|
||||
let res: any;
|
||||
const yamlContent: any = yamlFrontMatter.loadFront(markdown);
|
||||
const __content = yamlContent.__content;
|
||||
const file2Block = markdownToBlocks(__content, options);
|
||||
const frontMatter =
|
||||
app.metadataCache.getFileCache(nowFile)?.frontmatter;
|
||||
const {abName} = this.dbDetails
|
||||
const notionIDKey = `NotionID-${abName}`;
|
||||
const notionID = frontMatter ? frontMatter[notionIDKey] : null;
|
||||
|
||||
if (notionID) {
|
||||
res = await this.updatePage(
|
||||
notionID,
|
||||
cover,
|
||||
customValues,
|
||||
file2Block,
|
||||
);
|
||||
} else {
|
||||
res = await this.createPage(cover, customValues, file2Block);
|
||||
}
|
||||
|
||||
let {response, data} = res;
|
||||
|
||||
// console.log(response)
|
||||
|
||||
if (Platform.isDesktopApp) {
|
||||
if (response && response.status === 200) {
|
||||
await updateYamlInfo(
|
||||
markdown,
|
||||
nowFile,
|
||||
data,
|
||||
app,
|
||||
this.plugin,
|
||||
this.dbDetails
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (Platform.isMobileApp) {
|
||||
if (response && response.status === 200) {
|
||||
await updateYamlInfo(
|
||||
markdown,
|
||||
nowFile,
|
||||
response,
|
||||
app,
|
||||
this.plugin,
|
||||
this.dbDetails,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private buildPropertyObject(customName: string, customType: string, customValues: Record<string, any>) {
|
||||
const value = customValues[customName] || '';
|
||||
|
||||
switch (customType) {
|
||||
case "title":
|
||||
return {
|
||||
title: [
|
||||
{
|
||||
text: {
|
||||
content: value,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
case "rich_text":
|
||||
return {
|
||||
rich_text: [
|
||||
{
|
||||
text: {
|
||||
content: value || '',
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
case "date":
|
||||
return {
|
||||
date: {
|
||||
start: value || new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
case "number":
|
||||
return {
|
||||
number: Number(value),
|
||||
};
|
||||
case "phone_number":
|
||||
return {
|
||||
phone_number: value,
|
||||
};
|
||||
case "email":
|
||||
return {
|
||||
email: value,
|
||||
};
|
||||
case "url":
|
||||
return {
|
||||
url: value,
|
||||
};
|
||||
case "files":
|
||||
return {
|
||||
files: Array.isArray(value) ? value.map(url => ({
|
||||
name: url,
|
||||
type: "external",
|
||||
external: {
|
||||
url: url,
|
||||
},
|
||||
})) : [
|
||||
{
|
||||
name: value,
|
||||
type: "external",
|
||||
external: {
|
||||
url: value,
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
case "checkbox":
|
||||
return {
|
||||
checkbox: Boolean(value) || false,
|
||||
};
|
||||
case "select":
|
||||
return {
|
||||
select: {
|
||||
name: value,
|
||||
},
|
||||
};
|
||||
case "multi_select":
|
||||
return {
|
||||
multi_select: Array.isArray(value) ? value.map(item => ({name: item})) : [{name: value}],
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private buildBodyString(
|
||||
customProperties: { customName: string; customType: string }[],
|
||||
customValues: Record<string, string>,
|
||||
childArr: any,
|
||||
) {
|
||||
|
||||
const properties: { [key: string]: any } = {};
|
||||
|
||||
// Only include custom properties that have values
|
||||
customProperties.forEach(({customName, customType}) => {
|
||||
if (customValues[customName] !== undefined) {
|
||||
properties[customName] = this.buildPropertyObject(customName, customType, customValues);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// console.log(properties)
|
||||
|
||||
return {
|
||||
parent: {
|
||||
database_id: this.dbDetails.databaseID,
|
||||
},
|
||||
properties,
|
||||
children: childArr,
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -460,6 +460,13 @@ commander@^8.3.0:
|
||||
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
|
||||
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==
|
||||
|
||||
cross-fetch@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-4.0.0.tgz#f037aef1580bb3a1a35164ea2a848ba81b445983"
|
||||
integrity sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==
|
||||
dependencies:
|
||||
node-fetch "^2.6.12"
|
||||
|
||||
data-uri-to-buffer@^4.0.0:
|
||||
version "4.0.1"
|
||||
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz#d8feb2b2881e6a4f58c2e08acfd0e2834e26222e"
|
||||
@@ -1199,7 +1206,7 @@ node-domexception@^1.0.0:
|
||||
resolved "https://registry.yarnpkg.com/node-domexception/-/node-domexception-1.0.0.tgz#6888db46a1f71c0b76b3f7555016b63fe64766e5"
|
||||
integrity sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==
|
||||
|
||||
node-fetch@^2.6.1:
|
||||
node-fetch@^2.6.1, node-fetch@^2.6.12:
|
||||
version "2.7.0"
|
||||
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
|
||||
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
|
||||
|
||||
Reference in New Issue
Block a user