generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
258 lines (225 loc) · 6.88 KB
/
main.ts
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { App, Editor, FuzzySuggestModal, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from "obsidian";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
interface MarkdownBloggerSettings {
projectFolder: string;
showHiddenFolders: boolean;
}
const DEFAULT_SETTINGS: MarkdownBloggerSettings = {
projectFolder: "",
showHiddenFolders: false
}
enum Action {
Push,
Pull
}
export default class MarkdownBlogger extends Plugin {
settings: MarkdownBloggerSettings;
async onload() {
await this.loadSettings();
this.addCommand({
id: "validate-path",
name: "Validate path",
editorCallback: (editor: Editor, view: MarkdownView) => {
const { projectFolder } = this.settings;
if (!fs.existsSync(projectFolder)) {
new ErrorModal(this.app).open();
return;
}
new Notice(`Valid path: ${this.settings.projectFolder}`);
},
});
this.addCommand({
id: "push-md",
name: "Push markdown",
editorCallback: (editor: Editor, view: MarkdownView) => {
const { projectFolder } = this.settings;
if (!fs.existsSync(projectFolder)) {
new ErrorModal(this.app).open();
return;
}
const text = editor.getDoc().getValue();
const projectBlogPath = path.resolve(this.settings.projectFolder, view.file.name);
try {
fs.writeFileSync(`${projectBlogPath}`, text, {encoding: "utf8"});
new Notice(`Your file has been pushed! At ${projectBlogPath}`);
} catch (err) {
new Notice(err.message);
}
},
});
this.addCommand({
id: "pull-md",
name: "Pull markdown",
editorCheckCallback: (checking: boolean, editor: Editor, view: MarkdownView) => {
const projectBlogPath = path.resolve(this.settings.projectFolder, view.file.name);
if (fs.existsSync(projectBlogPath)) {
if (!checking) {
try {
const file = fs.readFileSync(projectBlogPath, "utf8");
editor.getDoc().setValue(file);
new Notice(`Your file has been pulled! From ${projectBlogPath}`);
} catch (err) {
new Notice(err.message);
}
}
return true;
}
return false;
},
});
this.addCommand({
id: "push-custom-path-md",
name: "Push to custom path",
editorCallback: (editor: Editor, view: MarkdownView) => {
new PathModal(this.app, this.settings, Action.Push).open();
}
});
this.addCommand({
id: "pull-custom-path",
name: "Pull from custom path",
editorCallback: (editor: Editor, view: MarkdownView) => {
new PathModal(this.app, this.settings, Action.Pull).open()
}
})
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new MarkdownBloggerSettingTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class ErrorModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
const {contentEl} = this;
contentEl.setText("The project folder does not exist. Please create the path or update the current path in plugin settings.");
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
class PathModal extends FuzzySuggestModal<string> {
currPath = os.homedir();
settings: MarkdownBloggerSettings
action: Action
constructor(app: App, settings: MarkdownBloggerSettings, action: Action) {
super(app);
this.settings = settings;
this.action = action;
}
getItems(): string[] {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
const paths = fs.readdirSync(this.currPath).filter((p) => {
const fullPath = path.resolve(this.currPath, p);
let stats;
try { stats = fs.statSync(fullPath, { throwIfNoEntry: false }); } catch (e) { return false; }
if (stats === undefined) return false;
return (
(stats.isDirectory()
|| (path.basename(fullPath) === view?.file.name))
&& (p[0] !== "." || this.settings.showHiddenFolders)
);
});
paths.push("..");
paths.push("Select");
return paths;
}
getItemText(dir: string): string {
return dir;
}
onChooseItem(dir: string, evt: MouseEvent | KeyboardEvent) {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (dir === "Select") {
if (view) {
if (!fs.existsSync(path.resolve(this.currPath))) {
new ErrorModal(this.app).open();
return;
}
const text = view.editor.getDoc().getValue();
const filePath = path.resolve(this.currPath, view.file.name);
if (this.action === Action.Push) {
try {
fs.writeFileSync(`${filePath}`, text, {encoding: "utf8"});
new Notice(`Your file has been pushed! At ${filePath}`);
} catch (err) {
new Notice(err.message);
}
} else if (this.action === Action.Pull) {
try {
const file = fs.readFileSync(filePath, "utf8");
view.editor.getDoc().setValue(file);
new Notice(`Your file has been pulled! From ${filePath}`);
} catch (err) {
new Notice(err.message);
}
}
}
return;
} else if (view && dir === view.file.name) {
const filePath = path.resolve(this.currPath, view.file.name);
if (this.action === Action.Push) {
const text = view.editor.getDoc().getValue();
try {
fs.writeFileSync(`${filePath}`, text, {encoding: "utf8"});
new Notice(`Your file has been pushed! At ${filePath}`);
} catch (err) {
new Notice(err.message);
}
} else if (this.action === Action.Pull) {
try {
const file = fs.readFileSync(filePath, "utf8");
view.editor.getDoc().setValue(file);
new Notice(`Your file has been pulled! From ${filePath}`);
} catch (err) {
new Notice(err.message);
}
}
return;
} else {
this.currPath = path.normalize(path.join(this.currPath, dir));
}
this.open();
}
}
class MarkdownBloggerSettingTab extends PluginSettingTab {
plugin: MarkdownBlogger;
constructor(app: App, plugin: MarkdownBlogger) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl("h2", {text: "Settings for Obsidian Markdown Blogger."});
new Setting(containerEl)
.setName("Local project folder path")
.setDesc("The local project folder for your blog, portfolio, or static site. Must be an absolute path.")
.addText(text => text
.setPlaceholder("/Users/johnsample/projects/astro-blog/collections/")
.setValue(this.plugin.settings.projectFolder)
.onChange(async (value) => {
this.plugin.settings.projectFolder = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl).setName("Show hidden folders")
.setDesc("Show hidden folders when pushing to a custom path")
.addToggle(cb => cb
.setValue(this.plugin.settings.showHiddenFolders)
.onChange(async (value) => {
this.plugin.settings.showHiddenFolders = value;
await this.plugin.saveSettings();
})
);
}
}