Added share url to yaml front matter

This commit is contained in:
chris
2022-05-24 00:30:21 +08:00
parent 455daa3b20
commit ed32ed23a8
5 changed files with 775 additions and 2066 deletions

View File

@@ -2,28 +2,16 @@ import { App, requestUrl } from "obsidian";
import { Client } from "@notionhq/client"; import { Client } from "@notionhq/client";
import { markdownToBlocks, markdownToRichText } from "@tryfabric/martian"; import { markdownToBlocks, markdownToRichText } from "@tryfabric/martian";
import * as fs from "fs"; import * as fs from "fs";
import { HttpsProxyAgent } from "https-proxy-agent"; import * as yamlFrontMatter from "yaml-front-matter";
import * as yaml from "yaml"
import MyPlugin from "main"; import MyPlugin from "main";
interface settings {
notionAPI: string;
databaseID: string;
proxy: string;
}
interface IApp {
app: App;
manifest: any;
settings: settings;
}
export class Upload2Notion { export class Upload2Notion {
app: MyPlugin; app: MyPlugin;
notion: Client; notion: Client;
agent: any; agent: any;
constructor(app: MyPlugin) { constructor(app: MyPlugin) {
this.app = app; this.app = app;
console.log(app);
} }
async createPage(title:string, childArr: any) { async createPage(title:string, childArr: any) {
@@ -42,7 +30,6 @@ export class Upload2Notion {
}, },
children: childArr, children: childArr,
} }
console.log(bodyString)
const response = await requestUrl({ const response = await requestUrl({
url: `https://api.notion.com/v1/pages`, url: `https://api.notion.com/v1/pages`,
method: 'POST', method: 'POST',
@@ -54,14 +41,30 @@ export class Upload2Notion {
}, },
body: JSON.stringify(bodyString), body: JSON.stringify(bodyString),
}) })
console.log('POST response', response);
return response; return response;
} }
async syncMarkdownToNotion(title:string, markdown: string): Promise<any> { async syncMarkdownToNotion(title:string, markdown: string, fullPath:string): Promise<any> {
const file2md = markdownToBlocks(markdown); const file2md = markdownToBlocks(markdown);
console.log(file2md)
const res = await this.createPage(title, file2md); const res = await this.createPage(title, file2md);
if (res.status === 200) {
this.updateYamlInfo(markdown, fullPath, res)
}
return res
}
async updateYamlInfo(yamlContent: string, fullPath: string, res: any) {
const yamlObj:any = yamlFrontMatter.loadFront(yamlContent);
const {url} = res.json
yamlObj.shareUrl = url;
const __content = yamlObj.__content;
delete yamlObj.__content
const yamlhead = yaml.stringify(yamlObj)
const content = '---\n' +yamlhead +'\n---\n' + __content;
//write content fo file
fs.writeFileSync(fullPath, content);
return res return res
} }
} }

12
main.ts
View File

@@ -49,13 +49,18 @@ export default class MyPlugin extends Plugin {
); );
return; return;
} }
const { nowFile, fileData } = const { nowFile, fileData,fullPath } =
await this.getNowFileMarkdwonContent(this.app); await this.getNowFileMarkdwonContent(this.app);
const { basename } = nowFile; const { basename } = nowFile;
console.log(nowFile, "nowfile");
if (fileData) { if (fileData) {
const upload = new Upload2Notion(this); const upload = new Upload2Notion(this);
await upload.syncMarkdownToNotion(basename,fileData); const res = await upload.syncMarkdownToNotion(basename,fileData, fullPath)
console.log(res)
if(res.status === 200){
new Notice(`${basename}同步成功`)
}else {
new Notice(`${basename}同步失败`)
}
} }
} }
); );
@@ -140,6 +145,7 @@ export default class MyPlugin extends Plugin {
return { return {
fileData, fileData,
nowFile, nowFile,
fullPath,
}; };
} else { } else {
new Notice("请打开需要同步的文件"); new Notice("请打开需要同步的文件");

1356
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -14,6 +14,7 @@
"license": "GNU GPLv3", "license": "GNU GPLv3",
"devDependencies": { "devDependencies": {
"@types/node": "^17.0.35", "@types/node": "^17.0.35",
"@types/yaml-front-matter": "^4.1.0",
"@typescript-eslint/eslint-plugin": "^5.2.0", "@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0", "@typescript-eslint/parser": "^5.2.0",
"builtin-modules": "^3.2.0", "builtin-modules": "^3.2.0",

1433
yarn.lock

File diff suppressed because it is too large Load Diff