diff --git a/apps/blog/public/txt/hello.txt b/apps/blog/public/txt/hello.txt new file mode 100644 index 0000000..64b3690 --- /dev/null +++ b/apps/blog/public/txt/hello.txt @@ -0,0 +1 @@ +Hello, this is a sample text file. diff --git a/apps/blog/src/pages/index.astro b/apps/blog/src/pages/index.astro index e7b39ce..8df5682 100644 --- a/apps/blog/src/pages/index.astro +++ b/apps/blog/src/pages/index.astro @@ -3,6 +3,8 @@ import { getCollection } from 'astro:content'; import '../styles/global.css'; import yaml from 'js-yaml'; import bookmarksRaw from '../data/bookmarks.yaml?raw'; +import fs from 'node:fs'; +import path from 'node:path'; interface Bookmark { date: string; @@ -10,12 +12,29 @@ interface Bookmark { url: string; } +interface TxtFile { + name: string; + mtime: Date; +} + const posts = await getCollection('posts'); const sorted = posts.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); const bookmarks = (yaml.load(bookmarksRaw) as Bookmark[]) .sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()); +// Auto-discover txt files from public/txt/ +const txtDir = path.join(process.cwd(), 'public/txt'); +const txtFiles: TxtFile[] = fs.existsSync(txtDir) + ? fs.readdirSync(txtDir) + .filter(file => file.endsWith('.txt')) + .map(name => { + const stat = fs.statSync(path.join(txtDir, name)); + return { name, mtime: stat.mtime }; + }) + .sort((a, b) => b.mtime.getTime() - a.mtime.getTime()) + : []; + function formatDate(date: Date): string { const d = String(date.getDate()).padStart(2, '0'); const m = String(date.getMonth() + 1).padStart(2, '0'); @@ -43,5 +62,6 @@ function extractDomain(url: string): string {
 `${formatDate(post.data.date)}    ${post.data.title}`).join('\n')} />
 
 `${formatBookmarkDate(b.date)}    ${b.title} (${extractDomain(b.url)})`).join('\n')} />
+
 `${formatDate(f.mtime)}    ${f.name}`).join('\n')} />