generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.ts
221 lines (191 loc) · 5.88 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { App, Editor, MarkdownRenderChild, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { SmartLinksPattern, parseNextLink } from 'replacements';
interface SmartLinksSettings {
patterns: [{regexp: string, replacement: string}];
}
const DEFAULT_SETTINGS: SmartLinksSettings = {
patterns: [
{regexp: 'T(\\d+)', replacement: 'https://phabricator.wikimedia.org/T$1'},
],
}
const isTextNodeMatchingLinkPatterns = (n: Node, ps: SmartLinksPattern[]): boolean => {
if (n.nodeType !== n.TEXT_NODE) {
return false;
}
for (const pattern of ps) {
if (n.textContent && pattern.match(n.textContent)) {
return true;
}
}
return false;
}
export default class SmartLinks extends Plugin {
settings: SmartLinksSettings;
patterns: SmartLinksPattern[] = [];
async onload() {
await this.loadSettings();
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SmartLinksSettingTab(this.app, this));
this.registerMarkdownPostProcessor((element, context) => {
element.querySelectorAll("p, li").forEach((el) => {
if (this.anyReplacableNodes(el)) {
context.addChild(new SmartLinkContainer(el as HTMLElement, this));
}
})
})
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
this.rebuildPatterns();
}
async saveSettings() {
await this.saveData(this.settings);
this.rebuildPatterns();
}
rebuildPatterns() {
this.patterns = [];
this.settings.patterns.forEach((pattern) => {
try {
this.patterns.push(new SmartLinksPattern(pattern.regexp, pattern.replacement));
} catch (e) {
// it's a user-input regex; I just want to avoid it outright dying here
}
})
}
anyReplacableNodes = (el: Element): boolean => {
for (let i = 0; i < el.childNodes.length; i++) {
const child = el.childNodes[i];
if (isTextNodeMatchingLinkPatterns(child, this.patterns)) {
return true;
}
}
return false;
}
}
class SmartLinksSettingTab extends PluginSettingTab {
plugin: SmartLinks;
constructor(app: App, plugin: SmartLinks) {
super(app, plugin);
this.plugin = plugin;
}
refresh(): void {
this.display();
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for Smart Links'});
// This might be abusing the settings system a bit?
this.plugin.settings.patterns.forEach((pattern, index) => {
const data = { ...pattern }; // because we don't want to insta-save
const setting = this.makePatternRow(containerEl, `#${index}`, data)
.addButton((button) => {
button.setButtonText("Save").onClick(async (evt) => {
this.plugin.settings.patterns[index] = data;
await this.plugin.saveSettings();
this.refresh();
})
}).addButton((button) => {
button.setButtonText("Remove").setClass("settings-delete-btn")
.onClick(async (evt) => {
this.plugin.settings.patterns.splice(index, 1);
await this.plugin.saveSettings();
this.refresh();
});
});
});
const data = {regexp: '', replacement: ''};
const setting = this.makePatternRow(containerEl, "New", data).addButton((button) => {
button.setButtonText("Add").onClick(async (evt) => {
if (!(data.regexp && data.replacement) || setting.controlEl.querySelector('.smart-links-setting-error')) {
return;
}
this.plugin.settings.patterns.push(data);
await this.plugin.saveSettings();
this.refresh();
});
});
}
makePatternRow(containerEl: HTMLElement, label: string, data: {regexp: string, replacement: string}): Setting {
const rowClass = 'smart-links-setting-section';
const setting = new Setting(containerEl).setClass(rowClass);
setting.setName(label);
setting.addText((text) => {
text.setValue(data.regexp)
.setPlaceholder("Regular expression").onChange((value) => {
try {
new RegExp(`\\b${value}`);
text.inputEl.removeClass('smart-links-setting-error');
} catch (error) {
text.inputEl.addClass('smart-links-setting-error');
}
data.regexp = value;
});
});
setting.addText((text) => {
text.setValue(data.replacement)
.setPlaceholder("Replacement").onChange((value) => {
try {
const regexp = new RegExp(`\\b${data.regexp}`);
"Arbitrary text".replace(regexp, value);
text.inputEl.removeClass('smart-links-setting-error');
} catch (error) {
text.inputEl.addClass('smart-links-setting-error');
}
data.replacement = value;
});
});
return setting;
}
}
class SmartLinkContainer extends MarkdownRenderChild {
plugin: SmartLinks;
constructor(containerEl: HTMLElement, plugin: SmartLinks) {
super(containerEl);
this.plugin = plugin;
}
onload(): void {
for (let pattern of this.plugin.patterns) {
this.containerEl.setChildrenInPlace(
this.buildNodeReplacements(this.containerEl, pattern)
);
}
}
buildNodeReplacements(containerEl: HTMLElement, pattern: SmartLinksPattern): Node[] {
const results: Node[] = [];
containerEl.childNodes.forEach((node) => {
if (!isTextNodeMatchingLinkPatterns(node, this.plugin.patterns)) {
// pass through nodes not matching the pattern
results.push(node);
return;
}
let remaining = node.textContent || "";
while (remaining) {
const nextLink = parseNextLink(remaining, pattern);
if (!nextLink.found) {
results.push(document.createTextNode(nextLink.remaining));
break;
}
results.push(document.createTextNode(nextLink.preText));
results.push(this.createLinkTag(containerEl, nextLink.link, nextLink.href));
remaining = nextLink.remaining;
}
});
return results;
}
createLinkTag(el: Element, link: string, href: string): Element {
return el.createEl("a", {
cls: "external-link",
href,
text: link,
attr: {
"aria-label": href,
"aria-label-position": "top",
rel: "noopener",
target: "_blank",
}
})
}
}