fix(route/rfi): parsing result logics
This commit is contained in:
+25
-9
@@ -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';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user