How to render icon content with d2 API
zrcoder opened this issue · comments
I want to use d2 API to render icons, as the test below:
func TestTmp(t *testing.T) {
d2 := `github: {icon: https://icons.terrastruct.com/dev/github.svg}
gg: {
icon: https://icons.terrastruct.com/dev/github.svg
shape: image
}`
var ruler *textmeasure.Ruler
var err error
if ruler, err = textmeasure.NewRuler(); err != nil {
t.Error(err)
}
layoutResolver := func(engine string) (d2graph.LayoutGraph, error) {
return d2elklayout.DefaultLayout, nil
}
compileOpts := &d2lib.CompileOptions{
LayoutResolver: layoutResolver,
Ruler: ruler,
}
renderOpt := &d2svg.RenderOpts{}
diagram, _, _ := d2lib.Compile(context.Background(), d2, compileOpts, renderOpt)
data, err := d2svg.Render(diagram, renderOpt)
if err != nil {
t.Error(err)
}
err = os.WriteFile("ttt.svg", data, 0600)
if err != nil {
t.Error(err)
}
}
the saved ttt.svg just contains only href=https://icons.terrastruct.com/dev/github.svg
but not the image content, how should I do?
@zrcoder You can use this code
https://github.com/alixander/d2/blob/808f5c3aaca6b9dbfb0f085c71cc09381da37bbd/d2cli/main.go#L868
It downloads the images and encodes them as base64 in the SVG. I assume that's what you want
@zrcoder You can use this code
https://github.com/alixander/d2/blob/808f5c3aaca6b9dbfb0f085c71cc09381da37bbd/d2cli/main.go#L868
It downloads the images and encodes them as base64 in the SVG. I assume that's what you want
That's it, thanks a lot.