summaryrefslogtreecommitdiff
path: root/src/utility/feed/json.ts
blob: 589aeae7d7535cf0d9ec1c3b31afa62d422af256 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*** UTILITY ------------------------------------------ ***/

import { BaseFeed, type FeedOptions } from "./helper.ts";

interface JsonItem {
  content_html?: string;
  date_published?: Date;
  id: string;
  title: string;
  url: string;
}

/*** EXPORT ------------------------------------------- ***/

export class FeedJSON extends BaseFeed<JsonItem> {
  constructor(options: FeedOptions) {
    super(options);
  }

  build(): string {
    const json: Record<string, unknown> = {
      feed_url: this.options.feed,
      home_page_url: this.options.link,
      icon: this.options.icon,
      items: this.items.map(({ content_html, date_published, id, title, url }) => ({
        ...(content_html && { content_html }),
        date_published: date_published?.toISOString() || new Date().toUTCString,
        id,
        title,
        url
      })),
      title: this.options.title,
      version: "https://jsonfeed.org/version/1"
    };

    return JSON.stringify(json, null, 2);
  }
}