diff --git a/src/plugins/english/royalroad.ts b/src/plugins/english/royalroad.ts index f211f71..4d4ea6c 100644 --- a/src/plugins/english/royalroad.ts +++ b/src/plugins/english/royalroad.ts @@ -8,7 +8,7 @@ import { isUrlAbsolute } from '@libs/isAbsoluteUrl'; class RoyalRoad implements Plugin.PluginBase { id = 'royalroad'; name = 'Royal Road'; - version = '2.2.1'; + version = '2.2.2'; icon = 'src/en/royalroad/icon.png'; site = 'https://www.royalroad.com/'; @@ -45,8 +45,10 @@ class RoyalRoad implements Plugin.PluginBase { } }, }); + parser.write(html); parser.end(); + return novels; } @@ -111,10 +113,14 @@ class RoyalRoad implements Plugin.PluginBase { } let state: ParsingState = ParsingState.Idle; - let scriptContent = ''; let statusText = ''; let statusSpanCounter = 0; + + const nameParts: string[] = []; + const summaryParts: string[] = []; + const scriptContentParts: string[] = []; const genreArray: string[] = []; + let chapterJson: ChapterEntry[] = []; let volumeJson: VolumeEntry[] = []; @@ -137,7 +143,16 @@ class RoyalRoad implements Plugin.PluginBase { case 'div': if (attribs['class'] === 'description') { state = ParsingState.InDescription; - novel.summary = ''; + } + break; + case 'hr': + if (state === ParsingState.InDescription) { + summaryParts.push('\n---\n'); + } + break; + case 'br': + if (state === ParsingState.InDescription) { + summaryParts.push('\n\n'); } break; case 'span': @@ -161,7 +176,6 @@ class RoyalRoad implements Plugin.PluginBase { break; case 'script': state = ParsingState.InScript; - scriptContent = ''; break; } }, @@ -171,13 +185,13 @@ class RoyalRoad implements Plugin.PluginBase { switch (state) { case ParsingState.InTitle: - novel.name += text; + nameParts.push(text); break; case ParsingState.InAuthor: novel.author = trimmedText; break; case ParsingState.InDescription: - novel.summary += text; + summaryParts.push(text); break; case ParsingState.InStatusSpan: statusText = trimmedText; @@ -186,7 +200,7 @@ class RoyalRoad implements Plugin.PluginBase { genreArray.push(trimmedText); break; case ParsingState.InScript: - scriptContent += text; + scriptContentParts.push(text); break; } }, @@ -194,7 +208,7 @@ class RoyalRoad implements Plugin.PluginBase { switch (name) { case 'h1': if (state === ParsingState.InTitle) { - novel.name = novel.name.trim(); + novel.name = nameParts.join('').trim(); state = ParsingState.Idle; } break; @@ -205,11 +219,20 @@ class RoyalRoad implements Plugin.PluginBase { state = ParsingState.Idle; } break; + case 'p': + if (state === ParsingState.InDescription) { + summaryParts.push('\n\n'); + } + break; case 'div': if (state === ParsingState.InDescription) { - if (novel.summary) { - novel.summary = novel.summary.trim(); - } + novel.summary = summaryParts + .join('') + .replace(/ /g, ' ') + .replace(/\s*\n\s*/g, '\n') + .replace(/\n{3,}/g, '\n\n') + .trim(); + summaryParts.length = 0; state = ParsingState.Idle; } break; @@ -224,6 +247,7 @@ class RoyalRoad implements Plugin.PluginBase { case 'script': if (state === ParsingState.InScript) { state = ParsingState.Idle; + const scriptContent = scriptContentParts.join(''); const chapterMatch = scriptContent.match( /window\.chapters\s*=\s*(\[.*?\]);/, ); @@ -272,9 +296,6 @@ class RoyalRoad implements Plugin.PluginBase { }; }); }, - onerror(error) { - console.error('HTML Parser Error:', error); - }, }); parser.write(html); @@ -298,13 +319,13 @@ class RoyalRoad implements Plugin.PluginBase { let stateDepth = 0; let depth = 0; - let chapterHtml = ''; - let notesHtml = ''; - let beforeNotes = ''; - let afterNotes = ''; + const chapterHtmlParts: string[] = []; + const notesHtmlParts: string[] = []; + const beforeNotesParts: string[] = []; + const afterNotesParts: string[] = []; let isBeforeChapter = true; - const match = html.match(/