Last active
October 24, 2020 19:25
-
-
Save BoxOfSnoo/24fff46fff4cc4a958793c40053bd495 to your computer and use it in GitHub Desktop.
Create JW Lib Scripture link
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
// - JW Library lookup #scripts | |
const e = editor; | |
let line_range = e.getSelectedLineRange(); | |
let eol = line_range[0] + line_range[1]; | |
if (e.getTextInRange(eol - 1, 1) === '\n' ) { | |
eol = eol - 1; | |
} | |
e.setSelectedRange(line_range[0], eol - line_range[0]); | |
let scripture = e.getSelectedText(); | |
const bookList = [ | |
['Ge', 'Gen'], ['Ex'], ['Le', 'Lev'], ['Nu', 'Num'], ['De', 'Deut'], ['Jo', 'Jos'], ['Jg'], | |
['Ru'], ['1Sa', '1Sam'], ['2Sa', '2Sam'], ['1Ki'], ['2Ki'], | |
['1Ch', '1Chr', '1Chron'], ['2Ch', '2Chr', '2Chron'],['Ezr'], | |
['Ne', 'Neh'], ['Es'], ['Job'], ['Ps'], ['Pr', 'Prov'], ['Ec', 'Eccl'], | |
['Ca'], ['Isa'], ['Jer'], ['La', 'Lam'], ['Ez', 'Eze', 'Ezek'], ['Da', 'Dan'], | |
['Ho', 'Hos'], ['Joe', 'Joel'], ['Am', 'Amos'], ['Ob'], ['Jon'], ['Mic'], | |
['Na', 'Nah'], ['Hab'], ['Zep', 'Zeph'], ['Hag'], ['Zec', 'Zech'], ['Mal'], | |
['Mt', 'Matt'], ['Mr', 'Mark'], ['Lu', 'Luke'], ['Joh', 'John'], | |
['Ac', 'Acts'], ['Ro', 'Rom'], ['1Co', '1Cor'], ['2Co', '2Cor'], | |
['Ga', 'Gal'], ['Ep', 'Eph'], ['Php', 'Phil'], ['Col'], | |
['1Th', '1Thess'], ['2Th', '2Thess'], ['1Ti', '1Tim'], ['2Ti', '2Tim'], ['Tit'], | |
['Phm'], ['Heb'], ['Jas'], | |
['1Pe', '1Pet'], ['2Pe', '2Pet'], ['1Jo', '1John'], ['2Jo', '2John'], ['3Jo', '3John'], | |
['Jude'], ['Rev'] | |
]; | |
Array.prototype.multiIndexOf = function (value) { | |
var result; | |
this.some(function iter(path) { | |
return function (a, i) { | |
if (a === value) { | |
result = path.concat(i); | |
return true; | |
}; | |
return Array.isArray(a) && a.some(iter(path.concat(i))); | |
} | |
}([])); | |
return result; | |
} | |
Array.prototype.isContiguous = function () { | |
for (i=1;i<this.length; i++) { | |
if (this[i-1]!=this[i]-1) { | |
return false; | |
} | |
}; | |
return true; | |
} | |
// Find the book number | |
let elements = scripture.match(/(\d?.*) (\d*)(:?.*)/); | |
let bookName = elements[1].replace(/ /gi,""); | |
let bookNum = '' + (bookList.multiIndexOf(bookName)[0]+1); | |
let chapter = elements[2].padStart(3,'0'); | |
// Parse verse out | |
if (elements[3]) { | |
verseList = elements[3].replace(/:/,'').split(','); | |
if (verseList.length > 1 && verseList.isContiguous()) { | |
elements[3] = verseList[0]+'-'+verseList[verseList.length-1]; | |
} else { | |
// Just first verse | |
elements[3] = verseList[0]; | |
} | |
verses = elements[3].split('-'); | |
} else { | |
// for single chapter books, we skipped specifying the chapter | |
// then the verse will return empty | |
verses = [chapter]; | |
chapter = '000'; | |
} | |
let range = verses.map(x => bookNum + chapter + x.padStart(3,'0')); | |
// let linkUrl = 'https://www.jw.org/finder?srcid=jwlshare&wtlocale=E&prefer=lang&bible=' + range.join('-') + '&pub=nwtsty'; | |
let linkUrl = 'https://www.jw.org/finder?wtlocale=E&prefer=lang&bible=' + range.join('-') + '&pub=nwtsty'; | |
e.replaceSelection('[' + scripture + '](' + linkUrl + ')'); | |
// For drafts, replace the above line with | |
//e.setSelectedText('[' + scripture + '](' + linkUrl + ')'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment