Skip to content

Commit 5e19ac2

Browse files
committed
fix(arabic): address review on hidden text, promo-only chapter bodies and status labels
- galaxynovels: drop [data-wor-decoy] and [hidden] elements before choosing the chapter body. - madara: choose the first chapter candidate that still has text or images after the source's customJs cleanup; rejected candidates are cleaned on a throwaway copy of the page. - madara: match NHV status labels exactly after Unicode/diacritic/whitespace normalization, reading only the value element so "غير مكتملة" is no longer Completed.
1 parent 47e9d6b commit 5e19ac2

2 files changed

Lines changed: 73 additions & 23 deletions

File tree

‎plugins/arabic/galaxynovels.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,10 @@ class GalaxyNovels implements Plugin.PluginBase {
292292
const url = `${this.baseUrl}${chapterPath}`;
293293
const html = await this.fetchHtml(url);
294294
const $ = loadCheerio(html);
295+
// The page ships decoy and hidden elements that still carry text; drop
296+
// them first so a candidate that only has hidden text does not count as
297+
// the chapter body.
298+
$('[data-wor-decoy], [hidden]').remove();
295299

296300
// The body is server-rendered inside the article marked up as
297301
// https://schema.org/Chapter, as the element with itemprop="text". Pick

‎plugins/multisrc/madara/template.ts‎

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,37 @@ export class MadaraPlugin implements Plugin.PluginBase {
388388
.map((i, el) => loadedCheerio(el).text())
389389
.get()
390390
.join(', ');
391-
const nhvStatus = loadedCheerio('.nhv-novel-status').text().trim();
391+
// The element holds an icon plus a "status:" caption (<span>) and the
392+
// value (<strong>); only the value is the label to match.
393+
const nhvStatus = loadedCheerio('.nhv-novel-status')
394+
.clone()
395+
.children('span')
396+
.remove()
397+
.end()
398+
.text()
399+
.normalize('NFKC')
400+
.split('')
401+
.filter(ch => {
402+
// Drop tatweel and Arabic diacritics.
403+
const code = ch.charCodeAt(0);
404+
return (
405+
code !== 0x640 &&
406+
code !== 0x670 &&
407+
!(code >= 0x64b && code <= 0x65f)
408+
);
409+
})
410+
.join('')
411+
.replace(/\s+/g, ' ')
412+
.trim();
392413
if (!novel.status && nhvStatus) {
393-
// Only recognised labels are mapped; unknown values stay unset.
394-
if (nhvStatus.includes('مستمرة')) novel.status = NovelStatus.Ongoing;
395-
else if (nhvStatus.includes('متوقفة'))
396-
novel.status = NovelStatus.OnHiatus;
397-
else if (nhvStatus.includes('مكتملة'))
398-
novel.status = NovelStatus.Completed;
414+
// Only exact labels are mapped (not substrings, so that "غير مكتملة",
415+
// "not completed", is not read as "مكتملة"); anything else stays unset.
416+
const nhvStatusMap: Record<string, NovelStatus> = {
417+
مستمرة: NovelStatus.Ongoing,
418+
متوقفة: NovelStatus.OnHiatus,
419+
مكتملة: NovelStatus.Completed,
420+
};
421+
novel.status = nhvStatusMap[nhvStatus] ?? novel.status;
399422
}
400423
if (!novel.rating) {
401424
const nhvRating = parseFloat(
@@ -615,10 +638,36 @@ export class MadaraPlugin implements Plugin.PluginBase {
615638
false,
616639
);
617640

641+
// The source's customJs cleanup (e.g. removing promos) runs on the chosen
642+
// chapter element. It receives the page and the element it should clean.
643+
/* eslint-disable @typescript-eslint/no-unused-vars -- used by customJs */
644+
const cleanChapter = (
645+
loadedCheerio: CheerioAPI,
646+
chapterText: Cheerio<AnyNode>,
647+
) => {
648+
if (this.options?.customJs) {
649+
try {
650+
// CustomJS HERE
651+
} catch (error) {
652+
console.error('Error executing customJs:', error);
653+
throw error;
654+
}
655+
}
656+
};
657+
/* eslint-enable @typescript-eslint/no-unused-vars */
658+
const hasContent = (element: Cheerio<AnyNode>) =>
659+
!!element.text().trim() || element.find('img').length > 0;
660+
618661
// A cheerio selection is always truthy, so walk the candidates and keep
619-
// the first one that actually holds text or images (illustration and comic
620-
// chapters have no text but did render before).
621-
let chapterText = loadedCheerio('.__no-chapter-content__');
662+
// the first one that still holds text or images (illustration and comic
663+
// chapters have no text but did render before) after the source's cleanup
664+
// ran: a promo-only candidate must not win over the real chapter body.
665+
// The cleanup is not idempotent for every source, so a rejected candidate
666+
// is cleaned on a throwaway copy of the page and the next one starts from
667+
// the pristine HTML.
668+
const pristineHtml = this.options?.customJs ? loadedCheerio.html() : '';
669+
let page = loadedCheerio;
670+
let chapterText = page('.__no-chapter-content__');
622671
for (const selector of [
623672
'.text-left',
624673
'.text-right',
@@ -629,20 +678,17 @@ export class MadaraPlugin implements Plugin.PluginBase {
629678
'.entry-content',
630679
'.c-blog-post > div > div:nth-child(2)',
631680
]) {
632-
const candidate = loadedCheerio(selector);
633-
if (candidate.text().trim() || candidate.find('img').length) {
634-
chapterText = candidate;
635-
break;
636-
}
637-
}
638-
639-
if (this.options?.customJs) {
640-
try {
641-
// CustomJS HERE
642-
} catch (error) {
643-
console.error('Error executing customJs:', error);
644-
throw error;
681+
const candidate = page(selector);
682+
if (!hasContent(candidate)) continue;
683+
if (this.options?.customJs) {
684+
cleanChapter(page, candidate);
685+
if (!hasContent(candidate)) {
686+
page = parseHTML(pristineHtml);
687+
continue;
688+
}
645689
}
690+
chapterText = candidate;
691+
break;
646692
}
647693

648694
if (!chapterText.length)

0 commit comments

Comments
 (0)