-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.ts
185 lines (156 loc) Β· 5.27 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
import { App, Plugin, PluginSettingTab, Setting, Notice, FileSystemAdapter } from 'obsidian';
import obsidiosaurusProcess from 'src/mainProcessor'
import { Config } from 'src/types'
import pino from 'pino';
import path from 'path';
import { setSettings } from 'config';
export const logger = pino();
export const config: Config = {
obsidianVaultDirectory: "./vault",
docusaurusWebsiteDirectory: "./website",
obsidianAssetSubfolderName: "assets",
docusaurusAssetSubfolderName: "assets",
mainLanguage: "en",
convertedImageType: "webp",
convertedImageMaxWidth: "2500",
debug: false,
developer: false
}
export default class Obsidisaurus extends Plugin {
settings: Config;
async onload() {
await this.loadSettings();
if (this.settings.debug) {
logger.info("π’ Obsidiosaurus Plugin loaded");
}
const ribbonIconEl = this.addRibbonIcon('file-up', 'Obsidiosaurus', async (evt: MouseEvent) => {
try {
logger.info("π Obsidiosaurus started");
new Notice("π Obsidiosaurus started")
// @ts-ignore, it says there is no property basePath, but it is?
if(this.app.vault.adapter instanceof FileSystemAdapter) {
const basePath = path.dirname(this.app.vault.adapter.getBasePath());
await obsidiosaurusProcess(basePath);
}
} catch (error) {
if (this.settings.debug) {
const errorMessage = `β Obsidiosaurus crashed in function with the following error:\n${error.stack}`;
logger.error(errorMessage);
new Notice(`β Obsidiosaurus crashed. \n${errorMessage}`);
} else {
logger.error(`β Obsidiosaurus crashed with error message: \n${error} `);
new Notice("β Obsidiosaurus crashed. \n Check log files for more info")
}
}
});
ribbonIconEl.addClass('my-plugin-ribbon-class');
this.addSettingTab(new SettingTab(this.app, this));
}
onunload() {
if (config.debug) {
logger.info('βͺ Obsidiosaurus Plugin unloaded');
}
}
async loadSettings() {
this.settings = Object.assign({}, config, await this.loadData());
setSettings(this.settings);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SettingTab extends PluginSettingTab {
plugin: Obsidisaurus;
constructor(app: App, plugin: Obsidisaurus) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h1', { text: 'Directories' });
new Setting(containerEl)
.setName('Docusaurus Directory')
.setDesc('Path to your docusaurus instance')
.addText(text => text
.setPlaceholder('Enter paths')
.setValue(this.plugin.settings.docusaurusWebsiteDirectory)
.onChange(async (value) => {
this.plugin.settings.docusaurusWebsiteDirectory = value;
await this.plugin.saveSettings();
}));
containerEl.createEl('h1', { text: 'Assets' });
new Setting(containerEl)
.setName('Obsidian Asset Folder')
.setDesc('Name of Obsidian Asset Folder')
.addText(text => text
.setPlaceholder('Enter folders')
.setValue(this.plugin.settings.obsidianAssetSubfolderName)
.onChange(async (value) => {
this.plugin.settings.obsidianAssetSubfolderName = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Docusaurus Asset Folder')
.setDesc('Name of Docusaurus Asset Folder')
.addText(text => text
.setPlaceholder('Enter folders')
.setValue(this.plugin.settings.docusaurusAssetSubfolderName)
.onChange(async (value) => {
this.plugin.settings.docusaurusAssetSubfolderName = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Image Type')
.setDesc('Format in which to convert all images')
.addDropdown(dropdown => dropdown
.addOptions({
'webp': 'WebP',
})
.setValue(this.plugin.settings.convertedImageType)
.onChange(async (value) => {
this.plugin.settings.convertedImageType = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Image Width')
.setDesc('Set the max width for the images in [px]')
.addText(number => number
.setPlaceholder('2500')
.setValue(this.plugin.settings.convertedImageMaxWidth)
.onChange(async (value) => {
this.plugin.settings.convertedImageMaxWidth = value;
await this.plugin.saveSettings();
}));
containerEl.createEl('h1', { text: 'Language' });
new Setting(containerEl)
.setName('Main Language')
.setDesc('Your main language code to publish')
.addText(text => text
.setPlaceholder('Enter language code')
.setValue(this.plugin.settings.mainLanguage)
.onChange(async (value) => {
this.plugin.settings.mainLanguage = value;
await this.plugin.saveSettings();
}));
containerEl.createEl('h1', { text: 'Dev Options' });
new Setting(containerEl)
.setName('Debug mode')
.setDesc('Better logging for debugging')
.addToggle((value) => {
value.setValue(this.plugin.settings.debug).onChange((value) => {
this.plugin.settings.debug = value;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName('Developer mode')
.setDesc('Only for plugin developers')
.addToggle((value) => {
value.setValue(this.plugin.settings.debug).onChange((value) => {
this.plugin.settings.debug = value;
this.plugin.saveSettings();
});
});
}
}