generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
443 lines (384 loc) · 12.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import {
App,
Editor,
MarkdownView,
Plugin,
PluginSettingTab, requestUrl,
Setting,
SuggestModal, TAbstractFile, TFile,
} from 'obsidian';
import * as _ from 'lodash';
import {FileSuggestionComponent} from "obsidian-file-suggestion-component";
// create ids interface
interface Ids {
mag: string;
openalex: string
wikidata: string
wikipedia: string
umls_cui: object
}
interface Entity {
display_name: string;
hint: string
description: string;
ids: Ids
"wikidata entity id": string
}
interface EntityLinkerSettings {
mySetting: string;
entityFolder: string
politeEmail: string
overwriteFlag: boolean
}
const DEFAULT_SETTINGS: EntityLinkerSettings = {
mySetting: 'default',
entityFolder: '',
politeEmail: '',
overwriteFlag: false
}
export class EntitySuggestionModal extends SuggestModal<Entity> {
search_term: string
polite_email: string
onSubmit: (result: object) => void;
private debouncedGetSuggestions: any;
constructor(app: App, search_term: string, polite_email: string, onSubmit: (result: object) => void) {
super(app);
this.polite_email = polite_email
this.search_term = search_term
this.onSubmit = onSubmit;
this.debouncedGetSuggestions = this.debounce(this.getSuggestionsImpl, 500);
}
onOpen() {
super.onOpen();
// workaround to populate input in case text selected in editor
if (this.search_term) {
this.inputEl.value = this.search_term;
this.inputEl.dispatchEvent(new InputEvent("input"));
}
}
debounce(func: { (query: string): Promise<any>; apply?: any; }, wait: number | undefined) {
let timeout: string | number | NodeJS.Timeout | undefined;
return function (...args: any) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this;
clearTimeout(timeout);
return new Promise((resolve) => {
timeout = setTimeout(() => resolve(func.apply(context, args)), wait);
});
};
}
isValidEmail(email: string) {
return /\S+@\S+\.\S+/.test(email);
}
async getSuggestions(query: string) {
if (!query) {
return []
}
let results = await this.debouncedGetSuggestions(query)
// console.log(results)
results = results.map((result: any) => {
return {
display_name: result.display_name,
hint: result.hint,
ids: {"openalex": result.id.split("/").last()}
}
})
if (results.length == 0) {
const wiki_result = await this.getWikiDataFromSearchTerm(query)
return [wiki_result]
}
return results
}
async getSuggestionsImpl(query: string) {
let url = "https://api.openalex.org/autocomplete/concepts?q=" + query
if (this.polite_email && this.isValidEmail(this.polite_email)) {
url += "&mailto=" + this.polite_email
}
const response = await requestUrl({
url: url,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
const res = response.json
return res.results
}
// Renders each suggestion item.
renderSuggestion(entity: Entity, el: HTMLElement) {
el.createEl("div", {text: entity.display_name});
el.createEl("small", {text: entity.hint ? entity.hint : ""});
}
async getWikiDataFromSearchTerm(search_term: string) {
const wiki_search_url = "https://en.wikipedia.org/wiki/Special:Search?go=Go&search=" + encodeURIComponent(search_term);
const wikidata_search_url = "https://www.wikidata.org/w/index.php?search=" + encodeURIComponent(search_term)
const response = await requestUrl({
"url": wiki_search_url,
"method": "GET",
"headers": {
"Content-Type": "text/html"
}
})
const html_content = response.text
const el = document.createElement('html');
el.innerHTML = html_content;
const scriptTag = el.querySelector('script[type="application/ld+json"]');
if (scriptTag) {
// Get the text content of the script tag
const jsonContent = scriptTag.textContent;
try {
// Parse the JSON content
const json_data = jsonContent ? JSON.parse(jsonContent) : null;
// console.log('Extracted JSON:', json_data);
const entity = {
display_name: json_data.hasOwnProperty("name") ? json_data.name : search_term,
wikipedia: json_data.hasOwnProperty("url") ? json_data.url : wiki_search_url,
wikidata: json_data.hasOwnProperty("mainEntity") ? json_data.mainEntity : wikidata_search_url,
"wikidata entity id": json_data.hasOwnProperty("mainEntity") ? json_data.mainEntity.split("/").last() : "",
description: json_data.hasOwnProperty("headline") ? json_data.headline : "",
hint: json_data.hasOwnProperty("headline") ? json_data.headline : ""
}
return entity;
} catch (error) {
console.error('Error parsing JSON:', error);
}
} else {
// console.error(search_term + ': No script tag with type "application/ld+json" found.');
}
return {
display_name: search_term,
wikipedia: wiki_search_url,
wikidata: wikidata_search_url,
description: "",
"wikidata entity id": "",
hint: "Create empty note"
};
}
async getRedirectedUrl(url: string) {
const response = await requestUrl({
"url": url,
"method": "GET",
"headers": {
"Content-Type": "text/html"
}
})
const html_content = response.text
const el = document.createElement('html');
el.innerHTML = html_content;
const canonical_link = el.querySelector('link[rel="canonical"]');
// Get the href attribute
const href_value = canonical_link ? canonical_link.getAttribute('href') : null;
return href_value
}
async generatePropertiesFromEntity(entity: Entity) {
if (!entity.hasOwnProperty("ids")) {
return entity
}
let concept_url = "https://api.openalex.org/concepts/" + entity.ids.openalex
if (this.polite_email && this.isValidEmail(this.polite_email)) {
concept_url += "?mailto=" + this.polite_email
}
const response = await requestUrl({
url: concept_url,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
const entity_result = response.json
const entity_props: { [key: string]: any } = {};
const properties_of_interest = ["wikidata entity id", "display_name", "description", "ids"]
for (const [key, value] of Object.entries(entity_result)) {
if (!properties_of_interest.includes(key)) {
continue
}
if (typeof value == "string" || Array.isArray(value)) {
entity_props[key] = value
// property_string += key + ": " + value + "\n"
} else if (value && typeof (value) == "object") {
for (const [key2, value2] of Object.entries(value)) {
entity_props[key2] = value2 //property_string += key2 + ": " + value2 + "\n"
}
}
}
entity_props["wikidata entity id"] = entity_props["wikidata"] ? entity_props["wikidata"].split("/").last() : ""
// console.log(entity_props)
return entity_props
}
async onChooseSuggestion(entity: Entity, evt: MouseEvent | KeyboardEvent) {
// fetch concept properties
const entity_props = await this.generatePropertiesFromEntity(entity)
this.onSubmit(entity_props)
}
}
interface EntityProps {
// ["display_name", "description", "openalex", "wikidata", "mag", "wikipedia", "umls_cui",
// "wikidata entity id"]
display_name: string
description: string
"openalex": string
wikidata: string
wikipedia: string
umls_cui: string
"mag": string
"wikidata entity id": string
"hint"?: string
}
export default class EntityLinker extends Plugin {
settings: EntityLinkerSettings;
async updateFrontMatter(file: TAbstractFile, entity_props: object, callback: () => void) {
const overwrite_flag = this.settings.overwriteFlag
if (file instanceof TFile) {
await this.app.fileManager.processFrontMatter(file, (frontmatter) => {
// set property if it doesn't exist or if overwrite flag is set
// console.log(frontmatter)
for (const [key, value] of Object.entries(entity_props)) {
if (!frontmatter.hasOwnProperty(key) || overwrite_flag) {
frontmatter[key] = value
}
}
callback()
})
}
}
async entitySearchCallback(search_term: string, is_active_note = false) {
const polite_email = this.settings.politeEmail
const emodal = new EntitySuggestionModal(this.app, search_term, polite_email, async (result: EntityProps) => {
// filter acceptable properties
result = _.pick(result, ["display_name", "description", "openalex", "wikidata", "mag", "wikipedia", "umls_cui",
"wikidata entity id"])
let path = ""
if (is_active_note) {
path = this.settings.entityFolder + "/" + search_term + ".md"
} else {
path = this.settings.entityFolder + "/" + result.display_name + ".md"
}
// eslint-disable-next-line
let entity_file = this.app.vault.getFileByPath(path)
if (!entity_file) {
// @ts-ignore
const new_file = await this.app.vault.create(this.settings.entityFolder + "/" + result.display_name + ".md", "")
if (!new_file) {
console.error("failed to create file")
return
}
this.updateFrontMatter(new_file, result, () => {
if (!is_active_note) {
this.app.workspace.getLeaf('tab').openFile(new_file)
}
})
} else {
this.updateFrontMatter(entity_file, result, () => {
if (!is_active_note) {
// @ts-ignore
this.app.workspace.getLeaf('tab').openFile(entity_file)
}
})
}
})
emodal.open()
}
async onload() {
await this.loadSettings();
this.addCommand({
id: 'link-selection',
name: 'Link selection to entity',
editorCallback: async (editor: Editor, view: MarkdownView) => {
const search_term = editor.getSelection()?.toString();
await this.entitySearchCallback(search_term, false)
}
});
this.addCommand({
id: 'link-active-note',
name: 'Link active note to entity',
editorCallback: (editor: Editor, view: MarkdownView) => {
const search_term = this.app.workspace.getActiveFile()?.basename.toString();
// console.log(search_term)
if (!search_term) {
return
}
this.entitySearchCallback(search_term, true)
}
});
// bind click event to active note
this.registerEvent(
this.app.workspace.on("editor-menu", (menu, editor, view) => {
menu.addItem((item) => {
item
.setTitle("Link selection to entity")
.setIcon("document")
.onClick(async () => {
const search_term = editor.getSelection();
this.entitySearchCallback(search_term)
});
})
menu.addItem((item) => {
item
.setTitle("Link active note to entity")
.setIcon("document")
.onClick(async () => {
const search_term = this.app.workspace.getActiveFile()?.basename.toString();
// console.log(search_term)
if (!search_term) {
return
}
this.entitySearchCallback(search_term, false)
});
})
}))
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new EntityLinkerSettingsTab(this.app, this));
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class EntityLinkerSettingsTab extends PluginSettingTab {
plugin: EntityLinker;
constructor(app: App, plugin: EntityLinker) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
new Setting(containerEl)
.setName("Polite email")
.setDesc("Adding email to openalex API requests(for faster and more consistent response times)")
.addText((text) =>
text
.setPlaceholder("Enter email here")
.setValue(this.plugin.settings.politeEmail)
.onChange(async (value) => {
this.plugin.settings.politeEmail = value;
await this.plugin.saveSettings();
}));
const saveLoc = new Setting(containerEl)
.setName('Entity folder')
.setDesc('Folder to store entities');
new FileSuggestionComponent(saveLoc.controlEl, this.app)
.setValue(this.plugin.settings.entityFolder)
.setPlaceholder(DEFAULT_SETTINGS.entityFolder)
.setFilter("folder")
.setLimit(10)
.onSelect(async (val: TAbstractFile) => {
this.plugin.settings.entityFolder = val.path;
await this.plugin.saveSettings();
});
new Setting(containerEl)
.setName("Overwrite existing properties")
.setDesc("If checked, existing properties will be overwritten")
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.overwriteFlag)
.onChange(async (value) => {
this.plugin.settings.overwriteFlag = value;
await this.plugin.saveSettings();
}));
}
}