This commit is contained in:
Alexander Wang 2023-06-06 11:09:19 -07:00
parent b37b5571e1
commit df7d6536e9
No known key found for this signature in database
GPG key ID: D89FA31966BDBECE
3 changed files with 17 additions and 3 deletions

View file

@ -109,6 +109,9 @@ An appendix for tooltips and links is added to PNG exports since they are not in
.Ns .
.It Fl d , -debug
Print debug logs.
.It Fl -img-cache Ar true
In watch mode, images used in icons are cached for subsequent compilations. This should be disabled if images might change
.Ns .
.It Fl h , -help
Print usage information and exit.
.It Fl v , -version

View file

@ -67,6 +67,10 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
if err != nil {
return err
}
imgCacheFlag, err := ms.Opts.Bool("IMG_CACHE", "img-cache", "", true, "in watch mode, images used in icons are cached for subsequent compilations. This should be disabled if images might change.")
if err != nil {
return err
}
layoutFlag := ms.Opts.String("D2_LAYOUT", "layout", "l", "dagre", `the layout engine used`)
themeFlag, err := ms.Opts.Int64("D2_THEME", "theme", "t", 0, "the diagram theme ID")
if err != nil {
@ -150,6 +154,9 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
if *debugFlag {
ms.Env.Setenv("DEBUG", "1")
}
if *imgCacheFlag {
ms.Env.Setenv("IMG_CACHE", "1")
}
if *browserFlag != "" {
ms.Env.Setenv("BROWSER", *browserFlag)
}

View file

@ -151,8 +151,10 @@ func runWorkers(ctx context.Context, ms *xmain.State, svg []byte, imgs [][][]byt
}
func worker(ctx context.Context, ms *xmain.State, href []byte, isRemote bool) ([]byte, error) {
if hit, ok := imgCache.Load(string(href)); ok {
return hit.([]byte), nil
if ms.Env.Getenv("IMG_CACHE") == "1" {
if hit, ok := imgCache.Load(string(href)); ok {
return hit.([]byte), nil
}
}
var buf []byte
var mimeType string
@ -175,7 +177,9 @@ func worker(ctx context.Context, ms *xmain.State, href []byte, isRemote bool) ([
b64 := base64.StdEncoding.EncodeToString(buf)
out := []byte(fmt.Sprintf(`<image href="data:%s;base64,%s"`, mimeType, b64))
imgCache.Store(string(href), out)
if ms.Env.Getenv("IMG_CACHE") == "1" {
imgCache.Store(string(href), out)
}
return out, nil
}