txt
`${formatDate(f.mtime)} ${f.name}${f.pinned ? ' [pinned]' : ''}`),
+ ...txtFiles.slice(0, 10).map(f => `${formatDate(f.date)} ${f.name}${f.pinned ? ' [pinned]' : ''}`),
...(txtFiles.length > 10 ? [`+${txtFiles.length - 10} more`] : [])
].join('\n')} />
diff --git a/apps/blog/src/pages/txt/index.astro b/apps/blog/src/pages/txt/index.astro
index bdfdbdc..2592466 100644
--- a/apps/blog/src/pages/txt/index.astro
+++ b/apps/blog/src/pages/txt/index.astro
@@ -2,11 +2,12 @@
import Layout from '../../layouts/Layout.astro';
import fs from 'node:fs';
import path from 'node:path';
+import { execSync } from 'node:child_process';
import yaml from 'js-yaml';
interface TxtFile {
name: string;
- mtime: Date;
+ date: Date;
pinned: boolean;
}
@@ -14,6 +15,15 @@ interface TxtConfig {
pinned?: string[];
}
+function getGitDate(filePath: string): Date {
+ try {
+ const timestamp = execSync(`git log -1 --format=%cI -- "${filePath}"`, { encoding: 'utf8' }).trim();
+ return timestamp ? new Date(timestamp) : new Date(0);
+ } catch {
+ return new Date(0);
+ }
+}
+
const txtDir = path.join(process.cwd(), 'public/txt');
const configPath = path.join(txtDir, 'config.yaml');
const config: TxtConfig = fs.existsSync(configPath)
@@ -25,13 +35,13 @@ 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, pinned: pinnedSet.has(name) };
+ const filePath = path.join(txtDir, name);
+ return { name, date: getGitDate(filePath), pinned: pinnedSet.has(name) };
})
.sort((a, b) => {
if (a.pinned && !b.pinned) return -1;
if (!a.pinned && b.pinned) return 1;
- return b.mtime.getTime() - a.mtime.getTime();
+ return b.date.getTime() - a.date.getTime();
})
: [];
@@ -46,6 +56,6 @@ function formatDate(date: Date): string {