From 9ef7a745fb88b9cd77a31e4b293f9babbeff1fcc Mon Sep 17 00:00:00 2001 From: lew Date: Fri, 23 Jan 2026 20:49:27 +0000 Subject: [PATCH] fix: sort .txt files by last edit in git history --- apps/blog/src/pages/index.astro | 20 +++++++++++++++----- apps/blog/src/pages/txt/index.astro | 20 +++++++++++++++----- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/apps/blog/src/pages/index.astro b/apps/blog/src/pages/index.astro index d85388e..babf6e3 100644 --- a/apps/blog/src/pages/index.astro +++ b/apps/blog/src/pages/index.astro @@ -5,6 +5,7 @@ import yaml from 'js-yaml'; import bookmarksRaw from '../data/bookmarks.yaml?raw'; import fs from 'node:fs'; import path from 'node:path'; +import { execSync } from 'node:child_process'; import { getApprovedEntries, type GuestbookEntry } from '../lib/db'; interface Bookmark { @@ -15,7 +16,7 @@ interface Bookmark { interface TxtFile { name: string; - mtime: Date; + date: Date; pinned: boolean; } @@ -23,6 +24,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 posts = await getCollection('posts'); // Group by category (default: "posts") @@ -64,13 +74,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(); }) : []; @@ -120,7 +130,7 @@ function extractDomain(url: string): string {
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 {
txt -
 `${formatDate(f.mtime)}    ${f.name}${f.pinned ? ' [pinned]' : ''}`).join('\n')} />
+
 `${formatDate(f.date)}    ${f.name}${f.pinned ? ' [pinned]' : ''}`).join('\n')} />