Merge pull request #1 from jxpeng98/test-slug

Test slug
This commit is contained in:
Jiaxin Peng
2023-08-09 19:33:01 +01:00
committed by GitHub
3 changed files with 778 additions and 695 deletions

View File

@@ -28,19 +28,19 @@ export class Upload2Notion {
// 因为需要解析notion的block进行对比非常的麻烦 // 因为需要解析notion的block进行对比非常的麻烦
// 暂时就直接删除新建一个page // 暂时就直接删除新建一个page
async updatePage(notionID:string, title:string, allowTags:boolean, tags:string[], childArr:any) { async updatePage(notionID:string, title:string, allowTags:boolean, tags:string[], type:string, slug:string, childArr:any) {
await this.deletePage(notionID) await this.deletePage(notionID)
const res = await this.createPage(title, allowTags, tags, childArr) const res = await this.createPage(title, allowTags, tags, type, slug, childArr)
return res return res
} }
async createPage(title:string, allowTags:boolean, tags:string[], childArr: any) { async createPage(title:string, allowTags:boolean, tags:string[], type:string, slug: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: { title: {
title: [ title: [
{ {
text: { text: {
@@ -49,11 +49,25 @@ export class Upload2Notion {
}, },
], ],
}, },
Tags: { tags: {
multi_select: allowTags && tags !== undefined ? tags.map(tag => { multi_select: allowTags && tags !== undefined ? tags.map(tag => {
return {"name": tag} return {"name": tag}
}) : [], }) : [],
}, },
type: {
select: {
name: type || 'Post'
}
},
slug: {
rich_text: [
{
text: {
content: slug
}
}
]
}
}, },
children: childArr, children: childArr,
} }
@@ -75,7 +89,7 @@ export class Upload2Notion {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
// 'User-Agent': 'obsidian.md', // 'User-Agent': 'obsidian.md',
'Authorization': 'Bearer ' + this.app.settings.notionAPI, 'Authorization': 'Bearer ' + this.app.settings.notionAPI,
'Notion-Version': '2021-08-16', 'Notion-Version': '2022-06-28',
}, },
body: JSON.stringify(bodyString), body: JSON.stringify(bodyString),
}) })
@@ -85,7 +99,7 @@ export class Upload2Notion {
} }
} }
async syncMarkdownToNotion(title:string, allowTags:boolean, tags:string[], markdown: string, nowFile: TFile, app:App, settings:any): Promise<any> { async syncMarkdownToNotion(title:string, allowTags:boolean, tags:string[], type:string, slug: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
@@ -94,9 +108,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, allowTags, tags, file2Block); res = await this.updatePage(notionID, title, allowTags, tags, type, slug, file2Block);
} else { } else {
res = await this.createPage(title, allowTags, tags, file2Block); res = await this.createPage(title, allowTags, tags, type, slug, 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)

16
main.ts
View File

@@ -80,13 +80,13 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
); );
return; return;
} }
const { markDownData, nowFile, tags } =await this.getNowFileMarkdownContent(this.app); const { markDownData, nowFile, tags, type, slug } =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, allowTags, tags, markDownData, nowFile, this.app, this.settings) const res = await upload.syncMarkdownToNotion(basename, allowTags, tags, type, slug, 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 {
@@ -99,9 +99,15 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
const nowFile = app.workspace.getActiveFile(); const nowFile = app.workspace.getActiveFile();
const { allowTags } = this.settings; const { allowTags } = this.settings;
let tags = [] let tags = []
let type = ''
let slug = ''
const FileCache = app.metadataCache.getFileCache(nowFile)
try { try {
if(allowTags) { if(allowTags) {
tags = app.metadataCache.getFileCache(nowFile).frontmatter.tags; tags = FileCache.frontmatter.tags;
type = FileCache.frontmatter.type;
slug = FileCache.frontmatter.slug;
} }
} catch (error) { } catch (error) {
new Notice(langConfig["set-tags-fail"]); new Notice(langConfig["set-tags-fail"]);
@@ -111,7 +117,9 @@ export default class ObsidianSyncNotionPlugin extends Plugin {
return { return {
markDownData, markDownData,
nowFile, nowFile,
tags tags,
type,
slug,
}; };
} else { } else {
new Notice(langConfig["open-file"]); new Notice(langConfig["open-file"]);

1425
yarn.lock

File diff suppressed because it is too large Load Diff