Skip to content

Commit 345ac20

Browse files
committed
d2 dark theme
1 parent 67788dd commit 345ac20

File tree

7 files changed

+32
-178
lines changed

7 files changed

+32
-178
lines changed

netlify.toml

-2
This file was deleted.

packages/docs/src/content/docs/diagrams/d2.mdx

+9-38
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Rehype plugin to generate [D2](https://d2lang.com/) diagrams in place of code fe
1010
<Tabs>
1111
<TabItem label="Diagram">
1212

13-
```d2 pad=10
13+
```d2
1414
direction: right
1515
x.y.z -> a.b.c: Label
1616
```
@@ -19,7 +19,7 @@ x.y.z -> a.b.c: Label
1919
<TabItem label="Markdown">
2020

2121
````md
22-
```d2 pad=10
22+
```d2
2323
direction: right
2424
x.y.z -> a.b.c: Label
2525
```
@@ -32,12 +32,6 @@ x.y.z -> a.b.c: Label
3232

3333
<PackageManagers pkg="@beoe/rehype-d2" />
3434

35-
You need to install D2 as well:
36-
37-
```sh
38-
curl -fsSL https://d2lang.com/install.sh | sh -s --
39-
```
40-
4135
## Usage
4236

4337
```js
@@ -82,50 +76,27 @@ export type D2Options = {
8276
* Set the diagram layout engine to the passed string. For a
8377
* list of available options, run layout.
8478
*/
85-
layout?: string;
86-
/**
87-
* @default 100
88-
* Pixels padded around the rendered diagram.
89-
*/
90-
pad?: number;
91-
/**
92-
* @default -1
93-
* Scale the output. E.g., 0.5 to halve the default size.
94-
* Default -1 means that SVG's will fit to screen and all others
95-
* will use their default render size. Setting to 1 turns off
96-
* SVG fitting to screen.
97-
*/
98-
scale?: number;
79+
layout?: "dagre" | "elk";
9980
/**
10081
* @default false
10182
* Renders the diagram to look like it was sketched by hand.
10283
*/
10384
sketch?: boolean;
104-
/**
105-
* @default true
106-
* Bundle all assets and layers into the output svg.
107-
*/
108-
bundle?: boolean;
109-
/**
110-
* Center the SVG in the containing viewbox, such as your
111-
* browser screen.
112-
*/
113-
center?: boolean;
11485
};
11586
```
11687

11788
You can set it globally:
11889

11990
```ts
12091
use(rehypeD2, {
121-
d2Options: { pad: 20 },
92+
d2Options: { layout: "dagre" },
12293
});
12394
```
12495

12596
Or locally:
12697

12798
````md
128-
```d2 pad=10
99+
```d2
129100
...
130101
```
131102
````
@@ -138,10 +109,10 @@ You can create small Netlify plugin to install D2. See example [here](https://gi
138109

139110
## Issues
140111

141-
- [ ] [Support links in connections](https://github.com/terrastruct/d2/pull/1955)
142-
- [ ] [JS package](https://github.com/terrastruct/d2/discussions/234#discussioncomment-11286029)
112+
- [x] [Support links in connections](https://github.com/terrastruct/d2/pull/1955)
113+
- [x] [JS package](https://github.com/terrastruct/d2/discussions/234#discussioncomment-11286029)
114+
- [ ] [Export JSON graph](https://github.com/terrastruct/d2/discussions/2224)
115+
- [ ] [Class-based dark mode](https://github.com/terrastruct/d2/pull/1803)
143116
- [ ] [Remove embeded fonts](https://github.com/terrastruct/d2/discussions/132)
144117
- [ ] [Smaller embeded icons](https://github.com/terrastruct/d2/discussions/2223)
145-
- [ ] [Export JSON graph](https://github.com/terrastruct/d2/discussions/2224)
146-
- [ ] [Class-based dark mode](https://github.com/terrastruct/d2/discussions/2225)
147118
- [ ] Link resolution callback

packages/rehype-d2/README.md

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ which can look like this:
2222

2323
## Usage
2424

25-
You need to install [D2](https://d2lang.com/tour/install) in order to use this plugin. **But** they are working on [WASM version](https://github.com/terrastruct/d2/discussions/234#discussioncomment-11286029), so hopefully this will change soon.
26-
2725
```js
2826
import rehypeD2 from "@beoe/rehype-d2";
2927

packages/rehype-d2/src/d2.ts

+5-120
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,18 @@
11
// @ts-ignore
22
import { D2 } from "@terrastruct/d2";
3-
import { spawn } from "node:child_process";
4-
5-
// https://github.com/stereobooster/venn-nodejs/blob/main/index.js
6-
export function exec(
7-
command: string,
8-
args: string[],
9-
stdin?: string,
10-
cwd?: string
11-
) {
12-
return new Promise<string>((resolve, reject) => {
13-
const child = spawn(command, args, {
14-
cwd,
15-
stdio: [],
16-
windowsHide: true,
17-
});
18-
19-
const output: string[] = [];
20-
let errorMessage = `Unable to run command: '${command} ${args.join(" ")}'.`;
21-
22-
child.stdout.on("data", (data: Buffer) => {
23-
const lines = data
24-
.toString()
25-
.split("\n")
26-
.filter((line) => line.length > 0);
27-
28-
output.push(...lines);
29-
});
30-
31-
child.stderr.on("data", (data: Buffer) => {
32-
errorMessage += `\n${data.toString()}`;
33-
});
34-
35-
child.on("error", (error) => {
36-
reject(new Error(errorMessage, { cause: error }));
37-
});
38-
39-
child.on("close", (code) => {
40-
if (code !== 0) {
41-
reject(new Error(errorMessage));
42-
43-
return;
44-
}
45-
46-
resolve(output.join(""));
47-
});
48-
49-
if (stdin) {
50-
child.stdin.write(stdin);
51-
child.stdin.end();
52-
}
53-
});
54-
}
553

564
export type D2Options = {
57-
/**
58-
* @default 0
59-
* Set the diagram theme ID.
60-
*/
61-
theme?: string;
62-
/**
63-
* @default -1
64-
* The theme to use when the viewer's browser is in dark mode.
65-
* When left unset --theme is used for both light and dark mode.
66-
* Be aware that explicit styles set in D2 code will still be
67-
* applied and this may produce unexpected results. We plan on
68-
* resolving this by making style maps in D2 light/dark mode
69-
* specific. See https://github.com/terrastruct/d2/issues/831.
70-
*/
71-
darkTheme?: string;
72-
/**
73-
* Set the diagram layout engine to the passed string. For a
74-
* list of available options, run layout.
75-
*/
765
layout?: "dagre" | "elk";
77-
/**
78-
* @default 100
79-
* Pixels padded around the rendered diagram.
80-
*/
81-
pad?: number;
82-
/**
83-
* @default -1
84-
* Scale the output. E.g., 0.5 to halve the default size.
85-
* Default -1 means that SVG's will fit to screen and all others
86-
* will use their default render size. Setting to 1 turns off
87-
* SVG fitting to screen.
88-
*/
89-
scale?: number;
90-
/**
91-
* @default false
92-
* Renders the diagram to look like it was sketched by hand.
93-
*/
946
sketch?: boolean;
95-
/**
96-
* @default true
97-
* Bundle all assets and layers into the output svg.
98-
*/
99-
bundle?: boolean;
100-
/**
101-
* Center the SVG in the containing viewbox, such as your
102-
* browser screen.
103-
*/
104-
center?: boolean;
7+
themeID?: number;
1058
};
1069

107-
export function d2(code: string, options?: D2Options) {
108-
const args = [];
109-
110-
if (options?.theme) args.push(`--theme=${options.theme}`);
111-
// if (options?.darkTheme) args.push(`--dark-theme=${options.darkTheme}`);
112-
if (options?.layout) args.push(`--layout=${options.layout}`);
113-
if (options?.pad !== undefined) args.push(`--pad=${options.pad}`);
114-
if (options?.scale) args.push(`--scale=${options.scale}`);
115-
if (options?.sketch) args.push(`--sketch`);
116-
if (options?.bundle) args.push(`--bundle`);
117-
if (options?.center) args.push(`--center`);
118-
119-
return exec("d2", [...args, "-"], code);
120-
}
121-
122-
export type CompileOptions = {
123-
layout?: "dagre" | "elk";
124-
sketch?: boolean;
125-
};
12610
const d2Instance = new D2();
127-
export async function d2New(
11+
12+
export async function d2(
12813
code: string,
129-
options?: CompileOptions
14+
options?: D2Options
13015
): Promise<string> {
13116
const result = await d2Instance.compile(code, options);
132-
return await d2Instance.render(result.diagram);
17+
return await d2Instance.render(result.diagram, options);
13318
}

packages/rehype-d2/src/index.ts

+18-11
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,33 @@ import type { Plugin } from "unified";
33
// @ts-expect-error The inferred type of '...' cannot be named without a reference to
44
import type { Root } from "hast";
55
import { rehypeCodeHookImg } from "@beoe/rehype-code-hook-img";
6-
import { D2Options, d2New } from "./d2.js";
6+
import { D2Options, d2 } from "./d2.js";
77

88
export type RehypeD2Config = {
9-
d2Options?: D2Options;
9+
d2Options?: Omit<D2Options, "themeID"> & {
10+
theme?: string;
11+
darkTheme?: string;
12+
};
1013
};
1114

1215
export const rehypeD2 = rehypeCodeHookImg<RehypeD2Config>({
1316
language: "d2",
1417
render: async (code: string, opts) => {
1518
const { darkMode, d2Options, ...rest } = opts;
1619
const newD2Options = { ...d2Options, ...rest };
17-
const svg = await d2New(code, newD2Options);
18-
return { svg };
19-
// const darkSvg = darkMode
20-
// ? await d2(code, {
21-
// ...d2Options,
22-
// theme: newD2Options?.darkTheme ?? "200",
23-
// })
24-
// : undefined;
25-
// return { svg, darkSvg };
20+
if (newD2Options.theme !== undefined) {
21+
// @ts-ignore
22+
newD2Options.themeID = parseFloat(newD2Options.theme);
23+
}
24+
const svg = await d2(code, newD2Options);
25+
const darkSvg = darkMode
26+
? await d2(code, {
27+
...newD2Options,
28+
// @ts-ignore
29+
themeID: parseFloat(newD2Options?.darkTheme ?? "200"),
30+
})
31+
: undefined;
32+
return { svg, darkSvg };
2633
},
2734
});
2835

plugins/netlify-plugin-d2/index.js

-4
This file was deleted.

plugins/netlify-plugin-d2/manifest.yml

-1
This file was deleted.

0 commit comments

Comments
 (0)