generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.ts
103 lines (83 loc) · 2.8 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
import { App, Plugin, PluginSettingTab, Setting, TAbstractFile } from 'obsidian';
import { DEFAULT_SETTINGS, OnyxBooxExtractorSettings } from 'src/models';
import { extractOnyxReadingNotes } from 'src/extract-onyx-notes';
export default class OnyxBooxExtractorPlugin extends Plugin {
settings: OnyxBooxExtractorSettings;
async onload() {
await this.loadSettings();
this.registerEvent(
this.app.workspace.on("file-menu", (menu, file) => {
menu.addItem((item) => {
item.setTitle("Extract Onyx file")
.setIcon("document")
.onClick(async () => {
await extractOnyxReadingNotesByFile(this.settings, file)
});
});
})
);
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new OnyxBooxExtractorSettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class OnyxBooxExtractorSettingTab extends PluginSettingTab {
plugin: OnyxBooxExtractorPlugin;
constructor(app: App, plugin: OnyxBooxExtractorPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings'});
new Setting(containerEl)
.setName('Permanent note id prefix')
.setDesc('')
.addText(text => text
.setPlaceholder('perm.')
.setValue(this.plugin.settings.permanentNotePrefix)
.onChange(async (value) => {
this.plugin.settings.permanentNotePrefix = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Literature note id prefix')
.setDesc('')
.addText(text => text
.setPlaceholder('lit.')
.setValue(this.plugin.settings.literatureNotePrefix)
.onChange(async (value) => {
this.plugin.settings.literatureNotePrefix = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Create Reference Notes?')
.setDesc('')
.addToggle(text => text
.setValue(this.plugin.settings.createReferenceNote)
.onChange(async (value) => {
this.plugin.settings.createReferenceNote = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('References note id prefix')
.setDesc('')
.addText(text => text
.setPlaceholder('ref.')
.setValue(this.plugin.settings.referenceNotePrefix)
.onChange(async (value) => {
this.plugin.settings.referenceNotePrefix = value;
await this.plugin.saveSettings();
}));
}
}
async function extractOnyxReadingNotesByFile(settings: OnyxBooxExtractorSettings, file: TAbstractFile) {
const fileContent = await this.app.vault.cachedRead(file)
await extractOnyxReadingNotes(this.app.vault, settings, fileContent);
}