You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
70 lines
1.6 KiB
package generator |
|
|
|
import ( |
|
"encoding/json" |
|
) |
|
|
|
// GenerateStructuredData generates JSON-LD structured data |
|
func GenerateStructuredData(siteName, siteURL, pageType, title, description, url string) string { |
|
var data map[string]interface{} |
|
|
|
switch pageType { |
|
case "article": |
|
data = map[string]interface{}{ |
|
"@context": "https://schema.org", |
|
"@type": "Article", |
|
"headline": title, |
|
"description": description, |
|
"url": url, |
|
"publisher": map[string]interface{}{ |
|
"@type": "Organization", |
|
"name": siteName, |
|
}, |
|
} |
|
case "website": |
|
data = map[string]interface{}{ |
|
"@context": "https://schema.org", |
|
"@type": "WebSite", |
|
"name": siteName, |
|
"url": siteURL, |
|
} |
|
default: |
|
data = map[string]interface{}{ |
|
"@context": "https://schema.org", |
|
"@type": "WebPage", |
|
"name": title, |
|
"url": url, |
|
} |
|
} |
|
|
|
jsonData, _ := json.Marshal(data) |
|
return string(jsonData) |
|
} |
|
|
|
// GenerateBreadcrumbStructuredData generates breadcrumb structured data |
|
func GenerateBreadcrumbStructuredData(items []BreadcrumbItem, siteURL string) string { |
|
breadcrumbList := make([]map[string]interface{}, len(items)) |
|
for i, item := range items { |
|
breadcrumbList[i] = map[string]interface{}{ |
|
"@type": "ListItem", |
|
"position": i + 1, |
|
"name": item.Name, |
|
"item": siteURL + item.URL, |
|
} |
|
} |
|
|
|
data := map[string]interface{}{ |
|
"@context": "https://schema.org", |
|
"@type": "BreadcrumbList", |
|
"itemListElement": breadcrumbList, |
|
} |
|
|
|
jsonData, _ := json.Marshal(data) |
|
return string(jsonData) |
|
} |
|
|
|
// BreadcrumbItem represents a breadcrumb item |
|
type BreadcrumbItem struct { |
|
Name string |
|
URL string |
|
}
|
|
|