mirror of
https://github.com/jxpeng98/obsidian-to-NotionNext
synced 2026-07-29 16:35:57 +08:00
restructure the code for further feature
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { FuzzySuggestModal, FuzzyMatch } from 'obsidian';
|
||||
import MyPlugin from "./main";
|
||||
import {i18nConfig} from "./lang/I18n";
|
||||
import MyPlugin from "../main";
|
||||
import {i18nConfig} from "../lang/I18n";
|
||||
|
||||
/**
|
||||
* Simple interface for what should be displayed and stored for suggester
|
||||
@@ -10,9 +10,7 @@ export interface DatabaseList {
|
||||
match: any //
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic suggester for quick reuse
|
||||
*/
|
||||
|
||||
export class FuzzySuggester extends FuzzySuggestModal<DatabaseList>{
|
||||
private plugin: MyPlugin;
|
||||
private data: DatabaseList[];
|
||||
@@ -1,17 +1,21 @@
|
||||
import {i18nConfig} from "src/lang/I18n";
|
||||
import MyPlugin from "src/main";
|
||||
import {Editor, MarkdownView} from "obsidian";
|
||||
import {FuzzySuggester, DatabaseList} from "./FuzzySuggester";
|
||||
import {uploadCommand} from "../upload/uploadCommand";
|
||||
import ObsidianSyncNotionPlugin from "src/main";
|
||||
|
||||
// create the commands list
|
||||
export default class RibbonCommands {
|
||||
plugin: MyPlugin;
|
||||
plugin: ObsidianSyncNotionPlugin;
|
||||
|
||||
// Total commands that will be used
|
||||
Ncommand = [
|
||||
{
|
||||
id: "share-to-notionnext",
|
||||
name: i18nConfig.ribbonIcon, // Use the translated text from i18nConfig
|
||||
editorCallback: async (editor: Editor, view: MarkdownView) => {
|
||||
await this.plugin.upload();
|
||||
// await this.plugin.uploadCommand()
|
||||
await uploadCommand(this.plugin, this.plugin.settings, this.plugin.app)
|
||||
}
|
||||
},
|
||||
// {
|
||||
@@ -39,7 +43,7 @@ export default class RibbonCommands {
|
||||
await fusg.display(async (results) => {await results.match()})
|
||||
};
|
||||
|
||||
constructor(plugin: MyPlugin) {
|
||||
constructor(plugin: ObsidianSyncNotionPlugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
// Register all the commands
|
||||
@@ -10,8 +10,8 @@ export const I18n: { [key: string]: any } = {
|
||||
NotionAPI: "Notion API Token",
|
||||
NotionAPIDesc: "It's a secret",
|
||||
NotionAPIText: "Enter your Notion API Token",
|
||||
NotionID: "Database ID",
|
||||
NotionIDText: "Enter your Database ID",
|
||||
DatabaseID: "Database ID",
|
||||
DatabaseIDText: "Enter your Database ID",
|
||||
BannerUrl: "Banner url(optional)",
|
||||
BannerUrlDesc:
|
||||
"page banner url(optional), default is empty, if you want to show a banner, please enter the url(like:https://raw.githubusercontent.com/EasyChris/obsidian-to-notion/ae7a9ac6cf427f3ca338a409ce6967ced9506f12/doc/2.png)",
|
||||
@@ -49,8 +49,8 @@ export const I18n: { [key: string]: any } = {
|
||||
NotionAPI: "Notion API 令牌",
|
||||
NotionAPIDesc: "显示为密码",
|
||||
NotionAPIText: "输入你的 Notion API 令牌",
|
||||
NotionID: "数据库 ID",
|
||||
NotionIDText: "输入你的数据库 ID",
|
||||
DatabaseID: "数据库 ID",
|
||||
DatabaseIDText: "输入你的数据库 ID",
|
||||
BannerUrl: "封面图片地址(可选)",
|
||||
BannerUrlDesc:
|
||||
"页面封面图片地址(可选),默认为空,如果你想显示封面图片,请输入图片地址(例如:https://raw.githubusercontent.com/EasyChris/obsidian-to-notion/ae7a9ac6cf427f3ca338a409ce6967ced9506f12/doc/2.png)",
|
||||
|
||||
275
src/main.ts
275
src/main.ts
@@ -3,30 +3,16 @@ import {addIcons} from 'src/ui/icon';
|
||||
import {Upload2NotionGeneral} from "src/upload/Upload2NotionGeneral";
|
||||
import {Upload2NotionNext} from "src/upload/Upload2NotionNext";
|
||||
import {i18nConfig} from "src/lang/I18n";
|
||||
import ribbonCommands from "src/NotionCommands";
|
||||
import ribbonCommands from "src/commands/NotionCommands";
|
||||
import { ObsidianSettingTab, PluginSettings, DEFAULT_SETTINGS } from "src/ui/settingTabs";
|
||||
import {getNowFileMarkdownContent} from "src/upload/getMarkdown";
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
interface PluginSettings {
|
||||
NNon: boolean;
|
||||
notionAPI: string;
|
||||
databaseID: string;
|
||||
bannerUrl: string;
|
||||
notionID: string;
|
||||
proxy: string;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: PluginSettings = {
|
||||
NNon: undefined,
|
||||
notionAPI: "",
|
||||
databaseID: "",
|
||||
bannerUrl: "",
|
||||
notionID: "",
|
||||
proxy: "",
|
||||
};
|
||||
|
||||
export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
settings: PluginSettings;
|
||||
commands: ribbonCommands;
|
||||
app: App;
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
@@ -39,23 +25,15 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
i18nConfig.ribbonIcon,
|
||||
async (evt: MouseEvent) => {
|
||||
// Called when the user clicks the icon.
|
||||
// await this.upload();
|
||||
// await this.uploadCommand();
|
||||
this.commands.ribbonDisplay();
|
||||
}
|
||||
);
|
||||
|
||||
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
|
||||
const statusBarItemEl = this.addStatusBarItem();
|
||||
// statusBarItemEl.setText("share to notion");
|
||||
|
||||
this.addCommand({
|
||||
id: "share-to-notionnext",
|
||||
name: i18nConfig.CommandName,
|
||||
editorCallback: async (editor: Editor, view: MarkdownView) => {
|
||||
await this.upload()
|
||||
},
|
||||
});
|
||||
|
||||
// const statusBarItemEl = this.addStatusBarItem();
|
||||
// // statusBarItemEl.setText("share to notion");
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new ObsidianSettingTab(this.app, this));
|
||||
@@ -65,103 +43,47 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
onunload() {
|
||||
}
|
||||
|
||||
async upload() {
|
||||
const {notionAPI, databaseID, NNon} = this.settings;
|
||||
// async uploadCommand() {
|
||||
// const {notionAPI, databaseID, NNon} = this.settings;
|
||||
//
|
||||
// // Check if NNon exists
|
||||
// if (NNon === undefined) {
|
||||
// const NNonmessage = i18nConfig.NNonMissing;
|
||||
// new Notice(NNonmessage);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// // Check if the user has set up the Notion API and database ID
|
||||
// if (notionAPI === "" || databaseID === "") {
|
||||
// const setAPIMessage = i18nConfig["set-api-id"];
|
||||
// new Notice(setAPIMessage);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// const {markDownData, nowFile, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime} = await getNowFileMarkdownContent(this.app, this.settings)
|
||||
//
|
||||
// if (markDownData) {
|
||||
// const {basename} = nowFile;
|
||||
// let upload;
|
||||
// let res;
|
||||
//
|
||||
// if (NNon) {
|
||||
// upload = new Upload2NotionNext(this);
|
||||
// res = await upload.syncMarkdownToNotionNext(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, markDownData, nowFile, this.app, this.settings);
|
||||
// } else {
|
||||
// upload = new Upload2NotionGeneral(this);
|
||||
// res = await upload.syncMarkdownToNotionGeneral(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, markDownData, nowFile, this.app, this.settings);
|
||||
// }
|
||||
//
|
||||
//
|
||||
// if (res.status === 200) {
|
||||
// new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
||||
// } else {
|
||||
// new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// Check if NNon exists
|
||||
if (NNon === undefined) {
|
||||
const NNonmessage = i18nConfig.NNonMissing;
|
||||
new Notice(NNonmessage);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the user has set up the Notion API and database ID
|
||||
if (notionAPI === "" || databaseID === "") {
|
||||
const setAPIMessage = i18nConfig["set-api-id"];
|
||||
new Notice(setAPIMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
const {markDownData, nowFile, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime} = await this.getNowFileMarkdownContent(this.app);
|
||||
|
||||
|
||||
if (markDownData) {
|
||||
const {basename} = nowFile;
|
||||
let upload;
|
||||
let res;
|
||||
|
||||
if (NNon) {
|
||||
upload = new Upload2NotionNext(this);
|
||||
res = await upload.syncMarkdownToNotionNext(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, markDownData, nowFile, this.app, this.settings);
|
||||
} else {
|
||||
upload = new Upload2NotionGeneral(this);
|
||||
res = await upload.syncMarkdownToNotionGeneral(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, markDownData, nowFile, this.app, this.settings);
|
||||
}
|
||||
|
||||
|
||||
if (res.status === 200) {
|
||||
new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
||||
} else {
|
||||
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getNowFileMarkdownContent(app: App) {
|
||||
const nowFile = app.workspace.getActiveFile();
|
||||
const {NNon} = this.settings;
|
||||
let emoji = ''
|
||||
let cover = ''
|
||||
let tags = []
|
||||
let type = ''
|
||||
let slug = ''
|
||||
let stats = ''
|
||||
let category = ''
|
||||
let summary = ''
|
||||
let paword = ''
|
||||
let favicon = ''
|
||||
let datetime = ''
|
||||
|
||||
const FileCache = app.metadataCache.getFileCache(nowFile)
|
||||
try {
|
||||
if (NNon) {
|
||||
emoji = FileCache.frontmatter.titleicon;
|
||||
cover = FileCache.frontmatter.coverurl;
|
||||
tags = FileCache.frontmatter.tags;
|
||||
type = FileCache.frontmatter.type;
|
||||
slug = FileCache.frontmatter.slug;
|
||||
stats = FileCache.frontmatter.stats;
|
||||
category = FileCache.frontmatter.category;
|
||||
summary = FileCache.frontmatter.summary;
|
||||
paword = FileCache.frontmatter.password;
|
||||
favicon = FileCache.frontmatter.icon;
|
||||
datetime = FileCache.frontmatter.date;
|
||||
}
|
||||
} catch (error) {
|
||||
new Notice(i18nConfig["set-tags-fail"]);
|
||||
}
|
||||
if (nowFile) {
|
||||
const markDownData = await nowFile.vault.read(nowFile);
|
||||
return {
|
||||
markDownData,
|
||||
nowFile,
|
||||
emoji,
|
||||
cover,
|
||||
tags,
|
||||
type,
|
||||
slug,
|
||||
stats,
|
||||
category,
|
||||
summary,
|
||||
paword,
|
||||
favicon,
|
||||
datetime,
|
||||
};
|
||||
} else {
|
||||
new Notice(i18nConfig["open-file"]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign(
|
||||
@@ -174,110 +96,9 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
|
||||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class ObsidianSettingTab extends PluginSettingTab {
|
||||
plugin: ObsidianSyncNotionPlugin;
|
||||
|
||||
constructor(app: App, plugin: ObsidianSyncNotionPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.NotionNextVersion)
|
||||
.setDesc(i18nConfig.NotionNextVersionDesc)
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.plugin.settings.NNon)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.NNon = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
containerEl.createEl('h2', {text: i18nConfig.NotionNextSetting})
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.NotionAPI)
|
||||
.setDesc(i18nConfig.NotionAPIDesc)
|
||||
.addText((text) => {
|
||||
text.inputEl.type = 'password';
|
||||
return text
|
||||
.setPlaceholder(i18nConfig.NotionAPIText)
|
||||
.setValue(this.plugin.settings.notionAPI)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.notionAPI = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
const notionDatabaseID = new Setting(containerEl)
|
||||
.setName(i18nConfig.NotionID)
|
||||
.setDesc(i18nConfig.NotionAPIDesc)
|
||||
.addText((text) => {
|
||||
text.inputEl.type = 'password';
|
||||
return text
|
||||
.setPlaceholder(i18nConfig.NotionIDText)
|
||||
.setValue(this.plugin.settings.databaseID)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.databaseID = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
// notionDatabaseID.controlEl.querySelector('input').type='password'
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.BannerUrl)
|
||||
.setDesc(i18nConfig.BannerUrlDesc)
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder(i18nConfig.BannerUrlText)
|
||||
.setValue(this.plugin.settings.bannerUrl)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.bannerUrl = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.NotionUser)
|
||||
.setDesc(i18nConfig.NotionUserDesc)
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder(i18nConfig.NotionUserText)
|
||||
.setValue(this.plugin.settings.notionID)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.notionID = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
// General Database Settings
|
||||
containerEl.createEl('h2', {text: i18nConfig.NotionGeneralSetting});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.NotYetFinish)
|
||||
|
||||
// new Setting(containerEl)
|
||||
// .setName("Convert tags(optional)")
|
||||
// .setDesc("Transfer the Obsidian tags to the Notion table. It requires the column with the name 'Tags'")
|
||||
// .addToggle((toggle) =>
|
||||
// toggle
|
||||
// .setValue(this.plugin.settings.allowTags)
|
||||
// .onChange(async (value) => {
|
||||
// this.plugin.settings.allowTags = value;
|
||||
// await this.plugin.saveSettings();
|
||||
// })
|
||||
// );
|
||||
}
|
||||
}
|
||||
|
||||
128
src/ui/settingTabs.ts
Normal file
128
src/ui/settingTabs.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import {App, PluginSettingTab, Setting} from "obsidian";
|
||||
import {i18nConfig} from "../lang/I18n";
|
||||
import ObsidianSyncNotionPlugin from "../main";
|
||||
|
||||
export interface PluginSettings {
|
||||
NNon: boolean;
|
||||
notionAPI: string;
|
||||
databaseID: string;
|
||||
bannerUrl: string;
|
||||
notionUser: string;
|
||||
proxy: string;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: PluginSettings = {
|
||||
NNon: undefined,
|
||||
notionAPI: "",
|
||||
databaseID: "",
|
||||
bannerUrl: "",
|
||||
notionUser: "",
|
||||
proxy: "",
|
||||
};
|
||||
|
||||
|
||||
export class ObsidianSettingTab extends PluginSettingTab {
|
||||
plugin: ObsidianSyncNotionPlugin;
|
||||
|
||||
constructor(app: App, plugin: ObsidianSyncNotionPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.NotionNextVersion)
|
||||
.setDesc(i18nConfig.NotionNextVersionDesc)
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.plugin.settings.NNon)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.NNon = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
containerEl.createEl('h2', {text: i18nConfig.NotionNextSetting})
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.NotionAPI)
|
||||
.setDesc(i18nConfig.NotionAPIDesc)
|
||||
.addText((text) => {
|
||||
text.inputEl.type = 'password';
|
||||
return text
|
||||
.setPlaceholder(i18nConfig.NotionAPIText)
|
||||
.setValue(this.plugin.settings.notionAPI)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.notionAPI = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
const notionDatabaseID = new Setting(containerEl)
|
||||
.setName(i18nConfig.DatabaseID)
|
||||
.setDesc(i18nConfig.NotionAPIDesc)
|
||||
.addText((text) => {
|
||||
text.inputEl.type = 'password';
|
||||
return text
|
||||
.setPlaceholder(i18nConfig.DatabaseIDText)
|
||||
.setValue(this.plugin.settings.databaseID)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.databaseID = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
}
|
||||
);
|
||||
|
||||
// notionDatabaseID.controlEl.querySelector('input').type='password'
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.BannerUrl)
|
||||
.setDesc(i18nConfig.BannerUrlDesc)
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder(i18nConfig.BannerUrlText)
|
||||
.setValue(this.plugin.settings.bannerUrl)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.bannerUrl = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.NotionUser)
|
||||
.setDesc(i18nConfig.NotionUserDesc)
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder(i18nConfig.NotionUserText)
|
||||
.setValue(this.plugin.settings.notionUser)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.notionUser = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
// General Database Settings
|
||||
containerEl.createEl('h2', {text: i18nConfig.NotionGeneralSetting});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(i18nConfig.NotYetFinish)
|
||||
|
||||
// new Setting(containerEl)
|
||||
// .setName("Convert tags(optional)")
|
||||
// .setDesc("Transfer the Obsidian tags to the Notion table. It requires the column with the name 'Tags'")
|
||||
// .addToggle((toggle) =>
|
||||
// toggle
|
||||
// .setValue(this.plugin.settings.allowTags)
|
||||
// .onChange(async (value) => {
|
||||
// this.plugin.settings.allowTags = value;
|
||||
// await this.plugin.saveSettings();
|
||||
// })
|
||||
// );
|
||||
}
|
||||
}
|
||||
@@ -1,87 +1,90 @@
|
||||
import { App, Notice, requestUrl, TFile } from "obsidian";
|
||||
import { Client } from '@notionhq/client';
|
||||
import { markdownToBlocks, } from "@tryfabric/martian";
|
||||
import {App, Notice, requestUrl, TFile} from "obsidian";
|
||||
import {Client} from '@notionhq/client';
|
||||
import {markdownToBlocks,} from "@tryfabric/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
// import * as yaml from "yaml"
|
||||
import MyPlugin from "src/main";
|
||||
|
||||
export class UploadBase {
|
||||
plugin: MyPlugin;
|
||||
notion: Client;
|
||||
agent: any;
|
||||
constructor(plugin: MyPlugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
plugin: MyPlugin;
|
||||
notion: Client;
|
||||
agent: any;
|
||||
|
||||
async deletePage(notionID: string) {
|
||||
return requestUrl({
|
||||
url: `https://api.notion.com/v1/blocks/${notionID}`,
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + this.plugin.settings.notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
},
|
||||
body: ''
|
||||
});
|
||||
}
|
||||
constructor(plugin: MyPlugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
async getDataBase(databaseID: string) {
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/databases/${databaseID}`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + this.plugin.settings.notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
}
|
||||
}
|
||||
)
|
||||
async deletePage(notionID: string) {
|
||||
return requestUrl({
|
||||
url: `https://api.notion.com/v1/blocks/${notionID}`,
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + this.plugin.settings.notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
},
|
||||
body: ''
|
||||
});
|
||||
}
|
||||
|
||||
// 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 getDataBase(databaseID: string) {
|
||||
const response = await requestUrl({
|
||||
url: `https://api.notion.com/v1/databases/${databaseID}`,
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': 'Bearer ' + this.plugin.settings.notionAPI,
|
||||
'Notion-Version': '2022-06-28',
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any, app: App, settings: any) {
|
||||
let { url, id } = res.json
|
||||
// replace www to notionID
|
||||
const { notionID } = settings;
|
||||
if (notionID !== "") {
|
||||
// replace url str "www" to notionID
|
||||
url = url.replace("www.notion.so", `${notionID}.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;
|
||||
});
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
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}`)
|
||||
// }
|
||||
}
|
||||
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}`)
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ import { markdownToBlocks, } from "@tryfabric/martian";
|
||||
import * as yamlFrontMatter from "yaml-front-matter";
|
||||
// import * as yaml from "yaml"
|
||||
import MyPlugin from "src/main";
|
||||
import {PluginSettings} from "../ui/settingTabs";
|
||||
|
||||
export class Upload2NotionNext extends UploadBase {
|
||||
constructor(plugin: MyPlugin) {
|
||||
settings: PluginSettings;
|
||||
constructor(plugin: MyPlugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@@ -155,7 +157,7 @@ export class Upload2NotionNext extends UploadBase {
|
||||
}
|
||||
}
|
||||
|
||||
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, settings: any): Promise<any> {
|
||||
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, settings: PluginSettings): Promise<any> {
|
||||
let res: any
|
||||
const yamlContent: any = yamlFrontMatter.loadFront(markdown);
|
||||
const __content = yamlContent.__content
|
||||
|
||||
62
src/upload/getMarkdown.ts
Normal file
62
src/upload/getMarkdown.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import {App, Notice} from "obsidian";
|
||||
import {i18nConfig} from "../lang/I18n";
|
||||
import {PluginSettings} from "../ui/settingTabs";
|
||||
|
||||
export async function getNowFileMarkdownContent(
|
||||
app: App,
|
||||
settings: PluginSettings,
|
||||
) {
|
||||
const nowFile = app.workspace.getActiveFile();
|
||||
const {NNon} = settings; // Change this line
|
||||
let emoji = '';
|
||||
let cover = '';
|
||||
let tags = [];
|
||||
let type = '';
|
||||
let slug = '';
|
||||
let stats = '';
|
||||
let category = '';
|
||||
let summary = '';
|
||||
let paword = '';
|
||||
let favicon = '';
|
||||
let datetime = '';
|
||||
|
||||
const FileCache = app.metadataCache.getFileCache(nowFile);
|
||||
try {
|
||||
if (NNon) {
|
||||
emoji = FileCache.frontmatter.titleicon;
|
||||
cover = FileCache.frontmatter.coverurl;
|
||||
tags = FileCache.frontmatter.tags;
|
||||
type = FileCache.frontmatter.type;
|
||||
slug = FileCache.frontmatter.slug;
|
||||
stats = FileCache.frontmatter.stats;
|
||||
category = FileCache.frontmatter.category;
|
||||
summary = FileCache.frontmatter.summary;
|
||||
paword = FileCache.frontmatter.password;
|
||||
favicon = FileCache.frontmatter.icon;
|
||||
datetime = FileCache.frontmatter.date;
|
||||
}
|
||||
} catch (error) {
|
||||
new Notice(i18nConfig["set-tags-fail"]);
|
||||
}
|
||||
if (nowFile) {
|
||||
const markDownData = await nowFile.vault.read(nowFile);
|
||||
return {
|
||||
markDownData,
|
||||
nowFile,
|
||||
emoji,
|
||||
cover,
|
||||
tags,
|
||||
type,
|
||||
slug,
|
||||
stats,
|
||||
category,
|
||||
summary,
|
||||
paword,
|
||||
favicon,
|
||||
datetime,
|
||||
};
|
||||
} else {
|
||||
new Notice(i18nConfig["open-file"]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
54
src/upload/uploadCommand.ts
Normal file
54
src/upload/uploadCommand.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import {i18nConfig} from "../lang/I18n";
|
||||
import {App, Notice} from "obsidian";
|
||||
import {getNowFileMarkdownContent} from "./getMarkdown";
|
||||
import {Upload2NotionNext} from "./Upload2NotionNext";
|
||||
import {Upload2NotionGeneral} from "./Upload2NotionGeneral";
|
||||
import {PluginSettings} from "../ui/settingTabs";
|
||||
import ObsidianSyncNotionPlugin from "../main";
|
||||
|
||||
export async function uploadCommand(
|
||||
plugin: ObsidianSyncNotionPlugin,
|
||||
settings: PluginSettings,
|
||||
app: App,
|
||||
) {
|
||||
|
||||
const {notionAPI, databaseID, NNon} = settings;
|
||||
|
||||
// Check if NNon exists
|
||||
if (NNon === undefined) {
|
||||
const NNonmessage = i18nConfig.NNonMissing;
|
||||
new Notice(NNonmessage);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the user has set up the Notion API and database ID
|
||||
if (notionAPI === "" || databaseID === "") {
|
||||
const setAPIMessage = i18nConfig["set-api-id"];
|
||||
new Notice(setAPIMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
const {markDownData, nowFile, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime} = await getNowFileMarkdownContent(app, settings)
|
||||
|
||||
if (markDownData) {
|
||||
const {basename} = nowFile;
|
||||
let upload;
|
||||
let res;
|
||||
|
||||
if (NNon) {
|
||||
upload = new Upload2NotionNext(plugin);
|
||||
res = await upload.syncMarkdownToNotionNext(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, markDownData, nowFile, this.app, this.settings);
|
||||
} else {
|
||||
upload = new Upload2NotionGeneral(plugin);
|
||||
res = await upload.syncMarkdownToNotionGeneral(basename, emoji, cover, tags, type, slug, stats, category, summary, paword, favicon, datetime, markDownData, nowFile, this.app, this.settings);
|
||||
}
|
||||
|
||||
|
||||
if (res.status === 200) {
|
||||
new Notice(`${i18nConfig["sync-success"]}${basename}`);
|
||||
} else {
|
||||
new Notice(`${i18nConfig["sync-fail"]}${basename}`, 5000);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user