mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
Move to TanStack Start from Next.js (#184)
This commit is contained in:
22
web/app/lib/utils/slug.ts
Normal file
22
web/app/lib/utils/slug.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
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}`
|
||||
}
|
||||
Reference in New Issue
Block a user