diff --git a/README.md b/README.md index e6460ef..700999f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ This plugin depends on: [Templater](https://github.com/SilentVoid13/Templater) a ### Exporting Notes -Mirroring files or folders: +Sync files or folders: 1. Right-click on the file or folder in the file list; 2. Click `Sync to other vault`; 3. Enter the target vault root directory; @@ -48,3 +48,35 @@ Then execute `Note Sync:Export current note` to set the export note. > [!Danger]+ Strict mode > When right-clicking to sync a folder, delete notes or attachments that are in the target folder but not in the source folder. This ensures that the synced folder and the current folder are the same. This setting will delete files, please operate with caution. + +### Functions + +`NoteSync` provides three functions for synchronizing files and folders, which can be used in your own script notes. + +`mode` specifies the handling method when a file already exists in the target vault: +- `pass`: Skip; +- `overwrite`: Overwrite; +- `mtime`: Take the latest modified time; + + +```js +let ns = app.plugins.plugins['note-sync'] + +// 同步系统文件夹 +ns.fsEditor.sync_folder( + src:string, + dst:string, + mode='mtime', + strict=false +) + +// 同步笔记文件夹 +ns.fsEditor.sync_tfolder( + tfolder:TFolder, + vault_root:string, + mode='mtime', + attachment=true, + outlink=false, + strict=false +) +``` \ No newline at end of file diff --git a/main.ts b/main.ts index ab271a7..0cfd3e3 100644 --- a/main.ts +++ b/main.ts @@ -59,10 +59,10 @@ export default class NoteSyncPlugin extends Plugin { } } if(file instanceof TFile){ - this.fsEditor.mirror_tfile(file,dst,'mtime',true,false); + this.fsEditor.sync_tfile(file,dst,'mtime',true,false); }else if(file instanceof TFolder){ - this.fsEditor.mirror_tfolder(file,dst,'mtime',true,false); + this.fsEditor.sync_tfolder(file,dst,'mtime',true,false); } }); }); diff --git a/readMe_中文.md b/readMe_中文.md index d7f0fac..a5c014e 100644 --- a/readMe_中文.md +++ b/readMe_中文.md @@ -1,12 +1,14 @@ -[Eng](./README.md) | [中文版](./readMe_中文.md) +v +[English](./README.md) | [中文版](./readMe_中文.md) + 本插件依赖:[Templater](https://github.com/SilentVoid13/Templater) 和 [NoteChain](https://github.com/zigholding/obsidian-notechain-plugin) ### 导出笔记 -镜像文件或文件夹: +同步文件或文件夹: 1. 在文件列表中,右键点击文件或文件夹; -2. 点击 `Sync to other vault`; +2. 点击 `mirror to other vault`; 3. 输入目标库根目录; 4. 笔记、文件夹以及笔记嵌入的附件,会按相同的文件结构复制到目标库。如果目录库中存在同名文件,则根据更新时间判定是否覆盖; @@ -48,6 +50,36 @@ > [!Danger]+ Strict mode > 右键同步文件夹时,删除在目标文件夹中,但不在源文件夹中的笔记或附件。保证同步的文件夹和当前文件夹是相同的。此设置会删除文件,请谨慎操作。 +### 函数 + +`NoteSync` 提供了三个函数用于同步文件和文件夹,可以在自己的脚本笔记中使用。 + +`mode` 指当目标库中存在文件时的处理方法: +- `pass`:跳过; +- `overwrite`:覆盖; +- `mtime`:取最新更新时间; + +```js +let ns = app.plugins.plugins['note-sync'] + +// 同步系统文件夹 +ns.fsEditor.sync_folder( + src:string, + dst:string, + mode='mtime', + strict=false +) + +// 同步笔记文件夹 +ns.fsEditor.sync_tfolder( + tfolder:TFolder, + vault_root:string, + mode='mtime', + attachment=true, + outlink=false, + strict=false +) +``` diff --git a/src/fseditor.ts b/src/fseditor.ts index 56abc80..6b652b6 100644 --- a/src/fseditor.ts +++ b/src/fseditor.ts @@ -131,7 +131,7 @@ export class FsEditor{ return false; } - mirror_tfile(tfile:TFile,vault_root:string,mode='mtime',attachment=true,outlink=false){ + sync_tfile(tfile:TFile,vault_root:string,mode='mtime',attachment=true,outlink=false){ // 将笔记镜像移动到别的库中,文件结构与当前库相同 if(tfile){ vault_root = vault_root.replace(/\\/g,'/'); @@ -145,22 +145,22 @@ export class FsEditor{ let tfiles = nc.chain.get_outlinks(tfile,false); for(let t of tfiles){ if(!(t.extension==='md')){ - this.mirror_tfile(t,vault_root,mode,false); + this.sync_tfile(t,vault_root,mode,false); }else if(outlink){ - this.mirror_tfile(t,vault_root,mode,false); + this.sync_tfile(t,vault_root,mode,false); } } } } } - mirror_tfolder(tfolder:TFolder,vault_root:string,mode='mtime',attachment=true,outlink=false,strict=false){ + sync_tfolder(tfolder:TFolder,vault_root:string,mode='mtime',attachment=true,outlink=false,strict=false){ if(tfolder){ for(let t of tfolder.children){ if(t instanceof TFolder){ - this.mirror_tfolder(t,vault_root,mode,attachment,outlink); + this.sync_tfolder(t,vault_root,mode,attachment,outlink); }else if(t instanceof TFile){ - this.mirror_tfile(t,vault_root,mode,attachment,outlink); + this.sync_tfile(t,vault_root,mode,attachment,outlink); } } if(strict){ @@ -206,7 +206,7 @@ export class FsEditor{ } } - mirror_folder(src:string,dst:string,mode='mtime',strict=false){ + sync_folder(src:string,dst:string,mode='mtime',strict=false){ if(!this.isdir(src)){return} this.mkdir_recursive(dst) if(!this.isdir(dst)){return} @@ -218,7 +218,7 @@ export class FsEditor{ if(this.isfile(asrc)){ this.copy_file(asrc,adst,mode) }else if(this.isdir(asrc)){ - this.mirror_folder(asrc,adst,mode,strict) + this.sync_folder(asrc,adst,mode,strict) } } if(strict){