Skip to content
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions d2cli/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const PPTX exportExtension = ".pptx"
const PDF exportExtension = ".pdf"
const SVG exportExtension = ".svg"
const TXT exportExtension = ".txt"
const JSON exportExtension = ".json"

var SUPPORTED_EXTENSIONS = []exportExtension{SVG, PNG, PDF, PPTX, GIF, TXT}
var SUPPORTED_EXTENSIONS = []exportExtension{SVG, PNG, PDF, PPTX, GIF, TXT, JSON}

var STDOUT_FORMAT_MAP = map[string]exportExtension{
"png": PNG,
Expand All @@ -25,9 +26,10 @@ var STDOUT_FORMAT_MAP = map[string]exportExtension{
"pdf": PDF,
"pptx": PPTX,
"gif": GIF,
"json": JSON,
}

var SUPPORTED_STDOUT_FORMATS = []string{"png", "svg", "ascii", "txt", "pdf", "pptx", "gif"}
var SUPPORTED_STDOUT_FORMATS = []string{"png", "svg", "ascii", "txt", "pdf", "pptx", "gif", "json"}

func getOutputFormat(stdoutFormatFlag *string, outputPath string) (exportExtension, error) {
if stdoutFormatFlag != nil && *stdoutFormatFlag != "" {
Expand Down
17 changes: 17 additions & 0 deletions d2cli/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ func TestOutputFormat(t *testing.T) {
requiresAnimationInterval: true,
requiresPngRender: true,
},
{
stdoutFormatFlag: "json",
outputPath: "-",
extension: JSON,
supportsDarkTheme: false,
supportsAnimation: false,
requiresAnimationInterval: false,
requiresPngRender: false,
},
{
outputPath: "/out.json",
extension: JSON,
supportsDarkTheme: false,
supportsAnimation: false,
requiresAnimationInterval: false,
requiresPngRender: false,
},
}

for _, tc := range testCases {
Expand Down
4 changes: 2 additions & 2 deletions d2cli/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import (
func help(ms *xmain.State) {
fmt.Fprintf(ms.Stdout, `%[1]s %[2]s
Usage:
%[1]s [--watch=false] [--theme=0] file.d2 [file.svg | file.png | file.pdf | file.pptx | file.gif | file.txt]
%[1]s [--watch=false] [--theme=0] file.d2 [file.svg | file.png | file.pdf | file.pptx | file.gif | file.txt | file.json]
%[1]s layout [name]
%[1]s fmt file.d2 ...
%[1]s play [--theme=0] [--sketch] file.d2
%[1]s validate file.d2

%[1]s compiles and renders file.d2 to file.svg | file.png | file.pdf | file.pptx | file.gif | file.txt
%[1]s compiles and renders file.d2 to file.svg | file.png | file.pdf | file.pptx | file.gif | file.txt | file.json
It defaults to file.svg if an output path is not provided.

Use - to have d2 read from stdin or write to stdout.
Expand Down
14 changes: 13 additions & 1 deletion d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
if err != nil {
return err
}
stdoutFormatFlag := ms.Opts.String("", "stdout-format", "", "", "output format when writing to stdout (svg, png, ascii, txt, pdf, pptx, gif). Usage: d2 input.d2 --stdout-format png - > output.png")
stdoutFormatFlag := ms.Opts.String("", "stdout-format", "", "", "output format when writing to stdout (svg, png, ascii, txt, pdf, pptx, gif, json). Usage: d2 input.d2 --stdout-format png - > output.png")
if err != nil {
return err
}
Expand Down Expand Up @@ -940,6 +940,18 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts
}
return ascii, nil
}
if outputFormat == JSON {
jsonBytes, err := json.MarshalIndent(diagram, "", " ")
if err != nil {
return nil, err
}
jsonBytes = append(jsonBytes, '\n')
err = Write(ms, outputPath, jsonBytes)
if err != nil {
return jsonBytes, err
}
return jsonBytes, nil
}
toPNG := outputFormat == PNG

var scale *float64
Expand Down