this repo has no description
at dev 24 lines 966 B view raw
1import { createHash } from "node:crypto"; 2 3/** 4 * Generate a URL-friendly slug from a title and publish date. 5 * @param title The title to slugify. 6 * @param publishDate The publish date to include in the slug. 7 * @returns A slugified string with a unique hash suffix. 8 */ 9export function generateSlug(title: string, publishDate: Date): string { 10 const slugified = title 11 .toLowerCase() 12 .trim() 13 .replace(/[.]/g, "-") // Dots to hyphens 14 .replace(/[^a-z0-9\s-]/g, "") // Remove non-alphanumeric, non-space, non-hyphen 15 .replace(/\s+/g, "-") // Spaces to hyphens 16 .replace(/--+/g, "-") // Collapse multiple hyphens 17 .replace(/^-+|-+$/g, ""); // Trim leading/trailing hyphens 18 19 // Create a deterministic hash based on title + publishDate (for uniqueness) 20 const hashInput = `${title}-${publishDate.toISOString()}`; 21 const hash = createHash("sha256").update(hashInput).digest("hex").slice(0, 5); // 5-char hex (e.g., 'r9fs3') 22 23 return `${slugified}-${hash}`; 24}