Skip to content

Commit 26bf27e

Browse files
committed
fix: Extract parseRssItems into its own file
Move the parseRssItems implementation out of lib/index.ts into a new lib/parseRssItems.ts and update lib/index.ts to re-export parseRssItems, goodreads, and substack. Also simplify the Vite build entry to use the single barrel ./lib/index.ts instead of listing multiple entry files, so the library is built from the unified exports.
1 parent 296a565 commit 26bf27e

3 files changed

Lines changed: 57 additions & 54 deletions

File tree

lib/index.ts

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,3 @@
1-
import { load } from "cheerio";
2-
3-
// Generic selector map keyed by the raw record keys
4-
export type SelectorMap<TRaw extends Record<string, string>> = Partial<
5-
Record<keyof TRaw, string>
6-
>;
7-
8-
export type ParseRssOptions<TRaw extends Record<string, string>> = {
9-
itemSelector?: string;
10-
selectors?: SelectorMap<TRaw>;
11-
fallback?: TRaw[];
12-
};
13-
14-
/**
15-
* Generic RSS → array of flat string records.
16-
*/
17-
export function parseRssItems<TRaw extends Record<string, string>>(
18-
xml: string,
19-
{
20-
itemSelector = "channel > item",
21-
selectors = {} as SelectorMap<TRaw>,
22-
fallback = [],
23-
}: ParseRssOptions<TRaw> = {},
24-
): TRaw[] {
25-
try {
26-
const $ = load(xml, { xmlMode: true });
27-
28-
const items: TRaw[] = [];
29-
30-
$(itemSelector).each((_, el) => {
31-
const result: Record<string, string> = {};
32-
33-
(Object.keys(selectors) as (keyof TRaw)[]).forEach((key) => {
34-
const selector = selectors[key];
35-
if (!selector) return;
36-
37-
result[key as string] = $(el).find(selector).first().text().trim();
38-
});
39-
40-
items.push(result as TRaw);
41-
});
42-
43-
return items;
44-
} catch (error) {
45-
console.error("[parseRssItems] Failed to parse RSS feed", {
46-
error,
47-
itemSelector,
48-
selectors,
49-
});
50-
51-
return fallback;
52-
}
53-
}
1+
export * from "./parseRssItems";
2+
export * from "./goodreads";
3+
export * from "./substack";

lib/parseRssItems.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { load } from "cheerio";
2+
3+
// Generic selector map keyed by the raw record keys
4+
export type SelectorMap<TRaw extends Record<string, string>> = Partial<
5+
Record<keyof TRaw, string>
6+
>;
7+
8+
export type ParseRssOptions<TRaw extends Record<string, string>> = {
9+
itemSelector?: string;
10+
selectors?: SelectorMap<TRaw>;
11+
fallback?: TRaw[];
12+
};
13+
14+
/**
15+
* Generic RSS → array of flat string records.
16+
*/
17+
export function parseRssItems<TRaw extends Record<string, string>>(
18+
xml: string,
19+
{
20+
itemSelector = "channel > item",
21+
selectors = {} as SelectorMap<TRaw>,
22+
fallback = [],
23+
}: ParseRssOptions<TRaw> = {},
24+
): TRaw[] {
25+
try {
26+
const $ = load(xml, { xmlMode: true });
27+
28+
const items: TRaw[] = [];
29+
30+
$(itemSelector).each((_, el) => {
31+
const result: Record<string, string> = {};
32+
33+
(Object.keys(selectors) as (keyof TRaw)[]).forEach((key) => {
34+
const selector = selectors[key];
35+
if (!selector) return;
36+
37+
result[key as string] = $(el).find(selector).first().text().trim();
38+
});
39+
40+
items.push(result as TRaw);
41+
});
42+
43+
return items;
44+
} catch (error) {
45+
console.error("[parseRssItems] Failed to parse RSS feed", {
46+
error,
47+
itemSelector,
48+
selectors,
49+
});
50+
51+
return fallback;
52+
}
53+
}

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default defineConfig(({ mode }) => {
5151
},
5252
build: {
5353
lib: {
54-
entry: ["./lib/index.ts", "./lib/goodreads.ts", "./lib/substack.ts"],
54+
entry: "./lib/index.ts",
5555
name: "SubstackFeedAPI",
5656
fileName: "substackFeedApi",
5757
},

0 commit comments

Comments
 (0)