Merge pull request #34 from jannikbuscha/master

feat: convert tags support
This commit is contained in:
Easy
2022-12-17 12:16:27 +08:00
committed by GitHub
4 changed files with 53 additions and 25 deletions

View File

@@ -87,7 +87,7 @@ A share link will be automatically generated after successful upload
![](./doc/5.png) ![](./doc/5.png)
## Banner URL[option] ## Banner URL [option]
banner url must be a image url like: https://i.imgur.com/xxx.jpg banner url must be a image url like: https://i.imgur.com/xxx.jpg
if you don't want to use banner, leave it blank if you don't want to use banner, leave it blank
@@ -100,6 +100,11 @@ if you write the Notion ID, it will share to the page link like:
https://your_user_name.notion.site/myworkspace/a8aec43384f447ed84390. https://your_user_name.notion.site/myworkspace/a8aec43384f447ed84390.
The visiter don't need to redirect url. The visiter don't need to redirect url.
## Convert Tags [option]
Transfer the Obsidian tags to the Notion table.
It requires the column with the name 'Tags'.
![](./doc/7.png)
## Sync image to Notion ## Sync image to Notion
To sync images to your oss or cos bucket, use the [Obsidian Image Auto Upload Plugin](https://github.com/renmu123/obsidian-image-auto-upload-plugin). To sync images to your oss or cos bucket, use the [Obsidian Image Auto Upload Plugin](https://github.com/renmu123/obsidian-image-auto-upload-plugin).
@@ -130,4 +135,4 @@ yarn dev
[GitHub - Quorafind/Obsidian-Memos: A quick capture plugin for Obsidian, all data from your notes.](https://github.com/Quorafind/Obsidian-Memos) [GitHub - Quorafind/Obsidian-Memos: A quick capture plugin for Obsidian, all data from your notes.](https://github.com/Quorafind/Obsidian-Memos)
# License # License
GNU GPLv3 GNU GPLv3

View File

@@ -12,7 +12,7 @@ export class Upload2Notion {
this.app = app; this.app = app;
} }
async deletePage(notionID:string){ async deletePage(notionID:string){
const response = await requestUrl({ const response = await requestUrl({
url: `https://api.notion.com/v1/blocks/${notionID}`, url: `https://api.notion.com/v1/blocks/${notionID}`,
method: 'DELETE', method: 'DELETE',
@@ -23,22 +23,22 @@ export class Upload2Notion {
}, },
body: '' body: ''
}) })
return response; return response;
} }
// 因为需要解析notion的block进行对比非常的麻烦 // 因为需要解析notion的block进行对比非常的麻烦
// 暂时就直接删除新建一个page // 暂时就直接删除新建一个page
async updatePage(notionID:string, title:string, childArr:any) { async updatePage(notionID:string, title:string, allowTags:boolean, tags:string[], childArr:any) {
await this.deletePage(notionID) await this.deletePage(notionID)
const res = await this.createPage(title, childArr) const res = await this.createPage(title, allowTags, tags, childArr)
return res return res
} }
async createPage(title:string, childArr: any) { async createPage(title:string, allowTags:boolean, tags:string[], childArr: any) {
const bodyString:any = { const bodyString:any = {
parent: { parent: {
database_id: this.app.settings.databaseID database_id: this.app.settings.databaseID
}, },
properties: { properties: {
Name: { Name: {
title: [ title: [
@@ -49,6 +49,11 @@ export class Upload2Notion {
}, },
], ],
}, },
Tags: {
multi_select: allowTags ? tags.map(tag => {
return {"name": tag}
}) : [],
},
}, },
children: childArr, children: childArr,
} }
@@ -77,10 +82,10 @@ export class Upload2Notion {
return response; return response;
} catch (error) { } catch (error) {
new Notice(`network error ${error}`) new Notice(`network error ${error}`)
} }
} }
async syncMarkdownToNotion(title:string, markdown: string, nowFile: TFile, app:App, settings:any): Promise<any> { async syncMarkdownToNotion(title:string, allowTags:boolean, tags:string[], markdown: string, nowFile: TFile, app:App, settings:any): Promise<any> {
let res:any let res:any
const yamlObj:any = yamlFrontMatter.loadFront(markdown); const yamlObj:any = yamlFrontMatter.loadFront(markdown);
const __content = yamlObj.__content const __content = yamlObj.__content
@@ -89,9 +94,9 @@ export class Upload2Notion {
const notionID = frontmasster ? frontmasster.notionID : null const notionID = frontmasster ? frontmasster.notionID : null
if(notionID){ if(notionID){
res = await this.updatePage(notionID, title, file2Block); res = await this.updatePage(notionID, title, allowTags, tags, file2Block);
} else { } else {
res = await this.createPage(title, file2Block); res = await this.createPage(title, allowTags, tags, file2Block);
} }
if (res.status === 200) { if (res.status === 200) {
await this.updateYamlInfo(markdown, nowFile, res, app, settings) await this.updateYamlInfo(markdown, nowFile, res, app, settings)
@@ -104,11 +109,11 @@ export class Upload2Notion {
async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any,app:App, settings:any) { async updateYamlInfo(yamlContent: string, nowFile: TFile, res: any,app:App, settings:any) {
const yamlObj:any = yamlFrontMatter.loadFront(yamlContent); const yamlObj:any = yamlFrontMatter.loadFront(yamlContent);
let {url, id} = res.json let {url, id} = res.json
// replace www to notionID // replace www to notionID
const {notionID} = settings; const {notionID} = settings;
if(notionID!=="") { if(notionID!=="") {
// replace url str "www" to notionID // replace url str "www" to notionID
url = url.replace("www.notion.so", `${notionID}.notion.site`) url = url.replace("www.notion.so", `${notionID}.notion.site`)
} }
yamlObj.link = url; yamlObj.link = url;
try { try {

BIN
doc/7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

36
main.ts
View File

@@ -24,6 +24,7 @@ interface PluginSettings {
bannerUrl: string; bannerUrl: string;
notionID: string; notionID: string;
proxy: string; proxy: string;
allowTags: boolean;
} }
const langConfig = NoticeMConfig( window.localStorage.getItem('language') || 'en') const langConfig = NoticeMConfig( window.localStorage.getItem('language') || 'en')
@@ -34,13 +35,14 @@ const DEFAULT_SETTINGS: PluginSettings = {
bannerUrl: "", bannerUrl: "",
notionID: "", notionID: "",
proxy: "", proxy: "",
allowTags: false
}; };
export default class ObsidianSyncNotionPlugin extends Plugin { export default class ObsidianSyncNotionPlugin extends Plugin {
settings: PluginSettings; settings: PluginSettings;
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
addIcons(); addIcons();
// This creates an icon in the left ribbon. // This creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon( const ribbonIconEl = this.addRibbonIcon(
"notion-logo", "notion-logo",
@@ -72,21 +74,21 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
onunload() {} onunload() {}
async upload(){ async upload(){
const { notionAPI, databaseID } = this.settings; const { notionAPI, databaseID, allowTags } = this.settings;
if (notionAPI === "" || databaseID === "") { if (notionAPI === "" || databaseID === "") {
new Notice( new Notice(
"Please set up the notion API and database ID in the settings tab." "Please set up the notion API and database ID in the settings tab."
); );
return; return;
} }
const { markDownData, nowFile } = const { markDownData, nowFile, tags } =
await this.getNowFileMarkdownContent(this.app); await this.getNowFileMarkdownContent(this.app);
if (markDownData) { if (markDownData) {
const { basename } = nowFile; const { basename } = nowFile;
const upload = new Upload2Notion(this); const upload = new Upload2Notion(this);
const res = await upload.syncMarkdownToNotion(basename, markDownData,nowFile, this.app, this.settings) const res = await upload.syncMarkdownToNotion(basename, allowTags, tags, markDownData, nowFile, this.app, this.settings)
if(res.status === 200){ if(res.status === 200){
new Notice(`${langConfig["sync-success"]}${basename}`) new Notice(`${langConfig["sync-success"]}${basename}`)
}else { }else {
@@ -97,11 +99,14 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
async getNowFileMarkdownContent(app: App) { async getNowFileMarkdownContent(app: App) {
const nowFile = app.workspace.getActiveFile(); const nowFile = app.workspace.getActiveFile();
const tags = app.metadataCache.getFileCache(nowFile).frontmatter.tags;
if (nowFile) { if (nowFile) {
const markDownData = await nowFile.vault.read(nowFile); const markDownData = await nowFile.vault.read(nowFile);
return { return {
markDownData, markDownData,
nowFile, nowFile,
tags
}; };
} else { } else {
new Notice(langConfig["open-file"]); new Notice(langConfig["open-file"]);
@@ -139,7 +144,7 @@ class SampleSettingTab extends PluginSettingTab {
text: "Settings for obsidian to notion plugin.", text: "Settings for obsidian to notion plugin.",
}); });
const notionApiKye = new Setting(containerEl) new Setting(containerEl)
.setName("Notion API Token") .setName("Notion API Token")
.setDesc("It's a secret") .setDesc("It's a secret")
.addText((text) =>{ .addText((text) =>{
@@ -153,7 +158,7 @@ class SampleSettingTab extends PluginSettingTab {
t.inputEl.type = 'password' t.inputEl.type = 'password'
return t return t
}); });
const notionDatabaseID = new Setting(containerEl) const notionDatabaseID = new Setting(containerEl)
.setName("Database ID") .setName("Database ID")
@@ -169,11 +174,11 @@ class SampleSettingTab extends PluginSettingTab {
t.inputEl.type = 'password' t.inputEl.type = 'password'
return t return t
} }
); );
notionDatabaseID.controlEl.querySelector('input').type='password' notionDatabaseID.controlEl.querySelector('input').type='password'
new Setting(containerEl) new Setting(containerEl)
.setName("Banner url(optional)") .setName("Banner url(optional)")
.setDesc("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)") .setDesc("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)")
@@ -201,5 +206,18 @@ class SampleSettingTab extends PluginSettingTab {
}) })
); );
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();
})
);
} }
} }