13 lines
361 B
TypeScript

import crypto from 'crypto';
/** Generate a URL-friendly slug from a title with a random suffix */
export function generateSlug(title: string): string {
const base = title
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '')
.slice(0, 60);
const suffix = crypto.randomBytes(3).toString('hex');
return `${base}-${suffix}`;
}