fix: sort .txt files by last edit in git history
This commit is contained in:
parent
f66fa9dff8
commit
9ef7a745fb
2 changed files with 30 additions and 10 deletions
|
|
@ -5,6 +5,7 @@ import yaml from 'js-yaml';
|
||||||
import bookmarksRaw from '../data/bookmarks.yaml?raw';
|
import bookmarksRaw from '../data/bookmarks.yaml?raw';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import { execSync } from 'node:child_process';
|
||||||
import { getApprovedEntries, type GuestbookEntry } from '../lib/db';
|
import { getApprovedEntries, type GuestbookEntry } from '../lib/db';
|
||||||
|
|
||||||
interface Bookmark {
|
interface Bookmark {
|
||||||
|
|
@ -15,7 +16,7 @@ interface Bookmark {
|
||||||
|
|
||||||
interface TxtFile {
|
interface TxtFile {
|
||||||
name: string;
|
name: string;
|
||||||
mtime: Date;
|
date: Date;
|
||||||
pinned: boolean;
|
pinned: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -23,6 +24,15 @@ interface TxtConfig {
|
||||||
pinned?: string[];
|
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');
|
const posts = await getCollection('posts');
|
||||||
|
|
||||||
// Group by category (default: "posts")
|
// Group by category (default: "posts")
|
||||||
|
|
@ -64,13 +74,13 @@ const txtFiles: TxtFile[] = fs.existsSync(txtDir)
|
||||||
? fs.readdirSync(txtDir)
|
? fs.readdirSync(txtDir)
|
||||||
.filter(file => file.endsWith('.txt'))
|
.filter(file => file.endsWith('.txt'))
|
||||||
.map(name => {
|
.map(name => {
|
||||||
const stat = fs.statSync(path.join(txtDir, name));
|
const filePath = path.join(txtDir, name);
|
||||||
return { name, mtime: stat.mtime, pinned: pinnedSet.has(name) };
|
return { name, date: getGitDate(filePath), pinned: pinnedSet.has(name) };
|
||||||
})
|
})
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
if (a.pinned && !b.pinned) return -1;
|
if (a.pinned && !b.pinned) return -1;
|
||||||
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 {
|
||||||
<details open>
|
<details open>
|
||||||
<summary>txt</summary>
|
<summary>txt</summary>
|
||||||
<pre set:html={[
|
<pre set:html={[
|
||||||
...txtFiles.slice(0, 10).map(f => `<span class="muted">${formatDate(f.mtime)}</span> <a href="/txt/${f.name}">${f.name}</a>${f.pinned ? ' [pinned]' : ''}`),
|
...txtFiles.slice(0, 10).map(f => `<span class="muted">${formatDate(f.date)}</span> <a href="/txt/${f.name}">${f.name}</a>${f.pinned ? ' [pinned]' : ''}`),
|
||||||
...(txtFiles.length > 10 ? [`<a href="/txt/">+${txtFiles.length - 10} more</a>`] : [])
|
...(txtFiles.length > 10 ? [`<a href="/txt/">+${txtFiles.length - 10} more</a>`] : [])
|
||||||
].join('\n')} />
|
].join('\n')} />
|
||||||
</details>
|
</details>
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,12 @@
|
||||||
import Layout from '../../layouts/Layout.astro';
|
import Layout from '../../layouts/Layout.astro';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import { execSync } from 'node:child_process';
|
||||||
import yaml from 'js-yaml';
|
import yaml from 'js-yaml';
|
||||||
|
|
||||||
interface TxtFile {
|
interface TxtFile {
|
||||||
name: string;
|
name: string;
|
||||||
mtime: Date;
|
date: Date;
|
||||||
pinned: boolean;
|
pinned: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -14,6 +15,15 @@ interface TxtConfig {
|
||||||
pinned?: string[];
|
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 txtDir = path.join(process.cwd(), 'public/txt');
|
||||||
const configPath = path.join(txtDir, 'config.yaml');
|
const configPath = path.join(txtDir, 'config.yaml');
|
||||||
const config: TxtConfig = fs.existsSync(configPath)
|
const config: TxtConfig = fs.existsSync(configPath)
|
||||||
|
|
@ -25,13 +35,13 @@ const txtFiles: TxtFile[] = fs.existsSync(txtDir)
|
||||||
? fs.readdirSync(txtDir)
|
? fs.readdirSync(txtDir)
|
||||||
.filter(file => file.endsWith('.txt'))
|
.filter(file => file.endsWith('.txt'))
|
||||||
.map(name => {
|
.map(name => {
|
||||||
const stat = fs.statSync(path.join(txtDir, name));
|
const filePath = path.join(txtDir, name);
|
||||||
return { name, mtime: stat.mtime, pinned: pinnedSet.has(name) };
|
return { name, date: getGitDate(filePath), pinned: pinnedSet.has(name) };
|
||||||
})
|
})
|
||||||
.sort((a, b) => {
|
.sort((a, b) => {
|
||||||
if (a.pinned && !b.pinned) return -1;
|
if (a.pinned && !b.pinned) return -1;
|
||||||
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 {
|
||||||
|
|
||||||
<details open>
|
<details open>
|
||||||
<summary>txt</summary>
|
<summary>txt</summary>
|
||||||
<pre set:html={txtFiles.map(f => `<span class="muted">${formatDate(f.mtime)}</span> <a href="/txt/${f.name}">${f.name}</a>${f.pinned ? ' [pinned]' : ''}`).join('\n')} />
|
<pre set:html={txtFiles.map(f => `<span class="muted">${formatDate(f.date)}</span> <a href="/txt/${f.name}">${f.name}</a>${f.pinned ? ' [pinned]' : ''}`).join('\n')} />
|
||||||
</details>
|
</details>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue