Files
RSSHub/lib/routes/science/utils.tsx
T
dependabot[bot]andTonyRL 6bce9493b5 chore(deps-dev): bump eslint-plugin-unicorn from 71.1.0 to 72.0.0 (#22723)
* chore(deps-dev): bump eslint-plugin-unicorn from 71.1.0 to 72.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 71.1.0 to 72.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v71.1.0...v72.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 72.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix prefer-dom-node-html-methods

* style: fix prefer-split-limit

* style: fix no-unnecessary-string-trim

* style: fix no-multiple-promise-resolver-calls

* style: add v72 preset

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: TonyRL <TonyRL@users.noreply.github.com>
2026-07-15 23:11:15 +08:00

73 lines
2.6 KiB
TypeScript

import { load } from 'cheerio';
import { raw } from 'hono/html';
import { renderToString } from 'hono/jsx/dom/server';
import cache from '@/utils/cache';
import { parseDate } from '@/utils/parse-date';
const baseUrl = 'https://www.science.org';
const fetchDesc = (list, context) =>
Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const page = await context.newPage();
await page.route('**/*', (route) => {
const request = route.request();
request.resourceType() === 'document' || request.resourceType() === 'script' ? route.continue() : route.abort();
});
await page.goto(item.link, {
waitUntil: 'domcontentloaded',
});
await page.waitForSelector('section#bodymatter, .news-article-content, .news-article-content--featured');
const res = await page.evaluate(() => document.documentElement.getHTML());
await page.close();
const $ = load(res);
const abstract = $('div#abstracts').html();
const content = $('.news-article-content--featured').length
? $('.news-article-content--featured').html()
: $('.news-article-content').length
? $('.news-article-content').html()
: $('.info-panel__formats a.btn__request-access').length || $('.info-panel__formats a.btn--access').length
? ''
: $('section#bodymatter').html();
item.description = renderDescription(abstract, content);
return item;
})
)
);
const getItem = (item, $) => {
item = $(item);
return {
title: item.find('.article-title a').attr('title'),
link: `${baseUrl}${item.find('.article-title a').attr('href')}`,
doi: item.find('.article-title a').attr('href').replace('/doi/', ''),
pubDate: parseDate(item.find('.card-meta__item time').text()),
author: item
.find('.card-meta ul[title="list of authors"] li')
.toArray()
.map((author) => $(author).text())
.join(', '),
};
};
export { baseUrl, fetchDesc, getItem };
const renderDescription = (abs: string | null, content: string | null): string =>
renderToString(
<>
{abs ? raw(abs) : null}
{content ? (
<>
<br />
{raw(content)}
</>
) : null}
</>
);