mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 08:08:34 +08:00
almost finish the structure
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { i18nConfig } from "src/lang/I18n";
|
import { i18nConfig } from "src/lang/I18n";
|
||||||
import {Editor, MarkdownView, setTooltip} from "obsidian";
|
import {Editor, MarkdownView, setTooltip} from "obsidian";
|
||||||
import { FuzzySuggester, DatabaseList } from "./FuzzySuggester";
|
import { FuzzySuggester, DatabaseList } from "./FuzzySuggester";
|
||||||
import { uploadCommandGeneral, uploadCommandNext } from "../upload/uploadCommand";
|
import {uploadCommandCustom, uploadCommandGeneral, uploadCommandNext} from "../upload/uploadCommand";
|
||||||
import ObsidianSyncNotionPlugin from "src/main";
|
import ObsidianSyncNotionPlugin from "src/main";
|
||||||
import {DatabaseDetails} from "../ui/settingTabs";
|
import {DatabaseDetails} from "../ui/settingTabs";
|
||||||
|
|
||||||
@@ -94,11 +94,11 @@ export default class RibbonCommands {
|
|||||||
await uploadCommandGeneral(this.plugin, this.plugin.settings, dbDetails, this.plugin.app);
|
await uploadCommandGeneral(this.plugin, this.plugin.settings, dbDetails, this.plugin.app);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
// else if (dbDetails.format === 'custom') {
|
else if (dbDetails.format === 'custom') {
|
||||||
// editorCallback = async (editor, view) => {
|
editorCallback = async (editor, view) => {
|
||||||
// await uploadCommandGeneral(this.plugin, dbDetails, this.plugin.app);
|
await uploadCommandCustom(this.plugin, this.plugin.settings, dbDetails, this.plugin.app);
|
||||||
// };
|
};
|
||||||
// }
|
}
|
||||||
|
|
||||||
this.Ncommand.push({ id: commandId, name: commandName, editorCallback });
|
this.Ncommand.push({ id: commandId, name: commandName, editorCallback });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import {ObsidianSettingTab} from "./settingTabs";
|
|||||||
|
|
||||||
export class CustomModal extends Modal {
|
export class CustomModal extends Modal {
|
||||||
propertyLines: Setting[] = []; // Store all property line settings
|
propertyLines: Setting[] = []; // Store all property line settings
|
||||||
properties: { customValue: string, customType: string }[] = []; // Array to store property values and types
|
properties: { customName: string, customType: string }[] = []; // Array to store property values and types
|
||||||
plugin: ObsidianSyncNotionPlugin;
|
plugin: ObsidianSyncNotionPlugin;
|
||||||
settingTab: ObsidianSettingTab;
|
settingTab: ObsidianSettingTab;
|
||||||
|
|
||||||
@@ -14,19 +14,45 @@ export class CustomModal extends Modal {
|
|||||||
|
|
||||||
createPropertyLine(containerEl: HTMLElement): void {
|
createPropertyLine(containerEl: HTMLElement): void {
|
||||||
const propertyIndex = this.properties.length;
|
const propertyIndex = this.properties.length;
|
||||||
this.properties.push({customValue: "", customType: ""}); // Initialize with empty values
|
this.properties.push({customName: "", customType: ""}); // Initialize with empty values
|
||||||
|
|
||||||
const propertyLine = new Setting(containerEl)
|
const propertyLine = new Setting(containerEl)
|
||||||
|
|
||||||
|
if (propertyIndex === 0) {
|
||||||
|
propertyLine
|
||||||
|
.setName("Title")
|
||||||
|
.setDesc("The title of the page, must be the first property")
|
||||||
|
|
||||||
|
propertyLine.addText((text) => {
|
||||||
|
text
|
||||||
|
.setPlaceholder("Property name")
|
||||||
|
.setValue("")
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.properties[propertyIndex].customName = value; // Update the customValue of the specific property
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
propertyLine.addDropdown((dropdown) => {
|
||||||
|
dropdown
|
||||||
|
.addOption("title", "Title")
|
||||||
|
.setValue("")
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.properties[propertyIndex].customType = value; // Update the customType of the specific property
|
||||||
|
});
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} else {
|
||||||
propertyLine
|
propertyLine
|
||||||
.setName("Property " + (propertyIndex + 1))
|
.setName("Property " + (propertyIndex))
|
||||||
|
|
||||||
propertyLine.addText((text) => {
|
propertyLine.addText((text) => {
|
||||||
text
|
text
|
||||||
.setPlaceholder("Property name")
|
.setPlaceholder("Property name")
|
||||||
.setValue("")
|
.setValue("")
|
||||||
.onChange(async (value) => {
|
.onChange(async (value) => {
|
||||||
this.properties[propertyIndex].customValue = value; // Update the customValue of the specific property
|
this.properties[propertyIndex].customName = value; // Update the customValue of the specific property
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -70,7 +96,7 @@ export class CustomModal extends Modal {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
}
|
||||||
this.propertyLines.push(propertyLine);
|
this.propertyLines.push(propertyLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -235,9 +235,6 @@ export class EditModal extends SettingModal {
|
|||||||
// add database id
|
// add database id
|
||||||
this.createSettingEl(nextTabs, i18nConfig.DatabaseID, i18nConfig.NotionAPIDesc, 'password', i18nConfig.DatabaseIDText, this.dataTemp.databaseIDTemp, 'dataTemp', 'databaseIDTemp')
|
this.createSettingEl(nextTabs, i18nConfig.DatabaseID, i18nConfig.NotionAPIDesc, 'password', i18nConfig.DatabaseIDText, this.dataTemp.databaseIDTemp, 'dataTemp', 'databaseIDTemp')
|
||||||
|
|
||||||
// add custom title name
|
|
||||||
this.createSettingEl(nextTabs, i18nConfig.NotionCustomTitleName, i18nConfig.NotionCustomTitleNameDesc, 'text', i18nConfig.NotionCustomTitleText, this.dataTemp.customTitleNameTemp, 'dataTemp', 'customTitleNameTemp')
|
|
||||||
|
|
||||||
// add new property button
|
// add new property button
|
||||||
new Setting(nextTabs)
|
new Setting(nextTabs)
|
||||||
.setName(i18nConfig.NotionCustomValues)
|
.setName(i18nConfig.NotionCustomValues)
|
||||||
|
|||||||
@@ -214,9 +214,6 @@ export class SettingModal extends Modal {
|
|||||||
// add database id
|
// add database id
|
||||||
this.createSettingEl(nextTabs, i18nConfig.DatabaseID, i18nConfig.NotionAPIDesc, 'password', i18nConfig.DatabaseIDText, this.data.databaseID, 'data', 'databaseID')
|
this.createSettingEl(nextTabs, i18nConfig.DatabaseID, i18nConfig.NotionAPIDesc, 'password', i18nConfig.DatabaseIDText, this.data.databaseID, 'data', 'databaseID')
|
||||||
|
|
||||||
// add custom title name
|
|
||||||
this.createSettingEl(nextTabs, i18nConfig.NotionCustomTitleName, i18nConfig.NotionCustomTitleNameDesc, 'text', i18nConfig.NotionCustomTitleText, this.data.customTitleName, 'data', 'customTitleName')
|
|
||||||
|
|
||||||
// add new property button
|
// add new property button
|
||||||
new Setting(nextTabs)
|
new Setting(nextTabs)
|
||||||
.setName(i18nConfig.NotionCustomValues)
|
.setName(i18nConfig.NotionCustomValues)
|
||||||
@@ -251,9 +248,9 @@ export class SettingModal extends Modal {
|
|||||||
renderCustomPreview(properties: any[], nextTabs: HTMLElement) {
|
renderCustomPreview(properties: any[], nextTabs: HTMLElement) {
|
||||||
const previewContainer = nextTabs.createDiv('preview-container');
|
const previewContainer = nextTabs.createDiv('preview-container');
|
||||||
|
|
||||||
properties.forEach((property: { customValue: any; customType: any; }) => {
|
properties.forEach((property: { customName: any; customType: any; }) => {
|
||||||
const propertyEl = previewContainer.createEl('div', {cls: 'property-preview'});
|
const propertyEl = previewContainer.createEl('div', {cls: 'property-preview'});
|
||||||
propertyEl.createEl('span', {text: `Property: ${property.customValue}, Type: ${property.customType}`});
|
propertyEl.createEl('span', {text: `Property: ${property.customName}, Type: ${property.customType}`});
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ export interface DatabaseDetails {
|
|||||||
tagButton: boolean;
|
tagButton: boolean;
|
||||||
customTitleButton: boolean;
|
customTitleButton: boolean;
|
||||||
customTitleName: string;
|
customTitleName: string;
|
||||||
customProperties:{ customValue: string, customType: string }[];
|
customProperties:{ customName: string, customType: string }[];
|
||||||
// customValues: string;
|
// customValues: string;
|
||||||
saved: boolean;
|
saved: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,118 +1,131 @@
|
|||||||
import { i18nConfig } from "../lang/I18n";
|
import {i18nConfig} from "../lang/I18n";
|
||||||
import { App, Notice } from "obsidian";
|
import {App, Notice} from "obsidian";
|
||||||
import { Upload2NotionNext } from "./upload_next/Upload2NotionNext";
|
import {Upload2NotionNext} from "./upload_next/Upload2NotionNext";
|
||||||
import { Upload2NotionGeneral } from "./upload_general/Upload2NotionGeneral";
|
import {Upload2NotionGeneral} from "./upload_general/Upload2NotionGeneral";
|
||||||
import { Upload2NotionCustom } from "./upoload_custom/Upload2NotionCustom";
|
import {Upload2NotionCustom} from "./upoload_custom/Upload2NotionCustom";
|
||||||
import {DatabaseDetails, PluginSettings} from "../ui/settingTabs";
|
import {DatabaseDetails, PluginSettings} from "../ui/settingTabs";
|
||||||
import ObsidianSyncNotionPlugin from "../main";
|
import ObsidianSyncNotionPlugin from "../main";
|
||||||
import { getNowFileMarkdownContentNext } from "./upload_next/getMarkdownNext";
|
import {getNowFileMarkdownContentNext} from "./upload_next/getMarkdownNext";
|
||||||
import { getNowFileMarkdownContentGeneral } from "./upload_general/getMarkdownGeneral";
|
import {getNowFileMarkdownContentGeneral} from "./upload_general/getMarkdownGeneral";
|
||||||
import {getNowFileMarkdownContentCustom} from "./upoload_custom/getMarkdownCustom";
|
import {getNowFileMarkdownContentCustom} from "./upoload_custom/getMarkdownCustom";
|
||||||
|
|
||||||
export async function uploadCommandNext(
|
export async function uploadCommandNext(
|
||||||
plugin: ObsidianSyncNotionPlugin,
|
plugin: ObsidianSyncNotionPlugin,
|
||||||
settings: PluginSettings,
|
settings: PluginSettings,
|
||||||
dbDetails: DatabaseDetails,
|
dbDetails: DatabaseDetails,
|
||||||
app: App,
|
app: App,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
const { notionAPI, databaseID } = dbDetails;
|
const {notionAPI, databaseID} = dbDetails;
|
||||||
|
|
||||||
// Check if NNon exists
|
// Check if NNon exists
|
||||||
// if (NNon === undefined) {
|
// if (NNon === undefined) {
|
||||||
// const NNonmessage = i18nConfig.NNonMissing;
|
// const NNonmessage = i18nConfig.NNonMissing;
|
||||||
// new Notice(NNonmessage);
|
// new Notice(NNonmessage);
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Check if the user has set up the Notion API and database ID
|
// Check if the user has set up the Notion API and database ID
|
||||||
if (notionAPI === "" || databaseID === "") {
|
if (notionAPI === "" || databaseID === "") {
|
||||||
const setAPIMessage = i18nConfig["set-api-id"];
|
const setAPIMessage = i18nConfig["set-api-id"];
|
||||||
new Notice(setAPIMessage);
|
new Notice(setAPIMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { markDownData, nowFile, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime } = await getNowFileMarkdownContentNext(app, settings)
|
const {
|
||||||
|
markDownData,
|
||||||
|
nowFile,
|
||||||
|
emoji,
|
||||||
|
cover,
|
||||||
|
tags,
|
||||||
|
type,
|
||||||
|
slug,
|
||||||
|
stats,
|
||||||
|
category,
|
||||||
|
summary,
|
||||||
|
paword,
|
||||||
|
favicon,
|
||||||
|
datetime
|
||||||
|
} = await getNowFileMarkdownContentNext(app, settings)
|
||||||
|
|
||||||
if (markDownData) {
|
if (markDownData) {
|
||||||
const { basename } = nowFile;
|
const {basename} = nowFile;
|
||||||
const upload = new Upload2NotionNext(plugin, dbDetails);
|
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 res = await upload.syncMarkdownToNotionNext(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, markDownData, nowFile, this.app);
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
||||||
} else {
|
} else {
|
||||||
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export async function uploadCommandGeneral(
|
export async function uploadCommandGeneral(
|
||||||
plugin: ObsidianSyncNotionPlugin,
|
plugin: ObsidianSyncNotionPlugin,
|
||||||
settings: PluginSettings,
|
settings: PluginSettings,
|
||||||
dbDetails: DatabaseDetails,
|
dbDetails: DatabaseDetails,
|
||||||
app: App,
|
app: App,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
const { notionAPI, databaseID } = dbDetails;
|
const {notionAPI, databaseID} = dbDetails;
|
||||||
|
|
||||||
// Check if the user has set up the Notion API and database ID
|
// Check if the user has set up the Notion API and database ID
|
||||||
if (notionAPI === "" || databaseID === "") {
|
if (notionAPI === "" || databaseID === "") {
|
||||||
const setAPIMessage = i18nConfig["set-api-id"];
|
const setAPIMessage = i18nConfig["set-api-id"];
|
||||||
new Notice(setAPIMessage);
|
new Notice(setAPIMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { markDownData, nowFile, cover, tags } = await getNowFileMarkdownContentGeneral(app, settings)
|
const {markDownData, nowFile, cover, tags} = await getNowFileMarkdownContentGeneral(app, settings)
|
||||||
|
|
||||||
if (markDownData) {
|
if (markDownData) {
|
||||||
const { basename } = nowFile;
|
const {basename} = nowFile;
|
||||||
|
|
||||||
const upload = new Upload2NotionGeneral(plugin, dbDetails);
|
const upload = new Upload2NotionGeneral(plugin, dbDetails);
|
||||||
const res = await upload.syncMarkdownToNotionGeneral(basename, cover, tags, markDownData, nowFile, this.app);
|
const res = await upload.syncMarkdownToNotionGeneral(basename, cover, tags, markDownData, nowFile, this.app);
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
||||||
} else {
|
} else {
|
||||||
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function uploadCommandCustom(
|
export async function uploadCommandCustom(
|
||||||
plugin: ObsidianSyncNotionPlugin,
|
plugin: ObsidianSyncNotionPlugin,
|
||||||
settings: PluginSettings,
|
settings: PluginSettings,
|
||||||
dbDetails: DatabaseDetails,
|
dbDetails: DatabaseDetails,
|
||||||
app: App,
|
app: App,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
const { notionAPI, databaseID } = settings;
|
const {notionAPI, databaseID} = settings;
|
||||||
|
|
||||||
// Check if the user has set up the Notion API and database ID
|
// Check if the user has set up the Notion API and database ID
|
||||||
if (notionAPI === "" || databaseID === "") {
|
if (notionAPI === "" || databaseID === "") {
|
||||||
const setAPIMessage = i18nConfig["set-api-id"];
|
const setAPIMessage = i18nConfig["set-api-id"];
|
||||||
new Notice(setAPIMessage);
|
new Notice(setAPIMessage);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { markDownData, nowFile, cover, customValues} = await getNowFileMarkdownContentCustom(app, dbDetails, settings)
|
const {markDownData, nowFile, cover, customValues} = await getNowFileMarkdownContentCustom(app, dbDetails, settings)
|
||||||
|
|
||||||
if (markDownData) {
|
if (markDownData) {
|
||||||
const { basename } = nowFile;
|
const { basename} = nowFile;
|
||||||
|
|
||||||
const upload = new Upload2NotionCustom(plugin,dbDetails);
|
const upload = new Upload2NotionCustom(plugin, dbDetails);
|
||||||
const res = await upload.syncMarkdownToNotionCustom(basename, cover, customValues, markDownData, nowFile, this.app);
|
const res = await upload.syncMarkdownToNotionCustom(cover, customValues, markDownData, nowFile, this.app);
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
||||||
} else {
|
} else {
|
||||||
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,89 +7,90 @@ import MyPlugin from "src/main";
|
|||||||
import {DatabaseDetails} from "../../ui/settingTabs";
|
import {DatabaseDetails} from "../../ui/settingTabs";
|
||||||
|
|
||||||
export class UploadBaseGeneral {
|
export class UploadBaseGeneral {
|
||||||
plugin: MyPlugin;
|
plugin: MyPlugin;
|
||||||
notion: Client;
|
notion: Client;
|
||||||
agent: any;
|
agent: any;
|
||||||
dbDetails: DatabaseDetails
|
dbDetails: DatabaseDetails
|
||||||
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
|
||||||
this.plugin = plugin;
|
constructor(plugin: MyPlugin, dbDetails: DatabaseDetails) {
|
||||||
|
this.plugin = plugin;
|
||||||
this.dbDetails = dbDetails
|
this.dbDetails = dbDetails
|
||||||
}
|
}
|
||||||
|
|
||||||
async deletePage(notionID: string) {
|
async deletePage(notionID: string) {
|
||||||
|
|
||||||
const {notionAPI} = this.dbDetails
|
const {notionAPI} = this.dbDetails
|
||||||
return requestUrl({
|
return requestUrl({
|
||||||
url: `https://api.notion.com/v1/blocks/${notionID}`,
|
url: `https://api.notion.com/v1/blocks/${notionID}`,
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Authorization': 'Bearer ' + notionAPI,
|
'Authorization': 'Bearer ' + notionAPI,
|
||||||
'Notion-Version': '2022-06-28',
|
'Notion-Version': '2022-06-28',
|
||||||
},
|
},
|
||||||
body: ''
|
body: ''
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDataBase(databaseID: string) {
|
async getDataBase(databaseID: string) {
|
||||||
const {notionAPI} = this.dbDetails
|
const {notionAPI} = this.dbDetails
|
||||||
const response = await requestUrl({
|
const response = await requestUrl({
|
||||||
url: `https://api.notion.com/v1/databases/${databaseID}`,
|
url: `https://api.notion.com/v1/databases/${databaseID}`,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': 'Bearer ' + notionAPI,
|
'Authorization': 'Bearer ' + notionAPI,
|
||||||
'Notion-Version': '2022-06-28',
|
'Notion-Version': '2022-06-28',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Check if cover is present in the JSON response and then get the URL
|
// Check if cover is present in the JSON response and then get the URL
|
||||||
if (response.json.cover && response.json.cover.external) {
|
if (response.json.cover && response.json.cover.external) {
|
||||||
return response.json.cover.external.url;
|
return response.json.cover.external.url;
|
||||||
} else {
|
} else {
|
||||||
return null; // or some other default value, if you prefer
|
return null; // or some other default value, if you prefer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any, app: App, settings: any) {
|
// async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any, app: App, settings: any) {
|
||||||
// let {url, id} = res.json
|
// let {url, id} = res.json
|
||||||
// // replace www to notionID
|
// // replace www to notionID
|
||||||
// const {notionUser} = this.plugin.settings;
|
// const {notionUser} = this.plugin.settings;
|
||||||
//
|
//
|
||||||
// if (notionUser !== "") {
|
// if (notionUser !== "") {
|
||||||
// // replace url str "www" to notionID
|
// // replace url str "www" to notionID
|
||||||
// url = url.replace("www.notion.so", `${notionUser}.notion.site`)
|
// url = url.replace("www.notion.so", `${notionUser}.notion.site`)
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// await app.fileManager.processFrontMatter(nowFile, yamlContent => {
|
// await app.fileManager.processFrontMatter(nowFile, yamlContent => {
|
||||||
// if (yamlContent['notionID']) {
|
// if (yamlContent['notionID']) {
|
||||||
// delete yamlContent['notionID']
|
// delete yamlContent['notionID']
|
||||||
// }
|
// }
|
||||||
// if (yamlContent['link']) {
|
// if (yamlContent['link']) {
|
||||||
// delete yamlContent['link']
|
// delete yamlContent['link']
|
||||||
// }
|
// }
|
||||||
// // add new notionID and link
|
// // add new notionID and link
|
||||||
// yamlContent.notionID = id;
|
// yamlContent.notionID = id;
|
||||||
// yamlContent.link = url;
|
// yamlContent.link = url;
|
||||||
// });
|
// });
|
||||||
//
|
//
|
||||||
// try {
|
// try {
|
||||||
// await navigator.clipboard.writeText(url)
|
// await navigator.clipboard.writeText(url)
|
||||||
// } catch (error) {
|
// } catch (error) {
|
||||||
// new Notice(`复制链接失败,请手动复制${error}`)
|
// new Notice(`复制链接失败,请手动复制${error}`)
|
||||||
// }
|
// }
|
||||||
// // const __content = yamlContent.__content;
|
// // const __content = yamlContent.__content;
|
||||||
// // delete yamlContent.__content
|
// // delete yamlContent.__content
|
||||||
// // const yamlhead = yaml.stringify(yamlContent)
|
// // const yamlhead = yaml.stringify(yamlContent)
|
||||||
// // // if yamlhead hava last \n remove it
|
// // // if yamlhead hava last \n remove it
|
||||||
// // const yamlhead_remove_n = yamlhead.replace(/\n$/, '')
|
// // const yamlhead_remove_n = yamlhead.replace(/\n$/, '')
|
||||||
// // // if __content have start \n remove it
|
// // // if __content have start \n remove it
|
||||||
// // const __content_remove_n = __content.replace(/^\n/, '')
|
// // const __content_remove_n = __content.replace(/^\n/, '')
|
||||||
// // const content = '---\n' +yamlhead_remove_n +'\n---\n' + __content_remove_n;
|
// // const content = '---\n' +yamlhead_remove_n +'\n---\n' + __content_remove_n;
|
||||||
// // try {
|
// // try {
|
||||||
// // await nowFile.vault.modify(nowFile, content)
|
// // await nowFile.vault.modify(nowFile, content)
|
||||||
// // } catch (error) {
|
// // } catch (error) {
|
||||||
// // new Notice(`write file error ${error}`)
|
// // new Notice(`write file error ${error}`)
|
||||||
// // }
|
// // }
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,31 +3,31 @@ import {i18nConfig} from "../../lang/I18n";
|
|||||||
import {PluginSettings} from "../../ui/settingTabs";
|
import {PluginSettings} from "../../ui/settingTabs";
|
||||||
|
|
||||||
export async function getNowFileMarkdownContentGeneral(
|
export async function getNowFileMarkdownContentGeneral(
|
||||||
app: App,
|
app: App,
|
||||||
settings: PluginSettings,
|
settings: PluginSettings,
|
||||||
) {
|
) {
|
||||||
const nowFile = app.workspace.getActiveFile();
|
const nowFile = app.workspace.getActiveFile();
|
||||||
let cover = '';
|
let cover = '';
|
||||||
let tags = [];
|
let tags = [];
|
||||||
|
|
||||||
const FileCache = app.metadataCache.getFileCache(nowFile);
|
const FileCache = app.metadataCache.getFileCache(nowFile);
|
||||||
try {
|
try {
|
||||||
cover = FileCache.frontmatter.coverurl;
|
cover = FileCache.frontmatter.coverurl;
|
||||||
tags = FileCache.frontmatter.tags;
|
tags = FileCache.frontmatter.tags;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
new Notice(i18nConfig["set-tags-fail"]);
|
new Notice(i18nConfig["set-tags-fail"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nowFile) {
|
if (nowFile) {
|
||||||
const markDownData = await nowFile.vault.read(nowFile);
|
const markDownData = await nowFile.vault.read(nowFile);
|
||||||
return {
|
return {
|
||||||
markDownData,
|
markDownData,
|
||||||
nowFile,
|
nowFile,
|
||||||
cover,
|
cover,
|
||||||
tags,
|
tags,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
new Notice(i18nConfig["open-file"]);
|
new Notice(i18nConfig["open-file"]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import { App, Notice, requestUrl, TFile } from "obsidian";
|
import {App, Notice, requestUrl, TFile} from "obsidian";
|
||||||
import { Client } from "@notionhq/client";
|
import {markdownToBlocks} from "@tryfabric/martian";
|
||||||
import { markdownToBlocks } from "@tryfabric/martian";
|
|
||||||
import * as yamlFrontMatter from "yaml-front-matter";
|
import * as yamlFrontMatter from "yaml-front-matter";
|
||||||
// import * as yaml from "yaml"
|
// import * as yaml from "yaml"
|
||||||
import MyPlugin from "src/main";
|
import MyPlugin from "src/main";
|
||||||
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
||||||
import { updateYamlInfo } from "../updateYaml";
|
import {updateYamlInfo} from "../updateYaml";
|
||||||
import {UploadBaseCustom} from "./BaseUpload2NotionCustom";
|
import {UploadBaseCustom} from "./BaseUpload2NotionCustom";
|
||||||
|
|
||||||
export class Upload2NotionCustom extends UploadBaseCustom {
|
export class Upload2NotionCustom extends UploadBaseCustom {
|
||||||
@@ -21,14 +20,13 @@ export class Upload2NotionCustom extends UploadBaseCustom {
|
|||||||
// 暂时就直接删除,新建一个page
|
// 暂时就直接删除,新建一个page
|
||||||
async updatePage(
|
async updatePage(
|
||||||
notionID: string,
|
notionID: string,
|
||||||
title: string,
|
|
||||||
cover: string,
|
cover: string,
|
||||||
customValues: Record<string, string>,
|
customValues: Record<string, string>,
|
||||||
childArr: any,
|
childArr: any,
|
||||||
) {
|
) {
|
||||||
await this.deletePage(notionID);
|
await this.deletePage(notionID);
|
||||||
|
|
||||||
const { databaseID } = this.dbDetails;
|
const {databaseID} = this.dbDetails;
|
||||||
|
|
||||||
const databaseCover = await this.getDataBase(
|
const databaseCover = await this.getDataBase(
|
||||||
databaseID
|
databaseID
|
||||||
@@ -38,11 +36,10 @@ export class Upload2NotionCustom extends UploadBaseCustom {
|
|||||||
cover = databaseCover;
|
cover = databaseCover;
|
||||||
}
|
}
|
||||||
|
|
||||||
return await this.createPage(title, cover, customValues, childArr);
|
return await this.createPage(cover, customValues, childArr);
|
||||||
}
|
}
|
||||||
|
|
||||||
async createPage(
|
async createPage(
|
||||||
title: string,
|
|
||||||
cover: string,
|
cover: string,
|
||||||
customValues: Record<string, string>,
|
customValues: Record<string, string>,
|
||||||
childArr: any,
|
childArr: any,
|
||||||
@@ -50,42 +47,11 @@ export class Upload2NotionCustom extends UploadBaseCustom {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
databaseID,
|
databaseID,
|
||||||
customTitleButton,
|
customProperties,
|
||||||
customTitleName,
|
|
||||||
notionAPI
|
notionAPI
|
||||||
} = this.dbDetails;
|
} = this.dbDetails;
|
||||||
|
|
||||||
const bodyString: any = {
|
const bodyString: any = this.buildBodyString(customProperties, customValues, childArr);
|
||||||
parent: {
|
|
||||||
database_id: databaseID,
|
|
||||||
},
|
|
||||||
properties: {
|
|
||||||
[customTitleButton
|
|
||||||
? customTitleName
|
|
||||||
: "title"]: {
|
|
||||||
title: [
|
|
||||||
{
|
|
||||||
text: {
|
|
||||||
content: title,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
...(Object.keys(customValues).reduce((acc, key) => {
|
|
||||||
acc[key] = {
|
|
||||||
rich_text: [
|
|
||||||
{
|
|
||||||
text: {
|
|
||||||
content: customValues[key] || '',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
return acc;
|
|
||||||
}, {} as Record<string, { rich_text: { text: { content: string } }[] } >)),
|
|
||||||
},
|
|
||||||
children: childArr,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (cover) {
|
if (cover) {
|
||||||
bodyString.cover = {
|
bodyString.cover = {
|
||||||
@@ -124,7 +90,6 @@ export class Upload2NotionCustom extends UploadBaseCustom {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async syncMarkdownToNotionCustom(
|
async syncMarkdownToNotionCustom(
|
||||||
title: string,
|
|
||||||
cover: string,
|
cover: string,
|
||||||
customValues: Record<string, string>,
|
customValues: Record<string, string>,
|
||||||
markdown: string,
|
markdown: string,
|
||||||
@@ -147,13 +112,12 @@ export class Upload2NotionCustom extends UploadBaseCustom {
|
|||||||
if (notionID) {
|
if (notionID) {
|
||||||
res = await this.updatePage(
|
res = await this.updatePage(
|
||||||
notionID,
|
notionID,
|
||||||
title,
|
|
||||||
cover,
|
cover,
|
||||||
customValues,
|
customValues,
|
||||||
file2Block,
|
file2Block,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
res = await this.createPage(title, cover, customValues, file2Block);
|
res = await this.createPage(cover, customValues, file2Block);
|
||||||
}
|
}
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
await updateYamlInfo(markdown, nowFile, res, app, this.plugin, this.dbDetails);
|
await updateYamlInfo(markdown, nowFile, res, app, this.plugin, this.dbDetails);
|
||||||
@@ -162,4 +126,72 @@ export class Upload2NotionCustom extends UploadBaseCustom {
|
|||||||
}
|
}
|
||||||
return res;
|
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 "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 } = {};
|
||||||
|
|
||||||
|
customProperties.forEach(({customName, customType}) => {
|
||||||
|
properties[customName] = this.buildPropertyObject(customName, customType, customValues);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
parent: {
|
||||||
|
database_id: this.dbDetails.databaseID,
|
||||||
|
},
|
||||||
|
properties,
|
||||||
|
children: childArr,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import {App, Notice} from "obsidian";
|
import {App, Notice, TAbstractFile, TFile} from "obsidian";
|
||||||
import {i18nConfig} from "../../lang/I18n";
|
import {i18nConfig} from "../../lang/I18n";
|
||||||
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
import {DatabaseDetails, PluginSettings} from "../../ui/settingTabs";
|
||||||
|
|
||||||
@@ -8,22 +8,29 @@ export async function getNowFileMarkdownContentCustom(
|
|||||||
settings: PluginSettings,
|
settings: PluginSettings,
|
||||||
) {
|
) {
|
||||||
const nowFile = app.workspace.getActiveFile();
|
const nowFile = app.workspace.getActiveFile();
|
||||||
let cover = '';
|
if (!nowFile) {
|
||||||
let customValues: Record<string, any> = {}; // Change 'any' to a more specific type if possible
|
new Notice(i18nConfig["open-file"]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let cover = '';
|
||||||
|
let customValues: Record<string, any> = {};
|
||||||
|
|
||||||
const FileCache = app.metadataCache.getFileCache(nowFile);
|
const FileCache = app.metadataCache.getFileCache(nowFile);
|
||||||
try {
|
try {
|
||||||
cover = FileCache.frontmatter.coverurl;
|
cover = FileCache.frontmatter.coverurl;
|
||||||
|
|
||||||
// Get custom property names from dbDetails
|
// Get custom property names from dbDetails
|
||||||
const customPropertyNames = dbDetails.customProperties.map(property => property.customValue);
|
const customPropertyValues = dbDetails.customProperties.map(property => property.customName);
|
||||||
|
|
||||||
// Extract custom values from the frontmatter based on the names
|
// Extract custom values from the frontmatter based on the names
|
||||||
customPropertyNames.forEach(propertyName => {
|
customPropertyValues.forEach( propertyName => {
|
||||||
if (FileCache.frontmatter[propertyName] !== undefined) {
|
if (FileCache.frontmatter[propertyName] !== undefined) {
|
||||||
customValues[propertyName] = FileCache.frontmatter[propertyName];
|
customValues[propertyName] = FileCache.frontmatter[propertyName];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
customValues['title'] = nowFile.basename; // Use 'basename' for the file name without extension
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
new Notice(i18nConfig["set-tags-fail"]);
|
new Notice(i18nConfig["set-tags-fail"]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user