From 9114b9a06a6ff668827c4b0fb68fb549d2c50470 Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Mon, 24 Mar 2025 14:08:43 +0100 Subject: Support multiple JSON fragments in HTML+XPath+JSON mode (#7369) * Support multiple JSON fragments in HTML+XPath+JSON mode fix https://github.com/FreshRSS/FreshRSS/discussions/7352#discussioncomment-12295475 E.g. HTML with one `` per item. * Better help messages --- app/Models/Feed.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'app/Models') diff --git a/app/Models/Feed.php b/app/Models/Feed.php index 57bf9d7a4..072948e1b 100644 --- a/app/Models/Feed.php +++ b/app/Models/Feed.php @@ -725,8 +725,25 @@ class FreshRSS_Feed extends Minz_Model { } $xpath = new DOMXPath($doc); - $json = @$xpath->evaluate('normalize-space(' . $xPathToJson . ')'); - return is_string($json) ? $json : null; + $jsonFragments = @$xpath->evaluate($xPathToJson); + if ($jsonFragments === false) { + return null; + } + if (is_string($jsonFragments)) { + return $jsonFragments; + } + if ($jsonFragments instanceof DOMNodeList && $jsonFragments->length > 0) { + // If the result is a list, then aggregate as a JSON array + $result = []; + foreach ($jsonFragments as $node) { + $json = json_decode($node->textContent, true); + if (json_last_error() === JSON_ERROR_NONE && is_array($json)) { + $result[] = $json; + } + } + return json_encode($result, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ?: null; + } + return null; } public function loadJson(): ?\SimplePie\SimplePie { -- cgit v1.2.3