Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
174 lines (145 loc) · 5.69 KB
/
Copy pathmain.js
File metadata and controls
174 lines (145 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
const { Plugin, PluginSettingTab, Setting, Notice } = require('obsidian');
const fs = require('fs');
const path = require('path');
const os = require('os');
const DEFAULT_SETTINGS = {
fileName: 'flashcards.md',
relativePath: ''
};
class Pot2ObsidianPlugin extends Plugin {
async onload() {
await this.loadSettings();
this.addSettingTab(new Pot2ObsidianSettingTab(this.app, this));
// Write config on load
await this.writeSharedConfig();
console.log('Pot2Obsidian plugin loaded');
}
onunload() {
console.log('Pot2Obsidian plugin unloaded');
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
await this.writeSharedConfig();
}
getConfigDir() {
const homeDir = os.homedir();
return path.join(homeDir, '.pot2obsidian');
}
getConfigPath() {
return path.join(this.getConfigDir(), 'config.json');
}
getFullFilePath() {
const vaultPath = this.app.vault.adapter.basePath;
const relativePath = this.settings.relativePath || '';
const fileName = this.settings.fileName || 'flashcards.md';
if (relativePath) {
return path.join(vaultPath, relativePath, fileName);
}
return path.join(vaultPath, fileName);
}
async writeSharedConfig() {
try {
const configDir = this.getConfigDir();
const configPath = this.getConfigPath();
const filePath = this.getFullFilePath();
// Ensure config directory exists
if (!fs.existsSync(configDir)) {
fs.mkdirSync(configDir, { recursive: true });
}
// Write config
const config = {
file_path: filePath,
vault_path: this.app.vault.adapter.basePath,
file_name: this.settings.fileName,
relative_path: this.settings.relativePath,
updated_at: new Date().toISOString()
};
fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
console.log('Pot2Obsidian config written to:', configPath);
} catch (error) {
console.error('Failed to write Pot2Obsidian config:', error);
new Notice('Pot2Obsidian: Failed to write config file');
}
}
async ensureVocabularyFile() {
const filePath = this.getFullFilePath();
const relativePath = this.settings.relativePath || '';
const fileName = this.settings.fileName || 'flashcards.md';
// Create folder if needed
if (relativePath) {
const folderExists = await this.app.vault.adapter.exists(relativePath);
if (!folderExists) {
await this.app.vault.createFolder(relativePath);
}
}
// Create file if needed
const fullRelativePath = relativePath ? `${relativePath}/${fileName}` : fileName;
const fileExists = await this.app.vault.adapter.exists(fullRelativePath);
if (!fileExists) {
await this.app.vault.create(fullRelativePath, '# Vocabulary\n\n');
new Notice(`Created vocabulary file: ${fullRelativePath}`);
}
return filePath;
}
}
class Pot2ObsidianSettingTab extends PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Pot2Obsidian Settings' });
new Setting(containerEl)
.setName('Vocabulary file name')
.setDesc('Name of the markdown file for vocabulary (e.g., flashcards.md)')
.addText(text => text
.setPlaceholder('flashcards.md')
.setValue(this.plugin.settings.fileName)
.onChange(async (value) => {
this.plugin.settings.fileName = value || 'flashcards.md';
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Folder path')
.setDesc('Relative folder path within the vault (leave empty for vault root)')
.addText(text => text
.setPlaceholder('e.g., Notes/Vocabulary')
.setValue(this.plugin.settings.relativePath)
.onChange(async (value) => {
this.plugin.settings.relativePath = value;
await this.plugin.saveSettings();
}));
// Show current full path
const fullPath = this.plugin.getFullFilePath();
containerEl.createEl('div', {
text: `Full path: ${fullPath}`,
cls: 'setting-item-description'
});
// Show config file location
const configPath = this.plugin.getConfigPath();
containerEl.createEl('div', {
text: `Config file: ${configPath}`,
cls: 'setting-item-description'
});
new Setting(containerEl)
.setName('Create vocabulary file')
.setDesc('Create the vocabulary file if it does not exist')
.addButton(button => button
.setButtonText('Create File')
.onClick(async () => {
try {
await this.plugin.ensureVocabularyFile();
new Notice('Vocabulary file ready!');
} catch (error) {
new Notice('Failed to create file: ' + error.message);
}
}));
}
}
module.exports = Pot2ObsidianPlugin;