modify by comments of joethei

This commit is contained in:
ZigHolding 2025-02-13 20:09:16 +08:00
parent 8d5cb12008
commit 9fcca1222e
6 changed files with 79 additions and 95 deletions

View file

@ -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

122
main.ts
View file

@ -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)
}
}
}
)
}
}
}

View file

@ -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 () => {

View file

@ -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)

View file

@ -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);

View file

@ -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.';
}
}