Last active
April 5, 2020 20:52
-
-
Save evantravers/f668eb4331baba943b59949ad2feef71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let workspace = Workspace.find("Booknotes"); | |
let booknotes = workspace.query("inbox"); | |
let bookTitles = []; | |
for (let booknote of booknotes) { | |
bookTitles.push(booknote.title.replace("title: ", "")); | |
} | |
var p = Prompt.create(); | |
p.addLabel("title", "Choose booknote:"); | |
p.addSelect( | |
"selectBook", | |
"Book:", | |
bookTitles, | |
[], | |
false); | |
p.addButton("Choose Book", 1, true); | |
p.show(); | |
if (p.buttonPressed == 1) { | |
let bookIndex = booknotes.findIndex(bk => bk.title.includes(p.fieldValues["selectBook"])); | |
let book = booknotes[bookIndex]; | |
// - Adds a citation to the book to the current note. | |
let book_data = {}; | |
book_data.authors = [zk_get_metadata(book, "author")] | |
book_data.title = zk_get_metadata(book, "title"); | |
book_data.publisher = zk_get_metadata(book, "publisher"); | |
book_data.publishedDate = zk_get_metadata(book, "year"); | |
draft.content = draft.content + `\n\n${md_mla_citation(book_data)}`; | |
// - Links the current note to the ID of the booknote. | |
draft.content = draft.content + `\n[[${zk_id(book)}]] ${zk_title(book)}` | |
draft.addTag("booknote"); | |
draft.update(); | |
// - Adds a link to this zettel in the main booknote, if there’s a title, uses that as a summary | |
book.content = book.content + `\n[[${zk_id(draft)}]] ${zk_title(draft)}` | |
book.update(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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