From 1e16e5c343ddc68b3ca8780c74c80996b7c2e9e1 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 13 Oct 2024 20:32:32 +0800 Subject: [PATCH] :bug: Fix truncate content panic --- pkg/internal/services/posts.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 1bab196..a1499f9 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -475,10 +475,12 @@ const TruncatePostContentThreshold = 160 func TruncatePostContent(post models.Post) models.Post { if post.Body["content"] != nil { - if val, ok := post.Body["content"].(string); ok && len(val) >= TruncatePostContentThreshold { + if val, ok := post.Body["content"].(string); ok { length := TruncatePostContentThreshold - post.Body["content"] = string([]rune(val)[:length]) + "..." - post.Body["content_truncated"] = true + if len([]rune(val)) >= length { + post.Body["content"] = string([]rune(val)[:length]) + "..." + post.Body["content_truncated"] = true + } } }