@@ -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