Skip to content

Commit

Permalink
Add build for BRAT beta testing
Browse files Browse the repository at this point in the history
  • Loading branch information
evanharmon1 committed Nov 1, 2023
1 parent a005798 commit 163b26e
Showing 1 changed file with 387 additions and 0 deletions.
387 changes: 387 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,387 @@
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin:
https://github.com/evanharmon1
*/

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// main.ts
var main_exports = {};
__export(main_exports, {
default: () => WikipediaData
});
module.exports = __toCommonJS(main_exports);
var import_obsidian = require("obsidian");
var wikimediaApiUrlBase = "wikipedia.org/api/rest_v1/";
var mediaWikiApiUrlBase = "wikipedia.org/w/rest.php/v1/";
var mediaWikiActionApiUrlBase = "wikipedia.org/w/api.php";
var defaultWikipediaTemplateOne = {
key: 1,
name: "Wikipedia template #1",
description: `Set the template to be inserted with the main command - 'Apply Template #1 for Active Note Title'.`,
value: `| {{thumbnailTemplate}} | {{summary}} |
|-|-|
| | wikipedia:: [{{title}}]({{url}}) |
> [!summary]- Wikipedia Synopsis
{{introText}}
`
};
var defaultWikipediaTemplateTwo = {
key: 2,
name: "Wikipedia template #2",
description: `Set the template to be inserted with the second command - 'Apply Template #2 for Active Note Title'.`,
value: `| {{thumbnailTemplate}} | {{summary}} |
|-|-|
| | wikipedia:: [{{title}}]({{url}}) |
> [!summary]- Wikipedia Synopsis
{{introText}}
`
};
var defaultWikipediaTemplateThree = {
key: 3,
name: "Wikipedia template #3",
description: `Set the template to be inserted with the third command - 'Apply Template #3 for Active Note Title'.`,
value: `| {{thumbnailTemplate}} | {{summary}} |
|-|-|
| | wikipedia:: [{{title}}]({{url}}) |
> [!summary]- Wikipedia Synopsis
{{introText}}
`
};
var DEFAULT_SETTINGS = {
language: "en",
shouldBoldSearchTerm: true,
wikipediaTemplates: [defaultWikipediaTemplateOne, defaultWikipediaTemplateTwo, defaultWikipediaTemplateThree],
thumbnailTemplate: `![img \\|150]({{thumbnailUrl}})`,
useParagraphTemplate: true,
paragraphTemplate: `> {{paragraphText}}
>
`
};
var WikipediaData = class extends import_obsidian.Plugin {
getLanguage() {
return this.settings.language ? this.settings.language : "en";
}
getWikimediaApiUrl() {
return `https://${this.getLanguage()}.` + wikimediaApiUrlBase + `page/summary/`;
}
getMediaWikiApiUrl() {
return `https://${this.getLanguage()}.` + mediaWikiApiUrlBase + `search/title?q=`;
}
getMediaWikiActionApiUrl() {
return `https://${this.getLanguage()}.` + mediaWikiActionApiUrlBase + `?format=json&action=query&prop=extracts&explaintext=1&redirects&origin=*&pageids=`;
}
handleNotFound(searchTerm) {
new import_obsidian.Notice(`${searchTerm} not found on Wikipedia.`);
}
handleDisambiguation(searchTerm, disambiguationUrl) {
let linkElement = document.createElement("a");
linkElement.innerHTML = `${searchTerm} Disambiguation Page
`;
linkElement.href = `${disambiguationUrl}`;
let fragment = new DocumentFragment();
fragment.appendChild(linkElement);
new import_obsidian.Notice(`${searchTerm} returned a disambiguation page.`, 1e4);
new import_obsidian.Notice(fragment, 1e4);
}
// Remove the occassional \n chars to make WikimediaData.summary always be one line.
formatWikimediaApiSummary(wikimediaApiResponse, searchTerm) {
const regex = /\n/g;
let formattedSummary = wikimediaApiResponse.summary.trim().replace(regex, " ");
if (this.settings.shouldBoldSearchTerm) {
const pattern = new RegExp(searchTerm, "i");
formattedSummary = formattedSummary.replace(pattern, `**${searchTerm}**`);
}
return formattedSummary;
}
// Split WikiText.fullText into paragraphs, extract just the intro section, and apply paragraphTemplate to each paragraph.
formatMediaWikiActionApiIntroText(mediaWikiActionApiResponse, searchTerm) {
const text = mediaWikiActionApiResponse.fullText;
let formattedText = "";
if (this.settings.useParagraphTemplate) {
const split = text.split("==")[0].trim().split("\n");
formattedText = split.map(
(paragraph) => this.settings.paragraphTemplate.replace(
"{{paragraphText}}",
paragraph
)
).join("").trim();
} else {
formattedText = text.split("==")[0].trim();
}
if (this.settings.shouldBoldSearchTerm) {
const pattern = new RegExp(searchTerm, "i");
formattedText = formattedText.replace(pattern, `**${searchTerm}**`);
}
if (formattedText.charAt(formattedText.length - 1) === ">") {
formattedText = formattedText.slice(0, formattedText.length - 2);
}
return formattedText;
}
// Build final template to be inserted into note and apply template variables.
formatTemplate(mediaWikiApiResponse, wikimediaApiResponse, mediaWikiActionApiResponse, searchTerm, wikipediaTemplateNum) {
const formattedSummary = this.formatWikimediaApiSummary(wikimediaApiResponse, searchTerm);
const formattedIntroText = this.formatMediaWikiActionApiIntroText(mediaWikiActionApiResponse, searchTerm);
const template = this.settings.wikipediaTemplates[wikipediaTemplateNum - 1].value;
let thumbnailTemplate = "";
if (wikimediaApiResponse.thumbnailUrl !== "") {
thumbnailTemplate = this.settings.thumbnailTemplate;
}
const formattedTemplate = template.replace("{{title}}", wikimediaApiResponse.title).replace("{{url}}", wikimediaApiResponse.url).replace("{{thumbnailTemplate}}", thumbnailTemplate).replace("{{thumbnailUrl}}", wikimediaApiResponse.thumbnailUrl).replace("{{description}}", wikimediaApiResponse.description).replace("{{summary}}", formattedSummary).replace("{{introText}}", formattedIntroText).replace("{{id}}", mediaWikiApiResponse.id.toString()).replace("{{key}}", mediaWikiApiResponse.key);
return formattedTemplate;
}
parseWikimediaApiResponse(json) {
const parsedWikimediaApiResponse = {
type: json.type,
title: json.title,
summary: json.extract,
description: json.description,
url: json.content_urls.desktop.page,
thumbnailUrl: json.hasOwnProperty("thumbnail") ? json.thumbnail.source : ""
};
return parsedWikimediaApiResponse;
}
parseMediaWikiApiResponse(json) {
;
const parsedMediaWikiApiResponse = {
resultCount: json.pages.length,
id: json.pages[0].id,
key: json.pages[0].key,
title: json.pages[0].title,
description: json.pages[0].description,
thumbnailUrl: json.pages[0].thumbnailUrl
};
return parsedMediaWikiApiResponse;
}
parseMediaWikiActionApiResponse(json, id) {
const parsedMediaWikiActionApiResponse = {
fullText: json.query.pages[id.toString()].extract
};
return parsedMediaWikiActionApiResponse;
}
async getWikimediaApiResponse(title) {
const url = this.getWikimediaApiUrl() + encodeURIComponent(title);
const requestParam = {
url
};
const resp = await (0, import_obsidian.request)(requestParam).then((r) => JSON.parse(r)).catch(
() => new import_obsidian.Notice(
"Failed to reach Wikimedia API for article data. Check your search term, internet connection, or language prefix."
)
);
const wikimediaApiResponse = this.parseWikimediaApiResponse(resp);
return wikimediaApiResponse;
}
async getMediaWikiApiResponse(searchTerm) {
const url = this.getMediaWikiApiUrl() + encodeURIComponent(searchTerm) + "&limit=5";
const requestParam = {
url
};
const resp = await (0, import_obsidian.request)(requestParam).then((r) => JSON.parse(r)).catch(
() => new import_obsidian.Notice(
"Failed to reach MediaWiki API for article title. Check your search term, internet connection, or language prefix."
)
);
const mediaWikiApiResponse = this.parseMediaWikiApiResponse(resp);
return mediaWikiApiResponse;
}
async getMediaWikiActionApiResponse(id) {
const url = this.getMediaWikiActionApiUrl() + encodeURIComponent(id.toString());
const requestParam = {
url
};
const resp = await (0, import_obsidian.request)(requestParam).then((r) => JSON.parse(r)).catch(
() => new import_obsidian.Notice(
"Failed to reach MediaWiki Action API for article text. Check your search term, internet connection, or language prefix."
)
);
const mediaWikiActionApiResponse = this.parseMediaWikiActionApiResponse(resp, id);
return mediaWikiActionApiResponse;
}
async pasteIntoEditor(editor, searchTerm, wikipediaTemplateNum) {
let mediaWikiApiResponse = await this.getMediaWikiApiResponse(searchTerm);
let wikimediaApiResponse = await this.getWikimediaApiResponse(mediaWikiApiResponse.title);
let mediaWikiActionApiResponse = await this.getMediaWikiActionApiResponse(mediaWikiApiResponse.id);
if (!mediaWikiApiResponse) {
this.handleNotFound(searchTerm);
return;
} else if (wikimediaApiResponse.type.contains("missingtitle") || mediaWikiApiResponse.resultCount == 0) {
this.handleNotFound(searchTerm);
return;
} else if (wikimediaApiResponse.type == "disambiguation" || mediaWikiApiResponse.description == "Topics referred to by the same term") {
this.handleDisambiguation(searchTerm, wikimediaApiResponse.url);
return;
} else {
editor.replaceSelection(this.formatTemplate(mediaWikiApiResponse, wikimediaApiResponse, mediaWikiActionApiResponse, searchTerm, wikipediaTemplateNum));
}
}
async applyTemplateForActiveNote(editor, wikipediaTemplateNum) {
const activeFile = await this.app.workspace.getActiveFile();
if (activeFile) {
const searchTerm = activeFile.basename;
if (searchTerm) {
await this.pasteIntoEditor(editor, searchTerm, wikipediaTemplateNum);
}
}
}
async onload() {
console.log("Loading Wikipedia Data Plugin");
await this.loadSettings();
this.addCommand({
id: "apply-template-one-for-active-note",
name: "Apply Template #1 for Active Note Title",
editorCallback: (editor) => this.applyTemplateForActiveNote(editor, 1)
});
this.addCommand({
id: "apply-template-two-for-active-note",
name: "Apply Template #2 for Active Note Title",
editorCallback: (editor) => this.applyTemplateForActiveNote(editor, 2)
});
this.addCommand({
id: "apply-template-three-for-active-note",
name: "Apply Template #3 for Active Note Title",
editorCallback: (editor) => this.applyTemplateForActiveNote(editor, 3)
});
this.addSettingTab(new WikipediaDataSettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
onunload() {
}
};
var WikipediaDataSettingTab = class extends import_obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
new import_obsidian.Setting(containerEl).setName("Wikipedia language prefix").setDesc(`Choose Wikipedia language prefix to use for API (e.g, 'en' for English)`).addText((textField) => {
textField.setValue(this.plugin.settings.language).onChange(async (value) => {
this.plugin.settings.language = value;
await this.plugin.saveSettings();
});
});
new import_obsidian.Setting(containerEl).setName("Bold search term?").setDesc(
"If set to true, the first instance of the search term will be **bolded**"
).addToggle(
(toggle) => toggle.setValue(this.plugin.settings.shouldBoldSearchTerm).onChange(async (value) => {
this.plugin.settings.shouldBoldSearchTerm = value;
await this.plugin.saveSettings();
})
);
this.containerEl.createEl("h1", { text: "Templates" });
const desc = document.createDocumentFragment();
desc.append(
"Define the templates for what data from Wikipedia will be inserted into your active Obsidian note.",
desc.createEl("br"),
desc.createEl("br"),
"If you just need one template corresponding to one command, you don't need to worry about the extra numbered templates below (Wikipedia template #2...). The extra numbered templates apply only if you want multiple unique templates in order to apply them in different situations. Each numbered template corresponds to the extra numbered plugin commands. So, e.g., 'Wikipedia template #2' gets applied via the 'Apply Template #2 for Active Note Title' command, etc.",
desc.createEl("br"),
desc.createEl("br"),
desc.createEl("strong", { text: "Available template variables are:" }),
desc.createEl("br"),
desc.createEl("strong", { text: "{{title}}" }),
" - Wikipedia page title. E.g, 'Ludwig Wittgenstein'.",
desc.createEl("br"),
desc.createEl("strong", { text: "{{url}}" }),
" - URL of the Wikipedia page",
desc.createEl("br"),
desc.createEl("strong", { text: "{{description}}" }),
" - Short, simple description of the article. Usually just a short fragment. E.g, 'Austrian philosopher and logician (1889\u20131951)'.",
desc.createEl("br"),
desc.createEl("strong", { text: "{{summary}}" }),
" - Medium length explanation of the article in 1 or a few sentences. E.g, 'Ludwig Josef Johann Wittgenstein was an Austrian philosopher who worked primarily in logic, the philosophy of mathematics, the philosophy of mind, and the philosophy of language.'.",
desc.createEl("br"),
desc.createEl("strong", { text: "{{introText}}" }),
" - Longer explanation, sometimes multiple paragraphs - the first intro section of a Wikipedia article.",
desc.createEl("br"),
desc.createEl("strong", { text: "{{id}}" }),
" - page id of the Wikipedia page. E.g, '17741'.",
desc.createEl("br"),
desc.createEl("strong", { text: "{{key}}" }),
" - Key of the Wikipedia page. E.g, 'Ludwig_Wittgenstein'.",
desc.createEl("br"),
desc.createEl("strong", { text: "{{thumbnailTemplate}}" }),
" - Inserts the Wikipedia 'Thumbnail template' defined below.",
desc.createEl("br"),
desc.createEl("strong", { text: "{{thumbnailUrl}}" }),
" - Inserts the url of the article's thumbnail image (if it has one). (Normally one would only use this variable inside the 'Thumbnail template' below, although you can use it directly in the 'Wikipedia templates' as well."
);
new import_obsidian.Setting(this.containerEl).setDesc(desc);
new import_obsidian.Setting(containerEl).setName(this.plugin.settings.wikipediaTemplates[0].name).setDesc(this.plugin.settings.wikipediaTemplates[0].description).addTextArea((textarea) => {
textarea.setValue(this.plugin.settings.wikipediaTemplates[0].value).onChange(async (value) => {
this.plugin.settings.wikipediaTemplates[0].value = value;
await this.plugin.saveSettings();
});
textarea.inputEl.rows = 10;
textarea.inputEl.cols = 40;
});
new import_obsidian.Setting(containerEl).setName(this.plugin.settings.wikipediaTemplates[1].name).setDesc(this.plugin.settings.wikipediaTemplates[1].description).addTextArea((textarea) => {
textarea.setValue(this.plugin.settings.wikipediaTemplates[1].value).onChange(async (value) => {
this.plugin.settings.wikipediaTemplates[1].value = value;
await this.plugin.saveSettings();
});
textarea.inputEl.rows = 10;
textarea.inputEl.cols = 40;
});
new import_obsidian.Setting(containerEl).setName(this.plugin.settings.wikipediaTemplates[2].name).setDesc(this.plugin.settings.wikipediaTemplates[2].description).addTextArea((textarea) => {
textarea.setValue(this.plugin.settings.wikipediaTemplates[2].value).onChange(async (value) => {
this.plugin.settings.wikipediaTemplates[2].value = value;
await this.plugin.saveSettings();
});
textarea.inputEl.rows = 10;
textarea.inputEl.cols = 40;
});
new import_obsidian.Setting(containerEl).setName("Thumbnail template").setDesc(
`Set the thumbnail template for what will be inserted with the 'thumbnailTemplate' variable within the above Wikipedia templates. If Wikipedia does not return a thumbnail image, this template will not be inserted. Use the '{{thumbnailUrl}}' variable here.`
).addTextArea((textarea) => {
textarea.setValue(this.plugin.settings.thumbnailTemplate).onChange(async (value) => {
this.plugin.settings.thumbnailTemplate = value;
await this.plugin.saveSettings();
});
textarea.inputEl.rows = 5;
textarea.inputEl.cols = 40;
});
new import_obsidian.Setting(containerEl).setName("Use paragraph template?").setDesc(
"If set to true, you can customize how each paragraph from the 'introText' template variable is formatted in the 'Paragraph template' below."
).addToggle(
(toggle) => toggle.setValue(this.plugin.settings.useParagraphTemplate).onChange(async (value) => {
this.plugin.settings.useParagraphTemplate = value;
await this.plugin.saveSettings();
})
);
new import_obsidian.Setting(containerEl).setName("Paragraph template").setDesc(
`Set the paragraph template for how each paragraph in the 'introText' template variable will be displayed when inserted in the Wikipedia templates above. Use the '{{paragraphText}} variable here.`
).addTextArea((textarea) => {
textarea.setValue(this.plugin.settings.paragraphTemplate).onChange(async (value) => {
this.plugin.settings.paragraphTemplate = value;
await this.plugin.saveSettings();
});
textarea.inputEl.rows = 5;
textarea.inputEl.cols = 40;
});
}
};

0 comments on commit 163b26e

Please sign in to comment.