feed

repository·main·Indexed 23 days ago

https://github.com/jpmonette/feed

A strongly typed Node.js generator for RSS 2.0, JSON Feed 1.0, and Atom 1.0 syndication formats. Version 6.0.0 allows developers to instantiate a Feed object with metadata, add items via addItem(), and render the output using rss2(), atom1(), or json1() methods.

Tokens
5.1K
Snippets
6
Records
17
Agent score
80%

What's inside feed

  1. Migrate from < 3.0.0 to modern versions

    main

    If you are upgrading from a version older than 3.0.0, you must update your import syntax because the project migrated to ES6 named imports.

    For ES6 module environments:

    import { Feed } from "feed";

    For CommonJS environments using require():

    const Feed = require('feed').Feed;
  2. Generate RSS, Atom, and JSON feeds

    main

    To generate feeds, instantiate a new Feed object with your feed's metadata, add items using addItem(), and then call the specific format method (rss2(), atom1(), or json1()) to get the string output.

    import { Feed } from "feed";
    
    const feed = new Feed({
      title: "Feed Title",
      description: "This is my personal feed!",
      id: "http://example.com/",
      link: "http://example.com/",
      language: "en",
      image: "http://example.com/image.png",
      favicon: "http://example.com/favicon.ico",
      copyright: "All rights reserved 2013, John Doe",
      updated: new Date(2013, 6, 14),
      generator: "awesome",
      feedLinks: {
        json: "https://example.com/json",
        atom: "https://example.com/atom",
      },
      author: {
        name: "John Doe",
        email: "johndoe@example.com",
        link: "https://example.com/johndoe",
      },
    });
    
    feed.addItem({
      title: "Post Title",
      id: "http://example.com/post",
      link: "http://example.com/post",
      description: "Post description",
      content: "Post content",
      author: [
        {
          name: "Jane Doe",
          email: "janedoe@example.com",
          link: "https://example.com/janedoe",
        },
      ],
      date: new Date(),
      image: "http://example.com/post-image.png",
    });
    
    feed.addCategory("Technologie");
    
    console.log(feed.rss2());   // Output: RSS 2.0
    console.log(feed.atom1());  // Output: Atom 1.0
    console.log(feed.json1());  // Output: JSON Feed 1.0
    import { Feed } from "feed";
    
    const feed = new Feed({
      title: "Feed Title",
      description: "This is my personal feed!",
      id: "http://example.com/",
      link: "http://example.com/",
      language: "en", // optional, used only in RSS 2.0, possible values: http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
      image: "http://example.com/image.png",
      favicon: "http://example.com/favicon.ico",
      copyright: "All rights reserved 2013, John Doe",
      updated: new Date(2013, 6, 14), // optional, default = today
      generator: "awesome", // optional, default = 'https://github.com/jpmonette/feed'
      feedLinks: {
        json: "https://example.com/json",
        atom: "https://example.com/atom",
      },
      author: {
        name: "John Doe",
        email: "johndoe@example.com",
        link: "https://example.com/johndoe",
      },
    });
    
    posts.forEach((post) => {
      feed.addItem({
        title: post.title,
        id: post.url,
        link: post.url,
        description: post.description,
        content: post.content,
        author: [
          {
            name: "Jane Doe",
            email: "janedoe@example.com",
            link: "https://example.com/janedoe",
          },
          {
            name: "Joe Smith",
            email: "joesmith@example.com",
            link: "https://example.com/joesmith",
          },
        ],
        contributor: [
          {
            name: "Shawn Kemp",
            email: "shawnkemp@example.com",
            link: "https://example.com/shawnkemp",
          },
          {
            name: "Reggie Miller",
            email: "reggiemiller@example.com",
            link: "https://example.com/reggiemiller",
          },
        ],
        date: post.date,
        image: post.image,
      });
    });
    
    feed.addCategory("Technologie");
    
    feed.addContributor({
      name: "Johan Cruyff",
      email: "johancruyff@example.com",
      link: "https://example.com/johancruyff",
    });
    
    console.log(feed.rss2());
    // Output: RSS 2.0
    
    console.log(feed.atom1());
    // Output: Atom 1.0
    
    console.log(feed.json1());
    // Output: JSON Feed 1.0
  3. Reference: Item Options

    main

    The following properties are used when calling addItem() to define the metadata for individual entries in your feed.

    | Property      | Type                  | Description                                                                    |
    | ------------- | --------------------- | ------------------------------------------------------------------------------ |
    | `title`       | `string`              | **Required.** Item title                                                       |
    | `link`        | `string`              | **Required.** URL to the item                                                   |
    | `date`        | `Date`                | **Required.** Last modified/updated date                                       |
    | `id`          | `string`              | Unique identifier (Atom/JSON)                                                   |
    | `guid`        | `string`              | RSS-specific GUID                                                              |
    | `description` | `string`              | Brief summary or excerpt                                                       |
    | `content`     | `string`              | Full content (HTML allowed)                                                    |
    | `author`      | `Author[]`            | Item authors                                                                   |
    | `contributor` | `Author[]`            | Item contributors                                                              |
    | `category`    | `Category[]`          | Item categories                                                                |
    | `published`   | `Date`                | Original publication date                                                      |
    | `copyright`   | `string`              | Item copyright notice                                                          |
    | `image`       | `string \| Enclosure` | Image attachment                                                               |
    | `audio`       | `string \| Enclosure` | Audio attachment                                                              |
    | `video`       | `string \| Enclosure` | Video attachment                                                               |
    | `enclosure`   | `Enclosure`           | Generic media enclosure                                                       |
    | `extensions`  | `Extension[]`          | Custom extensions                                                              |
  4. Reference: Core Types

    main

    The following TypeScript interfaces define the structure for Authors, Enclosures, Categories, and Extensions used within the feed package.

    interface Author {
      name?: string;
      email?: string;
      link?: string;
      avatar?: string; // JSON Feed only
    }
    
    interface Enclosure {
      url: string;
      type?: string; // MIME type (e.g., "audio/mpeg")
      length?: number; // File size in bytes
      title?: string;
      duration?: number; // Duration in seconds (for podcasts)
    }
    
    interface Category {
      name?: string;
      domain?: string; // RSS
      scheme?: string; // Atom
      term?: string; // Atom
    }
    
    interface Extension {
      name: string;
      objects: Record<string, unknown>;
    }
  5. Reference: Feed Options

    main

    The following properties are used when instantiating the Feed class to define the global metadata for your syndication feed.

    | Property      | Type      | Description                                                                                    |
    | ------------- | --------- | ---------------------------------------------------------------------------------------------- |
    | `title`       | `string`  | **Required.** Feed title                                                                       |
    | `id`          | `string`  | Feed identifier (required for Atom)                                                            |
    | `link`        | `string`  | URL to the feed's website                                                                      |
    | `description` | `string`  | Feed description/subtitle                                                                     |
    | `copyright`   | `string`  | Copyright notice                                                                               |
    | `language`    | `string`  | Language code (e.g., `"en"`, `"fr-CA"`)                                                       |
    | `updated`     | `Date`    | Last update date (default: `now`)                                                              |
    | `generator`   | `string`  | Generator name (default: `"https://github.com/jpmonette/feed"`)                               |
    | `image`       | `string`  | URL to feed image/logo                                                                         |
    | `favicon`     | `string`  | URL to feed favicon                                                                           |
    | `author`      | `Author`  | Feed author                                                                                    |
    | `feedLinks`   | `object`  | Links to feed formats (`{ rss, atom, json }`)                                                  |
    | `feed`        | `string`  | Self-referencing feed URL                                                                     |
    | `hub`         | `string`  | WebSub/PubSubHubbub hub URL                                                                   |
    | `ttl`         | `number`  | Time to live in minutes (RSS)                                                                 |
    | `docs`        | `string`  | RSS specification docs URL                                                                   |
    | `stylesheet`  | `string`  | URL to XSL stylesheet                                                                         |
    | `podcast`     | `boolean` | Enable iTunes/Google podcast extensions                                                       |
    | `category`    | `string`  | Podcast category name                                                                         |
  6. Generate feeds using the Feed class

    main
    The Feed class is the primary entrypoint for creating RSS, Atom, and JSON feeds. You initialize it with FeedOptions and then populate it using methods like addItem, addCategory, addContributor, and addExtension. Once the feed is populated, you can render it into a specific format using atom1(), rss2(), or json1().
  7. Render a Feed to specific formats

    main

    The Feed class provides methods to render the accumulated data into different standard formats:

    • atom1(): Returns the feed as an Atom 1.0 string.
    • rss2(): Returns the feed as an RSS 2.0 string.
    • json1(): Returns the feed as a JSON string.
  8. Populate a Feed with items and metadata

    main

    Use the following methods on a Feed instance to add content and metadata:

    • addItem(item: Item): Adds a new item to the feed.
    • addCategory(category: string): Adds a top-level category to the feed.
    • addContributor(contributor: Author): Adds an author/contributor to the feed.
    • addExtension(extension: Extension): Adds custom extensions to the feed.
  9. Sanitize text with sanitize()

    main
    The sanitize function is used to escape ampersands (&) in a string, replacing them with &amp;. This is useful for ensuring text content is safe for XML/RSS feeds. It accepts a string or undefined and returns the sanitized string or undefined.