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

inline images #224

Merged
merged 34 commits into from
Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d328b23
2022-11-26 03:26:14PM
alixander Nov 26, 2022
b1ed73a
2022-11-26 03:26:27PM
alixander Nov 26, 2022
d8e33fb
2022-11-26 03:28:34PM
alixander Nov 26, 2022
8c3b218
2022-11-26 03:30:13PM
alixander Nov 26, 2022
ffcf1a0
2022-11-26 03:30:19PM
alixander Nov 26, 2022
b8fd3de
2022-11-26 03:31:17PM
alixander Nov 26, 2022
0191306
2022-11-26 03:32:39PM
alixander Nov 26, 2022
bd8a389
2022-11-26 06:14:41PM
alixander Nov 27, 2022
dda3c6f
2022-11-26 06:17:33PM
alixander Nov 27, 2022
9ad44bd
2022-11-28 01:25:40PM
alixander Nov 28, 2022
9259079
small fry
alixander Nov 29, 2022
a5ab1fe
httptest
alixander Nov 29, 2022
ff02459
no passing respChan
alixander Nov 29, 2022
97f7679
2022-11-29 10:37:57AM
alixander Nov 29, 2022
6c921c9
bytes
alixander Nov 29, 2022
7368d15
typo
alixander Nov 29, 2022
d31ed1d
imgbundler: Fixes
nhooyr Nov 29, 2022
a542cb3
update ci
alixander Nov 29, 2022
38cedb2
fmt
alixander Nov 29, 2022
d7fb9c1
2022-11-29 12:54:30PM
alixander Nov 29, 2022
10e13c3
fixes
alixander Nov 29, 2022
b994318
update sub
alixander Nov 29, 2022
fe75e70
2022-11-29 01:30:22PM
alixander Nov 29, 2022
983b05b
2022-11-29 01:50:48PM
alixander Nov 29, 2022
66b6c7d
2022-11-29 02:20:17PM
alixander Nov 29, 2022
7ba3a93
Merge branch 'master' into png-img
alixander Nov 29, 2022
026ece0
Merge branch 'master' into png-img
alixander Nov 29, 2022
09ae636
2022-11-29 03:16:02PM
alixander Nov 29, 2022
a8b011c
Merge branch 'master' into png-img
alixander Nov 30, 2022
a295af2
2022-11-29 04:38:26PM
alixander Nov 30, 2022
dd1d920
Merge branch 'master' into png-img
alixander Nov 30, 2022
9b982a6
Merge branch 'master' into png-img
alixander Nov 30, 2022
7cbcd88
2022-11-29 05:01:15PM
alixander Nov 30, 2022
de56cda
2022-11-29 05:09:21PM
alixander Nov 30, 2022
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
4 changes: 4 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#### Improvements 🔧

- Local images can now be included, e.g. `icon: ./my_img.png`.
[#146](https://github.com/terrastruct/d2/issues/146)
- [install.sh](./install.sh) prints the dry run message more visibly.
[#266](https://github.com/terrastruct/d2/pull/266)

Expand All @@ -22,3 +24,5 @@
[#159](https://github.com/terrastruct/d2/issues/159)
- Fixes markdown newlines created with a trailing double space or backslash.
[#214](https://github.com/terrastruct/d2/pull/214)
- Fixes images not loading in PNG exports
[#224](https://github.com/terrastruct/d2/pull/224)
29 changes: 27 additions & 2 deletions cmd/d2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"oss.terrastruct.com/d2/d2renderers/textmeasure"
"oss.terrastruct.com/d2/d2themes"
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/imgbundler"
"oss.terrastruct.com/d2/lib/png"
"oss.terrastruct.com/d2/lib/version"
"oss.terrastruct.com/d2/lib/xmain"
Expand Down Expand Up @@ -170,15 +171,15 @@ 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
}
ms.Log.Success.Printf("successfully compiled %v to %v", inputPath, outputPath)
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
Expand Down Expand Up @@ -211,9 +212,26 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, theme
if err != nil {
return nil, err
}
svg, err = imgbundler.InlineLocal(ctx, ms, svg)
if err != nil {
if !isWatching {
ms.Log.Error.Printf("missing/broken remote image(s), writing partial output: %v", err)
} else {
ms.Log.Debug.Printf("ignoring missing/broken local image(s) in watch mode: %v", err)
}
}

out := svg
if filepath.Ext(outputPath) == ".png" {
svg, err = imgbundler.InlineRemote(ctx, ms, svg)
if err != nil {
if !isWatching {
ms.Log.Error.Printf("missing/broken remote image(s), writing partial output: %v", err)
} else {
ms.Log.Debug.Printf("ignoring missing/broken remote image(s) in watch mode: %v", err)
}
}

out, err = png.ConvertSVG(ms, page, svg)
if err != nil {
return nil, err
Expand All @@ -224,6 +242,13 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, theme
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 && ms.Log.Nerrors() > 0 {
os.Exit(1)
}

return svg, nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/d2/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 17 additions & 15 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading