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

d2lib: Incorporate relevant render opts for diagram hash id #2314

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions d2lib/d2.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ func Compile(ctx context.Context, input string, compileOpts *CompileOptions, ren

d, err := compile(ctx, g, compileOpts, renderOpts)
if d != nil {
if config == nil {
config = &d2target.Config{}
}
// These are fields that affect a diagram's appearance, so feed them back
// into diagram.Config to ensure the hash computed for CSS styling purposes
// is unique to its appearance
config.ThemeID = renderOpts.ThemeID
config.DarkThemeID = renderOpts.DarkThemeID
config.Sketch = renderOpts.Sketch
d.Config = config
}
return d, g, err
Expand Down
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/all_shapes/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 79 additions & 79 deletions d2renderers/d2sketch/testdata/all_shapes_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/animated/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 82 additions & 82 deletions d2renderers/d2sketch/testdata/animated_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/arrowheads/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 82 additions & 82 deletions d2renderers/d2sketch/testdata/arrowheads_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/basic/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 79 additions & 79 deletions d2renderers/d2sketch/testdata/basic_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/child_to_child/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 82 additions & 82 deletions d2renderers/d2sketch/testdata/child_to_child_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/class/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 79 additions & 79 deletions d2renderers/d2sketch/testdata/class_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/connection-style-fill/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/connection_label/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 82 additions & 82 deletions d2renderers/d2sketch/testdata/connection_label_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/crows_feet/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
158 changes: 79 additions & 79 deletions d2renderers/d2sketch/testdata/crows_feet_dark/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
168 changes: 84 additions & 84 deletions d2renderers/d2sketch/testdata/dots-3d/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 82 additions & 82 deletions d2renderers/d2sketch/testdata/dots-all/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
164 changes: 82 additions & 82 deletions d2renderers/d2sketch/testdata/dots-multiple/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
194 changes: 97 additions & 97 deletions d2renderers/d2sketch/testdata/dots-real/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/double-border/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/elk_corners/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/long_arrowhead_label/sketch.exp.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
576 changes: 288 additions & 288 deletions d2renderers/d2sketch/testdata/opacity/sketch.exp.svg

Large diffs are not rendered by default.

574 changes: 287 additions & 287 deletions d2renderers/d2sketch/testdata/opacity_dark/sketch.exp.svg

Large diffs are not rendered by default.

168 changes: 84 additions & 84 deletions d2renderers/d2sketch/testdata/overlay/sketch.exp.svg

Large diffs are not rendered by default.

190 changes: 95 additions & 95 deletions d2renderers/d2sketch/testdata/paper-real/sketch.exp.svg

Large diffs are not rendered by default.

570 changes: 285 additions & 285 deletions d2renderers/d2sketch/testdata/root-fill/sketch.exp.svg

Large diffs are not rendered by default.

160 changes: 80 additions & 80 deletions d2renderers/d2sketch/testdata/sql_tables/sketch.exp.svg

Large diffs are not rendered by default.

158 changes: 79 additions & 79 deletions d2renderers/d2sketch/testdata/sql_tables_dark/sketch.exp.svg

Large diffs are not rendered by default.

176 changes: 88 additions & 88 deletions d2renderers/d2sketch/testdata/terminal/sketch.exp.svg

Large diffs are not rendered by default.

580 changes: 290 additions & 290 deletions d2renderers/d2sketch/testdata/twitter/sketch.exp.svg

Large diffs are not rendered by default.

580 changes: 290 additions & 290 deletions d2renderers/d2sketch/testdata/twitter_dark/sketch.exp.svg

Large diffs are not rendered by default.

166 changes: 83 additions & 83 deletions d2renderers/d2sketch/testdata/unfilled_triangle/sketch.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/appendix/testdata/internal-links/sketch.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/appendix/testdata/links/sketch.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/appendix/testdata/links_dark/sketch.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/appendix/testdata/tooltip_fill/sketch.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/dark_theme/testdata/all_shapes/dark_theme.exp.svg

Large diffs are not rendered by default.

158 changes: 79 additions & 79 deletions d2renderers/d2svg/dark_theme/testdata/animated/dark_theme.exp.svg

Large diffs are not rendered by default.

158 changes: 79 additions & 79 deletions d2renderers/d2svg/dark_theme/testdata/arrowheads/dark_theme.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/dark_theme/testdata/basic/dark_theme.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/dark_theme/testdata/class/dark_theme.exp.svg

Large diffs are not rendered by default.

568 changes: 284 additions & 284 deletions d2renderers/d2svg/dark_theme/testdata/code/dark_theme.exp.svg

Large diffs are not rendered by default.

Large diffs are not rendered by default.

568 changes: 284 additions & 284 deletions d2renderers/d2svg/dark_theme/testdata/opacity/dark_theme.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/dark_theme/testdata/overlay/dark_theme.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions d2renderers/d2svg/dark_theme/testdata/sql_tables/dark_theme.exp.svg

Large diffs are not rendered by default.

568 changes: 284 additions & 284 deletions d2renderers/d2svg/dark_theme/testdata/twitter/dark_theme.exp.svg

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions docs/examples/lib/1-d2lib/d2lib_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,65 @@
package main

import (
"context"
"testing"

"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/textmeasure"
"oss.terrastruct.com/util-go/assert"
"oss.terrastruct.com/util-go/go2"
)

func TestMain_(t *testing.T) {
main()
}

func TestConfigHash(t *testing.T) {
var hash1, hash2 string
var err error

{
ruler, _ := textmeasure.NewRuler()
layoutResolver := func(engine string) (d2graph.LayoutGraph, error) {
return d2dagrelayout.DefaultLayout, nil
}
renderOpts := &d2svg.RenderOpts{
Pad: go2.Pointer(int64(5)),
ThemeID: &d2themescatalog.GrapeSoda.ID,
}
compileOpts := &d2lib.CompileOptions{
LayoutResolver: layoutResolver,
Ruler: ruler,
}
ctx := log.WithDefault(context.Background())
diagram, _, _ := d2lib.Compile(ctx, "x -> y", compileOpts, renderOpts)
hash1, err = diagram.HashID()
assert.Success(t, err)
}

{
ruler, _ := textmeasure.NewRuler()
layoutResolver := func(engine string) (d2graph.LayoutGraph, error) {
return d2dagrelayout.DefaultLayout, nil
}
renderOpts := &d2svg.RenderOpts{
Pad: go2.Pointer(int64(5)),
ThemeID: &d2themescatalog.NeutralGrey.ID,
}
compileOpts := &d2lib.CompileOptions{
LayoutResolver: layoutResolver,
Ruler: ruler,
}
ctx := log.WithDefault(context.Background())
diagram, _, _ := d2lib.Compile(ctx, "x -> y", compileOpts, renderOpts)
hash2, err = diagram.HashID()
assert.Success(t, err)
}

assert.NotEqual(t, hash1, hash2)
}
152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/abspath.exp.svg

Large diffs are not rendered by default.

180 changes: 90 additions & 90 deletions e2etests-cli/testdata/TestCLI_E2E/animation.exp.svg

Large diffs are not rendered by default.

158 changes: 79 additions & 79 deletions e2etests-cli/testdata/TestCLI_E2E/board_import/hello-world.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/center.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/chain_icon_import.exp.svg

Large diffs are not rendered by default.

152 changes: 76 additions & 76 deletions e2etests-cli/testdata/TestCLI_E2E/chain_import.exp.svg
Loading