From f85a15988188752e28b1b554d368a1480ab82ee8 Mon Sep 17 00:00:00 2001 From: pseudoyu Date: Sat, 28 Mar 2026 15:29:55 +0800 Subject: [PATCH] fix(route/rfi): parsing result logics --- lib/routes/rfi/news.ts | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/lib/routes/rfi/news.ts b/lib/routes/rfi/news.ts index d1fe744cc..163ffd9fa 100644 --- a/lib/routes/rfi/news.ts +++ b/lib/routes/rfi/news.ts @@ -59,22 +59,38 @@ async function handler(ctx) { content('.m-interstitial, .m-em-quote svg, .o-self-promo').remove(); - const ldJson = JSON.parse(content('script[type="application/ld+json"]').text() || '[]').find((x) => x['@type'] === 'NewsArticle'); + // Reason: pages may have multiple ld+json script tags; iterating separately + // avoids concatenation that produces invalid JSON like "{...}{...}" + let ldJson; + for (const el of content('script[type="application/ld+json"]').toArray()) { + try { + const parsed = JSON.parse(content(el).text()); + const candidates = Array.isArray(parsed) ? parsed : [parsed]; + ldJson = candidates.find((x) => x['@type'] === 'NewsArticle'); + if (ldJson) { + break; + } + } catch { + // skip malformed ld+json blocks + } + } item.description = content('.t-content__chapo').prop('outerHTML') + content('.t-content__main-media').prop('outerHTML') + content('.t-content__body').html(); - item.pubDate = parseDate(ldJson?.datePublished); - item.updated = parseDate(ldJson?.dateModified); - item.author = ldJson?.author.map((author) => author.name).join(', '); + item.pubDate = ldJson?.datePublished ? parseDate(ldJson.datePublished) : undefined; + item.updated = ldJson?.dateModified ? parseDate(ldJson.dateModified) : undefined; + item.author = ldJson?.author?.map((author) => author.name).join(', '); item.category = ldJson?.keywords; if (ldJson?.audio) { item.itunes_item_image = ldJson.audio.thumbnailUrl; // TODO: Use Temporal.Duration when https://tc39.es/proposal-temporal/ is GA - item.itunes_duration = ldJson.audio.duration - .match(/P0DT(\d+)H(\d+)M(\d+)S/) - .slice(1) - .map((x) => Number.parseInt(x)) - .reduce((a, b) => a * 60 + b); + const durationMatch = ldJson.audio.duration?.match(/P0DT(\d+)H(\d+)M(\d+)S/); + if (durationMatch) { + item.itunes_duration = durationMatch + .slice(1) + .map((x) => Number.parseInt(x)) + .reduce((a, b) => a * 60 + b); + } item.enclosure_url = ldJson.audio.contentUrl; item.enclosure_type = 'audio/mpeg'; }