2024-04-22 13:27:53 +00:00
|
|
|
|
import * as Module from 'module';
|
|
|
|
|
|
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting, TFile } from 'obsidian';
|
|
|
|
|
|
|
|
|
|
|
|
// Remember to rename these classes and interfaces!
|
|
|
|
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
|
|
|
|
interface VExporterSettings {
|
2024-04-22 14:01:24 +00:00
|
|
|
|
nameLocalGitProject: string;
|
|
|
|
|
|
assetsLocalGitProject:string;
|
2024-04-22 13:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const DEFAULT_SETTINGS: VExporterSettings = {
|
2024-04-22 14:01:24 +00:00
|
|
|
|
nameLocalGitProject: 'LocalGitProject',
|
|
|
|
|
|
assetsLocalGitProject: 'assets'
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const cmd_export_readme = (plugin:VaultExpoterPlugin) => ({
|
|
|
|
|
|
id: 'export_readme',
|
|
|
|
|
|
name: 'Export readMe',
|
|
|
|
|
|
callback: async () => {
|
|
|
|
|
|
let nc = await plugin.app.plugins.getPlugin('note-chain');
|
|
|
|
|
|
let tfile = nc.chain.current_note;
|
|
|
|
|
|
await plugin.export_readme(
|
|
|
|
|
|
tfile,null,true,plugin.settings.assetsLocalGitProject
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const cmd_set_git_project = (plugin:VaultExpoterPlugin) => ({
|
|
|
|
|
|
id: 'set_git_project',
|
|
|
|
|
|
name: 'Set Git Project',
|
|
|
|
|
|
callback: async () => {
|
|
|
|
|
|
let nc = await plugin.app.plugins.getPlugin('note-chain');
|
|
|
|
|
|
let dir = await nc.chain.tp_prompt('输入文件夹');
|
|
|
|
|
|
if(!dir || !fs.existsSync(dir)){
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
await nc.editor.set_frontmatter(
|
|
|
|
|
|
nc.chain.current_note,
|
|
|
|
|
|
plugin.settings.nameLocalGitProject,
|
|
|
|
|
|
dir
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const commandBuilders = [
|
|
|
|
|
|
cmd_export_readme,
|
|
|
|
|
|
cmd_set_git_project
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
function addCommands(plugin:VaultExpoterPlugin) {
|
|
|
|
|
|
commandBuilders.forEach((c) => {
|
|
|
|
|
|
plugin.addCommand(c(plugin));
|
|
|
|
|
|
});
|
2024-04-22 13:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default class VaultExpoterPlugin extends Plugin {
|
|
|
|
|
|
settings: VExporterSettings;
|
|
|
|
|
|
root : string;
|
2024-04-22 14:01:24 +00:00
|
|
|
|
|
2024-04-22 13:27:53 +00:00
|
|
|
|
async onload() {
|
|
|
|
|
|
await this.loadSettings();
|
|
|
|
|
|
this.app.vt = this;
|
|
|
|
|
|
this.fs = fs;
|
|
|
|
|
|
this.root = this.app.vault.adapter.basePath;
|
|
|
|
|
|
// This adds a settings tab so the user can configure various aspects of the plugin
|
|
|
|
|
|
this.addSettingTab(new VExporterSettingTab(this.app, this));
|
2024-04-22 14:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
addCommands(this);
|
2024-04-22 13:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onunload() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async loadSettings() {
|
|
|
|
|
|
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async saveSettings() {
|
|
|
|
|
|
await this.saveData(this.settings);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 附件 src 到 dst,不在 vault 中,需要绝对路径
|
|
|
|
|
|
* overwrite,复盖;mtime,新文件;
|
|
|
|
|
|
*/
|
|
|
|
|
|
copy_file_by_path(src:string, dst:string,mode='overwrite>mtime>pass') {
|
|
|
|
|
|
mode = mode.split('>')[0]
|
|
|
|
|
|
if(!fs.existsSync(src)){
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
if(fs.existsSync(dst)){
|
|
|
|
|
|
if(mode==='overwrite'){
|
|
|
|
|
|
fs.unlinkSync(dst);
|
|
|
|
|
|
fs.copyFileSync(src,dst);
|
|
|
|
|
|
}else if(mode==='mtime'){
|
|
|
|
|
|
// dst 更新时间小于 src
|
|
|
|
|
|
if(fs.statSync(dst).mtimeMs<fs.statSync(src).mtimeMs){
|
|
|
|
|
|
fs.unlinkSync(dst);
|
|
|
|
|
|
fs.copyFileSync(src,dst);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}else{
|
|
|
|
|
|
fs.copyFileSync(src,dst);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
abspath(tfile:TFile){
|
|
|
|
|
|
if(tfile){
|
|
|
|
|
|
return this.root+'/'+tfile.path;
|
|
|
|
|
|
}else{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
copy_tfile(tfile:TFile, dst:string) {
|
|
|
|
|
|
if(tfile){
|
|
|
|
|
|
let src = this.abspath(tfile);
|
|
|
|
|
|
src && this.copy_file_by_path(src,dst);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async export_readme(tfile:TFile,dst:string|null,as_readme=true,assets='assets'){
|
|
|
|
|
|
|
|
|
|
|
|
let nc = this.app.plugins.getPlugin('note-chain');
|
|
|
|
|
|
if(!tfile){tfile = nc.chain.current_note;}
|
|
|
|
|
|
|
|
|
|
|
|
if(!dst){
|
2024-04-22 14:01:24 +00:00
|
|
|
|
dst = this.settings.nameLocalGitProject;
|
2024-04-22 13:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
if(!dst.contains('/')){
|
|
|
|
|
|
dst = nc.editor.get_frontmatter(tfile,dst);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!dst){
|
|
|
|
|
|
dst = await nc.chain.tp_prompt('Path of LocalGitProject');
|
|
|
|
|
|
if(!dst){return;}
|
|
|
|
|
|
}
|
|
|
|
|
|
dst = dst.replace(/\\/g,'/');
|
|
|
|
|
|
if(!fs.existsSync(dst)){
|
|
|
|
|
|
console.log('No Dir:',dst);
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log(dst);
|
|
|
|
|
|
let olinks = nc.chain.get_outlinks(tfile);
|
|
|
|
|
|
|
|
|
|
|
|
let tmp;
|
|
|
|
|
|
if(as_readme){
|
|
|
|
|
|
tmp = dst+'/'+'readMe.md';
|
|
|
|
|
|
}else{
|
|
|
|
|
|
tmp = dst+'/'+tfile.basename+'.md';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.copy_tfile(tfile,tmp);
|
|
|
|
|
|
|
|
|
|
|
|
let adir = dst+'/'+assets;
|
|
|
|
|
|
if(!fs.existsSync(adir)){
|
|
|
|
|
|
fs.mkdirSync(adir);
|
|
|
|
|
|
}
|
|
|
|
|
|
for(let f of olinks){
|
|
|
|
|
|
if(!(f.extension==='md')){
|
|
|
|
|
|
this.copy_tfile(f,adir+'/'+f.basename+'.'+f.extension);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class VExporterSettingTab extends PluginSettingTab {
|
|
|
|
|
|
plugin: VaultExpoterPlugin;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(app: App, plugin: VaultExpoterPlugin) {
|
|
|
|
|
|
super(app, plugin);
|
|
|
|
|
|
this.plugin = plugin;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
display(): void {
|
|
|
|
|
|
const {containerEl} = this;
|
|
|
|
|
|
|
|
|
|
|
|
containerEl.empty();
|
|
|
|
|
|
|
|
|
|
|
|
new Setting(containerEl)
|
2024-04-22 14:01:24 +00:00
|
|
|
|
.setName('LocalGitProject')
|
|
|
|
|
|
.setDesc('Metadata Name for Dir of Git Porject')
|
|
|
|
|
|
.addText(text => text
|
|
|
|
|
|
.setPlaceholder('Enter your field')
|
|
|
|
|
|
.setValue(this.plugin.settings.nameLocalGitProject)
|
|
|
|
|
|
.onChange(async (value) => {
|
|
|
|
|
|
this.plugin.settings.nameLocalGitProject = value;
|
|
|
|
|
|
await this.plugin.saveSettings();
|
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
new Setting(containerEl)
|
|
|
|
|
|
.setName('Folder For Assets')
|
|
|
|
|
|
.setDesc('Dir Name for Assets')
|
|
|
|
|
|
.addText(text => text
|
|
|
|
|
|
.setValue(this.plugin.settings.assetsLocalGitProject)
|
2024-04-22 13:27:53 +00:00
|
|
|
|
.onChange(async (value) => {
|
2024-04-22 14:01:24 +00:00
|
|
|
|
this.plugin.settings.assetsLocalGitProject = value;
|
2024-04-22 13:27:53 +00:00
|
|
|
|
await this.plugin.saveSettings();
|
|
|
|
|
|
}));
|
2024-04-22 14:01:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
2024-04-22 13:27:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|