Wikipedia Plugin

linkPlugin

name

Wikipedia

description

Lookup up things on wikipedia, or fetch random articles.

instructions

Highlight text and use plugin menu to lookup the text on wikipedia.
Note option menu has option to get a random article title, summary and image. It is formatted with markdown.
Insert text menu can get a random article, but this is not formatted with markdown.

icon

local_library

{
constants: {
notFoundMessage: 'No matching article for the search. Try simplifying or rephrasing the provided search query.'
},
 
insertText: {
"Random Article": async function(app) {
const result = await this._callRandomWiki(app)
const output = this._formatResult(app, result)
return output
}
},
 
replaceText: {
"Lookup": async function(app, text) {
const result = await this._callWiki(app, text)
if(result.hasOwnProperty('title')) {
try{
const output = this._formatResult(app, result, false)
app.alert(output)
}
catch(e) {
console.log(e)
}
} else {
app.alert(this.contants.notFoundMessage)
}
return null
}
},
 
noteOption: {
"Random Article": async function (app, noteUUID) {
const note = await app.notes.find(noteUUID);
const result = await this._callRandomWiki(app);
 
note.insertContent(
this._formatResult(app, result, true)
);
}
},
 
_formatResult(app, result, markdown = false) {
let url
let title = result.title
let related = ""
let output
let image
 
if (result.extract.includes('may refer to')) {
related = "related pages\n"
for (const i in result.related.pages) {
related += `[${JSON.stringify(result.related.pages[i].title)}]` +
`(${JSON.stringify(result.related.pages[i].content_urls.desktop.page)})\n`
}
}
 
try {
image = `![](${result.originalimage.source})`
} catch(e) {
image = ''
}
 
try {
url = result.content_urls.desktop.page
} catch (e) {
url = ''
}
 
if (markdown === true) {
title = `# [${title}](${url})\n`
output = ("" +
title + "\n" +
result.extract + "\n\n" +
"\---" + "\n" +
image + "\n")
} else {
output = ("" +
title + "\n\n" +
result.extract + "\n\n" +
related + "\n" +
"original article: " + url)
}
 
return output
},
 
async _callWiki(app, text) {
try {
const article = await fetch("https://en.wikipedia.org/api/rest_v1/page/summary/" + text, {
method: "GET",
headers: {
"Content-Type": "application/problem+json"
}
});
 
const related = await fetch("https://en.wikipedia.org/api/rest_v1/page/related/" + text, {
method: "GET",
headers: {
"Content-Type": "application/problem+json"
}
});
 
if (article) {
const result = await article.json();
result.related = await related.json()
if (result.title === "Not found.") {throw new Error(this.constants.notFoundMessage)}
return result;
} else {
app.alert("Error");
return null;
}
} catch (error) {
app.alert("Failed to call Wikipedia: " + error);
return null;
}
},
 
async _callRandomWiki(app) {
try {
const article = await fetch("https://en.wikipedia.org/api/rest_v1/page/random/summary", {
method: "GET",
headers: {
"Content-Type": "application/problem+json"
}
});
 
if (article) {
const result = await article.json();
 
return result;
} else {
app.alert("Error");
return null;
}
} catch (error) {
app.alert("Failed to call Wikipedia: " + error);
return null;
}
}
}

linkExamples



linkCreate Note From Random Article Example

*** Example start ***

link1997 Sunbelt IndyCarnival

The 1997 Sunbelt IndyCarnival was the second round of the 1997 CART World Series Season, held on 6 April 1997 on the Surfers Paradise Street Circuit, Surfers Paradise, Queensland, Australia. Scott Pruett won his 2nd and final victory of his career as a driver in CART.





*** Example End **



linkInsert Random Article Example

*** Example Start ***

Klausenburg (Hasidic dynasty)



Klausenburg, also known as Sanz-Klausenburg, is a Hasidic dynasty that originated in the Transylvanian city of Cluj-Napoca, today in Romania.





original article: https://en.wikipedia.org/wiki/Klausenburg_(Hasidic_dynasty)


*** Example End ***





linkLookup Example

link

link
Release Notes

linkVersion 1.3

Added better error handling when article not found

Added link to article

Added related pages when result contains "may refer to"