Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions plugins/multisrc/novelfire/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"options": {
"lang": "English",
"minorVer": 4,
"versionIncrement": 5
"versionIncrement": 6
}
},
{
Expand All @@ -15,7 +15,7 @@
"sourceName": "Novel Phoenix",
"options": {
"lang": "English",
"versionIncrement": 2
"versionIncrement": 3
}
}
]
90 changes: 72 additions & 18 deletions plugins/multisrc/novelfire/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])));
Comment on lines +39 to +41

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Chapter numbers still go missing

formatChapterTitle mistakes numbered subtitles for chapter numbers. For chapter 1 with the subtitle 1st Nightmare, the s passes the non-digit check, so the function returns 1st Nightmare without Chapter 1. This leaves the display-name fix incomplete for titles starting with an ordinal. Require a chapter-number separator rather than accepting any non-digit character.


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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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();
}
}
Expand Down
Loading