A d2 extension for Emacs. This was heavily inspired of Mermaid Mode.
Status: Alpha. Available on Melpa.
It works you can use this to render d2 in both your browser and in an emacs buffer. See Bugs & Issues for known issues.
Table of Contents
- d2-mode
- Why D2
- Installation
- Usage
- Customization
d2
binary location- Output format
- Temp directory
- Key bindings
- Bonus
- Roadmap
- Bugs & Issues
- Contributors
Text to graph diagrams are awesome. I used Mermaid.js all the time and it was fantastic, but there were a few things it couldn't do so I wanted to expand my options.
Some things that I've noticed d2 has some interesting support in:
- More themes
- Code Blocks
- More visual customization
- More graph support
- Autoformat
Learn more about d2 here
- Load the d2-mode.el file or install from melpa:
M-x package-install d2-mode
- Install d2 binary from the d2 project if you plan to compile graphs in Emacs
C-c C-c - compile current file to an image
C-c C-f - compile given file to an image
C-c C-b - compile current buffer to an image
C-c C-r - compile current region to an image
C-c C-o - open in the live editor
C-c C-d - open the official doc
Note: All compile commands will open the output in a buffer to view the resulting image.
You can specify the location of d2
with the variable d2-location
, the default assumes you have the binary in your PATH
(and for that you probably want/need to install d2
).
By default d2
will compile to svg format. You can change that by setting the variable d2-output-format
.
By default d2-tmp-dir
points to \tmp\
. Feel free to set it to a more appropriate location that works for you (e.g. on windows).
To customize the key bindings put this into your init.el
...
(defvar d2-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-c") 'd2-compile)
(define-key map (kbd "C-c C-f") 'd2-compile-file)
(define-key map (kbd "C-c C-b") 'd2-compile-buffer)
(define-key map (kbd "C-c C-r") 'd2-compile-region)
(define-key map (kbd "C-c C-h") 'd2-compile-file-and-browse)
(define-key map (kbd "C-c C-j") 'd2-compile-buffer-and-browse)
(define-key map (kbd "C-c C-k") 'd2-compile-region-and-browse)
(define-key map (kbd "C-c C-o") 'd2-open-browser)
(define-key map (kbd "C-x C-o") 'd2-view-current-svg)
(define-key map (kbd "C-c C-d") 'd2-open-doc)
map))
See the snippets
folder for yas snippets. Start making d2 graphs today!
- Even better syntax highlighting and formatting
- Watch mode support
- SVG's have issues rending in emacs sometimes. See
#13 and
#8 for more details. You can use the
*browse
commands to render it directly in your browser instead!
Feel free to open an issue and or contribute to this repo over a PR!
We thank anyone that decides to contribute to this repository and encourage contributions.
Also thank you to @suliveevil for bringing up issues.