generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
191 lines (166 loc) · 4.72 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
import { App, Editor, EditorPosition, Plugin, PluginSettingTab, Setting } from 'obsidian';
type PluginSettings = Record<string, { pattern: string, link: string }>;
const DEFAULT_SETTINGS: PluginSettings = {};
export default class LinkMagicPlugin extends Plugin {
settings: PluginSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this));
const onKeyEvent = (evt: KeyboardEvent) => {
if (!evt.shiftKey) {
const mdFile = this.app.workspace.activeEditor
if (mdFile?.editor) {
this.triggerSnippet(mdFile.editor, evt)
}
}
}
this.registerDomEvent(document, "keydown", onKeyEvent)
this.registerEvent(this.app.workspace.on("window-open", (event) => {
this.registerDomEvent(activeWindow, "keydown", onKeyEvent)
}))
}
onunload() {
console.log('Unloading plugin.')
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
checkForMatch(editor: Editor, pos?: EditorPosition) {
let curPos = pos ? pos : editor.getCursor()
// @ts-expect-error, not typed
const editorView = editor.cm as EditorView;
let line = editor.getLine(curPos.line)
let word = "";
let i = curPos.ch - 1;
while (i >= 0 && line[i] !== " ") {
word = line[i] + word
i--
}
//We should check if we've already done the text replacement
if (RegExp("(\[.*\]\(.*\))", "g").test(word)) {
return
}
Object.values(this.settings)
.filter(({ pattern, link }) => pattern && link)
.some(({ pattern, link }) => {
const regex = RegExp(pattern, "g")
if (regex.test(word)) {
const enclosingRegex = /^(\()?(.*?)(\))?$/
let match = enclosingRegex.exec(word)
if (match) {
const startChar = match[1] || ""
const innerWord = match[2]
const endChar = match[3] || ""
let markdownLink = `${startChar}[${innerWord}](${link.replace("{pattern}", innerWord)})${endChar}`
editor.replaceRange(markdownLink, { line: curPos.line, ch: curPos.ch - word.length }, curPos);
}
}
})
}
handleEnter(editor: Editor, pos?: EditorPosition) {
// Get the current line position
let curPos = pos ? pos : editor.getCursor()
// Don't think we need to check if we're on the first line as this should only be called after enter
let lineAbove = editor.getLine(curPos.line - 1)
this.checkForMatch(editor, { line: curPos.line - 1, ch: lineAbove.length })
}
handleTab(editor: Editor, pos?: EditorPosition) {
//Remove all the white space
let curPos = pos ? pos : editor.getCursor()
editor.getLine(curPos.line).trimStart()
}
triggerSnippet(editor: Editor, evt: KeyboardEvent) {
switch (evt.key) {
case " ": {
this.checkForMatch(editor)
break;
}
case "Tab": {
this.handleTab(editor)
break;
}
case "Enter": {
this.handleEnter(editor)
break;
}
default: {
break;
}
}
}
}
class SettingTab extends PluginSettingTab {
plugin: LinkMagicPlugin;
constructor(app: App, plugin: LinkMagicPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
let settingName = ""
new Setting(containerEl)
.setName('Add a new rule')
.addText((text) => {
text.setPlaceholder("Rule Name").onChange((value) => {
settingName = value;
});
})
.addButton((button) => {
button.setButtonText("+").onClick(async () => {
if (!settingName) {
return;
}
this.plugin.settings = {
...this.plugin.settings,
[settingName]: {
pattern: "",
link: "",
},
};
await this.plugin.saveSettings();
this.display()
});
});
Object.keys(this.plugin.settings).forEach((key) => {
new Setting(containerEl)
.setName(key)
.addText((text) => {
text
.setPlaceholder("Pattern")
.setValue(this.plugin.settings[key].pattern)
.onChange(async (value) => {
this.plugin.settings[key].pattern = value
await this.plugin.saveSettings()
});
})
.addText((text) => {
text
.setPlaceholder("Link")
.setValue(this.plugin.settings[key].link)
.onChange(async (value) => {
this.plugin.settings[key].link = value
await this.plugin.saveSettings()
})
})
.addButton((button) => {
button.setButtonText("-").onClick(async () => {
this.plugin.settings = Object.keys(this.plugin.settings)
.filter((settingsKey) => settingsKey !== key)
.reduce(
(settings, settingsKey) => ({
...settings,
[settingsKey]: this.plugin.settings[settingsKey],
}),
{}
);
await this.plugin.saveSettings();
this.display();
});
});
})
}
}