zigholding_obsidian-notesyn.../main.ts

151 lines
3.6 KiB
TypeScript
Raw Normal View History

import {Notice, Plugin, TFile, TFolder } from 'obsidian';
2024-04-22 13:27:53 +00:00
2024-04-25 14:32:43 +00:00
import { FsEditor } from 'src/fseditor';
2024-12-14 05:17:19 +00:00
import { Strings } from 'src/strings';
2025-02-13 12:09:16 +00:00
import {MySettings,NoteSyncSettingTab,DEFAULT_SETTINGS} from 'src/setting'
2024-04-22 13:27:53 +00:00
2024-12-14 05:17:19 +00:00
import { addCommands } from 'src/commands';
2024-04-22 14:01:24 +00:00
import {dialog_suggest} from 'src/gui/inputSuggester'
import {dialog_prompt} from 'src/gui/inputPrompt'
2024-12-15 04:06:58 +00:00
export default class NoteSyncPlugin extends Plugin {
2024-12-14 05:17:19 +00:00
strings : Strings;
2024-12-15 04:06:58 +00:00
settings: MySettings;
2024-12-14 05:17:19 +00:00
fsEditor : FsEditor;
2024-12-15 05:01:31 +00:00
yaml: string;
dialog_suggest: Function
dialog_prompt: Function
2024-04-27 07:57:17 +00:00
2024-04-22 14:01:24 +00:00
2024-12-14 05:17:19 +00:00
async onload() {
this.dialog_suggest = dialog_suggest
this.dialog_prompt = dialog_prompt
this.app.workspace.onLayoutReady(
async()=>{
await this._onload_()
}
)
2024-04-25 14:32:43 +00:00
}
2024-12-14 05:17:19 +00:00
async _onload_() {
2024-12-15 05:01:31 +00:00
this.yaml = 'note-sync'
2024-12-14 05:17:19 +00:00
this.strings = new Strings();
2024-04-22 13:27:53 +00:00
await this.loadSettings();
2024-04-25 14:32:43 +00:00
this.fsEditor = new FsEditor(this);
2024-04-22 13:27:53 +00:00
// This adds a settings tab so the user can configure various aspects of the plugin
2025-02-13 12:09:16 +00:00
this.addSettingTab(new NoteSyncSettingTab(this.app, this));
2024-04-22 14:01:24 +00:00
addCommands(this);
2024-05-08 12:47:38 +00:00
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file) => {
menu.addItem((item) => {
item
2024-12-15 05:01:31 +00:00
.setTitle(this.strings.item_sync_vault)
2024-05-08 12:47:38 +00:00
.setIcon("document")
.onClick(async () => {
let dst = await this.fsEditor.select_valid_dir(
this.settings.vaultDir.split("\n")
);
if(!dst){
dst = await this.dialog_prompt("Root of vault");
2024-05-08 12:47:38 +00:00
if(!this.fsEditor.isdir(dst)){
new Notice("Invalid root: " + dst);
return;
}
}
if(file instanceof TFile){
2024-12-15 05:35:25 +00:00
this.fsEditor.sync_tfile(file,dst,'mtime',true,false);
2024-05-08 12:47:38 +00:00
}else if(file instanceof TFolder){
2024-12-15 05:35:25 +00:00
this.fsEditor.sync_tfolder(file,dst,'mtime',true,false);
2024-05-08 12:47:38 +00:00
}
});
});
})
);
2024-04-22 13:27:53 +00:00
}
onunload() {
2024-12-14 05:17:19 +00:00
2024-04-22 13:27:53 +00:00
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async export_readme(tfile:TFile|null,dst:string|null){
if(!tfile){tfile = this.app.workspace.getActiveFile();}
if(!tfile){return}
2025-02-13 12:09:16 +00:00
let mcache = this.app.metadataCache.getFileCache(tfile);
2025-05-21 17:09:55 +00:00
let ctx = await this.app.vault.read(tfile);
2025-02-13 12:09:16 +00:00
let fm: { [key: string]: any } = {};
if(mcache && mcache['frontmatter']){
fm = mcache['frontmatter'];
}
if(!dst){
dst = fm[this.yaml]?.Dir
if(!dst){
dst = await this.dialog_prompt('Path of LocalGitProject');
}
}
if(!dst || !this.fsEditor.isdir(dst)){
new Notice(this.strings.notice_nosuchdir,3000);
return;
}
dst = dst.replace(/\\/g,'/');
// set target filename/文件名
let target;
let name = fm[this.yaml]?.Name;
if(name && !(name=='')){
target = dst+'/'+name+'.md';
}else{
target = dst+'/'+tfile.basename+'.md';
}
if(fm[this.yaml]?.RemoveMeta){
ctx = ctx.replace(
/---[\n(\r\n)][\s\S]*?---[\n(\r\n)]/,
''
)
}
let assets = fm[this.yaml]?.Assets
if(fm[this.yaml]?.UseGitLink && assets){
if(mcache?.frontmatterPosition?.end?.offset){
ctx = ctx.slice(mcache.frontmatterPosition.end.offset);
}
}
await this.fsEditor.fs.writeFile(
target, ctx, 'utf-8',
(err:Error) => {return;}
)
new Notice(`Export to ${target}`,5000)
if(assets){
let olinks = this.fsEditor.get_outlinks(tfile,false);
let adir = this.fsEditor.path.join(dst,assets);
this.fsEditor.mkdir_recursive(adir);
for(let f of olinks){
if(!(f.extension==='md')){
let flag = this.fsEditor.copy_tfile(f,adir+'/'+f.basename+'.'+f.extension);
if(flag){
new Notice(`Copy ${f.name}`,5000)
2024-12-15 05:01:31 +00:00
}
2024-04-27 07:57:17 +00:00
}
2024-04-25 14:32:43 +00:00
}
2025-02-13 12:09:16 +00:00
}
2024-04-22 14:38:16 +00:00
}
2024-04-22 13:27:53 +00:00
}