|
1 |
| -import { spawn } from "node:child_process"; |
2 |
| - |
3 |
| -// https://github.com/stereobooster/venn-nodejs/blob/main/index.js |
4 |
| -export function exec( |
5 |
| - command: string, |
6 |
| - args: string[], |
7 |
| - stdin?: string, |
8 |
| - cwd?: string |
9 |
| -) { |
10 |
| - return new Promise<string>((resolve, reject) => { |
11 |
| - const child = spawn(command, args, { |
12 |
| - cwd, |
13 |
| - stdio: [], |
14 |
| - windowsHide: true, |
15 |
| - }); |
16 |
| - |
17 |
| - const output: string[] = []; |
18 |
| - let errorMessage = `Unable to run command: '${command} ${args.join(" ")}'.`; |
19 |
| - |
20 |
| - child.stdout.on("data", (data: Buffer) => { |
21 |
| - const lines = data |
22 |
| - .toString() |
23 |
| - .split("\n") |
24 |
| - .filter((line) => line.length > 0); |
25 |
| - |
26 |
| - output.push(...lines); |
27 |
| - }); |
28 |
| - |
29 |
| - child.stderr.on("data", (data: Buffer) => { |
30 |
| - errorMessage += `\n${data.toString()}`; |
31 |
| - }); |
32 |
| - |
33 |
| - child.on("error", (error) => { |
34 |
| - reject(new Error(errorMessage, { cause: error })); |
35 |
| - }); |
36 |
| - |
37 |
| - child.on("close", (code) => { |
38 |
| - if (code !== 0) { |
39 |
| - reject(new Error(errorMessage)); |
40 |
| - |
41 |
| - return; |
42 |
| - } |
43 |
| - |
44 |
| - resolve(output.join("")); |
45 |
| - }); |
46 |
| - |
47 |
| - if (stdin) { |
48 |
| - child.stdin.write(stdin); |
49 |
| - child.stdin.end(); |
50 |
| - } |
51 |
| - }); |
52 |
| -} |
| 1 | +// @ts-ignore https://github.com/terrastruct/d2/issues/2286 |
| 2 | +import { D2 } from "@terrastruct/d2"; |
53 | 3 |
|
54 | 4 | export type D2Options = {
|
55 |
| - /** |
56 |
| - * @default 0 |
57 |
| - * Set the diagram theme ID. |
58 |
| - */ |
59 |
| - theme?: string; |
60 |
| - /** |
61 |
| - * @default -1 |
62 |
| - * The theme to use when the viewer's browser is in dark mode. |
63 |
| - * When left unset --theme is used for both light and dark mode. |
64 |
| - * Be aware that explicit styles set in D2 code will still be |
65 |
| - * applied and this may produce unexpected results. We plan on |
66 |
| - * resolving this by making style maps in D2 light/dark mode |
67 |
| - * specific. See https://github.com/terrastruct/d2/issues/831. |
68 |
| - */ |
69 |
| - darkTheme?: string; |
70 |
| - /** |
71 |
| - * Set the diagram layout engine to the passed string. For a |
72 |
| - * list of available options, run layout. |
73 |
| - */ |
74 |
| - layout?: string; |
75 |
| - /** |
76 |
| - * @default 100 |
77 |
| - * Pixels padded around the rendered diagram. |
78 |
| - */ |
79 |
| - pad?: number; |
80 |
| - /** |
81 |
| - * @default -1 |
82 |
| - * Scale the output. E.g., 0.5 to halve the default size. |
83 |
| - * Default -1 means that SVG's will fit to screen and all others |
84 |
| - * will use their default render size. Setting to 1 turns off |
85 |
| - * SVG fitting to screen. |
86 |
| - */ |
87 |
| - scale?: number; |
88 |
| - /** |
89 |
| - * @default false |
90 |
| - * Renders the diagram to look like it was sketched by hand. |
91 |
| - */ |
| 5 | + layout?: "dagre" | "elk"; |
92 | 6 | sketch?: boolean;
|
93 |
| - /** |
94 |
| - * @default true |
95 |
| - * Bundle all assets and layers into the output svg. |
96 |
| - */ |
97 |
| - bundle?: boolean; |
98 |
| - /** |
99 |
| - * Center the SVG in the containing viewbox, such as your |
100 |
| - * browser screen. |
101 |
| - */ |
102 |
| - center?: boolean; |
| 7 | + themeID?: number; |
103 | 8 | };
|
104 | 9 |
|
105 |
| -export function d2(code: string, options?: D2Options) { |
106 |
| - const args = []; |
107 |
| - |
108 |
| - if (options?.theme) args.push(`--theme=${options.theme}`); |
109 |
| - // if (options?.darkTheme) args.push(`--dark-theme=${options.darkTheme}`); |
110 |
| - if (options?.layout) args.push(`--layout=${options.layout}`); |
111 |
| - if (options?.pad !== undefined) args.push(`--pad=${options.pad}`); |
112 |
| - if (options?.scale) args.push(`--scale=${options.scale}`); |
113 |
| - if (options?.sketch) args.push(`--sketch`); |
114 |
| - if (options?.bundle) args.push(`--bundle`); |
115 |
| - if (options?.center) args.push(`--center`); |
116 |
| - |
117 |
| - return exec("d2", [...args, "-"], code); |
| 10 | +export async function d2( |
| 11 | + code: string, |
| 12 | + options?: D2Options |
| 13 | +): Promise<string> { |
| 14 | + const d2Instance = new D2(); |
| 15 | + const result = await d2Instance.compile(code, options); |
| 16 | + const svg = await d2Instance.render(result.diagram, options); |
| 17 | + return svg |
118 | 18 | }
|
0 commit comments