diff --git a/lib/routes/easylife365/changelogs.ts b/lib/routes/easylife365/changelogs.ts index 565571a101b1..d3952eab2810 100644 --- a/lib/routes/easylife365/changelogs.ts +++ b/lib/routes/easylife365/changelogs.ts @@ -1,40 +1,37 @@ +import type { CheerioAPI } from 'cheerio'; import { load } from 'cheerio'; +import pMap from 'p-map'; +import cache from '@/utils/cache'; import got from '@/utils/got'; +import { parseDate } from '@/utils/parse-date'; -function parseDateFromLink(link: string) { - const match = link.match(/\/changelogs\/(\d{4})\/(\d{2})\/(\d{2})\//); - if (!match) { - return; - } +const rootUrl = 'https://docs.easylife365.cloud'; +const changelogPathPattern = /^\/changelogs\/(\d{4})\/(\d{2})\/(\d{2})\/([^/]+)\/[^/]+\/?$/; - const [, yyyy, mm, dd] = match; - return new Date(`${yyyy}-${mm}-${dd}T00:00:00Z`).toUTCString(); -} - -function cleanDescription(text: string) { - return text - .replaceAll(/\s+/g, ' ') - .replace(/^←\s*Back to Changelog/, '') - .replace(/Have questions\? Reach out to us for more information\.?Contact Us$/i, '') - .trim(); -} - -function getProductFromLink(link: string) { - const match = link.match(/\/changelogs\/\d{4}\/\d{2}\/\d{2}\/([^/]+)\//); - if (!match) { - return ''; - } - - const productMap: Record = { - collab: 'Collaboration', - identity: 'Identity', - mail: 'Mail', - meet: 'Meet', - tipster: 'Tipster', - }; +const productMap: Record = { + collab: 'Collaboration', + identity: 'Identity', + mail: 'Mail', + meet: 'Meet', + tipster: 'Tipster', +}; - return productMap[match[1]] ?? match[1]; +// Turns the Docusaurus post body into clean HTML for feed readers. +// Relative links and image URLs are resolved by the output middleware. +function getDescription($: CheerioAPI) { + const body = $('#__blog-post-container').first(); + body.find('a.hash-link').remove(); + body.find('[class]').removeAttr('class'); + body.find('img').removeAttr('decoding loading'); + + const availability = $('.product-intro li') + .toArray() + .map((el) => $(el).text().trim()) + .filter(Boolean); + const availabilityHtml = availability.length ? `

${availability.join(' · ')}

` : ''; + + return `${availabilityHtml}${body.html()?.trim() ?? ''}`; } export const route = { @@ -44,41 +41,38 @@ export const route = { example: '/easylife365/changelogs', maintainers: ['dein-github-name'], handler: async () => { - const rootUrl = 'https://docs.easylife365.cloud'; const currentUrl = `${rootUrl}/changelogs`; const response = await got(currentUrl); const $ = load(response.data); - const links = [ - ...new Set( - $('a[href*="/changelogs/"]') - .toArray() - .map((el) => $(el).attr('href')) - .filter((href): href is string => typeof href === 'string' && href.length > 0) - .map((href) => new URL(href, rootUrl).toString()) - ), - ]; - - const item = await Promise.all( - links.map(async (link) => { - const detailResponse = await got(link); - const detail$ = load(detailResponse.data); - - const rawTitle = detail$('main h1').first().text().trim() || detail$('h1').first().text().trim() || detail$('title').first().text().trim() || link.split('/').at(-1) || 'Untitled'; - - const product = getProductFromLink(link); - const title = product ? `${product}: ${rawTitle}` : rawTitle; - - const rawDescription = detail$('main').first().text() || detail$('article').first().text() || ''; - - return { - title, - link, - description: cleanDescription(rawDescription), - pubDate: parseDateFromLink(link), - }; - }) + const posts = new Map(); + for (const el of $('a[href*="/changelogs/"]').toArray()) { + const url = new URL($(el).attr('href') ?? '', rootUrl); + const match = url.pathname.match(changelogPathPattern); + if (url.origin === rootUrl && match) { + posts.set(`${url.origin}${url.pathname.replace(/\/$/, '')}`, match); + } + } + + const item = await pMap( + [...posts], + ([link, [, yyyy, mm, dd, product]]) => + cache.tryGet(link, async () => { + const detailResponse = await got(link); + const detail$ = load(detailResponse.data); + + const rawTitle = detail$('main h1').first().text().trim() || detail$('title').first().text().trim() || link.split('/').at(-1); + const datetime = detail$('main time[datetime]').first().attr('datetime') ?? `${yyyy}-${mm}-${dd}T00:00:00Z`; + + return { + title: `${productMap[product] ?? product}: ${rawTitle}`, + link, + description: getDescription(detail$), + pubDate: parseDate(datetime), + }; + }), + { concurrency: 5 } ); return {