fix: add slug auto-generation fallback for articles without slugs
- Add generateSlug() helper to create URL-friendly slugs from titles - Apply slug fallback in syncArticles() and syncSingleArticle() - Handles articles created before slug field was added to schema - Prevents 'slug cannot be null' database constraint errors
This commit is contained in:
parent
cbdb801655
commit
a5008a3646
@ -111,6 +111,17 @@ export class StrapiService {
|
||||
return {};
|
||||
}
|
||||
|
||||
private generateSlug(title: string): string {
|
||||
// Generate slug from title if missing
|
||||
return title
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^\w\s-]/g, '') // Remove special characters
|
||||
.replace(/\s+/g, '-') // Replace spaces with hyphens
|
||||
.replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
|
||||
.substring(0, 100); // Limit length
|
||||
}
|
||||
|
||||
private async findOrCreateCategory(
|
||||
categorySlug: string,
|
||||
): Promise<Category | null> {
|
||||
@ -241,7 +252,7 @@ export class StrapiService {
|
||||
title: strapiArticle.title,
|
||||
excerpt: strapiArticle.description,
|
||||
content: strapiArticle.content,
|
||||
slug: strapiArticle.slug,
|
||||
slug: strapiArticle.slug || this.generateSlug(strapiArticle.title),
|
||||
status: strapiArticle.publishedAt
|
||||
? ArticleStatus.PUBLISHED
|
||||
: ArticleStatus.DRAFT,
|
||||
@ -328,7 +339,7 @@ export class StrapiService {
|
||||
title: strapiArticle.title,
|
||||
excerpt: strapiArticle.description,
|
||||
content: strapiArticle.content,
|
||||
slug: strapiArticle.slug,
|
||||
slug: strapiArticle.slug || this.generateSlug(strapiArticle.title),
|
||||
status,
|
||||
tags: [],
|
||||
featuredImage: imageUrl,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user