From 9fcca1222e52097a9b451b04661a49a9ad77f489 Mon Sep 17 00:00:00 2001 From: ZigHolding Date: Thu, 13 Feb 2025 20:09:16 +0800 Subject: [PATCH] modify by comments of joethei --- LICENSE | 2 +- main.ts | 122 ++++++++++++++++++++++++------------------------ src/commands.ts | 6 +-- src/fseditor.ts | 40 ++++++---------- src/setting.ts | 2 +- src/strings.ts | 2 +- 6 files changed, 79 insertions(+), 95 deletions(-) diff --git a/LICENSE b/LICENSE index 9a0b63d..f057067 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2024 ZigHolding +Copyright (c) 2025 ZigHolding Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/main.ts b/main.ts index 610477e..e3e6f5f 100644 --- a/main.ts +++ b/main.ts @@ -2,7 +2,7 @@ import {Notice, Plugin, TFile, TFolder } from 'obsidian'; import { FsEditor } from 'src/fseditor'; import { Strings } from 'src/strings'; -import {MySettings,MySettingTab,DEFAULT_SETTINGS} from 'src/setting' +import {MySettings,NoteSyncSettingTab,DEFAULT_SETTINGS} from 'src/setting' import { addCommands } from 'src/commands'; @@ -35,7 +35,7 @@ export default class NoteSyncPlugin extends Plugin { await this.loadSettings(); this.fsEditor = new FsEditor(this); // This adds a settings tab so the user can configure various aspects of the plugin - this.addSettingTab(new MySettingTab(this.app, this)); + this.addSettingTab(new NoteSyncSettingTab(this.app, this)); addCommands(this); this.registerEvent( @@ -83,70 +83,68 @@ export default class NoteSyncPlugin extends Plugin { if(!tfile){tfile = this.app.workspace.getActiveFile();} if(!tfile){return} - await this.app.fileManager.processFrontMatter( - tfile, - async(fm) =>{ - if(!tfile){return} - // set output dir/设置输出目录 - 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,'/'); + let mcache = this.app.metadataCache.getFileCache(tfile); + let ctx = await this.app.vault.cachedRead(tfile) - // set target filename/文件名 - let target; - let name = fm[this.yaml]?.Name; - if(name && !(name=='')){ - target = dst+'/'+name+'.md'; - }else{ - target = dst+'/'+tfile.basename+'.md'; - } - - - let data = await this.app.vault.cachedRead(tfile) - if(fm[this.yaml]?.RemoveMeta){ - data = data.replace( - /---[\n(\r\n)][\s\S]*?---[\n(\r\n)]/, - '' - ) - } - let assets = fm[this.yaml]?.Assets + let fm: { [key: string]: any } = {}; + if(mcache && mcache['frontmatter']){ + fm = mcache['frontmatter']; + } - if(fm[this.yaml]?.UseGitLink && assets){ - data = data.replace( - /\!\[\[(.*?)\]\]/g, - (match:any, name:string) => { - return `![${name}](${assets}/${name.replace(/ /g,'%20')})`; - } - ) - } - await this.fsEditor.fs.writeFile( - target, data, '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) - } - } + 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) } } } - ) + } } } diff --git a/src/commands.ts b/src/commands.ts index 3dc3e6d..efe3393 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -6,7 +6,7 @@ import NoteSyncPlugin from '../main'; import { it } from 'node:test'; const cmd_export_current_note = (plugin:NoteSyncPlugin) => ({ - id: 'cmd_export_current_note', + id: 'export_current_note', name: plugin.strings.cmd_export_current_note, callback: async () => { let tfile = plugin.app.workspace.getActiveFile(); @@ -15,7 +15,7 @@ const cmd_export_current_note = (plugin:NoteSyncPlugin) => ({ }); const cmd_set_vexporter = (plugin:NoteSyncPlugin) => ({ - id: 'cmd_set_vexporter', + id: 'set_vexporter', name: plugin.strings.cmd_set_vexporter, callback: async () => { let tfile = plugin.app.workspace.getActiveFile(); @@ -41,7 +41,7 @@ const cmd_set_vexporter = (plugin:NoteSyncPlugin) => ({ }); const cmd_export_plugin = (plugin:NoteSyncPlugin) => ({ - id: 'cmd_export_plugin', + id: 'export_plugin', name: plugin.strings.cmd_export_plugin, callback: async () => { diff --git a/src/fseditor.ts b/src/fseditor.ts index 5c7fd6a..d0f49c6 100644 --- a/src/fseditor.ts +++ b/src/fseditor.ts @@ -1,6 +1,7 @@ import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile, TFolder } from 'obsidian'; import NoteSyncPlugin from "../main"; +import { on } from 'node:events'; export class FsEditor{ fs; app:App; @@ -26,33 +27,18 @@ export class FsEditor{ if(tfile){ return tfile; } - - let tfiles = (this.app.metadataCache as any).uniqueFileLookup.get(path.toLowerCase()); - if(!tfiles){ - tfiles = (this.app.metadataCache as any).uniqueFileLookup.get(path.toLowerCase()+'.md'); - if(!tfiles){ - return null; - }else{ - path = path+'.md' - } - } - - let ctfiles = tfiles.filter((x:TFile)=>x.name==path) - if(ctfiles.length>0){ - if(only_first){ - return ctfiles[0] - }else{ - return ctfiles - } - } - - if(tfiles.length>0){ - if(only_first){ - return tfiles[0] - }else{ - return tfiles - } - } + + if(only_first){ + let tfile = (this.app.metadataCache as any).getFirstLinkpathDest(path.split('/').last()); + if(tfile){ + return tfile; + } + }else{ + let tfiles = (this.app.metadataCache as any).getLinkpathDest(path.split('/').last()); + if(tfiles && tfiles.length>0){ + return tfiles; + } + } return null; }catch{ // console.log(path) diff --git a/src/setting.ts b/src/setting.ts index 113241c..df3dbee 100644 --- a/src/setting.ts +++ b/src/setting.ts @@ -14,7 +14,7 @@ export const DEFAULT_SETTINGS: MySettings = { vaultDir:'' } -export class MySettingTab extends PluginSettingTab { +export class NoteSyncSettingTab extends PluginSettingTab { plugin: NoteSyncPlugin; constructor(app: App, plugin: NoteSyncPlugin) { super(app, plugin); diff --git a/src/strings.ts b/src/strings.ts index 3bd2752..8347984 100644 --- a/src/strings.ts +++ b/src/strings.ts @@ -77,7 +77,7 @@ export class Strings{ if(this.language=='zh'){ return '危险!同步时删除目标库中多出的文件'; }else{ - return 'Danger! Delet files or folders in target vault but not in current vault.'; + return 'Danger! Delete files or folders in target vault but not in current vault.'; } }