feat: Add continuous Bible reading mode, backend-driven translation API integration, localized book names, and preference caching

This commit is contained in:
Adolfo Reyna
2026-02-25 18:13:53 -05:00
parent fc6f740fd2
commit 9da5874977
8 changed files with 460 additions and 56 deletions

View File

@@ -1,5 +1,24 @@
import API from '../API.js';
const BIBLE_TOKEN_REGEX = /@bible:([^\s]+)/gi;
const DEFAULT_TRANSLATION = "web";
const DEFAULT_TRANSLATION = "de4e12af7f28f599-01"; // KJV
export const BIBLE_BOOK_IDS = {
"Genesis": "GEN", "Exodus": "EXO", "Leviticus": "LEV", "Numbers": "NUM", "Deuteronomy": "DEU",
"Joshua": "JOS", "Judges": "JDG", "Ruth": "RUT", "1 Samuel": "1SA", "2 Samuel": "2SA",
"1 Kings": "1KI", "2 Kings": "2KI", "1 Chronicles": "1CH", "2 Chronicles": "2CH", "Ezra": "EZR",
"Nehemiah": "NEH", "Esther": "EST", "Job": "JOB", "Psalms": "PSA", "Proverbs": "PRO",
"Ecclesiastes": "ECC", "Song of Solomon": "SNG", "Isaiah": "ISA", "Jeremiah": "JER",
"Lamentations": "LAM", "Ezekiel": "EZK", "Daniel": "DAN", "Hosea": "HOS", "Joel": "JOL",
"Amos": "AMO", "Obadiah": "OBA", "Jonah": "JON", "Micah": "MIC", "Nahum": "NAM",
"Habakkuk": "HAB", "Zephaniah": "ZEP", "Haggai": "HAG", "Zechariah": "ZEC", "Malachi": "MAL",
"Matthew": "MAT", "Mark": "MRK", "Luke": "LUK", "John": "JHN", "Acts": "ACT",
"Romans": "ROM", "1 Corinthians": "1CO", "2 Corinthians": "2CO", "Galatians": "GAL",
"Ephesians": "EPH", "Philippians": "PHP", "Colossians": "COL", "1 Thessalonians": "1TH",
"2 Thessalonians": "2TH", "1 Timothy": "1TI", "2 Timothy": "2TI", "Titus": "TIT",
"Philemon": "PHM", "Hebrews": "HEB", "James": "JAS", "1 Peter": "1PE", "2 Peter": "2PE",
"1 John": "1JN", "2 John": "2JN", "3 John": "3JN", "Jude": "JUD", "Revelation": "REV"
};
const getNormalizedLocale = (locale = "") => {
return String(locale || "").toLowerCase().replace("_", "-").trim();
@@ -7,13 +26,10 @@ const getNormalizedLocale = (locale = "") => {
const getTranslationForLocale = (locale = "") => {
const normalized = getNormalizedLocale(locale);
if (!normalized) return "kjv";
if (normalized.startsWith("en-gb")) return "webbe";
if (normalized.startsWith("en")) return "kjv";
if (normalized.startsWith("zh")) return "cuv";
if (normalized.startsWith("cs")) return "bkr";
if (normalized.startsWith("pt")) return "almeida";
if (normalized.startsWith("ro")) return "rccv";
if (!normalized) return "de4e12af7f28f599-01"; // KJV
if (normalized.startsWith("en")) return "de4e12af7f28f599-01"; // KJV
if (normalized.startsWith("es")) return "592420522e16049f-01"; // RVR1909
// Default to KJV
return DEFAULT_TRANSLATION;
};
@@ -62,39 +78,144 @@ export const stripBibleTokens = (content = "") => {
.trim();
};
export const fetchBibleReference = async (reference = "", locale = "") => {
const safeReference = normalizeReference(reference);
const preferredTranslation = getTranslationForLocale(locale);
const preferredUrl = `https://bible-api.com/${encodeURIComponent(safeReference)}?translation=${preferredTranslation}`;
const fallbackUrl = `https://bible-api.com/${encodeURIComponent(safeReference)}?translation=${DEFAULT_TRANSLATION}`;
export const AVAILABLE_TRANSLATIONS = [
{ id: "de4e12af7f28f599-01", name: "King James Version (English)" },
{ id: "9879dbb7cfe39e4d-01", name: "World English Bible (English)" },
{ id: "592420522e16049f-01", name: "Reina Valera 1909 (Spanish)" },
{ id: "48acedcf8595c754-01", name: "Palabla de Dios para ti (Spanish)" }
];
let response = await fetch(preferredUrl);
if (!response.ok && preferredTranslation !== DEFAULT_TRANSLATION) {
response = await fetch(fallbackUrl);
const parseChapterHtml = (htmlStr) => {
const parts = String(htmlStr || "").split(/<span[^>]*data-number=\"/);
const versesMap = {};
for (let i = 1; i < parts.length; i++) {
const part = parts[i];
const numMatch = part.match(/^(\d+)\"/);
if (!numMatch) continue;
const vNum = numMatch[1];
let text = part.replace(/^[^>]+>/, '');
text = text.replace(/<[^>]+>/g, '');
text = text.replace(new RegExp('^' + vNum + '\\s*'), '').trim();
text = text.replace(/&nbsp;/g, ' ').replace(/&#160;/g, ' ');
if (!versesMap[vNum]) versesMap[vNum] = '';
versesMap[vNum] += text + ' ';
}
if (!response.ok) throw new Error("Failed to load Bible passage");
const payload = await response.json();
if (payload?.error) throw new Error(payload.error);
return payload;
return versesMap;
};
export const fetchBiblePassage = async (reference = "", locale = "") => {
const safeReference = normalizeReference(reference);
if (!safeReference) {
throw new Error("Missing Bible reference");
const parseChapterJson = (items) => {
const versesMap = {};
let currentVerse = null;
const extract = (nodes) => {
if (!nodes) return;
for (const item of nodes) {
if (item.name === 'verse') {
currentVerse = item.attrs?.number || currentVerse;
if (currentVerse && !versesMap[currentVerse]) versesMap[currentVerse] = "";
} else if (item.type === 'text') {
if (item.attrs?.verseId) {
const vNum = item.attrs.verseId.split('.').pop();
if (!versesMap[vNum]) versesMap[vNum] = "";
versesMap[vNum] += item.text;
} else if (currentVerse) {
// Skip if the text is exactly the verse number to prevent duplicates
if (item.text.trim() === String(currentVerse)) {
continue;
}
versesMap[currentVerse] += item.text;
}
}
if (item.items) {
extract(item.items);
}
}
};
extract(items);
return versesMap;
};
export const fetchBibleChapter = async (chapterReference = "", locale = "", customTranslation = "") => {
const { book, chapter } = parseBibleReference(chapterReference);
const bookId = BIBLE_BOOK_IDS[book];
if (!bookId) throw new Error("Missing or unknown Bible reference book");
const chapterId = `${bookId}.${chapter}`;
const preferredTranslation = customTranslation || getTranslationForLocale(locale);
// Call our backend API which has the correct key and handles auth
let response;
try {
const url = `/bible/chapters/${chapterId}`;
const queryParams = { bibleId: preferredTranslation, "content-type": "json" };
let queryParamsString = "?";
Object.keys(queryParams).forEach(p => {
queryParamsString += p + "=" + queryParams[p] + "&";
});
const localBaseUrl = global.baseUrl ?? "http://localhost:3000";
// To bypass getCall's strict JSON structure mapping (since our backend just passes the API response directly),
// we can use standard fetch with the same cookie inclusion strategy.
const fetchRes = await fetch(localBaseUrl + url + queryParamsString, {
method: 'GET',
mode: 'cors',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'Accept-Language': locale,
'x-app-language': locale,
}
});
if (!fetchRes.ok) throw new Error("Failed to load Bible passage");
response = await fetchRes.json();
} catch (error) {
throw new Error(error.message || "Failed to load Bible passage");
}
const payload = await fetchBibleReference(safeReference, locale);
if (response?.error) throw new Error(response.error);
const payload = response.data;
if (!payload || !payload.content) throw new Error("Invalid Bible data");
let versesMap = {};
if (typeof payload.content === 'string') {
versesMap = parseChapterHtml(payload.content);
} else if (Array.isArray(payload.content)) {
versesMap = parseChapterJson(payload.content);
}
const verses = Object.keys(versesMap)
.sort((a, b) => Number(a) - Number(b))
.map(vNum => ({
verse: Number(vNum),
text: versesMap[vNum].replace(/\n/g, " ").trim()
}));
return {
reference: payload?.reference || safeReference,
text: (payload?.text || "").trim(),
translation: payload?.translation_name || payload?.translation_id || "KJV",
reference: payload.reference || chapterReference,
verses: verses,
translation_id: preferredTranslation,
translation_name: AVAILABLE_TRANSLATIONS.find(t => t.id === preferredTranslation)?.name || "Translation",
};
};
export const fetchBibleChapter = async (chapterReference = "", locale = "") => {
const safeReference = normalizeReference(chapterReference);
return fetchBibleReference(safeReference, locale);
export const fetchBiblePassage = async (reference = "", locale = "", customTranslation = "") => {
const { verse } = parseBibleReference(reference);
const chapterData = await fetchBibleChapter(reference, locale, customTranslation);
const verseData = chapterData.verses.find(v => v.verse === Number(verse));
if (!verseData) throw new Error("Verse not found");
return {
reference: `${chapterData.reference}:${verse}`,
text: verseData.text,
translation: chapterData.translation_name,
};
};
export const fetchBibleReference = async (reference = "", locale = "", customTranslation = "") => {
return fetchBibleChapter(reference, locale, customTranslation);
};
export const parseBibleReference = (reference = "") => {
@@ -262,3 +383,83 @@ export const BIBLE_BOOK_CHAPTERS = {
export const getBookChapterCount = (book = "") => {
return BIBLE_BOOK_CHAPTERS[book] || 1;
};
const BIBLE_BOOK_TRANSLATIONS = {
"Genesis": { "es": "Génesis", "fr": "Genèse", "da": "1 Mosebog" },
"Exodus": { "es": "Éxodo", "fr": "Exode", "da": "2 Mosebog" },
"Leviticus": { "es": "Levítico", "fr": "Lévitique", "da": "3 Mosebog" },
"Numbers": { "es": "Números", "fr": "Nombres", "da": "4 Mosebog" },
"Deuteronomy": { "es": "Deuteronomio", "fr": "Deutéronome", "da": "5 Mosebog" },
"Joshua": { "es": "Josué", "fr": "Josué", "da": "Josva" },
"Judges": { "es": "Jueces", "fr": "Juges", "da": "Dommerne" },
"Ruth": { "es": "Rut", "fr": "Ruth", "da": "Ruth" },
"1 Samuel": { "es": "1 Samuel", "fr": "1 Samuel", "da": "1 Samuel" },
"2 Samuel": { "es": "2 Samuel", "fr": "2 Samuel", "da": "2 Samuel" },
"1 Kings": { "es": "1 Reyes", "fr": "1 Rois", "da": "1 Kongebog" },
"2 Kings": { "es": "2 Reyes", "fr": "2 Rois", "da": "2 Kongebog" },
"1 Chronicles": { "es": "1 Crónicas", "fr": "1 Chroniques", "da": "1 Krønikebog" },
"2 Chronicles": { "es": "2 Crónicas", "fr": "2 Chroniques", "da": "2 Krønikebog" },
"Ezra": { "es": "Esdras", "fr": "Esdras", "da": "Ezra" },
"Nehemiah": { "es": "Nehemías", "fr": "Néhémie", "da": "Nehemias" },
"Esther": { "es": "Ester", "fr": "Esther", "da": "Ester" },
"Job": { "es": "Job", "fr": "Job", "da": "Job" },
"Psalms": { "es": "Salmos", "fr": "Psaumes", "da": "Salmerne" },
"Proverbs": { "es": "Proverbios", "fr": "Proverbes", "da": "Ordsprogene" },
"Ecclesiastes": { "es": "Eclesiastés", "fr": "Ecclésiaste", "da": "Prædikeren" },
"Song of Solomon": { "es": "Cantares", "fr": "Cantique des Cantiques", "da": "Højsangen" },
"Isaiah": { "es": "Isaías", "fr": "Ésaïe", "da": "Esajas" },
"Jeremiah": { "es": "Jeremías", "fr": "Jérémie", "da": "Jeremias" },
"Lamentations": { "es": "Lamentaciones", "fr": "Lamentations", "da": "Klagesangene" },
"Ezekiel": { "es": "Ezequiel", "fr": "Ézéchiel", "da": "Ezekiel" },
"Daniel": { "es": "Daniel", "fr": "Daniel", "da": "Daniel" },
"Hosea": { "es": "Oseas", "fr": "Osée", "da": "Hoseas" },
"Joel": { "es": "Joel", "fr": "Joël", "da": "Joel" },
"Amos": { "es": "Amós", "fr": "Amos", "da": "Amos" },
"Obadiah": { "es": "Abdías", "fr": "Abdias", "da": "Obadias" },
"Jonah": { "es": "Jonás", "fr": "Jonas", "da": "Jonas" },
"Micah": { "es": "Miqueas", "fr": "Michée", "da": "Mika" },
"Nahum": { "es": "Nahúm", "fr": "Nahum", "da": "Nahum" },
"Habakkuk": { "es": "Habacuc", "fr": "Habacuc", "da": "Habakkuk" },
"Zephaniah": { "es": "Sofonías", "fr": "Sophonie", "da": "Sefanias" },
"Haggai": { "es": "Hageo", "fr": "Aggée", "da": "Haggaj" },
"Zechariah": { "es": "Zacarías", "fr": "Zacharie", "da": "Zakarias" },
"Malachi": { "es": "Malaquías", "fr": "Malachie", "da": "Malakias" },
"Matthew": { "es": "Mateo", "fr": "Matthieu", "da": "Matthæus" },
"Mark": { "es": "Marcos", "fr": "Marc", "da": "Markus" },
"Luke": { "es": "Lucas", "fr": "Luc", "da": "Lukas" },
"John": { "es": "Juan", "fr": "Jean", "da": "Johannes" },
"Acts": { "es": "Hechos", "fr": "Actes", "da": "Apostlenes Gerninger" },
"Romans": { "es": "Romanos", "fr": "Romains", "da": "Romerne" },
"1 Corinthians": { "es": "1 Corintios", "fr": "1 Corinthiens", "da": "1 Korinther" },
"2 Corinthians": { "es": "2 Corintios", "fr": "2 Corinthiens", "da": "2 Korinther" },
"Galatians": { "es": "Gálatas", "fr": "Galates", "da": "Galaterne" },
"Ephesians": { "es": "Efesios", "fr": "Éphésiens", "da": "Efeserne" },
"Philippians": { "es": "Filipenses", "fr": "Philippiens", "da": "Filipperne" },
"Colossians": { "es": "Colosenses", "fr": "Colossiens", "da": "Kolossenserne" },
"1 Thessalonians": { "es": "1 Tesalonicenses", "fr": "1 Thessaloniciens", "da": "1 Thessaloniker" },
"2 Thessalonians": { "es": "2 Tesalonicenses", "fr": "2 Thessaloniciens", "da": "2 Thessaloniker" },
"1 Timothy": { "es": "1 Timoteo", "fr": "1 Timothée", "da": "1 Timotheus" },
"2 Timothy": { "es": "2 Timoteo", "fr": "2 Timothée", "da": "2 Timotheus" },
"Titus": { "es": "Tito", "fr": "Tite", "da": "Titus" },
"Philemon": { "es": "Filemón", "fr": "Philémon", "da": "Filemon" },
"Hebrews": { "es": "Hebreos", "fr": "Hébreux", "da": "Hebræerne" },
"James": { "es": "Santiago", "fr": "Jacques", "da": "Jakob" },
"1 Peter": { "es": "1 Pedro", "fr": "1 Pierre", "da": "1 Peter" },
"2 Peter": { "es": "2 Pedro", "fr": "2 Pierre", "da": "2 Peter" },
"1 John": { "es": "1 Juan", "fr": "1 Jean", "da": "1 Johannes" },
"2 John": { "es": "2 Juan", "fr": "2 Jean", "da": "2 Johannes" },
"3 John": { "es": "3 Juan", "fr": "3 Jean", "da": "3 Johannes" },
"Jude": { "es": "Judas", "fr": "Jude", "da": "Judas" },
"Revelation": { "es": "Apocalipsis", "fr": "Apocalypse", "da": "Åbenbaringen" }
};
export const translateBibleReference = (reference = "", locale = "en") => {
const lang = String(locale || "en").substring(0, 2).toLowerCase();
if (lang === "en") return reference;
const parsed = parseBibleReference(reference);
if (!parsed || !parsed.book || !BIBLE_BOOK_TRANSLATIONS[parsed.book]) return reference;
const translatedBook = BIBLE_BOOK_TRANSLATIONS[parsed.book][lang] || parsed.book;
return reference.replace(parsed.book, translatedBook);
};