Skip to content

Commit

Permalink
use new lib to support book start with number and spanish
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-hub committed Oct 1, 2023
1 parent c3b4e65 commit 336404e
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 48 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
},
"dependencies": {
"ackee-tracker": "^5.1.0",
"bible-reference-toolkit": "^2.1.1",
"bible-reference-toolkit": "^2.3.0",
"flagsmith-nodejs": "^3.1.1"
}
}
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 25 additions & 21 deletions src/data/BibleVersionCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,148 +7,152 @@ export const BibleVersionCollection: IBibleVersion[] = [
key: 'web',
versionName: 'World English Bible',
language: 'English',
code: 'en',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'clementine',
versionName: 'Clementine Latin Vulgate',
language: 'Latin',
language: 'Latin', code: 'la',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'kjv',
versionName: 'King James Version',
language: 'English',
language: 'English', code: 'en',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'bbe',
versionName: 'Bible in Basic English',
language: 'English',
language: 'English', code: 'en',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'oeb-us',
versionName: 'Open English Bible, US Edition',
language: 'English',
language: 'English', code: 'en',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'almeida',
versionName: 'João Ferreira de Almeida',
language: 'Portuguese',
language: 'Portuguese', code: 'pt',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'rccv',
versionName: 'Romanian Corrected Cornilescu Version',
language: 'Romanian',
language: 'Romanian', code: 'ro',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'cherokee',
versionName: 'Cherokee New Testament',
language: 'Cherokee',
language: 'Cherokee', code: 'cu',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'oeb-cw',
versionName: 'Open English Bible, Commonwealth Edition',
language: 'English (UK)',
language: 'English (UK)', code: 'en',
apiSource: BibleAPISourceCollection.bibleApi,
},
{
key: 'webbe',
versionName: 'World English Bible, British Edition',
language: 'English (UK)',
language: 'English (UK)', code: 'en',
apiSource: BibleAPISourceCollection.bibleApi,
},
// bolls life
{
key: 'nkjv',
versionName: 'New King James Version',
language: 'English',
language: 'English', code: 'en',
apiSource: BibleAPISourceCollection.bollsLife,
infoUrl: 'https://wikipedia.org/wiki/New_King_James_Version',
},
{
key: 'niv',
versionName: 'New International Version, 1984',
language: 'English',
language: 'English', code: 'en',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'nrsvce',
versionName: 'New Revised Standard Version Catholic Edition (NRSVCE)',
language: 'English',
language: 'English', code: 'en',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'esv',
versionName: 'English Standard Version',
language: 'English',
language: 'English', code: 'en',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'cuv',
versionName: 'Chinese Union Version (Traditional)',
language: 'Chinese',
language: 'Chinese', code: 'cn',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'nr06',
versionName: 'Nuova Riveduta, 2006',
language: 'Latin / Italian',
language: 'Latin / Italian', code: 'it',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'svd',
versionName: 'Smith and Van Dyke',
language: 'Arabic العربية',
language: 'Arabic العربية', code: 'ar',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'btx3',
versionName: 'La Biblia Textual 3ra Edicion',
language: 'Spanish',
language: 'Spanish', code: 'sp',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'rv1960',
versionName: 'Reina Valera 1960',
language: 'Spanish',
language: 'Spanish', code: 'sp',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'rv2004',
versionName: 'Reina Valera Gomez 2004',
language: 'Spanish',
language: 'Spanish', code: 'sp',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'pdt',
versionName: 'Palabra de Dios para Todos',
language: 'Spanish',
language: 'Spanish', code: 'sp',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'nvi',
versionName: 'Nueva Version Internacional',
language: 'Spanish',
language: 'Spanish', code: 'sp',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'ntv',
versionName: 'Nueva Traduccion Viviente, 2009',
language: 'Spanish',
code: 'sp',
apiSource: BibleAPISourceCollection.bollsLife,
},
{
key: 'lbla',
versionName: 'La Biblia de Las Americas, 1997',
language: 'Spanish',
code: 'sp',
apiSource: BibleAPISourceCollection.bollsLife,
},
// todo add langauge code
{
key: 'bg',
versionName: 'Biblia gdańska, 1881',
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IBibleVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface IBibleVersion {
key: string
versionName: string
language: string
code?: string
apiSource: IBibleApiSource
infoUrl?: string
apiUrl?: string //deprecated
Expand Down
23 changes: 18 additions & 5 deletions src/provider/BaseBibleAPIProvider.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { IVerse } from '../interfaces/IVerse'
import { Notice } from 'obsidian'
import { EventStats } from './EventStats'
import { IBibleVersion } from '../interfaces/IBibleVersion';

export abstract class BaseBibleAPIProvider {
protected _key: string // the version selected
protected _versionKey: string // the version selected, for example kjv
protected _apiUrl: string
protected _queryUrl: string
protected _bibleReferenceHead: string
protected _bibleVersiopn: IBibleVersion

constructor(bibleVersion: IBibleVersion) {
this._bibleVersiopn = bibleVersion
const { key } = bibleVersion
this._versionKey = key
this._apiUrl = bibleVersion.apiSource.apiUrl
}

protected get LanguageShortCode(): string|undefined {
return this._bibleVersiopn.code
}

/**
* Get the Key Identity for the Bible version
*/
public get BibleVersionKey(): string {
return this._key
return this._versionKey
}

/**
Expand Down Expand Up @@ -53,14 +66,14 @@ export abstract class BaseBibleAPIProvider {
verse: number[],
versionName?: string
): Promise<IVerse[]> {
if (!this._key && versionName) {
if (!this._versionKey && versionName) {
throw new Error('version (language) not set yet')
}
const url = this.buildRequestURL(
bookName,
chapter,
verse,
versionName || this._key
versionName || this._versionKey
)
console.debug(url, 'url to query')
try {
Expand All @@ -78,7 +91,7 @@ export abstract class BaseBibleAPIProvider {
bookName,
chapter,
verse,
versionName || this._key
versionName || this._versionKey
)
} catch (e) {
console.error('error while querying', e)
Expand Down
5 changes: 1 addition & 4 deletions src/provider/BibleAPIDotComProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import { BaseBibleAPIProvider } from './BaseBibleAPIProvider'

export class BibleAPIDotComProvider extends BaseBibleAPIProvider {
public constructor(bibleVersion: IBibleVersion) {
super()
const { key } = bibleVersion
this._key = key
this._apiUrl = bibleVersion.apiSource.apiUrl
super(bibleVersion)
}

/**
Expand Down
11 changes: 4 additions & 7 deletions src/provider/BollyLifeProvider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { IVerse } from '../interfaces/IVerse'
import { IBibleVersion } from '../interfaces/IBibleVersion'
import { BaseBibleAPIProvider } from './BaseBibleAPIProvider'
import Reference from 'bible-reference-toolkit/dist/lib/reference'
import { getBookIdFromBookName } from '../utils/bookNameReference';

export class BollyLifeProvider extends BaseBibleAPIProvider {
//private _verseApiUrl: string; // we do not support get verse api yet, but the api supported it
private _chapterApiUrl: string

public constructor(bibleVersion: IBibleVersion) {
super()
const { key } = bibleVersion
this._key = key
this._apiUrl = bibleVersion.apiSource.apiUrl
super(bibleVersion)
//this._verseApiUrl = `${this._apiUrl}/get-paralel-verses/`;
//this._chapterApiUrl = `${this._apiUrl}/get-chapter/`;
this._chapterApiUrl = this._apiUrl
Expand All @@ -28,8 +25,8 @@ export class BollyLifeProvider extends BaseBibleAPIProvider {
versionName?: string
): string {
const baseUrl = this._chapterApiUrl
const book = Reference.bookIdFromName(bookName)
this._queryUrl = `${baseUrl}/${versionName?.toUpperCase()}/${book}/${chapter}/`
const bookId = getBookIdFromBookName(bookName, this._bibleVersiopn.code)
this._queryUrl = `${baseUrl}/${versionName?.toUpperCase()}/${bookId}/${chapter}/`
return this._queryUrl
}

Expand Down
23 changes: 23 additions & 0 deletions src/utils/bookNameReference.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getBookIdFromBookName } from './bookNameReference';

describe('test bookNameReference', () => {
it('should return the book id', () => {
expect(getBookIdFromBookName('Genesis')).toBe(1);
})

it('should return the book id even start with number', () => {
expect(getBookIdFromBookName('1 John', 'en')).toBe(62);
})

it('should return the correct id in Spanish', () => {
expect(getBookIdFromBookName('Génesis', 'sp')).toBe(1);
})

it('shoud throw an error is code is wrong or cannot find the book', () => {
try {
getBookIdFromBookName('Genesis', 'wrongCode');
} catch (e) {
expect(e.message).toBe('No translation found for language wrongcode');
}
})
})
21 changes: 21 additions & 0 deletions src/utils/bookNameReference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Reference from 'bible-reference-toolkit'


export const getBookIdFromBookName = (bookName: string, langaugeCode: string = 'en'): number => {
try {
return Reference.bookIdFromTranslationAndName(langaugeCode, bookName)
} catch (e) {
// try in slow but in all supported languages
return Reference.bookIdFromName(bookName)
}
}

export const getFullBookName = (name: string, langaugeCode: string = 'en'): string => {
console.debug('getFullBookName', name, langaugeCode)
const bookId = getBookIdFromBookName(name, langaugeCode)
try {
return Reference.bookNameFromTranslationAndId(langaugeCode, bookId)
} catch (e) {
return Reference.bookEnglishFullNameFromId(bookId)
}
}
9 changes: 6 additions & 3 deletions src/utils/getSuggestionsFromQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BibleReferencePluginSettings } from '../data/constants'
import { VerseSuggesting } from '../verse/VerseSuggesting'
import { BOOK_REG } from './regs'
import Reference from 'bible-reference-toolkit/dist/lib/reference'
import { getFullBookName } from './bookNameReference';
import { BibleVersionCollection } from '../data/BibleVersionCollection';
import { IBibleVersion } from '../interfaces/IBibleVersion';

/**
* Get suggestions from string query
Expand All @@ -27,8 +29,9 @@ export const getSuggestionsFromQuery = async (
const verseNumber = parseInt(numbers[1])
const verseEndNumber = numbers.length === 3 ? parseInt(numbers[2]) : undefined

const bookId = Reference.bookIdFromName(rawBookName)
const bookName = Reference.bookNameFromId(bookId)
const selectedBibleVersion = BibleVersionCollection.find((bible: IBibleVersion) => bible.key === settings.bibleVersion)
const bookName = getFullBookName(rawBookName, selectedBibleVersion?.code)
console.debug('bookName', bookName)

// todo get bibleVersion and language from settings
const suggestingVerse = new VerseSuggesting(
Expand Down

0 comments on commit 336404e

Please sign in to comment.