7
"xelbot.com/reprogl/models"
10
func GenerateArticleStyles(article *models.Article, acceptAvif, acceptWebp bool) string {
13
style += commonStyle() + "\n"
14
if article.HasImage() {
15
style += styleWithImage(article.FeaturedImage, acceptAvif, acceptWebp)
17
style += defaultStyleWithoutImage() + "\n"
21
return cdnReplace(style)
24
func styleWithImage(image models.FeaturedImage, acceptAvif, acceptWebp bool) string {
27
srcSet := image.DecodeSrcSet()
28
if acceptAvif && image.HasAvif() {
29
cssRules = stylesImagesByFormat(srcSet, "avif")
30
} else if acceptWebp && image.HasWebp() {
31
cssRules = stylesImagesByFormat(srcSet, "webp")
33
cssRules = stylesImagesByFormat(srcSet, "origin")
35
srcSetItem, _ := srcSet["webp"]
36
cssRules += " @supports (background-image:url(%cdn%/uploads/"
37
cssRules += srcSetItem.Items[0].Path + ")){\n"
38
cssRules += strings.TrimRight(stylesImagesByFormat(srcSet, "webp"), "\n") + "}\n"
45
func stylesImagesByFormat(srcSet map[string]models.SrcSetItem, format string) string {
49
if srcSetItem, found := srcSet[format]; found {
50
for _, srcImage := range srcSetItem.Items {
52
cssRules += " .post-view .post-view-sidebar{background-image:url(%cdn%/uploads/"
53
cssRules += srcImage.Path + ")}\n"
56
cssRules += " @media only screen and (max-width:" + strconv.Itoa(srcImage.Width) + "px){"
57
cssRules += ".post-view .post-view-sidebar{background-image:url(%cdn%/uploads/"
58
cssRules += srcImage.Path + ")}}\n"