From de03ab064b5ced576269ebb087b5a808e160ec44 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 11 Jan 2026 05:04:09 +0000 Subject: [PATCH] fix(chapters): remove duplicate chapter title lines in content Fixed issue where chapter marker lines appeared twice: 1. Once in the navigation bar as the chapter title 2. Again at the start of the chapter content Changes: - Modified detectChapters() to exclude marker lines from chapter text - When a chapter marker is found, content now starts from the next line - Special handling for when first line is a chapter marker - Chapter title is extracted but not duplicated in the content This eliminates the "two pages per chapter" issue where users saw an empty page with just the title, then the title repeated with the actual content. --- src/lib/chapterDetection.ts | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/lib/chapterDetection.ts b/src/lib/chapterDetection.ts index 305baed..b6e8eb9 100644 --- a/src/lib/chapterDetection.ts +++ b/src/lib/chapterDetection.ts @@ -35,21 +35,27 @@ export function detectChapters(text: string, maxChunkSize = 50000): Chapter[] { // Check if this line is a chapter marker const isChapterMarker = chapterPatterns.some(pattern => pattern.test(trimmed)); - if (isChapterMarker && index > 0) { - // Save the previous chapter - const chapterText = lines.slice(currentChapterStart, index).join('\n'); - if (chapterText.trim().length > 0) { - chapters.push({ - title: currentChapterTitle, - startIndex: currentChapterStart, - endIndex: index, - text: chapterText, - }); - } + if (isChapterMarker) { + if (index === 0) { + // First line is a chapter marker - use it as title and start content from next line + currentChapterTitle = trimmed || 'Chapter 1'; + currentChapterStart = 1; + } else { + // Save the previous chapter + const chapterText = lines.slice(currentChapterStart, index).join('\n'); + if (chapterText.trim().length > 0) { + chapters.push({ + title: currentChapterTitle, + startIndex: currentChapterStart, + endIndex: index, + text: chapterText, + }); + } - // Start new chapter - currentChapterStart = index; - currentChapterTitle = trimmed || `Chapter ${chapters.length + 1}`; + // Start new chapter (skip the marker line itself) + currentChapterStart = index + 1; + currentChapterTitle = trimmed || `Chapter ${chapters.length + 1}`; + } } });