mirror of
https://github.com/zigholding/obsidian-notesync-plugin.git
synced 2026-07-22 05:43:30 +00:00
mirror vault
This commit is contained in:
parent
bcb0bd4a2d
commit
719a6be2db
5 changed files with 160 additions and 40 deletions
|
|
@ -1,7 +1,4 @@
|
|||
{
|
||||
"pluginDirExporter": "D:\\iLanix\\notes\\Obsidian\\.obsidian\\plugins\nD:\\iLanix\\isync\\Obsidian\\.obsidian\\plugins",
|
||||
"nameLocalGitProject": "LocalGitProject",
|
||||
"readmeRemoveFrontmatter": true,
|
||||
"assetsLocalGitProject": "assets",
|
||||
"mySetting": "D:/iLanix/isync/test"
|
||||
"pluginDirExporter": "D:\\iLanix\\isync\\Obsidian\\.obsidian\\plugins",
|
||||
"vaultDir": "D:\\iLanix\\isync\\Obsidian"
|
||||
}
|
||||
95
main.js
95
main.js
File diff suppressed because one or more lines are too long
55
main.ts
55
main.ts
|
|
@ -1,15 +1,17 @@
|
|||
import * as Module from 'module';
|
||||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
|
||||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile, TFolder } from 'obsidian';
|
||||
|
||||
import { FsEditor } from 'src/fseditor';
|
||||
import { strings } from 'src/strings';
|
||||
|
||||
interface VExporterSettings {
|
||||
pluginDirExporter:string;
|
||||
vaultDir:string;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: VExporterSettings = {
|
||||
pluginDirExporter:''
|
||||
pluginDirExporter:'',
|
||||
vaultDir:''
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -69,12 +71,11 @@ const cmd_export_plugin = (plugin:VaultExpoterPlugin) => ({
|
|||
if(!plugin.fsEditor.fs.existsSync(target)){
|
||||
plugin.fsEditor.fs.mkdirSync(target);
|
||||
}
|
||||
let items = ['main.js','manifest.json','styles.css','data.json'];
|
||||
let items = ['main.js','manifest.json','styles.css'];
|
||||
for(let item of items){
|
||||
let src = `${plugin.fsEditor.root}/${eplugin.manifest.dir}/${item}`;
|
||||
let dst = `${target}/${item}`;
|
||||
plugin.fsEditor.copy_file_by_path(src,dst,'overwrite');
|
||||
new Notice(`${strings.notice_output}${p}/${item}`,3000);
|
||||
plugin.fsEditor.copy_file(src,dst,'overwrite');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -101,6 +102,39 @@ export default class VaultExpoterPlugin extends Plugin {
|
|||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new VExporterSettingTab(this.app, this));
|
||||
addCommands(this);
|
||||
|
||||
this.registerEvent(
|
||||
this.app.workspace.on("file-menu", (menu, file) => {
|
||||
menu.addItem((item) => {
|
||||
item
|
||||
.setTitle("Mirror to other vault")
|
||||
.setIcon("document")
|
||||
.onClick(async () => {
|
||||
let dst = await this.fsEditor.select_valid_dir(
|
||||
this.settings.vaultDir.split("\n")
|
||||
);
|
||||
if(!dst){
|
||||
let nc= (this.app as any).plugins.getPlugin("note-chain");
|
||||
if(!nc){
|
||||
new Notice("Plugin note-chain is needed!");
|
||||
return;
|
||||
}
|
||||
dst = await nc.chain.tp_prompt("Root of vault");
|
||||
if(!this.fsEditor.isdir(dst)){
|
||||
new Notice("Invalid root: " + dst);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(file instanceof TFile){
|
||||
this.fsEditor.mirror_tfile(file,dst,'mtime',true,false);
|
||||
|
||||
}else if(file instanceof TFolder){
|
||||
this.fsEditor.mirror_tfolder(file,dst,'mtime',true,false);
|
||||
}
|
||||
});
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
onunload() {
|
||||
|
|
@ -174,7 +208,7 @@ export default class VaultExpoterPlugin extends Plugin {
|
|||
if(assets){
|
||||
let olinks = nc.chain.get_outlinks(tfile);
|
||||
let adir = dst+'/'+assets;
|
||||
this.fsEditor.mkdirRecursiveSync(adir);
|
||||
this.fsEditor.mkdir_recursive(adir);
|
||||
for(let f of olinks){
|
||||
if(!(f.extension==='md')){
|
||||
this.fsEditor.copy_tfile(f,adir+'/'+f.basename+'.'+f.extension);
|
||||
|
|
@ -226,5 +260,14 @@ class VExporterSettingTab extends PluginSettingTab {
|
|||
this.plugin.settings.pluginDirExporter = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName(strings.setting_vault_dir)
|
||||
.addTextArea(text => text
|
||||
.setValue(this.plugin.settings.vaultDir)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.vaultDir = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
|
||||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile, TFolder } from 'obsidian';
|
||||
|
||||
export class FsEditor{
|
||||
fs;
|
||||
|
|
@ -33,7 +33,7 @@ export class FsEditor{
|
|||
return this.fs.existsSync(path) && this.fs.statSync(path).isDirectory();
|
||||
}
|
||||
|
||||
first_valid_dir(paths:Array<string>){
|
||||
first_valid_dir(paths:Array<string>|string){
|
||||
for(let path of paths){
|
||||
if(this.isdir(path)){
|
||||
return path;
|
||||
|
|
@ -60,11 +60,11 @@ export class FsEditor{
|
|||
}
|
||||
|
||||
|
||||
mkdirRecursiveSync(path:string){
|
||||
mkdir_recursive(path:string){
|
||||
if(this.isdir(path)){return true;}
|
||||
const parent = this.path.dirname(path);
|
||||
if(!this.isdir(parent)){
|
||||
this.mkdirRecursiveSync(parent);
|
||||
this.mkdir_recursive(parent);
|
||||
}
|
||||
this.fs.mkdirSync(path);
|
||||
}
|
||||
|
|
@ -73,7 +73,7 @@ export class FsEditor{
|
|||
* 附件 src 到 dst,不在 vault 中,需要绝对路径
|
||||
* overwrite,复盖;mtime,新文件;
|
||||
*/
|
||||
copy_file_by_path(src:string,dst:string,mode='pass>overwrite>mtime') {
|
||||
copy_file(src:string,dst:string,mode='pass>overwrite>mtime') {
|
||||
const fs = this.fs;
|
||||
|
||||
mode = mode.split('>')[0]
|
||||
|
|
@ -84,17 +84,20 @@ export class FsEditor{
|
|||
if(mode==='overwrite'){
|
||||
fs.unlinkSync(dst);
|
||||
fs.copyFileSync(src,dst);
|
||||
new Notice(`Copy:${src}-->${dst}`,5000);
|
||||
return true;
|
||||
}else if(mode==='mtime'){
|
||||
// dst 更新时间小于 src
|
||||
if(fs.statSync(dst).mtimeMs<fs.statSync(src).mtimeMs){
|
||||
fs.unlinkSync(dst);
|
||||
fs.copyFileSync(src,dst);
|
||||
new Notice(`Copy:${src}-->${dst}`,5000);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
fs.copyFileSync(src,dst);
|
||||
new Notice(`Copy:${src}-->${dst}`,5000);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
|
@ -103,8 +106,7 @@ export class FsEditor{
|
|||
copy_tfile(tfile:TFile, dst:string,mode='mtime') {
|
||||
if(tfile){
|
||||
let src = this.abspath(tfile);
|
||||
|
||||
return src && this.copy_file_by_path(src,dst,mode);
|
||||
return src && this.copy_file(src,dst,mode);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
@ -115,10 +117,11 @@ export class FsEditor{
|
|||
vault_root = vault_root.replace(/\\g/,'/');
|
||||
let src = this.root + '/' + tfile.path;
|
||||
let dst = vault_root+'/'+tfile.path;
|
||||
this.mkdirRecursiveSync(this.path.dirname);
|
||||
this.copy_file_by_path(src,dst,mode);
|
||||
this.mkdir_recursive(this.path.dirname(dst));
|
||||
this.copy_file(src,dst,mode);
|
||||
if(attachment){
|
||||
let nc = (this.plugin.app as any).plugins.getPlugin('note-chain');
|
||||
if(!nc){return;}
|
||||
let tfiles = nc.chain.get_outlinks(tfile);
|
||||
for(let t of tfiles){
|
||||
if(!(t.extension==='md')){
|
||||
|
|
@ -131,6 +134,18 @@ export class FsEditor{
|
|||
}
|
||||
}
|
||||
|
||||
mirror_tfolder(tfolder:TFolder,vault_root:string,mode='mtime',attachment=true,outlink=false){
|
||||
if(tfolder){
|
||||
for(let t of tfolder.children){
|
||||
if(t instanceof TFolder){
|
||||
this.mirror_tfolder(t,vault_root,mode,attachment,outlink);
|
||||
}else if(t instanceof TFile){
|
||||
this.mirror_tfile(t,vault_root,mode,attachment,outlink);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
modify(path:string,callback:Function,encoding='utf8'){
|
||||
const fs = this.fs;
|
||||
if(!fs.existsSync(path)){return};
|
||||
|
|
|
|||
|
|
@ -61,7 +61,15 @@ class Strings{
|
|||
if(this.language=='zh'){
|
||||
return '插件导出目录';
|
||||
}else{
|
||||
return 'Plugin dir To export';
|
||||
return 'Plugin dir to export';
|
||||
}
|
||||
}
|
||||
|
||||
get setting_vault_dir(){
|
||||
if(this.language=='zh'){
|
||||
return '库目录';
|
||||
}else{
|
||||
return 'Root dir of vault';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue