From b2096c67ae5955804077d43b3cd99e23cce35f6c Mon Sep 17 00:00:00 2001 From: Ryderjj89 Date: Mon, 15 Sep 2025 17:48:10 -0400 Subject: [PATCH] Fix chapter loading issue caused by double padding - Backend now properly handles chapter names that already include 'Chapter_' prefix - Prevents double padding that was causing file not found errors - Maintains backward compatibility for legacy chapter number requests - Chapter loading should now work correctly alongside search functionality --- backend/src/index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/src/index.js b/backend/src/index.js index 25673bd3..0f97fc14 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -126,9 +126,18 @@ app.get('/books/:book', async (req, res) => { app.get('/books/:book/:chapter', async (req, res) => { try { const { book, chapter } = req.params; - // Format chapter number with leading zero if needed (e.g., "1" -> "01") - const paddedChapter = chapter.padStart(2, '0'); - const chapterPath = path.join(BIBLE_DATA_DIR, book, `Chapter_${paddedChapter}.md`); + + // Check if chapter already includes "Chapter_" prefix (from frontend) + let chapterFileName; + if (chapter.startsWith('Chapter_')) { + chapterFileName = `${chapter}.md`; + } else { + // Legacy support: if just a number, format with leading zero + const paddedChapter = chapter.padStart(2, '0'); + chapterFileName = `Chapter_${paddedChapter}.md`; + } + + const chapterPath = path.join(BIBLE_DATA_DIR, book, chapterFileName); const content = await readMarkdownFile(chapterPath); res.type('text/markdown').send(content);