From 5b23ed35b6085c264ca6de014a26b6cc4f497016 Mon Sep 17 00:00:00 2001 From: Huang Xin Date: Fri, 10 Oct 2025 12:45:50 +0800 Subject: [PATCH] txt: properly detect chapters in TXT files with dot-number format, closes #2188 (#2191) --- apps/readest-app/src/utils/txt.ts | 32 ++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/apps/readest-app/src/utils/txt.ts b/apps/readest-app/src/utils/txt.ts index ccea35c4..7594cb09 100644 --- a/apps/readest-app/src/utils/txt.ts +++ b/apps/readest-app/src/utils/txt.ts @@ -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 => {