generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
121 lines (95 loc) · 3.01 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
import { App, Editor, MarkdownView, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import markdownToTxt from 'markdown-to-txt';
interface CleanUpSettings {
isCleanHTML: boolean;
isCleanMarkdown: boolean;
}
const DEFAULT_SETTINGS: CleanUpSettings = {
isCleanHTML: true,
isCleanMarkdown: false
}
export default class CleanUp extends Plugin {
settings: CleanUpSettings;
async onload() {
await this.loadSettings();
// Creates an icon in the left ribbon.
const ribbonIconEl = this.addRibbonIcon('eraser', 'Clean Up', (evt: MouseEvent) => {
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
const editor = markdownView.editor;
this.cleanSelectContent(editor);
}
});
// adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'clean-up-selection',
name: 'Clean Up Selection',
editorCallback: (editor: Editor, view: MarkdownView) => {
this.cleanSelectContent(editor);
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new CleanUpSettingTab(this.app, this));
}
onunload() {
}
cleanSelectContent(editor: Editor) {
// Get plugin settings
const isCleanHTML = this.settings.isCleanHTML;
const isCleanMarkdown = this.settings.isCleanMarkdown;
// Get the selected content
const selectedContent = editor.getSelection();
const selectedContentLines = selectedContent.split('\n').length;
let cleanedContent = selectedContent;
if (isCleanHTML) {
cleanedContent = selectedContent.replace(/<[^>]+>/g, '');
}
if (isCleanMarkdown) {
cleanedContent = markdownToTxt(selectedContent);
}
editor.replaceSelection(cleanedContent);
new Notice(selectedContentLines + ' rows cleaned');
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class CleanUpSettingTab extends PluginSettingTab {
plugin: CleanUp;
constructor(app: App, plugin: CleanUp) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Settings for cleanable content.' });
//Switch button for clean HTML
new Setting(containerEl)
.setName('Clean HTML')
.setDesc('Will clear all HTML tags if enabled')
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.isCleanHTML)
.onChange(async (value) => {
this.plugin.settings.isCleanHTML = value;
await this.plugin.saveSettings();
});
});
//Switch button for clean Markdown
new Setting(containerEl)
.setName('Clean Markdown')
.setDesc('Will clear all Markdown formats if enabled')
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.isCleanMarkdown)
.onChange(async (value) => {
this.plugin.settings.isCleanMarkdown = value;
await this.plugin.saveSettings();
});
});
}
}