GitHub - syntax-tree/xast-util-feed: xast utility to build feeds (rss, atom) (original) (raw)
xast-util-feed
xast utility to build (web) feeds (RSS, Atom).
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This package generates RSS or Atom feeds from data.
When should I use this?
This package helps you add feeds to your site. It focusses on a small set of widely used and supported parts of feeds. It has a few good options instead of overwhelming with hundreds of things to configure. If you do need more things, well: this utility gives you a syntax tree, which you can change.
Itβs good to use this package to build several feeds and to only include recent posts (often 15-20 items are included in a channel). You should make a channel for all your posts; when relevant, separate channels per language as well; and potentially, channels per post type (such as separate ones for blog posts, notes, and images).
Just using either RSS or Atom is probably fine: no need to do both.
Install
This package is ESM only. In Node.js (version 16+), install with npm:
npm install xast-util-feed
In Deno with esm.sh:
import {atom, rss} from 'https://esm.sh/xast-util-feed@2'
In browsers with esm.sh:
Use
import {atom, rss} from 'xast-util-feed' import {toXml} from 'xast-util-to-xml'
const channel = { title: 'NYT > Top Stories', url: 'https://www.nytimes.com', feedUrl: 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml', lang: 'en', author: 'The New York Times Company' }
const data = [ { title: 'Senate Balances Impeachment Trial With an Incoming President', url: 'https://www.nytimes.com/2021/01/14/us/politics/impeachment-senate-trial-trump.html', descriptionHtml: '
Senate leaders etc etc etc.
', author: 'Nicholas Fandos and Catie Edmondson', published: 'Fri, 15 Jan 2021 01π49 +0000', tags: ['Senate', 'Murkowski, Lisa', 'Trump, Donald J'] } ]console.log(toXml(rss(channel, data))) console.log(toXml(atom(channel, data)))
Yields (pretty printed):
NYT > Top Stories https://www.nytimes.com/ Sun, 17 Jan 2021 09:00:54 GMT 2021-01-17T09:00:54.781Z en en Β© 2021 The New York Times Company Β© 2021 The New York Times Company Senate Balances Impeachment Trial With an Incoming President Nicholas Fandos and Catie Edmondson https://www.nytimes.com/2021/01/14/us/politics/impeachment-senate-trial-trump.html https://www.nytimes.com/2021/01/14/us/politics/impeachment-senate-trial-trump.html Fri, 15 Jan 2021 01π49 GMT 2021-01-15T01π49.000Z Senate Murkowski, Lisa Trump, Donald J <p>Senate leaders etc etc etc.</p> NYT > Top Stories https://www.nytimes.com/ https://www.nytimes.com/ Sun, 17 Jan 2021 09:00:54 GMT Β© 2021 The New York Times Company The New York Times Company Senate Balances Impeachment Trial With an Incoming President Nicholas Fandos and Catie Edmondson https://www.nytimes.com/2021/01/14/us/politics/impeachment-senate-trial-trump.html 2021-01-15T01π49.000Z <p>Senate leaders etc etc etc.</p>API
This package exports the identifiers atom and rss. There is no default export.
atom(channel, data)
Build an Atom feed.
Parameters
channel
(Channel) β data on the feed (the group of items)data
(Array, optional) β list of entries
Returns
Atom feed (Root).
rss(channel, data)
Build an RSS feed.
Parameters
channel
(Channel) β data on the feed (the group of items)data
(Array, optional) β list of entries
Returns
RSS feed (Root).
Author
Author object (TypeScript type).
Fields
name
Name (string
, required, example: 'Acme, Inc.'
or 'Jane Doe'
).
email
Email address (string
, optional, ,example: john@example.org
)
url
URL to author (string
, optional, example: 'https://example.org/john'
).
url
is used in atom
, not in rss
.
Channel
Data on the feed (the group of items) (TypeScript type).
Fields
title
Title of the channel (string
, required, example: Zimbabwe | The Guardian
).
url
Full URL to the site (string
, required, example:'https://www.theguardian.com/world/zimbabwe'
).
feedUrl
Full URL to this channel (string?
, example: 'https://www.adweek.com/feed/'
).
Make sure to pass different ones to rss
and atom
when you build both!
You should define this.
description
Short description of the channel (string?
, example: Album Reviews
).
You should define this.
lang
BCP 47 language tag representing the language of the whole channel (string?
, example: 'fr-BE'
).
You should define this.
author
Optional author of the whole channel (string
or Author).
Either string
, in which case itβs as passing {name: string}
. Or an author object.
tags
Categories of the channel (Array<string>?
, example: ['JavaScript', 'React']
).
Enclosure
Media (TypeScript type).
Fields
url
Full URL to the resource (string
, required, example:'http://dallas.example.com/joebob_050689.mp3'
).
size
Resource size in bytes (number
, required, example: 24986239
).
type
Mime type of the resource (string
, required, example: 'audio/mpeg'
).
Entry
Data on a single item (TypeScript type).
Fields
title
Title of the item (string?
, example: 'Playboi Carti: Whole Lotta Red'
).
Either title
, description
, or descriptionHtml
must be set.
description
Either the whole post or an excerpt of it (string?
, example: 'Lorem'
).
Should be plain text.descriptionHtml
is preferred over plain text description
.
Either title
, description
, or descriptionHtml
must be set.
descriptionHtml
Either the whole post or an excerpt of it (string?
, example: '<p>Lorem</p>'
).
Should be serialized HTML.descriptionHtml
is preferred over plain text description
.
Either title
, description
, or descriptionHtml
must be set.
author
Entry version of channel.author
.
You should define this.
For atom
, it is required to either set channel.author
or set author
on all entries.
url
Full URL of this entry on the site (string?
, example:'https://pitchfork.com/reviews/albums/roberta-flack-first-take'
).
published
When the entry was first published (Date
or value for new Date(x)
, optional).
modified
When the entry was last modified (Date
or value for new Date(x)
, optional).
tags
Categories of the entry (Array<string>?
, example: ['laravel', 'debugging']
).
enclosure
Attached media (Enclosure?).
Types
This package is fully typed with TypeScript. It exports the additional types Author,Channel,Enclosure, andEntry.
Compatibility
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, xast-util-feed@^2
, compatible with Node.js 16.
Security
XML can be a dangerous language: donβt trust user-provided data.
Related
- xast-util-to-xmlβ serialize xast to XML
- xast-util-sitemapβ build a sitemap
- xastscriptβ create xast trees
Contribute
See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
License
MIT Β© Titus Wormer