Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

render: update background rect width and height for appendix #941

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions d2renderers/d2svg/appendix/appendix.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
var viewboxRegex = regexp.MustCompile(`viewBox=\"([0-9\- ]+)\"`)
var widthRegex = regexp.MustCompile(`width=\"([.0-9]+)\"`)
var heightRegex = regexp.MustCompile(`height=\"([.0-9]+)\"`)
var svgRegex = regexp.MustCompile(`<svg(.*?)>`)

func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []byte {
svg := string(in)
Expand Down Expand Up @@ -91,14 +92,22 @@ func Append(diagram *d2target.Diagram, ruler *textmeasure.Ruler, in []byte) []by
newOuterViewbox := fmt.Sprintf(`viewBox="0 0 %d %d"`, viewboxWidth, viewboxHeight)
newViewbox := fmt.Sprintf(`viewBox="%s %s %s %s"`, viewboxSlice[0], viewboxSlice[1], strconv.Itoa(viewboxWidth), strconv.Itoa(viewboxHeight))

widthMatches := widthRegex.FindAllStringSubmatch(svg, 2)
heightMatches := heightRegex.FindAllStringSubmatch(svg, 2)
dimensionsToUpdate := 2
outerSVG := svgRegex.FindString(svg)
// if outer svg has dimensions set we also need to update it
if widthRegex.FindString(outerSVG) != "" {
dimensionsToUpdate++
}

// update 1st 3 matches of width and height 1st is outer svg (if dimensions are set), 2nd inner svg, 3rd is background color rect
widthMatches := widthRegex.FindAllStringSubmatch(svg, dimensionsToUpdate)
heightMatches := heightRegex.FindAllStringSubmatch(svg, dimensionsToUpdate)
newWidth := fmt.Sprintf(`width="%s"`, strconv.Itoa(viewboxWidth))
newHeight := fmt.Sprintf(`height="%s"`, strconv.Itoa(viewboxHeight))

svg = strings.Replace(svg, viewboxMatches[0][0], newOuterViewbox, 1)
svg = strings.Replace(svg, viewboxMatch[0], newViewbox, 1)
for i := 0; i < 2; i++ {
for i := 0; i < dimensionsToUpdate; i++ {
svg = strings.Replace(svg, widthMatches[i][0], newWidth, 1)
svg = strings.Replace(svg, heightMatches[i][0], newHeight, 1)
}
Expand Down