feat: use 'center' for image widths 760<x<1800

This commit is contained in:
Johan Fagerberg 2026-02-19 10:56:26 +01:00 committed by Manuel Rüger
parent cbc7400f92
commit 9c58c36b46
2 changed files with 62 additions and 47 deletions

View File

@ -16,42 +16,49 @@ import (
"github.com/yuin/goldmark/util" "github.com/yuin/goldmark/util"
) )
// calculateAlign determines the appropriate ac:align value based on width // calculateAlign determines the appropriate ac:align value
// Images >= 760px wide use "wide", otherwise use the configured alignment // Images >= 760px must use "center" alignment, smaller images can use configured alignment
func calculateAlign(configuredAlign string, width string) string { func calculateAlign(configuredAlign string, width string) string {
// No alignment configured
if configuredAlign == "" { if configuredAlign == "" {
return "" return ""
} }
if width == "" { // Check if image is wide enough to require center alignment
return configuredAlign
}
// Parse width and check if >= 760
widthInt, err := strconv.Atoi(width)
if err != nil {
return configuredAlign
}
if widthInt >= 760 {
return "wide"
}
return configuredAlign
}
// calculateLayout determines the appropriate ac:layout value based on alignment and width
// Images >= 1800px use "full-width", otherwise based on alignment
func calculateLayout(align string, width string) string {
// Check if full-width should be used
if width != "" { if width != "" {
widthInt, err := strconv.Atoi(width) widthInt, err := strconv.Atoi(width)
if err == nil && widthInt >= 1800 { if err == nil && widthInt >= 760 {
return "full-width" return "center"
} }
} }
// Otherwise use layout based on alignment // For images < 760px, use configured alignment
return configuredAlign
}
// calculateLayout determines the appropriate ac:layout value based on width and alignment
// Images >= 1800px use "full-width", images >= 760px use "wide", otherwise based on alignment
// Returns empty string if no alignment is configured
func calculateLayout(align string, width string) string {
// If no alignment configured, don't set layout
if align == "" {
return ""
}
// Check width thresholds first
if width != "" {
widthInt, err := strconv.Atoi(width)
if err == nil {
if widthInt >= 1800 {
return "full-width"
}
if widthInt >= 760 {
return "wide"
}
}
}
// For images < 760px, use layout based on alignment
switch align { switch align {
case "left": case "left":
return "align-start" return "align-start"
@ -59,8 +66,6 @@ func calculateLayout(align string, width string) string {
return "center" return "center"
case "right": case "right":
return "align-end" return "align-end"
case "wide":
return "center"
default: default:
return "" return ""
} }

View File

@ -10,14 +10,13 @@ func TestCalculateAlign(t *testing.T) {
expectedAlign string expectedAlign string
}{ }{
{"No alignment configured", "", "1000", ""}, {"No alignment configured", "", "1000", ""},
{"No width available", "center", "", "center"}, {"Center alignment small", "center", "500", "center"},
{"Below threshold", "center", "500", "center"}, {"Left alignment small", "left", "500", "left"},
{"At threshold", "center", "760", "wide"}, {"Right alignment small", "right", "500", "right"},
{"Above threshold", "center", "1000", "wide"}, {"Left forced to center at 760px", "left", "760", "center"},
{"Left below threshold", "left", "700", "left"}, {"Left forced to center above 760px", "left", "1000", "center"},
{"Left at threshold", "left", "760", "wide"}, {"Right forced to center at 1800px", "right", "1800", "center"},
{"Invalid width", "center", "abc", "center"}, {"No width provided", "left", "", "left"},
{"Large image", "center", "2000", "wide"},
} }
for _, tt := range tests { for _, tt := range tests {
@ -37,16 +36,26 @@ func TestCalculateLayout(t *testing.T) {
width string width string
expectedLayout string expectedLayout string
}{ }{
{"Left alignment", "left", "500", "align-start"}, // Small images (< 760px) use alignment-based layout
{"Center alignment", "center", "500", "center"}, {"Left alignment small", "left", "500", "align-start"},
{"Right alignment", "right", "500", "align-end"}, {"Center alignment small", "center", "500", "center"},
{"Wide alignment", "wide", "1000", "center"}, {"Right alignment small", "right", "500", "align-end"},
{"Full-width threshold", "center", "1800", "full-width"}, {"No alignment small", "", "500", ""},
{"Above full-width", "left", "2000", "full-width"},
{"Below full-width", "center", "1799", "center"}, // Medium images (760-1799px) use "wide" layout (must be center align)
{"No alignment", "", "1000", ""}, {"Center at 760px", "center", "760", "wide"},
{"Unknown alignment", "justify", "500", ""}, {"Center at 1000px", "center", "1000", "wide"},
{"Center at 1799px", "center", "1799", "wide"},
// Large images (>= 1800px) use "full-width" layout (must be center align)
{"Center at 1800px", "center", "1800", "full-width"},
{"Center at 2000px", "center", "2000", "full-width"},
// Edge cases
{"No width", "center", "", "center"},
{"Invalid width", "center", "abc", "center"}, {"Invalid width", "center", "abc", "center"},
{"Empty alignment and width", "", "", ""},
{"No alignment configured", "", "1000", ""},
} }
for _, tt := range tests { for _, tt := range tests {
@ -67,8 +76,9 @@ func TestCalculateDisplayWidth(t *testing.T) {
expectedWidth string expectedWidth string
}{ }{
{"Full-width layout", "2000", "full-width", "1800"}, {"Full-width layout", "2000", "full-width", "1800"},
{"Center layout keeps original", "1000", "center", "1000"}, {"Wide layout keeps original", "1000", "wide", "1000"},
{"Align-start keeps original", "800", "align-start", "800"}, {"Center layout keeps original", "800", "center", "800"},
{"Align-start keeps original", "500", "align-start", "500"},
{"Empty original", "", "center", ""}, {"Empty original", "", "center", ""},
{"Empty layout", "1000", "", "1000"}, {"Empty layout", "1000", "", "1000"},
} }