From ba5c7bc25fe0fc23e8c6aa79c2545878ac8f785f Mon Sep 17 00:00:00 2001 From: K1ngfish3r Date: Thu, 24 Apr 2025 17:14:33 +0500 Subject: [PATCH] Readfrom: encase text nodes with p (#1647) * readfrom encase p * increment version number --- src/plugins/english/readfrom.ts | 42 +++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/src/plugins/english/readfrom.ts b/src/plugins/english/readfrom.ts index c80efd0..8a1ee99 100644 --- a/src/plugins/english/readfrom.ts +++ b/src/plugins/english/readfrom.ts @@ -9,7 +9,7 @@ class ReadFromPlugin implements Plugin.PluginBase { name = 'Read From Net'; icon = 'src/en/readfrom/icon.png'; site = 'https://readfrom.net'; - version = '1.0.1'; + version = '1.0.2'; filters: Filters | undefined = undefined; imageRequestInit?: Plugin.ImageRequestInit | undefined = undefined; @@ -156,12 +156,44 @@ class ReadFromPlugin implements Plugin.PluginBase { } async parseChapter(chapterPath: string): Promise { - let data = await fetchApi('https://readfrom.net/' + chapterPath); - let text = await data.text(); - let loadedCheerio = loadCheerio(text); + const data = await fetchApi('https://readfrom.net/' + chapterPath); + const text = await data.text(); + const loadedCheerio = loadCheerio(text); loadedCheerio('#textToRead > span:empty').remove(); loadedCheerio('#textToRead > center').remove(); - return loadedCheerio('#textToRead').html() || ''; + + const textToRead = loadedCheerio('#textToRead'); + + let paragraph: string[] = []; + const chapterHtml: string[] = []; + + textToRead.contents().each((_, element) => { + switch (element.type) { + case 'text': + const content = element.data.trim(); + if (content) { + paragraph.push(content); + } + break; + + case 'tag': + if (paragraph.length > 0) { + chapterHtml.push(`

${paragraph.join(' ')}

`); + paragraph = []; + } + if (element.tagName !== 'br') { + chapterHtml.push(loadedCheerio.html(element)); + } + break; + } + }); + + // Close any remaining paragraph + if (paragraph.length > 0) { + chapterHtml.push(`

${paragraph.join(' ')}

`); + } + + return chapterHtml.join(''); } async searchNovels(searchTerm: string, pageNo: number) {