【小众插件分享】 d2-mode: 从文本到图表,一个相比 mermaid 更具可读性的选择

大佬,发现一个问题啊,在MacOS下的org babel src里会报错,报错内容和下面的命令行的报错内容一样:

#+BEGIN_SRC d2 :file d2_20221212_212218.png
x -> y: hello world
#+END_SRC

到命令行里看了下,貌似没有 -o-i 的参数:

$ d2 -o ~/Temp/test.png -i d2-test.txt
err: bad usage: failed to parse flags: unknown shorthand flag: 'o' in -o
err: Run with --help to see usage.

我改了下这个函数可以了:

  (defun org-babel-execute:d2 (body params)
    "Execute command with BODY and PARAMS from src block."
    (let* ((out-file (or (cdr (assoc :file params))
                         (error "D2 requires a \":file\" header argument")))
           (temp-file (org-babel-temp-file "d2-"))
           (cmd (concat (shell-quote-argument d2-location)
                        " " temp-file
                        " " (org-babel-process-file-name out-file)                        
                        " " d2-flags))
           )
      (with-temp-file temp-file (insert body))
      (org-babel-eval cmd "")
      nil))

大佬有空可以看看是不是MacOS下的d2不太一样?

/opt/homebrew/bin/d2

version 0.1.1

d2 --help
Usage:
  d2 [--watch=false] [--theme=0] file.d2 [file.svg | file.png]
  d2 layout [name]
  d2 fmt file.d2

d2 compiles and renders file.d2 to file.svg | file.png
It defaults to file.svg if an output path is not provided.

Use - to have d2 read from stdin or write to stdout.

See man d2 for more detailed docs.

Flags:
  -w, --watch           $D2_WATCH   watch for changes to input and live reload. Use $HOST and $PORT to specify the listening address.
                                    (default localhost:0, which is will open on a randomly available local port). (default false)
  -h, --host string     $HOST       host listening address when used with watch (default "localhost")
  -p, --port string     $PORT       port listening address when used with watch (default "0")
  -b, --bundle          $D2_BUNDLE  when outputting SVG, bundle all assets and layers into the output file. (default true)
  -d, --debug           $DEBUG      print debug logs. (default false)
  -l, --layout string   $D2_LAYOUT  the layout engine used. (default "dagre")
  -t, --theme int       $D2_THEME   the diagram theme ID. For a list of available options, see https://oss.terrastruct.com/d2 (default 0)
  -v, --version                     get the version (default false)


Subcommands:
  d2 layout - Lists available layout engine options with short help
  d2 layout [name] - Display long help for a particular layout engine
  d2 fmt file.d2 - Format file.d2

See more docs and the source code at https://oss.terrastruct.com/d2

为啥默认 box 那么大⋯⋯

排版引擎的问题吧。

你可以设置 $D2_LAYOUT 的环境变量为 elk,换一个引擎效果就不一样了,D2支持3种Layout引擎,你这张图里的是 tala 是要License的。然后就是加一行 direction: right

2 个赞

对于org-mode中的多级headline 是否可以用D2 来绘图?

抱歉,没有从d2的相关文档里看到有这个功能。

我理解你的需求是将Org mode里的多级headline变成类似脑图?如果确实是这样的需求的话可以看下org-mind-map插件

通过 org mode headline 绘图可以看看:

如果headline 下有时间戳,ĬÐ的记录好像不能画mind-map图?

:rofl: 这个倒是没测过。我通常不会在headline 加这些信息。可以在 github 上提个issue,我看看是否能修复。