mirror of
https://github.com/Dvorinka/bizoni.git
synced 2026-06-03 18:22:57 +00:00
Fix duplicate blog entries and image paths
- Add deduplication logic to avoid showing same blog twice - Add extractBlogID function to get correct numeric ID from meta tags - Use blogID for image paths instead of filename ID - This fixes duplicate entries in admin interface and broken images
This commit is contained in:
+29
-2
@@ -554,6 +554,22 @@ func extractSlug(path, filename string) string {
|
|||||||
return strings.TrimSuffix(filename, ".html")
|
return strings.TrimSuffix(filename, ".html")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractBlogID(path, filename string) string {
|
||||||
|
b, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return strings.TrimSuffix(filename, ".html")
|
||||||
|
}
|
||||||
|
s := string(b)
|
||||||
|
// Try to find ID in meta tag first
|
||||||
|
re := regexp.MustCompile(`(?is)<meta name="id" content="([^"]+)"`)
|
||||||
|
m := re.FindStringSubmatch(s)
|
||||||
|
if len(m) >= 2 {
|
||||||
|
return m[1]
|
||||||
|
}
|
||||||
|
// Fallback: use filename without extension
|
||||||
|
return strings.TrimSuffix(filename, ".html")
|
||||||
|
}
|
||||||
|
|
||||||
func listLatestBlogs(siteRoot string, limit int) ([]BlogItem, error) {
|
func listLatestBlogs(siteRoot string, limit int) ([]BlogItem, error) {
|
||||||
// Use the siteRoot path where blogs are actually located
|
// Use the siteRoot path where blogs are actually located
|
||||||
blogDir := filepath.Join(siteRoot, "blog")
|
blogDir := filepath.Join(siteRoot, "blog")
|
||||||
@@ -576,24 +592,35 @@ func listLatestBlogs(siteRoot string, limit int) ([]BlogItem, error) {
|
|||||||
// Match both numeric (0001.html) and slug-based filenames
|
// Match both numeric (0001.html) and slug-based filenames
|
||||||
re := regexp.MustCompile(`^(\d{4}|[a-z0-9-]+)\.html$`)
|
re := regexp.MustCompile(`^(\d{4}|[a-z0-9-]+)\.html$`)
|
||||||
var items []BlogItem
|
var items []BlogItem
|
||||||
|
seenIDs := make(map[string]bool) // Track seen IDs to avoid duplicates
|
||||||
for _, e := range entries {
|
for _, e := range entries {
|
||||||
name := e.Name()
|
name := e.Name()
|
||||||
if !re.MatchString(name) {
|
if !re.MatchString(name) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
id := strings.TrimSuffix(name, ".html")
|
id := strings.TrimSuffix(name, ".html")
|
||||||
|
|
||||||
|
// Skip if this ID was already processed (deduplication)
|
||||||
|
if seenIDs[id] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
// Title and categories extraction from blog HTML
|
// Title and categories extraction from blog HTML
|
||||||
blogPath := filepath.Join(blogDir, name)
|
blogPath := filepath.Join(blogDir, name)
|
||||||
title := extractTitle(blogPath)
|
title := extractTitle(blogPath)
|
||||||
slug := extractSlug(blogPath, name)
|
slug := extractSlug(blogPath, name)
|
||||||
cats := extractCategories(blogPath)
|
cats := extractCategories(blogPath)
|
||||||
|
blogID := extractBlogID(blogPath, name)
|
||||||
|
|
||||||
|
// Mark this ID as seen
|
||||||
|
seenIDs[id] = true
|
||||||
// Determine mod time - prefer image modtime if exists, else html
|
// Determine mod time - prefer image modtime if exists, else html
|
||||||
mtime := time.Time{}
|
mtime := time.Time{}
|
||||||
htmlInfo, err1 := os.Stat(filepath.Join(blogDir, name))
|
htmlInfo, err1 := os.Stat(filepath.Join(blogDir, name))
|
||||||
if err1 == nil {
|
if err1 == nil {
|
||||||
mtime = htmlInfo.ModTime()
|
mtime = htmlInfo.ModTime()
|
||||||
}
|
}
|
||||||
if imgInfo, err2 := os.Stat(filepath.Join(imgDir, id+".png")); err2 == nil {
|
if imgInfo, err2 := os.Stat(filepath.Join(imgDir, blogID+".png")); err2 == nil {
|
||||||
// If image is newer, use that as a proxy for recency
|
// If image is newer, use that as a proxy for recency
|
||||||
if imgInfo.ModTime().After(mtime) {
|
if imgInfo.ModTime().After(mtime) {
|
||||||
mtime = imgInfo.ModTime()
|
mtime = imgInfo.ModTime()
|
||||||
@@ -611,7 +638,7 @@ func listLatestBlogs(siteRoot string, limit int) ([]BlogItem, error) {
|
|||||||
Title: title,
|
Title: title,
|
||||||
Slug: slug,
|
Slug: slug,
|
||||||
Link: link,
|
Link: link,
|
||||||
Image: "/img/blog/" + id + ".png",
|
Image: "/img/blog/" + blogID + ".png",
|
||||||
MTime: mtime,
|
MTime: mtime,
|
||||||
Categories: cats,
|
Categories: cats,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user