txt: properly detect chapters in TXT files with dot-number format, closes #2188 (#2191)

This commit is contained in:
Huang Xin
2025-10-10 12:45:50 +08:00
committed by GitHub
parent 42c8702704
commit 5b23ed35b6
+29 -3
View File
@@ -133,9 +133,35 @@ export class TxtToEpubConverter {
),
);
} else {
chapterRegexps.push(
/(?:^|\n|\s)(?:(Chapter|Part)\s+(\d+|[IVXLCDM]+)(?:[:.\-–—]?\s+[^\n]*)?|(?:Prologue|Epilogue|Introduction|Foreword)(?:[:.\-–—]?\s+[^\n]*)?)(?=\s|$)/gi,
);
const chapterKeywords = ['Chapter', 'Part', 'Section', 'Book', 'Volume', 'Act'];
const prefaceKeywords = [
'Prologue',
'Epilogue',
'Introduction',
'Foreword',
'Preface',
'Afterword',
];
const numberPattern = String.raw`(\d+|[IVXLCDM]+)`;
const dotNumberPattern = String.raw`\.\d{1,4}`;
const titlePattern = String.raw`[^\n]{0,50}`;
const normalChapterPattern = chapterKeywords
.map(
(k) =>
String.raw`${k}\s*(?:${numberPattern}|${dotNumberPattern})(?:[:.\-–—]?\s*${titlePattern})?`,
)
.join('|');
const prefacePattern = prefaceKeywords
.map((k) => String.raw`${k}(?:[:.\-–—]?\s*${titlePattern})?`)
.join('|');
const combinedPattern = String.raw`(?:^|\n|\s)(?:${normalChapterPattern}|${prefacePattern})(?=\s|$)`;
chapterRegexps.push(new RegExp(combinedPattern, 'gi'));
}
const formatSegment = (segment: string): string => {