- Each translation has a licence; FREE_BIBLE_TRANSLATION_IDS (KJV and the new Berean Standard Bible) is what the Hub and App 1.0 offer, and the CSV import refuses any other when told to, naming the publisher to ask. - The publishers' exact notices (NLT, ESV, AFR53, AFR83 corrected), an acknowledgement per translation, the Bible Society's disclaimer, publisher and permissionsUrl, FREE_QUOTE_VERSES and quotedVerses. AFR83 is named "Afrikaans 1983/1992-vertaling". - docs/BIBLE-PERMISSIONS.md: what each publisher allows, the status and the drafted permission requests. APP-1.0-PLAN decision 14, HANDOVER (with the Website 1.8.0 approach), VERSIONS, CHANGELOG. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
304 lines
22 KiB
TypeScript
304 lines
22 KiB
TypeScript
import { z } from "zod";
|
||
|
||
/**
|
||
* Bible passages for the Church App's devotionals (contracts 1.20.0, docs/APP-1.0-PLAN.md §6,
|
||
* decisions 5 and 10): the books (USFM codes, as YouVersion uses them), the translations a
|
||
* devotional can quote, reading a reference someone typed ("John 3:16-18", "Johannes 3:16–18",
|
||
* "1 Kor 13:4-7"), and the "Read in YouVersion" link.
|
||
*
|
||
* Shared by the Hub and the App, so a CSV row the Hub accepts is one the App accepts.
|
||
*/
|
||
|
||
// ─── Translations ────────────────────────────────────────────────────────────
|
||
|
||
/**
|
||
* The translations a devotional can use. An id never changes once released; more can be added.
|
||
* `youVersionId` is the version's number on bible.com (`bible.com/bible/<id>/JHN.3.16.<abbr>`).
|
||
*
|
||
* The notices are the publishers' own wording (checked 2026-11-30, docs/BIBLE-PERMISSIONS.md):
|
||
* - `copyright` is shown under the verse, wherever it's quoted.
|
||
* - `acknowledgement` is the full notice, for the App's Acknowledgements page (the Bible Society
|
||
* asks for the long form there and allows the short one next to the text).
|
||
* - `disclaimer`, where not null, goes with the notice whenever the translation is quoted next to
|
||
* content the publisher hasn't approved, which is every devotional (Bible Society, §6.4).
|
||
* - `publisher` and `permissionsUrl`: who to ask past the free allowance (`FREE_QUOTE_VERSES`).
|
||
* - `licence`: "public-domain" may be used freely; "permission" needs the publisher's written
|
||
* permission for our use. **App 1.0 and the Hub offer only the public-domain ones**
|
||
* (`FREE_BIBLE_TRANSLATION_IDS`, decided by Joshua 2026-11-30); the others stay in the list,
|
||
* ids unchanged, for when a publisher says yes.
|
||
*
|
||
* None of these lets Cross Code quote past that allowance: that needs each publisher's written
|
||
* permission (docs/BIBLE-PERMISSIONS.md). Never send a passage's text to an AI feature: Crossway
|
||
* (ESV) and Biblica (NIV) forbid it outright without a separate licence.
|
||
*/
|
||
const BIBLE_SOCIETY_DISCLAIMER_AF =
|
||
"Die Bybelgenootskap van Suid-Afrika verklaar hiermee dat die mening, sienings en teologiese standpunte in die bykomende materiaal in hierdie publikasie nie noodwendig dié van die Bybelgenootskap van Suid-Afrika weerspieël nie. Die Bybelgenootskap van Suid-Afrika onderskryf nie die gebruik van Bybelteks op enige manier of op enige platform wat moontlik as antichristelik beskou kan word of wat onwettige aktiwiteite bevorder.";
|
||
const BIBLE_SOCIETY = { licence: "permission", publisher: "Bible Society of South Africa", permissionsUrl: "https://biblesociety.co.za/index/copyright-and-permissions/" } as const;
|
||
|
||
export const BIBLE_TRANSLATIONS = [
|
||
{
|
||
id: "NLT", name: "New Living Translation", language: "en", youVersionId: 116,
|
||
copyright: "Scripture quotations are taken from the Holy Bible, New Living Translation, copyright © 1996, 2004, 2015 by Tyndale House Ministries. Used by permission. All rights reserved.",
|
||
acknowledgement: "Scripture quotations marked (NLT) are taken from the Holy Bible, New Living Translation, copyright © 1996, 2004, 2015 by Tyndale House Ministries. Used by permission. All rights reserved.",
|
||
disclaimer: null,
|
||
licence: "permission", publisher: "Tyndale House Publishers", permissionsUrl: "https://www.tyndale.com/permissions",
|
||
},
|
||
{
|
||
id: "ESV", name: "English Standard Version", language: "en", youVersionId: 59,
|
||
copyright: "Scripture quotations are from the ESV® Bible (The Holy Bible, English Standard Version®), © 2001 by Crossway, a publishing ministry of Good News Publishers. ESV Text Edition: 2025. The ESV text may not be quoted in any publication made available to the public by a Creative Commons license. The ESV may not be translated in whole or in part into any other language. Used by permission. All rights reserved.",
|
||
acknowledgement: "Scripture quotations marked (ESV) are from the ESV® Bible (The Holy Bible, English Standard Version®), © 2001 by Crossway, a publishing ministry of Good News Publishers. ESV Text Edition: 2025. The ESV text may not be quoted in any publication made available to the public by a Creative Commons license. The ESV may not be translated in whole or in part into any other language. Used by permission. All rights reserved.",
|
||
disclaimer: null,
|
||
licence: "permission", publisher: "Crossway", permissionsUrl: "https://www.crossway.org/permissions/digital/",
|
||
},
|
||
{
|
||
id: "NIV", name: "New International Version", language: "en", youVersionId: 111,
|
||
copyright: "Scripture quotations taken from The Holy Bible, New International Version® NIV®. Copyright © 1973, 1978, 1984, 2011 by Biblica, Inc.™ Used by permission. All rights reserved worldwide.",
|
||
acknowledgement: "Scripture quotations marked (NIV) are taken from The Holy Bible, New International Version® NIV®. Copyright © 1973, 1978, 1984, 2011 by Biblica, Inc.™ Used by permission. All rights reserved worldwide.",
|
||
disclaimer: null,
|
||
licence: "permission", publisher: "Biblica, Inc.", permissionsUrl: "https://www.biblica.com/permissions/",
|
||
},
|
||
{
|
||
id: "NKJV", name: "New King James Version", language: "en", youVersionId: 114,
|
||
copyright: "Scripture taken from the New King James Version®. Copyright © 1982 by Thomas Nelson. Used by permission. All rights reserved.",
|
||
acknowledgement: "Scripture marked (NKJV) taken from the New King James Version®. Copyright © 1982 by Thomas Nelson. Used by permission. All rights reserved.",
|
||
disclaimer: null,
|
||
licence: "permission", publisher: "HarperCollins Christian Publishing (Thomas Nelson)", permissionsUrl: "https://www.harpercollinschristian.com/sales-and-rights/permissions/",
|
||
},
|
||
{
|
||
id: "KJV", name: "King James Version", language: "en", youVersionId: 1,
|
||
copyright: "King James Version. Public domain.",
|
||
acknowledgement: "Scripture quotations marked (KJV) are from the King James Version, which is in the public domain.",
|
||
disclaimer: null,
|
||
licence: "public-domain", publisher: null, permissionsUrl: null,
|
||
},
|
||
{
|
||
id: "AFR53", name: "Afrikaans 1933/1953-vertaling", language: "af", youVersionId: 5,
|
||
copyright: "Bybel in Afrikaans 1933/1953-vertaling © Bybelgenootskap van Suid-Afrika 1933, 1953. Digitale Weergawe © Bybelgenootskap van Suid-Afrika 2012. Alle regte voorbehou. www.biblesociety.co.za / www.biblesa.co.za.",
|
||
acknowledgement: "Skrifaanhalings geneem uit die Bybel in Afrikaans 1933/1953-vertaling © Bybelgenootskap van Suid-Afrika 1933, 1953. Digitale Weergawe © Bybelgenootskap van Suid-Afrika 2012. Gebruik met toestemming. Alle regte voorbehou. www.biblesociety.co.za / www.biblesa.co.za. Kontak: copyright@biblesociety.co.za",
|
||
disclaimer: BIBLE_SOCIETY_DISCLAIMER_AF,
|
||
...BIBLE_SOCIETY,
|
||
},
|
||
{
|
||
id: "AFR83", name: "Afrikaans 1983/1992-vertaling", language: "af", youVersionId: 6,
|
||
copyright: "Bybel in Afrikaans 1983-vertaling met herformulerings tot en met 1992 © Bybelgenootskap van Suid-Afrika 1983, 1992. Alle regte voorbehou. www.biblesociety.co.za / www.biblesa.co.za.",
|
||
acknowledgement: "Skrifaanhalings geneem uit die Bybel in Afrikaans 1983-vertaling met herformulerings tot en met 1992 © Bybelgenootskap van Suid-Afrika 1983, 1992. Gebruik met toestemming. Alle regte voorbehou. www.biblesociety.co.za / www.biblesa.co.za. Kontak: copyright@biblesociety.co.za",
|
||
disclaimer: BIBLE_SOCIETY_DISCLAIMER_AF,
|
||
...BIBLE_SOCIETY,
|
||
},
|
||
{
|
||
// Dedicated to the public domain (CC0) on 30 April 2023 (berean.bible/terms.htm). The
|
||
// attribution is "appreciated but not required"; the Berean name only for the verbatim text.
|
||
id: "BSB", name: "Berean Standard Bible", language: "en", youVersionId: 3034,
|
||
copyright: "Berean Standard Bible (BSB). Public domain.",
|
||
acknowledgement: "The Holy Bible, Berean Standard Bible, BSB is produced in cooperation with Bible Hub, Discovery Bible, OpenBible.com, and the Berean Bible Translation Committee. This text of God's Word has been dedicated to the public domain.",
|
||
disclaimer: null,
|
||
licence: "public-domain", publisher: null, permissionsUrl: null,
|
||
},
|
||
] as const;
|
||
|
||
export type BibleTranslation = (typeof BIBLE_TRANSLATIONS)[number];
|
||
export const BIBLE_TRANSLATION_IDS = BIBLE_TRANSLATIONS.map((t) => t.id) as [BibleTranslation["id"], ...BibleTranslation["id"][]];
|
||
export const bibleTranslationIdSchema = z.enum(BIBLE_TRANSLATION_IDS);
|
||
export type BibleTranslationId = z.infer<typeof bibleTranslationIdSchema>;
|
||
|
||
/** The translations anyone may quote freely: the only ones the Hub and App 1.0 offer. */
|
||
export const FREE_BIBLE_TRANSLATION_IDS: readonly BibleTranslationId[] = BIBLE_TRANSLATIONS.filter((t) => t.licence === "public-domain").map((t) => t.id);
|
||
|
||
export function bibleTranslation(id: BibleTranslationId): BibleTranslation {
|
||
return BIBLE_TRANSLATIONS.find((t) => t.id === id)!;
|
||
}
|
||
|
||
/** A passage longer than this gets a warning: quote the passage, not whole chapters (§6). */
|
||
export const PASSAGE_WARN_VERSES = 25;
|
||
|
||
/**
|
||
* What every publisher but the KJV's lets anyone quote without asking, counted across the whole
|
||
* work (Biblica: across a whole app or website), with the quotes under 25% of it and never a whole
|
||
* book. Cross Code's devotionals, and a church's own, pass it within a few months, so each needs
|
||
* the publisher's written permission (docs/BIBLE-PERMISSIONS.md).
|
||
*/
|
||
export const FREE_QUOTE_VERSES = 500;
|
||
|
||
// ─── Books ───────────────────────────────────────────────────────────────────
|
||
|
||
/**
|
||
* The 66 books in order: USFM code, English and Afrikaans names, chapters (English numbering) and
|
||
* the other names and abbreviations a reference may use. Matching ignores case, accents, dots and
|
||
* spaces, so "1 Kor.", "1kor" and "1 Korintiërs" are the same book.
|
||
*/
|
||
export const BIBLE_BOOKS = [
|
||
{ code: "GEN", en: "Genesis", af: "Genesis", chapters: 50, aliases: ["Gen", "Ge", "Gn"] },
|
||
{ code: "EXO", en: "Exodus", af: "Eksodus", chapters: 40, aliases: ["Exod", "Exo", "Ex", "Eks"] },
|
||
{ code: "LEV", en: "Leviticus", af: "Levitikus", chapters: 27, aliases: ["Lev", "Le", "Lv"] },
|
||
{ code: "NUM", en: "Numbers", af: "Numeri", chapters: 36, aliases: ["Num", "Nu", "Nm", "Nb"] },
|
||
{ code: "DEU", en: "Deuteronomy", af: "Deuteronomium", chapters: 34, aliases: ["Deut", "Deu", "Dt", "Dtn"] },
|
||
{ code: "JOS", en: "Joshua", af: "Josua", chapters: 24, aliases: ["Josh", "Jos", "Jsh"] },
|
||
{ code: "JDG", en: "Judges", af: "Rigters", chapters: 21, aliases: ["Judg", "Jdg", "Jdgs", "Rig"] },
|
||
{ code: "RUT", en: "Ruth", af: "Rut", chapters: 4, aliases: ["Rth", "Ru"] },
|
||
{ code: "1SA", en: "1 Samuel", af: "1 Samuel", chapters: 31, aliases: ["1 Sam", "1 Sa", "1Sm"] },
|
||
{ code: "2SA", en: "2 Samuel", af: "2 Samuel", chapters: 24, aliases: ["2 Sam", "2 Sa", "2Sm"] },
|
||
{ code: "1KI", en: "1 Kings", af: "1 Konings", chapters: 22, aliases: ["1 Kgs", "1 Ki", "1 Kon"] },
|
||
{ code: "2KI", en: "2 Kings", af: "2 Konings", chapters: 25, aliases: ["2 Kgs", "2 Ki", "2 Kon"] },
|
||
{ code: "1CH", en: "1 Chronicles", af: "1 Kronieke", chapters: 29, aliases: ["1 Chron", "1 Chr", "1 Ch", "1 Kron", "1 Kr"] },
|
||
{ code: "2CH", en: "2 Chronicles", af: "2 Kronieke", chapters: 36, aliases: ["2 Chron", "2 Chr", "2 Ch", "2 Kron", "2 Kr"] },
|
||
{ code: "EZR", en: "Ezra", af: "Esra", chapters: 10, aliases: ["Ezr", "Esr"] },
|
||
{ code: "NEH", en: "Nehemiah", af: "Nehemia", chapters: 13, aliases: ["Neh", "Ne"] },
|
||
{ code: "EST", en: "Esther", af: "Ester", chapters: 10, aliases: ["Esth", "Est", "Es"] },
|
||
{ code: "JOB", en: "Job", af: "Job", chapters: 42, aliases: ["Jb"] },
|
||
{ code: "PSA", en: "Psalms", af: "Psalms", chapters: 150, aliases: ["Psalm", "Ps", "Psa", "Pss", "Psalmen"] },
|
||
{ code: "PRO", en: "Proverbs", af: "Spreuke", chapters: 31, aliases: ["Prov", "Pro", "Prv", "Pr", "Spr"] },
|
||
{ code: "ECC", en: "Ecclesiastes", af: "Prediker", chapters: 12, aliases: ["Eccl", "Ecc", "Ec", "Qoh", "Pred", "Pr"] },
|
||
{ code: "SNG", en: "Song of Songs", af: "Hooglied", chapters: 8, aliases: ["Song of Solomon", "Song", "Sos", "Sng", "Hgl", "Hoogl"] },
|
||
{ code: "ISA", en: "Isaiah", af: "Jesaja", chapters: 66, aliases: ["Isa", "Is", "Jes"] },
|
||
{ code: "JER", en: "Jeremiah", af: "Jeremia", chapters: 52, aliases: ["Jer", "Je", "Jr"] },
|
||
{ code: "LAM", en: "Lamentations", af: "Klaagliedere", chapters: 5, aliases: ["Lam", "La", "Klaagl", "Kla"] },
|
||
{ code: "EZK", en: "Ezekiel", af: "Esegiël", chapters: 48, aliases: ["Ezek", "Eze", "Ezk", "Eseg", "Ese"] },
|
||
{ code: "DAN", en: "Daniel", af: "Daniël", chapters: 12, aliases: ["Dan", "Da", "Dn"] },
|
||
{ code: "HOS", en: "Hosea", af: "Hosea", chapters: 14, aliases: ["Hos", "Ho"] },
|
||
{ code: "JOL", en: "Joel", af: "Joël", chapters: 3, aliases: ["Joe", "Jl"] },
|
||
{ code: "AMO", en: "Amos", af: "Amos", chapters: 9, aliases: ["Am", "Amo"] },
|
||
{ code: "OBA", en: "Obadiah", af: "Obadja", chapters: 1, aliases: ["Obad", "Ob", "Oba"] },
|
||
{ code: "JON", en: "Jonah", af: "Jona", chapters: 4, aliases: ["Jnh", "Jon"] },
|
||
{ code: "MIC", en: "Micah", af: "Miga", chapters: 7, aliases: ["Mic", "Mi", "Mig"] },
|
||
{ code: "NAM", en: "Nahum", af: "Nahum", chapters: 3, aliases: ["Nah", "Na"] },
|
||
{ code: "HAB", en: "Habakkuk", af: "Habakuk", chapters: 3, aliases: ["Hab", "Hb"] },
|
||
{ code: "ZEP", en: "Zephaniah", af: "Sefanja", chapters: 3, aliases: ["Zeph", "Zep", "Zp", "Sef"] },
|
||
{ code: "HAG", en: "Haggai", af: "Haggai", chapters: 2, aliases: ["Hag", "Hg"] },
|
||
{ code: "ZEC", en: "Zechariah", af: "Sagaria", chapters: 14, aliases: ["Zech", "Zec", "Zc", "Sag"] },
|
||
{ code: "MAL", en: "Malachi", af: "Maleagi", chapters: 4, aliases: ["Mal", "Ml"] },
|
||
{ code: "MAT", en: "Matthew", af: "Matteus", chapters: 28, aliases: ["Matt", "Mat", "Mt", "Mattheus", "Matt."] },
|
||
{ code: "MRK", en: "Mark", af: "Markus", chapters: 16, aliases: ["Mrk", "Mar", "Mk", "Mr", "Mark."] },
|
||
{ code: "LUK", en: "Luke", af: "Lukas", chapters: 24, aliases: ["Luk", "Lk", "Lu"] },
|
||
{ code: "JHN", en: "John", af: "Johannes", chapters: 21, aliases: ["Jhn", "Jn", "Joh"] },
|
||
{ code: "ACT", en: "Acts", af: "Handelinge", chapters: 28, aliases: ["Act", "Ac", "Hand", "Hnd"] },
|
||
{ code: "ROM", en: "Romans", af: "Romeine", chapters: 16, aliases: ["Rom", "Ro", "Rm"] },
|
||
{ code: "1CO", en: "1 Corinthians", af: "1 Korintiërs", chapters: 16, aliases: ["1 Cor", "1 Co", "1 Kor", "1 Ko"] },
|
||
{ code: "2CO", en: "2 Corinthians", af: "2 Korintiërs", chapters: 13, aliases: ["2 Cor", "2 Co", "2 Kor", "2 Ko"] },
|
||
{ code: "GAL", en: "Galatians", af: "Galasiërs", chapters: 6, aliases: ["Gal", "Ga"] },
|
||
{ code: "EPH", en: "Ephesians", af: "Efesiërs", chapters: 6, aliases: ["Eph", "Ephes", "Ef"] },
|
||
{ code: "PHP", en: "Philippians", af: "Filippense", chapters: 4, aliases: ["Phil", "Php", "Pp", "Fil"] },
|
||
{ code: "COL", en: "Colossians", af: "Kolossense", chapters: 4, aliases: ["Col", "Co", "Kol"] },
|
||
{ code: "1TH", en: "1 Thessalonians", af: "1 Tessalonisense", chapters: 5, aliases: ["1 Thess", "1 Thes", "1 Th", "1 Tess", "1 Tes"] },
|
||
{ code: "2TH", en: "2 Thessalonians", af: "2 Tessalonisense", chapters: 3, aliases: ["2 Thess", "2 Thes", "2 Th", "2 Tess", "2 Tes"] },
|
||
{ code: "1TI", en: "1 Timothy", af: "1 Timoteus", chapters: 6, aliases: ["1 Tim", "1 Ti", "1 Tm"] },
|
||
{ code: "2TI", en: "2 Timothy", af: "2 Timoteus", chapters: 4, aliases: ["2 Tim", "2 Ti", "2 Tm"] },
|
||
{ code: "TIT", en: "Titus", af: "Titus", chapters: 3, aliases: ["Tit", "Ti"] },
|
||
{ code: "PHM", en: "Philemon", af: "Filemon", chapters: 1, aliases: ["Philem", "Phm", "Pm", "Filem", "Flm"] },
|
||
{ code: "HEB", en: "Hebrews", af: "Hebreërs", chapters: 13, aliases: ["Heb", "He"] },
|
||
{ code: "JAS", en: "James", af: "Jakobus", chapters: 5, aliases: ["Jas", "Jm", "Jak"] },
|
||
{ code: "1PE", en: "1 Peter", af: "1 Petrus", chapters: 5, aliases: ["1 Pet", "1 Pe", "1 Pt", "1 Petr"] },
|
||
{ code: "2PE", en: "2 Peter", af: "2 Petrus", chapters: 3, aliases: ["2 Pet", "2 Pe", "2 Pt", "2 Petr"] },
|
||
{ code: "1JN", en: "1 John", af: "1 Johannes", chapters: 5, aliases: ["1 Jn", "1 Jhn", "1 Joh"] },
|
||
{ code: "2JN", en: "2 John", af: "2 Johannes", chapters: 1, aliases: ["2 Jn", "2 Jhn", "2 Joh"] },
|
||
{ code: "3JN", en: "3 John", af: "3 Johannes", chapters: 1, aliases: ["3 Jn", "3 Jhn", "3 Joh"] },
|
||
{ code: "JUD", en: "Jude", af: "Judas", chapters: 1, aliases: ["Jud", "Jd"] },
|
||
{ code: "REV", en: "Revelation", af: "Openbaring", chapters: 22, aliases: ["Rev", "Re", "Rv", "Revelations", "Op", "Openb"] },
|
||
] as const;
|
||
|
||
export type BibleBook = (typeof BIBLE_BOOKS)[number];
|
||
export const BIBLE_BOOK_CODES = BIBLE_BOOKS.map((b) => b.code) as [BibleBook["code"], ...BibleBook["code"][]];
|
||
export const bibleBookCodeSchema = z.enum(BIBLE_BOOK_CODES);
|
||
export type BibleBookCode = z.infer<typeof bibleBookCodeSchema>;
|
||
|
||
export function bibleBook(code: BibleBookCode): BibleBook {
|
||
return BIBLE_BOOKS.find((b) => b.code === code)!;
|
||
}
|
||
|
||
/** "1 Korintiërs." → "1korintiers": lower case, no accents, dots or spaces. */
|
||
function bookKey(name: string): string {
|
||
return name.normalize("NFD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[.\s]/g, "");
|
||
}
|
||
|
||
/**
|
||
* Name → book. An abbreviation two books share (`Pr`: Proverbs and Prediker, `Co`, `Ti`) goes to
|
||
* the first, so the full names always work and the common short forms mostly do.
|
||
*/
|
||
const BOOK_BY_KEY: ReadonlyMap<string, BibleBook> = (() => {
|
||
const map = new Map<string, BibleBook>();
|
||
for (const book of BIBLE_BOOKS) {
|
||
for (const name of [book.code, book.en, book.af, ...book.aliases]) {
|
||
const key = bookKey(name);
|
||
if (!map.has(key)) map.set(key, book);
|
||
}
|
||
}
|
||
return map;
|
||
})();
|
||
|
||
/** The book a name or abbreviation means, or null. */
|
||
export function findBibleBook(name: string): BibleBook | null {
|
||
return BOOK_BY_KEY.get(bookKey(name)) ?? null;
|
||
}
|
||
|
||
// ─── Passages ────────────────────────────────────────────────────────────────
|
||
|
||
/**
|
||
* Where a passage is: a book, a start verse and an end verse (the same chapter or a later one).
|
||
* A whole chapter isn't a passage here: devotionals quote the verses they're about (§6).
|
||
*/
|
||
export const passageLocationSchema = z.object({
|
||
book: bibleBookCodeSchema,
|
||
chapter: z.int().min(1).max(150),
|
||
verse: z.int().min(1).max(176),
|
||
endChapter: z.int().min(1).max(150),
|
||
endVerse: z.int().min(1).max(176),
|
||
}).refine((p) => p.endChapter > p.chapter || (p.endChapter === p.chapter && p.endVerse >= p.verse), "The passage ends before it starts")
|
||
.refine((p) => p.endChapter <= bibleBook(p.book).chapters, "The book has fewer chapters");
|
||
export type PassageLocation = z.infer<typeof passageLocationSchema>;
|
||
|
||
export type ParsedReference = { ok: true; passage: PassageLocation } | { ok: false; error: string };
|
||
|
||
/**
|
||
* Reads a reference as people type it: `John 3:16`, `John 3:16-18`, `Johannes 3:16–18`,
|
||
* `1 Kor. 13:4-7`, `Rom 8:38-9:1`, `Ps 23 1-3` (a space for the colon), `Jude 3` and `Jude 1:3`
|
||
* (one-chapter books). Dashes may be `-`, `–` or `—`; `.` or `,` are taken for the colon too.
|
||
*/
|
||
export function parseBibleReference(input: string): ParsedReference {
|
||
const text = input.trim().replace(/\s+/g, " ");
|
||
const match = /^((?:[1-3]\s?)?[^\d\s][^\d]*?)\s*(\d+)(?:(?:\s*[:.,]\s*|\s+)(\d+))?(?:\s*[-–—]\s*(\d+)(?:\s*[:.,]\s*(\d+))?)?$/.exec(text);
|
||
if (!match) return { ok: false, error: `"${input}" isn't a Bible reference (e.g. John 3:16-18)` };
|
||
const [, name = "", a = "", b, c, d] = match;
|
||
const book = findBibleBook(name);
|
||
if (!book) return { ok: false, error: `"${name.trim()}" isn't a book of the Bible` };
|
||
|
||
let chapter: number, verse: number, endChapter: number, endVerse: number;
|
||
if (b === undefined) {
|
||
// "Jude 3" / "Jude 3-5": a verse in a one-chapter book. "John 3" alone is a whole chapter.
|
||
if (book.chapters !== 1) return { ok: false, error: `Give the verses as well as the chapter (e.g. ${book.en} ${a}:1-12)` };
|
||
if (d !== undefined) return { ok: false, error: `"${input}" isn't a Bible reference (e.g. ${book.en} 1:3-5)` };
|
||
chapter = 1; verse = Number(a); endChapter = 1; endVerse = c === undefined ? verse : Number(c);
|
||
} else {
|
||
chapter = Number(a); verse = Number(b);
|
||
if (d !== undefined) { endChapter = Number(c); endVerse = Number(d); }
|
||
else { endChapter = chapter; endVerse = c === undefined ? verse : Number(c); }
|
||
}
|
||
if (chapter > book.chapters || endChapter > book.chapters) {
|
||
return { ok: false, error: `${book.en} has ${book.chapters} chapter${book.chapters === 1 ? "" : "s"}` };
|
||
}
|
||
const parsed = passageLocationSchema.safeParse({ book: book.code, chapter, verse, endChapter, endVerse });
|
||
if (!parsed.success) return { ok: false, error: parsed.error.issues[0]?.message ?? "Not a passage" };
|
||
return { ok: true, passage: parsed.data };
|
||
}
|
||
|
||
/** `John 3:16–18`, `Johannes 3:16`, `Romans 8:38–9:1`, in the translation's language. */
|
||
export function formatBibleReference(p: PassageLocation, language: "en" | "af" = "en"): string {
|
||
const book = bibleBook(p.book);
|
||
const start = `${language === "af" ? book.af : book.en} ${p.chapter}:${p.verse}`;
|
||
if (p.endChapter !== p.chapter) return `${start}–${p.endChapter}:${p.endVerse}`;
|
||
return p.endVerse === p.verse ? start : `${start}–${p.endVerse}`;
|
||
}
|
||
|
||
/** How many verses, when the passage stays in one chapter (null across chapters: verse counts per chapter aren't kept). */
|
||
export function passageVerseCount(p: PassageLocation): number | null {
|
||
return p.endChapter === p.chapter ? p.endVerse - p.verse + 1 : null;
|
||
}
|
||
|
||
/**
|
||
* "Read in YouVersion": `https://www.bible.com/bible/116/JHN.3.16-18.NLT`. YouVersion opens its
|
||
* app instead where it's installed. Across chapters it opens at the first verse's chapter.
|
||
*/
|
||
export function youVersionUrl(p: PassageLocation, translation: BibleTranslationId): string {
|
||
const t = bibleTranslation(translation);
|
||
const verses = p.endChapter === p.chapter && p.endVerse !== p.verse ? `${p.verse}-${p.endVerse}` : `${p.verse}`;
|
||
return `https://www.bible.com/bible/${t.youVersionId}/${p.book}.${p.chapter}.${verses}.${t.id}`;
|
||
}
|