feat: extracts some repeated logic out into lib/ files

This commit is contained in:
Lewis Wynne 2026-01-31 22:10:03 +00:00
parent 38653f2aa1
commit 99c1539aad
22 changed files with 200 additions and 336 deletions

53
www/src/lib/txt.ts Normal file
View file

@ -0,0 +1,53 @@
import fs from 'node:fs';
import path from 'node:path';
import yaml from 'js-yaml';
import { getGitDate } from '../utils';
export interface TxtFile {
name: string;
date: Date;
pinned: boolean;
}
export interface TxtConfig {
pinned?: string[];
}
export function getTxtDir(): string {
return path.join(process.cwd(), 'public/txt');
}
export function loadTxtConfig(): TxtConfig {
const configPath = path.join(getTxtDir(), 'config.yaml');
return fs.existsSync(configPath)
? yaml.load(fs.readFileSync(configPath, 'utf8')) as TxtConfig
: {};
}
export function getTxtFiles(): TxtFile[] {
const txtDir = getTxtDir();
if (!fs.existsSync(txtDir)) return [];
const config = loadTxtConfig();
const pinnedSet = new Set(config.pinned || []);
return fs.readdirSync(txtDir)
.filter(file => file.endsWith('.txt'))
.map(name => ({
name,
date: getGitDate(path.join(txtDir, name)),
pinned: pinnedSet.has(name),
}))
.sort((a, b) => {
if (a.pinned && !b.pinned) return -1;
if (!a.pinned && b.pinned) return 1;
return b.date.getTime() - a.date.getTime();
});
}
export function getTxtFileNames(): string[] {
const txtDir = getTxtDir();
if (!fs.existsSync(txtDir)) return [];
return fs.readdirSync(txtDir).filter(file => file.endsWith('.txt'));
}