Add hashtag extraction and management in CreatePost and EditPost logic; improve error handling
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
This commit is contained in:
@@ -2,6 +2,7 @@ package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ExtractImageKeysFromContent parses the rich text JSON and extracts all image fileKey values
|
||||
@@ -37,3 +38,47 @@ func extractImageKeys(data interface{}, keys *[]string) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ExtractHashtagsFromContent parses the rich text JSON and extracts all hashtag text values
|
||||
func ExtractHashtagsFromContent(content string) ([]string, error) {
|
||||
var contentData map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(content), &contentData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var hashtags []string
|
||||
hashtagSet := make(map[string]bool) // Use map to avoid duplicates
|
||||
extractHashtags(contentData, hashtagSet)
|
||||
|
||||
// Convert map keys to slice
|
||||
for tag := range hashtagSet {
|
||||
hashtags = append(hashtags, tag)
|
||||
}
|
||||
return hashtags, nil
|
||||
}
|
||||
|
||||
// extractHashtags recursively searches for hashtag nodes and collects their text values
|
||||
func extractHashtags(data interface{}, hashtags map[string]bool) {
|
||||
switch v := data.(type) {
|
||||
case map[string]interface{}:
|
||||
// Check if this is a hashtag node
|
||||
if nodeType, ok := v["type"].(string); ok && nodeType == "hashtag" {
|
||||
if text, ok := v["text"].(string); ok && text != "" {
|
||||
// Remove the # prefix if present
|
||||
tag := strings.TrimPrefix(text, "#")
|
||||
if tag != "" {
|
||||
hashtags[tag] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
// Recurse into all map values
|
||||
for _, value := range v {
|
||||
extractHashtags(value, hashtags)
|
||||
}
|
||||
case []interface{}:
|
||||
// Recurse into all array elements
|
||||
for _, item := range v {
|
||||
extractHashtags(item, hashtags)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user