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:
parent
b13bf96b3b
commit
de03ab064b
1 changed files with 20 additions and 14 deletions
|
|
@ -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}`;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue