Skip to content

Commit

Permalink
add types for build RSS
Browse files Browse the repository at this point in the history
  • Loading branch information
JeelRajodiya committed Dec 12, 2024
1 parent ba0a99a commit c76a556
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
29 changes: 18 additions & 11 deletions scripts/build-rss.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import assert from 'assert';
import fs from 'fs/promises';
import json2xml from 'jgexml/json2xml';

import type { BlogPostTypes, Enclosure, RSS, RSSItemType } from '@/types/scripts/build-rss';

async function getAllPosts() {
const posts = (await import('../config/posts.json', { assert: { type: 'json' } })).default;

Expand All @@ -20,9 +23,9 @@ function clean(s: string) {
return cleanS;
}

export async function rssFeed(type, title, desc, outputPath) {
export async function rssFeed(type: BlogPostTypes, rssTitle: string, desc: string, outputPath: string) {
try {
let posts = (await getAllPosts())[`${type}`];
let posts = (await getAllPosts())[`${type}`] as any[];
const missingDatePosts = posts.filter((post) => !post.date);

posts = posts.filter((post) => post.date);
Expand All @@ -33,7 +36,7 @@ export async function rssFeed(type, title, desc, outputPath) {
if (i1.featured && !i2.featured) return -1;
if (!i1.featured && i2.featured) return 1;

return i2Date - i1Date;
return i2Date.getTime() - i1Date.getTime();
});

if (missingDatePosts.length > 0) {
Expand All @@ -43,15 +46,15 @@ export async function rssFeed(type, title, desc, outputPath) {
const base = 'https://www.asyncapi.com';
const tracking = '?utm_source=rss';

const feed = {};
const rss = {};
const feed = {} as { rss: RSS };
const rss = {} as RSS;

rss['@version'] = '2.0';
rss['@xmlns:atom'] = 'http://www.w3.org/2005/Atom';
rss.channel = {};
rss.channel.title = title;
rss.channel = {} as RSS['channel'];
rss.channel.title = rssTitle;
rss.channel.link = `${base}/${outputPath}`;
rss.channel['atom:link'] = {};
rss.channel['atom:link'] = {} as RSS['channel']['atom:link'];
rss.channel['atom:link']['@rel'] = 'self';
rss.channel['atom:link']['@href'] = rss.channel.link;
rss.channel['atom:link']['@type'] = 'application/rss+xml';
Expand All @@ -75,26 +78,29 @@ export async function rssFeed(type, title, desc, outputPath) {
const pubDate = new Date(date).toUTCString();
const description = clean(excerpt);
const guid = { '@isPermaLink': true, '': link };
const item = {
const item: RSSItemType = {
title,
description,
link,
category: type,
guid,
pubDate
};
} as RSSItemType;

if (post.cover) {
const enclosure = {};
const enclosure = {} as Enclosure;

enclosure['@url'] = base + post.cover;
enclosure['@length'] = 15026; // dummy value, anything works
enclosure['@type'] = 'image/jpeg';
if (typeof enclosure['@url'] === 'string') {
const tmp = enclosure['@url'].toLowerCase();

// eslint-disable-next-line max-depth
if (tmp.indexOf('.png') >= 0) enclosure['@type'] = 'image/png';
// eslint-disable-next-line max-depth
if (tmp.indexOf('.svg') >= 0) enclosure['@type'] = 'image/svg+xml';
// eslint-disable-next-line max-depth
if (tmp.indexOf('.webp') >= 0) enclosure['@type'] = 'image/webp';
}
item.enclosure = enclosure;
Expand All @@ -108,6 +114,7 @@ export async function rssFeed(type, title, desc, outputPath) {

await fs.writeFile(`./public/${outputPath}`, xml, 'utf8');
} catch (err) {
assert(err instanceof Error);
throw new Error(`Failed to generate RSS feed: ${err.message}`);
}
}
37 changes: 37 additions & 0 deletions types/scripts/build-rss.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export type BlogPostTypes = 'docs' | 'blog' | 'about' | 'docsTree';
export type Enclosure = {
'@url': string;
'@length': number;
'@type': string;
enclosure: Enclosure;
};

export type RSSItemType = {
title: string;
description: string;
link: string;
category: BlogPostTypes;
guid: any;
pubDate: string;
enclosure: Enclosure;
};
export type RSS = {
'@version': string;
'@xmlns:atom': string;
channel: {
title: string;
link: string;
'atom:link': {
'@rel': string;
'@href': string;
'@type': string;
};
description: string;
language: string;
copyright: string;
webMaster: string;
pubDate: string; // UTC string format
generator: string;
item: RSSItemType[];
};
};

0 comments on commit c76a556

Please sign in to comment.