diff --git a/plugins/multisrc/novelfire/sources.json b/plugins/multisrc/novelfire/sources.json index e58eb2afb0..eb3b5fc52f 100644 --- a/plugins/multisrc/novelfire/sources.json +++ b/plugins/multisrc/novelfire/sources.json @@ -6,7 +6,7 @@ "options": { "lang": "English", "minorVer": 4, - "versionIncrement": 5 + "versionIncrement": 6 } }, { @@ -15,7 +15,7 @@ "sourceName": "Novel Phoenix", "options": { "lang": "English", - "versionIncrement": 2 + "versionIncrement": 3 } } ] diff --git a/plugins/multisrc/novelfire/template.ts b/plugins/multisrc/novelfire/template.ts index e9811587f5..ff25d99953 100644 --- a/plugins/multisrc/novelfire/template.ts +++ b/plugins/multisrc/novelfire/template.ts @@ -21,6 +21,39 @@ export type NovelFireMetadata = { filters?: Filters; }; +const CHAPTER_PREFIX_REGEX = /^(chapter|ch\.?)\s*\d+/i; +const LEADING_PUNCT_REGEX = /^[–—\-\s:]+/; +const INVISIBLE_CHARS_REGEX = /[\u200B-\u200D\uFEFF]/g; + +function formatChapterTitle( + rawTitle: string, + chapterPrefixOrNum: number | string, +): string { + const cleanTitle = rawTitle.replace(LEADING_PUNCT_REGEX, '').trim(); + const prefixStr = String(chapterPrefixOrNum).trim(); + const numMatch = prefixStr.match(/\d+/); + const num = numMatch ? numMatch[0] : ''; + + const hasChapterPrefix = + CHAPTER_PREFIX_REGEX.test(cleanTitle) || + (num.length > 0 && + cleanTitle.startsWith(num) && + (cleanTitle.length === num.length || !/\d/.test(cleanTitle[num.length]))); + + if (hasChapterPrefix) { + return cleanTitle; + } + + const prefix = + typeof chapterPrefixOrNum === 'number' + ? `Chapter ${chapterPrefixOrNum}` + : prefixStr.startsWith('Ch') || prefixStr.startsWith('Chapter') + ? prefixStr + : `Chapter ${prefixStr}`; + + return cleanTitle ? `${prefix} ${cleanTitle}` : prefix; +} + export class NovelFirePlugin implements Plugin.PagePlugin { id: string; name: string; @@ -219,21 +252,22 @@ export class NovelFirePlugin implements Plugin.PagePlugin { return (JSON.parse(body).data || []) .flatMap( (idx: { title?: string; slug: string; n_sort: string | number }) => { - const name = load(idx.title || idx.slug) - .text() - .replace(/[\u200B-\u200D\uFEFF]/g, '') - .trim(); const num = Number(idx.n_sort); - - return name && !isNaN(num) - ? [ - { - name, - path: `${novelPath}/chapter-${num}`, - chapterNumber: num, - }, - ] - : []; + if (isNaN(num)) return []; + + const raw = idx.title || idx.slug || ''; + const decoded = + raw.includes('<') || raw.includes('&') ? load(raw).text() : raw; + const cleanRaw = decoded.replace(INVISIBLE_CHARS_REGEX, '').trim(); + const name = formatChapterTitle(cleanRaw, num); + + return [ + { + name, + path: `${novelPath}/chapter-${num}`, + chapterNumber: num, + }, + ]; }, ) .sort( @@ -420,12 +454,32 @@ export class NovelFirePlugin implements Plugin.PagePlugin { const chapters = loadedCheerio('.chapter-list li') .map((_, ele) => { - const chapterName = - loadedCheerio(ele).find('a').attr('title') || 'No Title Found'; - const chapterPath = loadedCheerio(ele).find('a').attr('href'); + const $ele = loadedCheerio(ele); + const $a = $ele.find('a'); + const chapterPath = $a.attr('href'); if (!chapterPath) return null; + const chapterNo = $ele.find('.chapter-no').text().trim(); + const chapterTitle = $ele.find('.chapter-title').text().trim(); + const attrTitle = $a.attr('title')?.trim() || ''; + + let chapterName = ''; + if (chapterNo && chapterTitle) { + chapterName = formatChapterTitle(chapterTitle, chapterNo); + } else if (chapterNo && attrTitle) { + chapterName = formatChapterTitle(attrTitle, chapterNo); + } else if (attrTitle) { + chapterName = attrTitle; + } else if (chapterNo) { + chapterName = chapterNo; + } else if (chapterTitle) { + chapterName = chapterTitle; + } else { + chapterName = + $a.text().replace(/\s+/g, ' ').trim() || 'No Title Found'; + } + return { name: chapterName, path: new URL(chapterPath, this.site).pathname.substring(1), @@ -473,7 +527,7 @@ export class NovelFirePlugin implements Plugin.PagePlugin { ); for (const ele of odds.toArray()) { const tag = ele.name.toString(); - if (tag.length > 5 && tag.substring(0, 1) == 'nf') { + if (tag.length > 5 && tag.startsWith('nf')) { loadedCheerio(ele).remove(); } }