diff --git a/www/src/lib/posts.ts b/www/src/lib/posts.ts index 6ee9805..d3fc859 100644 --- a/www/src/lib/posts.ts +++ b/www/src/lib/posts.ts @@ -2,6 +2,11 @@ import type { CollectionEntry } from 'astro:content'; type Post = CollectionEntry<'md'>; +export function getSlug(postId: string): string { + const parts = postId.split('/'); + return parts[parts.length - 1]; +} + export function sortPosts(posts: Post[]): Post[] { return posts.slice().sort((a, b) => { if (a.data.pinned && !b.data.pinned) return -1; diff --git a/www/src/lib/txt.ts b/www/src/lib/txt.ts index c5e9afe..233d24a 100644 --- a/www/src/lib/txt.ts +++ b/www/src/lib/txt.ts @@ -1,7 +1,16 @@ +import { execSync } from 'node:child_process'; import fs from 'node:fs'; import path from 'node:path'; import yaml from 'js-yaml'; -import { getGitDate } from '../utils'; + +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); + } +} export interface TxtFile { name: string; diff --git a/www/src/pages/draft/[slug].astro b/www/src/pages/draft/[slug].astro index 7c6b454..78c919f 100644 --- a/www/src/pages/draft/[slug].astro +++ b/www/src/pages/draft/[slug].astro @@ -6,7 +6,7 @@ import { getCollection, render } from 'astro:content'; import { isAdmin } from '../../lib/auth'; import Layout from '../../layouts/Layout.astro'; import { formatDate } from '../../lib/format'; -import { getSlug } from '../../utils'; +import { getSlug } from '../../lib/posts'; let session; try { diff --git a/www/src/pages/draft/index.astro b/www/src/pages/draft/index.astro index 611eb3b..4fb6cf6 100644 --- a/www/src/pages/draft/index.astro +++ b/www/src/pages/draft/index.astro @@ -6,8 +6,7 @@ import { getCollection } from 'astro:content'; import { isAdmin } from '../../lib/auth'; import Layout from '../../layouts/Layout.astro'; import { formatDate } from '../../lib/format'; -import { organizePostsByCategory } from '../../lib/posts'; -import { getSlug } from '../../utils'; +import { organizePostsByCategory, getSlug } from '../../lib/posts'; let session; try { diff --git a/www/src/pages/feed.xml.ts b/www/src/pages/feed.xml.ts index a95b65a..606e013 100644 --- a/www/src/pages/feed.xml.ts +++ b/www/src/pages/feed.xml.ts @@ -1,8 +1,8 @@ import rss from '@astrojs/rss'; import { getCollection } from 'astro:content'; import type { APIContext } from 'astro'; +import { getSlug } from '../lib/posts'; import { getTxtFiles } from '../lib/txt'; -import { getSlug } from '../utils'; export async function GET(context: APIContext) { const posts = await getCollection('md', ({ data }) => data.draft !== true); diff --git a/www/src/pages/index.astro b/www/src/pages/index.astro index fa25f03..03fc3de 100644 --- a/www/src/pages/index.astro +++ b/www/src/pages/index.astro @@ -3,9 +3,8 @@ import { getCollection } from 'astro:content'; import Layout from '../layouts/Layout.astro'; import { getApprovedEntries, type GuestbookEntry } from '../lib/db'; import { formatDate, extractDomain } from '../lib/format'; -import { organizePostsByCategory } from '../lib/posts'; +import { organizePostsByCategory, getSlug } from '../lib/posts'; import { getTxtFiles } from '../lib/txt'; -import { getSlug } from '../utils'; const posts = await getCollection('md', ({ data }) => data.draft !== true); const { grouped, categories: sortedCategories } = organizePostsByCategory(posts); diff --git a/www/src/pages/md/[slug].astro b/www/src/pages/md/[slug].astro index 82c8db5..615a11d 100644 --- a/www/src/pages/md/[slug].astro +++ b/www/src/pages/md/[slug].astro @@ -2,7 +2,7 @@ import { getCollection, render } from 'astro:content'; import Layout from '../../layouts/Layout.astro'; import { formatDate } from '../../lib/format'; -import { getSlug } from '../../utils'; +import { getSlug } from '../../lib/posts'; export async function getStaticPaths() { const posts = await getCollection('md', ({ data }) => data.draft !== true); diff --git a/www/src/pages/md/index.astro b/www/src/pages/md/index.astro index 9cd469b..9382df8 100644 --- a/www/src/pages/md/index.astro +++ b/www/src/pages/md/index.astro @@ -2,8 +2,7 @@ import { getCollection } from 'astro:content'; import Layout from '../../layouts/Layout.astro'; import { formatDate } from '../../lib/format'; -import { organizePostsByCategory } from '../../lib/posts'; -import { getSlug } from '../../utils'; +import { organizePostsByCategory, getSlug } from '../../lib/posts'; const posts = await getCollection('md', ({ data }) => data.draft !== true); const { grouped, categories: sortedCategories } = organizePostsByCategory(posts); diff --git a/www/src/pages/random.ts b/www/src/pages/random.ts index 2cce017..24b036b 100644 --- a/www/src/pages/random.ts +++ b/www/src/pages/random.ts @@ -1,7 +1,7 @@ import { getCollection } from 'astro:content'; import type { APIContext } from 'astro'; +import { getSlug } from '../lib/posts'; import { getTxtFileNames } from '../lib/txt'; -import { getSlug } from '../utils'; export const prerender = false; diff --git a/www/src/pages/sitemap.txt.ts b/www/src/pages/sitemap.txt.ts index cf3f8ef..c0857ba 100644 --- a/www/src/pages/sitemap.txt.ts +++ b/www/src/pages/sitemap.txt.ts @@ -1,7 +1,7 @@ import { getCollection } from 'astro:content'; import type { APIContext } from 'astro'; +import { getSlug } from '../lib/posts'; import { getTxtFileNames } from '../lib/txt'; -import { getSlug } from '../utils'; const SUBDOMAINS = [ 'https://penfield.wynne.rs/', diff --git a/www/src/utils.ts b/www/src/utils.ts deleted file mode 100644 index f0a6e20..0000000 --- a/www/src/utils.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { execSync } from 'node:child_process'; - -export function getSlug(postId: string): string { - const parts = postId.split('/'); - return parts[parts.length - 1]; -} - -export 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); - } -}