PR comments

This commit is contained in:
Bernard Xie 2022-11-18 13:29:30 -08:00
parent 8b453eae9a
commit 0f95288b5c
No known key found for this signature in database
GPG key ID: 3C3E0036CE0F892C
3 changed files with 25 additions and 20 deletions

View file

@ -186,7 +186,7 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, input
} }
if filepath.Ext(outputPath) == ".png" { if filepath.Ext(outputPath) == ".png" {
outputImage, err = png.ExportPNG(ms, page, outputImage) outputImage, err = png.ConvertSVG(ms, page, outputImage)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -332,6 +332,11 @@ func (w *watcher) compileLoop(ctx context.Context) error {
if filepath.Ext(w.outputPath) == ".png" && !w.pw.Browser.IsConnected() { if filepath.Ext(w.outputPath) == ".png" && !w.pw.Browser.IsConnected() {
newPW, err := w.pw.RestartBrowser() newPW, err := w.pw.RestartBrowser()
if err != nil { if err != nil {
broadcastErr := fmt.Errorf("issue encountered with PNG exporter, stopping")
w.ms.Log.Error.Print(broadcastErr)
w.broadcast(&compileResult{
Err: broadcastErr.Error(),
})
return fmt.Errorf("Playwright could not be restarted: %w", err) return fmt.Errorf("Playwright could not be restarted: %w", err)
} }
w.pw = newPW w.pw = newPW

View file

@ -64,25 +64,28 @@ func InitPlaywright() (Playwright, error) {
if err != nil { if err != nil {
return Playwright{}, err return Playwright{}, err
} }
if _, err := os.Stat(driver.DriverBinaryLocation); os.IsNotExist(err) { _, err = os.Stat(driver.DriverBinaryLocation)
err = playwright.Install() if err != nil {
if err != nil { if os.IsNotExist(err) {
return Playwright{}, err
}
} else if err == nil {
cmd := exec.Command(driver.DriverBinaryLocation, "--version")
output, err := cmd.Output()
if err != nil {
return Playwright{}, fmt.Errorf("error getting Playwright version: %w\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", err)
}
if !bytes.Contains(output, []byte(driver.Version)) {
err = playwright.Install() err = playwright.Install()
if err != nil { if err != nil {
return Playwright{}, err return Playwright{}, err
} }
} else {
return Playwright{}, fmt.Errorf("could not access Playwright binary location: %v\nerror: %w\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", driver.DriverBinaryLocation, err)
}
}
cmd := exec.Command(driver.DriverBinaryLocation, "--version")
output, err := cmd.Output()
if err != nil {
return Playwright{}, fmt.Errorf("error getting Playwright version: %w\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", err)
}
if !bytes.Contains(output, []byte(driver.Version)) {
err = playwright.Install()
if err != nil {
return Playwright{}, err
} }
} else {
return Playwright{}, fmt.Errorf("could not access Playwright binary location: %v\nerror: %w\nplease report this issue here: https://github.com/terrastruct/d2/issues/new", driver.DriverBinaryLocation, err)
} }
pw, err := playwright.Run() pw, err := playwright.Run()
@ -95,11 +98,9 @@ func InitPlaywright() (Playwright, error) {
//go:embed generate_png.js //go:embed generate_png.js
var genPNGScript string var genPNGScript string
func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage []byte, err error) { const pngPrefix = "data:image/png;base64,"
if page == nil {
return nil, fmt.Errorf("Playwright was not initialized properly\nplease report this issue here: https://github.com/terrastruct/d2/issues/new")
}
func ConvertSVG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage []byte, err error) {
encodedSVG := base64.StdEncoding.EncodeToString(svg) encodedSVG := base64.StdEncoding.EncodeToString(svg)
pngInterface, err := page.Evaluate(genPNGScript, "data:image/svg+xml;charset=utf-8;base64,"+encodedSVG) pngInterface, err := page.Evaluate(genPNGScript, "data:image/svg+xml;charset=utf-8;base64,"+encodedSVG)
if err != nil { if err != nil {
@ -107,7 +108,6 @@ func ExportPNG(ms *xmain.State, page playwright.Page, svg []byte) (outputImage [
} }
pngString := fmt.Sprintf("%v", pngInterface) pngString := fmt.Sprintf("%v", pngInterface)
pngPrefix := "data:image/png;base64,"
if !strings.HasPrefix(pngString, pngPrefix) { if !strings.HasPrefix(pngString, pngPrefix) {
if len(pngString) > 50 { if len(pngString) > 50 {
pngString = pngString[0:50] + "..." pngString = pngString[0:50] + "..."