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 {};
|
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(
|
private async findOrCreateCategory(
|
||||||
categorySlug: string,
|
categorySlug: string,
|
||||||
): Promise<Category | null> {
|
): Promise<Category | null> {
|
||||||
@ -241,7 +252,7 @@ export class StrapiService {
|
|||||||
title: strapiArticle.title,
|
title: strapiArticle.title,
|
||||||
excerpt: strapiArticle.description,
|
excerpt: strapiArticle.description,
|
||||||
content: strapiArticle.content,
|
content: strapiArticle.content,
|
||||||
slug: strapiArticle.slug,
|
slug: strapiArticle.slug || this.generateSlug(strapiArticle.title),
|
||||||
status: strapiArticle.publishedAt
|
status: strapiArticle.publishedAt
|
||||||
? ArticleStatus.PUBLISHED
|
? ArticleStatus.PUBLISHED
|
||||||
: ArticleStatus.DRAFT,
|
: ArticleStatus.DRAFT,
|
||||||
@ -328,7 +339,7 @@ export class StrapiService {
|
|||||||
title: strapiArticle.title,
|
title: strapiArticle.title,
|
||||||
excerpt: strapiArticle.description,
|
excerpt: strapiArticle.description,
|
||||||
content: strapiArticle.content,
|
content: strapiArticle.content,
|
||||||
slug: strapiArticle.slug,
|
slug: strapiArticle.slug || this.generateSlug(strapiArticle.title),
|
||||||
status,
|
status,
|
||||||
tags: [],
|
tags: [],
|
||||||
featuredImage: imageUrl,
|
featuredImage: imageUrl,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user