refactor: moves util functions into their respective library file

This commit is contained in:
Lewis Wynne 2026-01-31 22:12:33 +00:00
parent 98ac61a6c8
commit 38b5413a37
11 changed files with 23 additions and 27 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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 {

View file

@ -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 {

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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;

View file

@ -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/',

View file

@ -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);
}
}