Wikipedia Plugin

linkTable of Contents


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')) {
result.extract = "Could not find the requested page, showed instead the first related page:\n\n" + result.extract
related = ""
for (const i in result.related) {
related += `https://en.wikipedia.org/wiki/${result.related[i]}\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" +
related + "\n\n" +
"original article: " + url)
}
 
return output
},
 
async _callWiki(app, text) {
const url = 'https://en.wikipedia.org/w/api.php';
 
let params = new URLSearchParams({
origin: '*',
format: 'json',
action: 'query',
titles: text,
prop: 'links',
pllimit: 5
});
let response = await fetch(`${url}?${params.toString()}`);
let data = await response.json();
 
try {
const article = await fetch("https://en.wikipedia.org/api/rest_v1/page/summary/" + text, {
method: "GET",
headers: {
"Content-Type": "application/problem+json"
}
});
 
if (data.query.pages && Object.values(data.query.pages)[0].hasOwnProperty('links')) {
let result = await article.json();
const title = [Object.values(data.query.pages)[0].title]
 
if (result.hasOwnProperty('extract') && result.extract.includes('may refer to')) {
const article = await fetch("https://en.wikipedia.org/api/rest_v1/page/summary/" + Object.values(data.query.pages)[0].links[0].title, {
method: "GET",
headers: {
"Content-Type": "application/json"
}
});
let oldExtract = result.extract
result = await article.json();
if (oldExtract === result.extract) { oldExtract = "" }
result.extract += ("\n\n" + oldExtract)
}
let pages = Object.values(data.query.pages)[0].links.map(e=>e.title)
result.related = pages
return result;
} else {
app.alert(this.constants.notFoundMessage);
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.1

Wikipedia deprecated related pages api, so had to move it to their open search api

linkVersion 1.3

Added better error handling when article not found

Added link to article

Added related pages when result contains "may refer to"