Skip to content

Commit

Permalink
Merge pull request #2302 from alixander/no-xml-tag
Browse files Browse the repository at this point in the history
d2cli: --no-xml-tag
  • Loading branch information
alixander authored Jan 24, 2025
2 parents dc0e5fa + 46796f8 commit 87ce08c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- `d2 fmt` now supports a `--check` flag [#2253](https://github.com/terrastruct/d2/pull/2253)
- CLI: PNG output to stdout is supported using `--stdout-format png -` [#2291](https://github.com/terrastruct/d2/pull/2291)
- Globs: `&connected` and `&leaf` filters are implemented [#2299](https://github.com/terrastruct/d2/pull/2299)
- CLI: add --no-xml-tag for direct HTML embedding [#2302](https://github.com/terrastruct/d2/pull/2302)

#### Improvements 🧹

Expand Down
5 changes: 5 additions & 0 deletions ci/release/template/man/d2.1
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ Lists available themes
.Ns .
.It Ar fmt Ar file.d2 ...
Format all passed files
.It Fl -no-xml-tag Ar false
Omit XML tag (<?xml ...?>) from output SVG files. Useful when generating SVGs for direct HTML embedding
.Ns .
.Ns .
.El
.Sh ENVIRONMENT VARIABLES
Expand Down Expand Up @@ -202,6 +205,8 @@ See -p[ort] flag.
See --browser flag.
.It Ev Sy D2_STDOUT_FORMAT
See --stdout-format flag.
.It Ev Sy D2_NO_XML_TAG
See --no-xml-tag flag.
.El
.Sh SEE ALSO
.Xr d2plugin-tala 1
Expand Down
7 changes: 7 additions & 0 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
return err
}

noXMLTagFlag, err := ms.Opts.Bool("D2_NO_XML_TAG", "no-xml-tag", "", false, "omit XML tag (<?xml ...?>) from output SVG files. Useful when generating SVGs for direct HTML embedding")
if err != nil {
return err
}

plugins, err := d2plugin.ListPlugins(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -318,6 +323,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
ThemeID: themeFlag,
DarkThemeID: darkThemeFlag,
Scale: scale,
NoXMLTag: noXMLTagFlag,
}

if *watchFlag {
Expand Down Expand Up @@ -868,6 +874,7 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts
DarkThemeID: opts.DarkThemeID,
ThemeOverrides: opts.ThemeOverrides,
DarkThemeOverrides: opts.DarkThemeOverrides,
NoXMLTag: opts.NoXMLTag,
Scale: scale,
})
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion d2renderers/d2svg/d2svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type RenderOpts struct {
// MasterID is passed when the diagram should use something other than its own hash for unique targeting
// Currently, that's when multi-boards are collapsed
MasterID string
NoXMLTag *bool
}

func dimensions(diagram *d2target.Diagram, pad int) (left, top, width, height int) {
Expand Down Expand Up @@ -2117,7 +2118,9 @@ func Render(diagram *d2target.Diagram, opts *RenderOpts) ([]byte, error) {
w, h,
dimensions,
)
xmlTag = `<?xml version="1.0" encoding="utf-8"?>`
if opts.NoXMLTag == nil || !*opts.NoXMLTag {
xmlTag = `<?xml version="1.0" encoding="utf-8"?>`
}
fitToScreenWrapperClosing = "</svg>"
idAttr = `id="d2-svg"`
tag = "svg"
Expand Down
23 changes: 23 additions & 0 deletions e2etests-cli/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,29 @@ layers: {
testdataIgnoreDiff(t, ".pptx", file)
},
},
{
name: "no_xml_tag",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
writeFile(t, dir, "test.d2", `x -> y`)
err := runTestMain(t, ctx, dir, env, "--no-xml-tag", "test.d2", "no-xml.svg")
assert.Success(t, err)
noXMLSvg := readFile(t, dir, "no-xml.svg")
assert.False(t, strings.Contains(string(noXMLSvg), "<?xml"))

writeFile(t, dir, "test.d2", `x -> y`)
err = runTestMain(t, ctx, dir, env, "test.d2", "with-xml.svg")
assert.Success(t, err)
withXMLSvg := readFile(t, dir, "with-xml.svg")
assert.True(t, strings.Contains(string(withXMLSvg), "<?xml"))

env.Setenv("D2_NO_XML_TAG", "1")
writeFile(t, dir, "test.d2", `x -> y`)
err = runTestMain(t, ctx, dir, env, "test.d2", "no-xml-env.svg")
assert.Success(t, err)
noXMLEnvSvg := readFile(t, dir, "no-xml-env.svg")
assert.False(t, strings.Contains(string(noXMLEnvSvg), "<?xml"))
},
},
{
name: "basic-fmt",
run: func(t *testing.T, ctx context.Context, dir string, env *xos.Env) {
Expand Down

0 comments on commit 87ce08c

Please sign in to comment.