Created
November 4, 2019 16:45
-
-
Save evantravers/aeb64c8570f430edc2174b3e1a92ac5e to your computer and use it in GitHub Desktop.
Updated bible reference Drafts.app script.
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 getVerseFromRef = function(attemptedRef) { | |
var url = "https://api.esv.org/v3/passage/text/?q=" + encodeURIComponent(attemptedRef) + "&include-footnote-body=false&include-passage-references=false&indent-poetry-lines=2"; //define url for parsing | |
var http = HTTP.create(); //create HTTP object | |
var response = http.request({ | |
"url": url, | |
"method": "GET", | |
"headers": { | |
"Authorization": "<insert API token>" | |
} | |
}); | |
if (response.success) { | |
var jsonObj = JSON.parse(response.responseText); | |
var text = jsonObj.passages.toString(); | |
var ref = jsonObj.canonical; | |
if (text.length > 0) { | |
text = text.replace(/^/gm, "> "); | |
text = "#### " + ref + "\n\n" + text + "\n"; | |
return text; | |
} | |
else { | |
return attemptedRef; | |
} | |
} else { | |
console.log("Couldn't find verse for " + attemptedRef); | |
return attemptedRef; | |
} | |
} | |
if (editor.getSelectedRange()[1] != 0) { | |
// we've selected some lines. | |
var result = | |
editor | |
.getTextInRange(...editor.getSelectedLineRange()) | |
.split("\n") | |
.map(ref => (ref.match(/#/)) ? ref : getVerseFromRef(ref)) | |
.join("\n"); | |
editor.setTextInRange(...editor.getSelectedLineRange(), result); | |
} | |
else { | |
// no selection, try every line. | |
draft.content = | |
draft.content | |
.split("\n") | |
.map(ref => getVerseFromRef(ref)) | |
.join("\n") | |
} | |
draft.update(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment