This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
import { db } from '$lib/server/db';
|
||||
import { blogArticles } from '$lib/server/db/schema';
|
||||
import { blogArticles, lists } from '$lib/server/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
export function slugify(title: string): string {
|
||||
export function slugify(title: string, fallback = 'article'): string {
|
||||
return (
|
||||
title
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '') || 'article'
|
||||
.replace(/^-+|-+$/g, '') || fallback
|
||||
);
|
||||
}
|
||||
|
||||
@@ -27,3 +27,17 @@ export async function uniqueSlug(title: string): Promise<string> {
|
||||
|
||||
return slug;
|
||||
}
|
||||
|
||||
export async function uniqueListSlug(name: string): Promise<string> {
|
||||
const base = slugify(name, 'list');
|
||||
let slug = base;
|
||||
let suffix = 2;
|
||||
while ((await db.select({ id: lists.id }).from(lists).where(eq(lists.slug, slug))).length > 0) {
|
||||
slug = `${base}-${suffix++}`;
|
||||
}
|
||||
return slug;
|
||||
}
|
||||
|
||||
export function validSlug(slug: string): boolean {
|
||||
return slug.length <= 120 && /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(slug);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user