Files
archived-linsa/web/lib/utils/slug.ts
Aslam 5f537d5618 feat: pages (#151)
* wip

* wip

* wip

* wwip

* wip

* wip

* fix(util): rmeove checking to existing in slug

* wip

* chore: handle create page

* chore: handle page title untitled
2024-09-09 13:35:15 +03:00

15 lines
389 B
TypeScript

import slugify from "slugify"
import crypto from "crypto"
export function generateUniqueSlug(title: string, maxLength: number = 60): string {
const baseSlug = slugify(title, {
lower: true,
strict: true
})
const randomSuffix = crypto.randomBytes(4).toString("hex")
const truncatedSlug = baseSlug.slice(0, Math.min(maxLength, 75) - 9)
return `${truncatedSlug}-${randomSuffix}`
}