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.
This commit is contained in:
Claude 2026-01-11 05:04:09 +00:00
parent b13bf96b3b
commit de03ab064b
No known key found for this signature in database

View file

@ -35,21 +35,27 @@ export function detectChapters(text: string, maxChunkSize = 50000): Chapter[] {
// Check if this line is a chapter marker // Check if this line is a chapter marker
const isChapterMarker = chapterPatterns.some(pattern => pattern.test(trimmed)); const isChapterMarker = chapterPatterns.some(pattern => pattern.test(trimmed));
if (isChapterMarker && index > 0) { if (isChapterMarker) {
// Save the previous chapter if (index === 0) {
const chapterText = lines.slice(currentChapterStart, index).join('\n'); // First line is a chapter marker - use it as title and start content from next line
if (chapterText.trim().length > 0) { currentChapterTitle = trimmed || 'Chapter 1';
chapters.push({ currentChapterStart = 1;
title: currentChapterTitle, } else {
startIndex: currentChapterStart, // Save the previous chapter
endIndex: index, const chapterText = lines.slice(currentChapterStart, index).join('\n');
text: chapterText, if (chapterText.trim().length > 0) {
}); chapters.push({
} title: currentChapterTitle,
startIndex: currentChapterStart,
endIndex: index,
text: chapterText,
});
}
// Start new chapter // Start new chapter (skip the marker line itself)
currentChapterStart = index; currentChapterStart = index + 1;
currentChapterTitle = trimmed || `Chapter ${chapters.length + 1}`; currentChapterTitle = trimmed || `Chapter ${chapters.length + 1}`;
}
} }
}); });