feed.ts 745 B

12345678910111213141516171819202122232425
  1. import { parseAtomFeed } from './atom';
  2. import { parseRSSFeed } from './rss';
  3. export async function fetchFeedText(url: string) {
  4. const rsp = await fetch(url);
  5. const txt = await rsp.text();
  6. return txt;
  7. }
  8. export function isAtomFeed(txt: string) {
  9. const domParser = new DOMParser();
  10. const doc = domParser.parseFromString(txt, 'text/xml');
  11. return doc.querySelector('feed') !== null;
  12. }
  13. export function getProperty(node: Element, property: string): string {
  14. const propNode = node.querySelector(property);
  15. return propNode?.textContent ?? '';
  16. }
  17. export async function loadFeed(url: string) {
  18. const res = await fetchFeedText(url);
  19. const parsedFeed = isAtomFeed(res) ? parseAtomFeed(res) : parseRSSFeed(res);
  20. return parsedFeed;
  21. }