Skip to content

Instantly share code, notes, and snippets.

@evantravers
Last active April 5, 2020 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evantravers/f668eb4331baba943b59949ad2feef71 to your computer and use it in GitHub Desktop.
Save evantravers/f668eb4331baba943b59949ad2feef71 to your computer and use it in GitHub Desktop.
let t = `title: {{title}}{{#subtitle}}
subtitle: {{subtitle}}{{/subtitle}}
author: {{authors}}
publisher: {{publisher}}
year: {{publishedDate}}
identifier: {{identifier}}
id: {{id}}
`
let template = MustacheTemplate.createWithTemplate(t);
var findBooks = function(reference) {
let items = searchGoogleBooks(reference);
if (items) {
if (items.length > 3) {
return items.slice(0, 3);
}
else {
return items
}
}
}
var googleBooks = Prompt.create();
googleBooks.addLabel("title", "Search Google Books...");
googleBooks.addTextField("q", "🔎", "")
googleBooks.addButton("Confirm", 1, true);
googleBooks.show();
if (googleBooks.buttonPressed == 1) {
var search = findBooks(googleBooks.fieldValues["q"]);
let bookTitles = search.map((b) => md_mla_citation(b.volumeInfo));
var bookSelect = Prompt.create();
bookSelect.addLabel("title", "Choose a match:");
bookSelect.addSelect(
"selectBook",
"Book:",
bookTitles,
[],
false);
bookSelect.addButton("Create Booknote", 1, true);
bookSelect.show();
if (bookSelect.buttonPressed == 1) {
let d = Draft.create();
book =
search.find(b =>
md_mla_citation(b.volumeInfo) == bookSelect.fieldValues["selectBook"]).volumeInfo;
book.identifier = book.industryIdentifiers[0].identifier;
book.id = zk_id(d);
d.content = template.render(book);
d.addTag("booknote");
d.update();
}
}
var mla_authors = function(authors) {
switch (authors.length) {
case 1: return authors[0]; break;
case 2: return authors.join(", "); break;
case 3: return authors[0] + " et al."; break;
}
}
// Takes a string, returns the year
var mla_date = function(d) {
let date = new Date(d);
return date.getFullYear();
}
var md_mla_citation = function(data) {
return `${mla_authors(data.authors)}. _${data.title}_. ${data.publisher}, ${mla_date(data.publishedDate)}.`
}
var searchGoogleBooks = function(query) {
var url = "https://www.googleapis.com/books/v1/volumes?q=" + encodeURIComponent(query);
var http = HTTP.create(); //create HTTP object
var response = http.request({
"url": url,
"method": "GET"
});
if (response.success) {
var jsonObj = JSON.parse(response.responseText);
return jsonObj.items;
} else {
return null;
}
}
var findBook = function(reference) {
let items = searchGoogleBooks(reference);
if (items) {
return md_mla_citation(items[0].volumeInfo);
} else {
return reference;
}
}
if (editor.getSelectedRange()[1] != 0) {
// we've selected some lines.
var result =
editor
.getTextInRange(...editor.getSelectedLineRange())
.split("\n")
.map(ref => (ref.match(/#/)) ? ref : findBook(ref))
.join("\n");
editor.setTextInRange(...editor.getSelectedLineRange(), result);
}
draft.update();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment