diff --git a/lib/index.ts b/lib/index.ts index 053dc66..353cbc8 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,53 +1,3 @@ -import { load } from "cheerio"; - -// Generic selector map keyed by the raw record keys -export type SelectorMap> = Partial< - Record ->; - -export type ParseRssOptions> = { - itemSelector?: string; - selectors?: SelectorMap; - fallback?: TRaw[]; -}; - -/** - * Generic RSS → array of flat string records. - */ -export function parseRssItems>( - xml: string, - { - itemSelector = "channel > item", - selectors = {} as SelectorMap, - fallback = [], - }: ParseRssOptions = {}, -): TRaw[] { - try { - const $ = load(xml, { xmlMode: true }); - - const items: TRaw[] = []; - - $(itemSelector).each((_, el) => { - const result: Record = {}; - - (Object.keys(selectors) as (keyof TRaw)[]).forEach((key) => { - const selector = selectors[key]; - if (!selector) return; - - result[key as string] = $(el).find(selector).first().text().trim(); - }); - - items.push(result as TRaw); - }); - - return items; - } catch (error) { - console.error("[parseRssItems] Failed to parse RSS feed", { - error, - itemSelector, - selectors, - }); - - return fallback; - } -} +export * from "./parseRssItems"; +export * from "./goodreads"; +export * from "./substack"; diff --git a/lib/parseRssItems.ts b/lib/parseRssItems.ts new file mode 100644 index 0000000..053dc66 --- /dev/null +++ b/lib/parseRssItems.ts @@ -0,0 +1,53 @@ +import { load } from "cheerio"; + +// Generic selector map keyed by the raw record keys +export type SelectorMap> = Partial< + Record +>; + +export type ParseRssOptions> = { + itemSelector?: string; + selectors?: SelectorMap; + fallback?: TRaw[]; +}; + +/** + * Generic RSS → array of flat string records. + */ +export function parseRssItems>( + xml: string, + { + itemSelector = "channel > item", + selectors = {} as SelectorMap, + fallback = [], + }: ParseRssOptions = {}, +): TRaw[] { + try { + const $ = load(xml, { xmlMode: true }); + + const items: TRaw[] = []; + + $(itemSelector).each((_, el) => { + const result: Record = {}; + + (Object.keys(selectors) as (keyof TRaw)[]).forEach((key) => { + const selector = selectors[key]; + if (!selector) return; + + result[key as string] = $(el).find(selector).first().text().trim(); + }); + + items.push(result as TRaw); + }); + + return items; + } catch (error) { + console.error("[parseRssItems] Failed to parse RSS feed", { + error, + itemSelector, + selectors, + }); + + return fallback; + } +} diff --git a/package-lock.json b/package-lock.json index 94f8910..3b0fb66 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "substack-feed-api", - "version": "2.0.1", + "version": "2.0.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "substack-feed-api", - "version": "2.0.1", + "version": "2.0.3", "dependencies": { "cheerio": "^1.2.0", "node-fetch": "^3.3.2", @@ -59,7 +59,6 @@ "integrity": "sha512-H3mcG6ZDLTlYfaSNi0iOKkigqMFvkTKlGUYlD8GW7nNOYRrevuA46iTypPyv+06V3fEmvvazfntkBU34L0azAw==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "@babel/code-frame": "^7.28.6", "@babel/generator": "^7.28.6", diff --git a/package.json b/package.json index 876ac01..9da62c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "substack-feed-api", - "version": "2.0.1", + "version": "2.0.3", "type": "module", "files": [ "dist", diff --git a/vite.config.ts b/vite.config.ts index 4b8fa79..6010570 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -51,7 +51,7 @@ export default defineConfig(({ mode }) => { }, build: { lib: { - entry: ["./lib/index.ts", "./lib/goodreads.ts", "./lib/substack.ts"], + entry: "./lib/index.ts", name: "SubstackFeedAPI", fileName: "substackFeedApi", },