@ykocaman/astro-medium-loader
Version:
Astro plugin to fetch Medium feeds by username
29 lines (28 loc) • 1.02 kB
JavaScript
import { existsSync, readFileSync, mkdirSync, writeFileSync } from 'fs';
import path from 'path';
export function fromStorage(file) {
if (!existsSync(file)) {
console.warn('Storage file does not exist:', file);
return [];
}
try {
const cached = readFileSync(file, 'utf-8');
console.log('Loaded RSS feed from storage:', file);
return JSON.parse(cached).map((item) => {
return {
...item,
pubDate: item.pubDate ? new Date(item.pubDate) : undefined,
updatedDate: item.updatedDate ? new Date(item.updatedDate) : undefined,
isoDate: item.isoDate ? new Date(item.isoDate) : undefined,
};
});
}
catch (err) {
console.warn('Failed to load RSS feed from storage:', err);
return [];
}
}
export function toStorage(file, items) {
mkdirSync(path.dirname(file), { recursive: true });
writeFileSync(file, JSON.stringify(items, null, 2), 'utf-8');
}