mirror of
https://github.com/linsa-io/linsa.git
synced 2026-02-23 10:54:55 +01:00
23 lines
535 B
TypeScript
23 lines
535 B
TypeScript
import slugify from "slugify"
|
|
|
|
export function generateUniqueSlug(
|
|
title: string,
|
|
maxLength: number = 60,
|
|
): string {
|
|
const baseSlug = slugify(title, {
|
|
lower: true,
|
|
strict: true,
|
|
})
|
|
|
|
// Web Crypto API
|
|
const randomValues = new Uint8Array(4)
|
|
crypto.getRandomValues(randomValues)
|
|
const randomSuffix = Array.from(randomValues)
|
|
.map((byte) => byte.toString(16).padStart(2, "0"))
|
|
.join("")
|
|
|
|
const truncatedSlug = baseSlug.slice(0, Math.min(maxLength, 75) - 9)
|
|
|
|
return `${truncatedSlug}-${randomSuffix}`
|
|
}
|