generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
106 lines (82 loc) · 2.85 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
import 'components/MySettingTab';
import MySettingTab from 'components/MySettingTab';
import { DEFAULT_SETTINGS, IssueTrackerPattern, PluginSettings } from 'components/PluginsSettings';
import RegexMarkdownParser from 'components/RegexMarkdownParser';
import { Editor, MarkdownView, Notice, Plugin } from 'obsidian';
export default class IssueTrackerMain extends Plugin {
settings: PluginSettings;
async onload() {
await this.loadSettings();
this.addInitialIssueTracker();
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'issue-tracker-open-editor',
name: 'Apply Issue Trackers',
editorCallback: (editor: Editor, view: MarkdownView) => {
console.log(editor.getSelection());
const document = editor.getDoc();
const documentToReplace = document.getValue()
const changeDocument = this.renderIssueTrackers(this.settings.patterns, documentToReplace)
editor.getDoc().setValue(changeDocument)
}
});
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new MySettingTab(this.app, this));
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
}
addInitialIssueTracker() {
if (this.settings.patterns?.length > 0)
return;
this.AddPattern(
{
name: "Clickup",
regex: "(CU-)(/\d+)",
url: "https://app.clickup.com/t/$2",
test: "Some task with description finished CU-49827",
}
)
}
onunload() {
}
renderIssueTrackers(patterns: IssueTrackerPattern[], content: string) {
let modifiedContent = content;
try {
patterns.forEach(pattern => {
//apply here alle patterns from patterns like:
const parser = new RegexMarkdownParser(pattern.regex, pattern.url);
modifiedContent = parser.parse(modifiedContent);
});
} catch (e) {
return content;
}
return modifiedContent;
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
SavePattern(changed: IssueTrackerPattern, index: number) {
this.settings.patterns[index] = changed;
this.saveSettings();
}
AddPattern(created: IssueTrackerPattern) {
this.settings.patterns.push(created)
this.saveSettings();
}
DeletePatter(index: number): void {
if (index >= 0 && index < this.settings.patterns.length) {
// Remove the pattern at the specified index
this.settings.patterns.splice(index, 1);
// Save the updated settings
this.saveData(this.settings).then(() => {
new Notice('Pattern deleted successfully.');
// Additional code if needed, like refreshing a view or UI
});
} else {
new Notice('Invalid pattern index.');
}
}
}