Skip to content

Commit 45e265d

Browse files
committed
Fix things according to feedback suggestions
1 parent 47ffbaf commit 45e265d

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

main.ts

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ import {
88
Editor,
99
MarkdownView,
1010
TextComponent,
11+
RequestParam,
12+
request,
1113
} from "obsidian";
1214

13-
interface WikiExtract {
15+
interface WikipediaExtract {
1416
title: string;
1517
text: string;
1618
url: string;
1719
}
1820

19-
interface MyPluginSettings {
21+
interface WikipediaPluginSettings {
2022
template: string;
2123
shouldUseParagraphTemplate: boolean;
2224
shouldBoldSearchTerm: boolean;
2325
paragraphTemplate: string;
2426
language: string;
2527
}
2628

27-
const DEFAULT_SETTINGS: MyPluginSettings = {
29+
const DEFAULT_SETTINGS: WikipediaPluginSettings = {
2830
template: `{{text}}\n> [Wikipedia]({{url}})`,
2931
shouldUseParagraphTemplate: true,
3032
shouldBoldSearchTerm: true,
@@ -36,8 +38,8 @@ const extractApiUrl =
3638
"wikipedia.org/w/api.php?format=json&action=query&prop=extracts&explaintext=1&redirects&origin=*&titles=";
3739

3840
const disambiguationIdentifier = "may refer to:";
39-
export default class MyPlugin extends Plugin {
40-
settings: MyPluginSettings;
41+
export default class WikipediaPlugin extends Plugin {
42+
settings: WikipediaPluginSettings;
4143

4244
getLanguage(): string {
4345
return this.settings.language ? this.settings.language : "en";
@@ -53,7 +55,7 @@ export default class MyPlugin extends Plugin {
5355
return `https://${this.getLanguage()}.` + extractApiUrl;
5456
}
5557

56-
formatExtractText(extract: WikiExtract, searchTerm: string): string {
58+
formatExtractText(extract: WikipediaExtract, searchTerm: string): string {
5759
const text = extract.text;
5860
let formattedText: string = "";
5961
if (this.settings.shouldUseParagraphTemplate) {
@@ -85,22 +87,22 @@ export default class MyPlugin extends Plugin {
8587
new Notice(`Could not automatically resolve disambiguation.`);
8688
}
8789

88-
hasDisambiguation(extract: WikiExtract) {
90+
hasDisambiguation(extract: WikipediaExtract) {
8991
if (extract.text.includes(disambiguationIdentifier)) {
9092
return true;
9193
}
9294
return false;
9395
}
9496

95-
parseResponse(json: any): WikiExtract | undefined {
97+
parseResponse(json: any): WikipediaExtract | undefined {
9698
const pages = json.query.pages;
9799
const pageKeys = Object.keys(pages);
98100
if (pageKeys.includes("-1")) {
99101
return undefined;
100102
}
101-
const extracts: WikiExtract[] = pageKeys.map((key) => {
103+
const extracts: WikipediaExtract[] = pageKeys.map((key) => {
102104
const page = pages[key];
103-
const extract: WikiExtract = {
105+
const extract: WikipediaExtract = {
104106
title: page.title,
105107
text: page.extract,
106108
url: this.getUrl(page.title),
@@ -110,7 +112,7 @@ export default class MyPlugin extends Plugin {
110112
return extracts[0];
111113
}
112114

113-
formatExtractInsert(extract: WikiExtract, searchTerm: string): string {
115+
formatExtractInsert(extract: WikipediaExtract, searchTerm: string): string {
114116
const formattedText = this.formatExtractText(extract, searchTerm);
115117
const template = this.settings.template;
116118
const formattedTemplate = template
@@ -120,22 +122,25 @@ export default class MyPlugin extends Plugin {
120122
return formattedTemplate;
121123
}
122124

123-
async getWikipediaText(title: string): Promise<WikiExtract | undefined> {
125+
async getWikipediaText(title: string): Promise<WikipediaExtract | undefined> {
124126
const url = this.getApiUrl() + encodeURIComponent(title);
125-
const json = await fetch(url)
126-
.then((response) => response.json())
127+
const requestParam: RequestParam = {
128+
url: url,
129+
};
130+
const resp = await request(requestParam)
131+
.then((r) => JSON.parse(r))
127132
.catch(
128133
() =>
129134
new Notice(
130135
"Failed to get Wikipedia. Check your internet connection or language prefix."
131136
)
132137
);
133-
const extract = this.parseResponse(json);
138+
const extract = this.parseResponse(resp);
134139
return extract;
135140
}
136141

137-
async pasteIntoEditor(searchTerm: string) {
138-
let extract: WikiExtract = await this.getWikipediaText(searchTerm);
142+
async pasteIntoEditor(editor: Editor, searchTerm: string) {
143+
let extract: WikipediaExtract = await this.getWikipediaText(searchTerm);
139144
if (!extract) {
140145
this.handleNotFound(searchTerm);
141146
return;
@@ -144,32 +149,31 @@ export default class MyPlugin extends Plugin {
144149
new Notice(
145150
`Disambiguation found for ${searchTerm}. Choosing first result.`
146151
);
147-
console.log(extract);
148152
const newSearchTerm = extract.text
149153
.split(disambiguationIdentifier)[1]
150154
.trim()
151155
.split(",")[0]
152156
.split("==")
153157
.pop()
154158
.trim();
155-
console.log(newSearchTerm);
156159
extract = await this.getWikipediaText(newSearchTerm);
157160
if (!extract) {
158161
this.handleCouldntResolveDisambiguation();
159162
return;
160163
}
161164
}
162-
const editor = this.getEditor();
163165
editor.replaceSelection(this.formatExtractInsert(extract, searchTerm));
164166
}
165167

166-
async getWikipediaTextForActiveFile() {
168+
async getWikipediaTextForActiveFile(editor: Editor) {
167169
const searchTerm = await this.app.workspace.getActiveFile().basename;
168-
await this.pasteIntoEditor(searchTerm);
170+
if (searchTerm) {
171+
await this.pasteIntoEditor(editor, searchTerm);
172+
}
169173
}
170174

171-
async getWikipediaTextForSearchTerm() {
172-
new SearchModal(this.app, this).open();
175+
async getWikipediaTextForSearchTerm(editor: Editor) {
176+
new WikipediaSearchModal(this.app, this, editor).open();
173177
}
174178

175179
async onload() {
@@ -178,16 +182,18 @@ export default class MyPlugin extends Plugin {
178182
this.addCommand({
179183
id: "wikipedia-get-active-note-title",
180184
name: "Get Wikipedia for Active Note Title",
181-
callback: () => this.getWikipediaTextForActiveFile(),
185+
editorCallback: (editor: Editor) =>
186+
this.getWikipediaTextForActiveFile(editor),
182187
});
183188

184189
this.addCommand({
185190
id: "wikipedia-get-search-term",
186191
name: "Get Wikipedia for Search Term",
187-
callback: () => this.getWikipediaTextForSearchTerm(),
192+
editorCallback: (editor: Editor) =>
193+
this.getWikipediaTextForSearchTerm(editor),
188194
});
189195

190-
this.addSettingTab(new SampleSettingTab(this.app, this));
196+
this.addSettingTab(new WikipediaSettingTab(this.app, this));
191197
}
192198

193199
async loadSettings() {
@@ -197,21 +203,17 @@ export default class MyPlugin extends Plugin {
197203
async saveSettings() {
198204
await this.saveData(this.settings);
199205
}
200-
201-
private getEditor(): Editor {
202-
let activeLeaf = this.app.workspace.getActiveViewOfType(MarkdownView);
203-
if (activeLeaf == null) return;
204-
return activeLeaf.editor;
205-
}
206206
}
207207

208-
class SearchModal extends Modal {
208+
class WikipediaSearchModal extends Modal {
209209
searchTerm: string;
210-
plugin: MyPlugin;
210+
plugin: WikipediaPlugin;
211+
editor: Editor;
211212

212-
constructor(app: App, plugin: MyPlugin) {
213+
constructor(app: App, plugin: WikipediaPlugin, editor: Editor) {
213214
super(app);
214215
this.plugin = plugin;
216+
this.editor = editor;
215217
}
216218

217219
onOpen() {
@@ -248,15 +250,15 @@ class SearchModal extends Modal {
248250

249251
contentEl.empty();
250252
if (this.searchTerm) {
251-
await this.plugin.pasteIntoEditor(this.searchTerm);
253+
await this.plugin.pasteIntoEditor(this.editor, this.searchTerm);
252254
}
253255
}
254256
}
255257

256-
class SampleSettingTab extends PluginSettingTab {
257-
plugin: MyPlugin;
258+
class WikipediaSettingTab extends PluginSettingTab {
259+
plugin: WikipediaPlugin;
258260

259-
constructor(app: App, plugin: MyPlugin) {
261+
constructor(app: App, plugin: WikipediaPlugin) {
260262
super(app, plugin);
261263
this.plugin = plugin;
262264
}

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "obsidian-wikipedia",
33
"name": "Wikipedia",
4-
"version": "1.0.0",
4+
"version": "1.0.1",
55
"minAppVersion": "0.9.12",
66
"description": "Grabs information from Wikipedia for a topic and brings it into Obsidian notes",
77
"author": "Jonathan Miller",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-wikipedia",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Grabs information from Wikipedia for a topic and brings it into Obsidian notes",
55
"main": "main.js",
66
"scripts": {

0 commit comments

Comments
 (0)