// Package assets ports head/images/icons HTML assembly (W8 / W-G6).
package assets
import (
"fmt"
"path"
"strings"
"github.com/marqdo/marqdo/plugins/web/internal/table"
)
// HeadLink is one or `, esc(href))
s.WriteString(tag)
continue
}
tag := fmt.Sprintf(`"
s.WriteString(tag)
}
return s.String()
}
// MakeHeadHTML assembles head table → HTML string.
func MakeHeadHTML(v any) string {
return RenderHeadLinks(AsHeadLinks(v))
}
// NormalizeIcons converts icons table → (icons json, site_head links, file routes).
func NormalizeIcons(v any) ([]any, []HeadLink, []IconRoute) {
var icons []any
var head []HeadLink
var routes []IconRoute
sawFavicon := false
for _, m := range rowsOf(v) {
p := rowStr(m, pathKeys)
rel := rowStr(m, relKeys)
if rel == "" {
rel = "icon"
}
type_ := rowStr(m, typeKeys)
sizes := rowStr(m, sizesKeys)
url := rowStr(m, urlKeys)
if type_ == "" && p != "" {
type_ = MimeForPath(p)
}
if url == "" {
if p == "" {
continue
}
name := path.Base(p)
lower := strings.ToLower(p)
if strings.Contains(strings.ToLower(rel), "icon") && strings.HasSuffix(lower, ".ico") {
url = "/favicon.ico"
} else {
url = "/icons/" + name
}
}
if url == "/favicon.ico" {
sawFavicon = true
}
icons = append(icons, map[string]any{
"path": p,
"rel": rel,
"type": type_,
"sizes": sizes,
"url": url,
})
if url != "" {
head = append(head, HeadLink{
Rel: rel, Href: url, Type: type_, Sizes: sizes,
})
}
if p != "" && url != "" {
ct := type_
if ct == "" {
ct = MimeForPath(p)
}
routes = append(routes, IconRoute{URL: url, Path: p, ContentType: ct})
}
}
if !sawFavicon {
for _, ic := range icons {
m, ok := ic.(map[string]any)
if !ok {
continue
}
rel, _ := m["rel"].(string)
p, _ := m["path"].(string)
if p == "" || !strings.Contains(strings.ToLower(rel), "icon") {
continue
}
type_ := cellStr(m["type"])
if type_ == "" {
type_ = MimeForPath(p)
}
routes = append(routes, IconRoute{
URL: "/favicon.ico", Path: p, ContentType: type_,
})
sizes := cellStr(m["sizes"])
head = append([]HeadLink{{
Rel: "icon", Href: "/favicon.ico", Type: type_, Sizes: sizes,
}}, head...)
break
}
}
return icons, head, routes
}
// MergeSiteHead merges site_head into page.head (dedupe by href+rel).
func MergeSiteHead(page map[string]any, siteHead []HeadLink) {
if len(siteHead) == 0 {
return
}
existing := HeadLinksFromJSON(page["head"])
for _, link := range siteHead {
dup := false
for _, e := range existing {
if e.Href == link.Href && strings.EqualFold(e.Rel, link.Rel) {
dup = true
break
}
}
if !dup {
existing = append(existing, link)
}
}
page["head"] = HeadLinksToJSON(existing)
}
// MakeImagesHTML converts GFM image table → HTML fragment.
func MakeImagesHTML(v any) string {
var figures strings.Builder
for _, m := range rowsOf(v) {
src := rowStr(m, imgSrc)
if src == "" {
continue
}
alt := rowStr(m, imgAlt)
title := rowStr(m, imgTitle)
class := rowStr(m, imgClass)
href := rowStr(m, imgHref)
width := rowStr(m, imgWidth)
height := rowStr(m, imgHeight)
loading := rowStr(m, imgLoad)
if loading == "" {
loading = "lazy"
}
caption := rowStr(m, imgCap)
img := fmt.Sprintf(``, esc(loading))
inner := img
if href != "" {
inner = fmt.Sprintf(`%s`, esc(href), img)
}
figClass := "mq-img"
if class != "" {
figClass = "mq-img " + class
}
figures.WriteString(fmt.Sprintf(`