2022-11-29 12:54:30PM
This commit is contained in:
parent
38cedb21cb
commit
d7fb9c16b8
4 changed files with 24 additions and 44 deletions
|
|
@ -169,7 +169,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
_ = 343
|
||||
}
|
||||
|
||||
_, err = compile(ctx, ms, plugin, *themeFlag, inputPath, outputPath, pw.Page)
|
||||
_, err = compile(ctx, ms, false, plugin, *themeFlag, inputPath, outputPath, pw.Page)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -177,7 +177,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
|
|||
return nil
|
||||
}
|
||||
|
||||
func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, themeID int64, inputPath, outputPath string, page playwright.Page) ([]byte, error) {
|
||||
func compile(ctx context.Context, ms *xmain.State, isWatching bool, plugin d2plugin.Plugin, themeID int64, inputPath, outputPath string, page playwright.Page) ([]byte, error) {
|
||||
input, err := ms.ReadPath(inputPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -205,16 +205,24 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, theme
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
svg, err = imgbundler.InlineLocal(ms, svg)
|
||||
svg, err = imgbundler.InlineLocal(svg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// Missing/broken images are fine during watch mode, as the user is likely building up a diagram.
|
||||
// Otherwise, the assumption is that this diagram is building for production, and broken images are not okay.
|
||||
if !isWatching {
|
||||
return nil, err
|
||||
}
|
||||
ms.Log.Debug.Printf("ignoring missing/broken local image in watch mode: %v", err)
|
||||
}
|
||||
|
||||
out := svg
|
||||
if filepath.Ext(outputPath) == ".png" {
|
||||
svg, err = imgbundler.InlineRemote(ms, svg)
|
||||
svg, err = imgbundler.InlineRemote(svg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if !isWatching {
|
||||
return nil, err
|
||||
}
|
||||
ms.Log.Debug.Printf("ignoring missing/broken remote image in watch mode: %v", err)
|
||||
}
|
||||
|
||||
out, err = png.ConvertSVG(ms, page, svg)
|
||||
|
|
|
|||
|
|
@ -345,7 +345,7 @@ func (w *watcher) compileLoop(ctx context.Context) error {
|
|||
w.pw = newPW
|
||||
}
|
||||
|
||||
b, err := compile(ctx, w.ms, w.layoutPlugin, w.themeID, w.inputPath, w.outputPath, w.pw.Page)
|
||||
b, err := compile(ctx, w.ms, true, w.layoutPlugin, w.themeID, w.inputPath, w.outputPath, w.pw.Page)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to %scompile: %w", recompiledPrefix, err)
|
||||
w.ms.Log.Error.Print(err)
|
||||
|
|
|
|||
|
|
@ -16,8 +16,6 @@ import (
|
|||
|
||||
"go.uber.org/multierr"
|
||||
"oss.terrastruct.com/xdefer"
|
||||
|
||||
"oss.terrastruct.com/d2/lib/xmain"
|
||||
)
|
||||
|
||||
var imageRe = regexp.MustCompile(`<image href="([^"]+)"`)
|
||||
|
|
@ -28,15 +26,15 @@ type resp struct {
|
|||
err error
|
||||
}
|
||||
|
||||
func InlineLocal(ms *xmain.State, in []byte) ([]byte, error) {
|
||||
return inline(ms, in, false)
|
||||
func InlineLocal(in []byte) ([]byte, error) {
|
||||
return inline(in, false)
|
||||
}
|
||||
|
||||
func InlineRemote(ms *xmain.State, in []byte) ([]byte, error) {
|
||||
return inline(ms, in, true)
|
||||
func InlineRemote(in []byte) ([]byte, error) {
|
||||
return inline(in, true)
|
||||
}
|
||||
|
||||
func inline(ms *xmain.State, svg []byte, isRemote bool) (_ []byte, err error) {
|
||||
func inline(svg []byte, isRemote bool) (_ []byte, err error) {
|
||||
defer xdefer.Errorf(&err, "failed to bundle images")
|
||||
imgs := imageRe.FindAllSubmatch(svg, -1)
|
||||
|
||||
|
|
@ -95,7 +93,7 @@ func inline(ms *xmain.State, svg []byte, isRemote bool) (_ []byte, err error) {
|
|||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, fmt.Errorf("failed to wait for imgbundler workers: %w", ctx.Err())
|
||||
return nil, fmt.Errorf("failed waiting for imgbundler workers: %w", ctx.Err())
|
||||
case resp, ok := <-respChan:
|
||||
if !ok {
|
||||
return svg, nil
|
||||
|
|
@ -112,7 +110,7 @@ func inline(ms *xmain.State, svg []byte, isRemote bool) (_ []byte, err error) {
|
|||
var transport = http.DefaultTransport
|
||||
|
||||
func fetch(ctx context.Context, href string) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
ctx, cancel := context.WithTimeout(ctx, time.Minute)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", href, nil)
|
||||
|
|
|
|||
|
|
@ -5,15 +5,9 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"oss.terrastruct.com/cmdlog"
|
||||
"oss.terrastruct.com/xos"
|
||||
|
||||
"oss.terrastruct.com/d2/lib/xmain"
|
||||
)
|
||||
|
||||
//go:embed test_png.png
|
||||
|
|
@ -90,17 +84,7 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
|
|||
return respRecorder.Result()
|
||||
})
|
||||
|
||||
ms := &xmain.State{
|
||||
Name: "test",
|
||||
|
||||
Stdin: os.Stdin,
|
||||
Stdout: os.Stdout,
|
||||
Stderr: os.Stderr,
|
||||
|
||||
Env: xos.NewEnv(os.Environ()),
|
||||
}
|
||||
ms.Log = cmdlog.Log(ms.Env, os.Stderr)
|
||||
out, err := InlineRemote(ms, []byte(sampleSVG))
|
||||
out, err := InlineRemote([]byte(sampleSVG))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -151,17 +135,7 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
|
|||
}]]></style></svg>
|
||||
`, svgURL, pngURL)
|
||||
|
||||
ms := &xmain.State{
|
||||
Name: "test",
|
||||
|
||||
Stdin: os.Stdin,
|
||||
Stdout: os.Stdout,
|
||||
Stderr: os.Stderr,
|
||||
|
||||
Env: xos.NewEnv(os.Environ()),
|
||||
}
|
||||
ms.Log = cmdlog.Log(ms.Env, os.Stderr)
|
||||
out, err := InlineLocal(ms, []byte(sampleSVG))
|
||||
out, err := InlineLocal([]byte(sampleSVG))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue