From ff337b8bd21ccb089b04a53c57e6a445ba539d56 Mon Sep 17 00:00:00 2001 From: Michael Toohig <=> Date: Fri, 19 Apr 2024 00:27:49 -0400 Subject: [PATCH 1/2] Add support for global sketch and pad configuration --- packages/astro-d2/config.ts | 12 ++++++++++++ packages/astro-d2/libs/attributes.ts | 4 ++-- packages/astro-d2/libs/d2.ts | 12 ++++++++++-- packages/astro-d2/tests/remark.test.ts | 13 +++++++++++-- 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/packages/astro-d2/config.ts b/packages/astro-d2/config.ts index cd894d9..00eeb71 100644 --- a/packages/astro-d2/config.ts +++ b/packages/astro-d2/config.ts @@ -15,6 +15,18 @@ export const AstroD2ConfigSchema = z * @default 'd2' */ output: z.string().default('d2'), + /** + * Defines the default pad value for all diagrams. + * + * @default 100 + */ + pad: z.number().default(100), + /** + * Whether the Astro D2 integration should default to render the diagram as if sketched by hand. + * + * @default false + */ + sketch: z.boolean().default(false), /** * Whether the Astro D2 integration should skip the generation of diagrams. * diff --git a/packages/astro-d2/libs/attributes.ts b/packages/astro-d2/libs/attributes.ts index 336b5d8..d885dc6 100644 --- a/packages/astro-d2/libs/attributes.ts +++ b/packages/astro-d2/libs/attributes.ts @@ -23,13 +23,13 @@ export const AttributesSchema = z * * @default 100 */ - pad: z.coerce.number().default(100), + pad: z.coerce.string().optional(), /** * Whether to render the diagram as if it was sketched by hand. * * @default 'false' */ - sketch: z.union([z.literal('true'), z.literal('false')]).default('false'), + sketch: z.union([z.literal('true'), z.literal('false')]).optional(), /** * Defines the target board to render when using composition. * Use `root` to target the root board. diff --git a/packages/astro-d2/libs/d2.ts b/packages/astro-d2/libs/d2.ts index af47f19..f4b5186 100644 --- a/packages/astro-d2/libs/d2.ts +++ b/packages/astro-d2/libs/d2.ts @@ -40,6 +40,14 @@ export async function generateD2Diagram( extraArgs.push(`--target='${attributes.target}'`) } + if (attributes.sketch !== undefined) { + extraArgs.push(`--sketch=${attributes.sketch}`) + } + + if (attributes.pad !== undefined) { + extraArgs.push(`--pad=${attributes.pad}`) + } + try { // The `-` argument is used to read from stdin instead of a file. await exec( @@ -47,8 +55,8 @@ export async function generateD2Diagram( [ `--layout=${config.layout}`, `--theme=${attributes.theme ?? config.theme.default}`, - `--sketch=${attributes.sketch}`, - `--pad=${attributes.pad}`, + `--sketch=${attributes.sketch ?? config.sketch}`, + `--pad=${attributes.pad ?? config.pad}`, ...extraArgs, '-', outputPath, diff --git a/packages/astro-d2/tests/remark.test.ts b/packages/astro-d2/tests/remark.test.ts index 4fe077d..cfb6a55 100644 --- a/packages/astro-d2/tests/remark.test.ts +++ b/packages/astro-d2/tests/remark.test.ts @@ -141,6 +141,15 @@ ${defaultDiagram} expectD2ToHaveBeenCalledWithArg('--sketch=true') }) +test('disables the `sketch` attribute if specified', async () => { + await transformMd(`\`\`\`d2 sketch=false +${defaultDiagram} +\`\`\` +`) + + expectD2ToHaveBeenCalledWithArg('--sketch=false') +}) + test('uses the `pad` attribute if specified', async () => { await transformMd(`\`\`\`d2 pad=50 ${defaultDiagram} @@ -222,8 +231,8 @@ function expectD2ToHaveBeenNthCalledWith( [ `--layout=${config.layout}`, `--theme=${config.theme.default}`, - `--sketch=false`, - `--pad=100`, + `--sketch=${config.sketch}`, + `--pad=${config.pad}`, `--dark-theme=${config.theme.dark}`, '-', fileURLToPath(new URL(`../public/${config.output}/tests/index-${diagramIndex}.svg`, import.meta.url)), From c14263f39b95fb18880255170dae9a65be66ca23 Mon Sep 17 00:00:00 2001 From: HiDeoo <494699+HiDeoo@users.noreply.github.com> Date: Sat, 27 Apr 2024 15:45:51 +0200 Subject: [PATCH 2/2] feat: add tests and documentation --- docs/src/content/docs/attributes.md | 6 +-- docs/src/content/docs/configuration.md | 14 +++++++ packages/astro-d2/config.ts | 4 +- packages/astro-d2/libs/attributes.ts | 15 ++++--- packages/astro-d2/libs/d2.ts | 8 ---- packages/astro-d2/tests/attributes.test.ts | 6 +++ packages/astro-d2/tests/remark.test.ts | 48 ++++++++++++++++++++-- 7 files changed, 76 insertions(+), 25 deletions(-) diff --git a/docs/src/content/docs/attributes.md b/docs/src/content/docs/attributes.md index 61fb8a9..649a838 100644 --- a/docs/src/content/docs/attributes.md +++ b/docs/src/content/docs/attributes.md @@ -78,10 +78,9 @@ See the D2 documentation for more information about the available [themes](https ### `sketch` -**Default:** `'false'` **Example**: [Sketch attribute](/examples/attributes/sketch/) -Whether to render the diagram as if it was sketched by hand. +Overrides the [global `sketch` configuration](/configuration/#sketch) and defines whether to render the diagram as if it was sketched by hand. ````md title="src/content/docs/example.md" "sketch" ```d2 sketch @@ -91,10 +90,9 @@ x -> y ### `pad` -**Default:** `'100'` **Example**: [Padding attribute](/examples/attributes/padding/) -The padding (in pixels) around the rendered diagram. +Overrides the [global `pad` configuration](/configuration/#pad) and defines the padding (in pixels) around the rendered diagram. ````md title="src/content/docs/example.md" "pad=10" ```d2 pad=10 diff --git a/docs/src/content/docs/configuration.md b/docs/src/content/docs/configuration.md index b87bb24..3384c75 100644 --- a/docs/src/content/docs/configuration.md +++ b/docs/src/content/docs/configuration.md @@ -54,6 +54,20 @@ This will require you to [build](https://docs.astro.build/en/reference/cli-refer Defines the layout engine to use to generate the diagrams. See the D2 documentation for more information about the available [layout engines](https://d2lang.com/tour/layouts#layout-engines). +### `sketch` + +**Type:** `boolean` +**Default:** `false` + +Whether to render the diagrams as if they were sketched by hand. + +### `pad` + +**Type:** `number` +**Default:** `100` + +The padding (in pixels) around the rendered diagrams. + --- ## Theme configuration diff --git a/packages/astro-d2/config.ts b/packages/astro-d2/config.ts index 00eeb71..a17c507 100644 --- a/packages/astro-d2/config.ts +++ b/packages/astro-d2/config.ts @@ -16,13 +16,13 @@ export const AstroD2ConfigSchema = z */ output: z.string().default('d2'), /** - * Defines the default pad value for all diagrams. + * The padding (in pixels) around the rendered diagrams. * * @default 100 */ pad: z.number().default(100), /** - * Whether the Astro D2 integration should default to render the diagram as if sketched by hand. + * Whether to render the diagrams as if they were sketched by hand. * * @default false */ diff --git a/packages/astro-d2/libs/attributes.ts b/packages/astro-d2/libs/attributes.ts index d885dc6..2d87d11 100644 --- a/packages/astro-d2/libs/attributes.ts +++ b/packages/astro-d2/libs/attributes.ts @@ -19,17 +19,16 @@ export const AttributesSchema = z .optional() .transform((value) => (value === 'false' ? false : value)), /** - * The padding (in pixels) around the rendered diagram. - * - * @default 100 + * Overrides the global `pad` configuration for the diagram. */ - pad: z.coerce.string().optional(), + pad: z.coerce.number().optional(), /** - * Whether to render the diagram as if it was sketched by hand. - * - * @default 'false' + * Overrides the global `sketch` configuration for the diagram. */ - sketch: z.union([z.literal('true'), z.literal('false')]).optional(), + sketch: z + .union([z.literal('true'), z.literal('false')]) + .optional() + .transform((value) => (value === 'false' ? false : value)), /** * Defines the target board to render when using composition. * Use `root` to target the root board. diff --git a/packages/astro-d2/libs/d2.ts b/packages/astro-d2/libs/d2.ts index f4b5186..0bfdcd7 100644 --- a/packages/astro-d2/libs/d2.ts +++ b/packages/astro-d2/libs/d2.ts @@ -40,14 +40,6 @@ export async function generateD2Diagram( extraArgs.push(`--target='${attributes.target}'`) } - if (attributes.sketch !== undefined) { - extraArgs.push(`--sketch=${attributes.sketch}`) - } - - if (attributes.pad !== undefined) { - extraArgs.push(`--pad=${attributes.pad}`) - } - try { // The `-` argument is used to read from stdin instead of a file. await exec( diff --git a/packages/astro-d2/tests/attributes.test.ts b/packages/astro-d2/tests/attributes.test.ts index 1f2d64e..2b33fa2 100644 --- a/packages/astro-d2/tests/attributes.test.ts +++ b/packages/astro-d2/tests/attributes.test.ts @@ -58,6 +58,12 @@ test('supports a shorthand syntax for `sketch`', () => { expect(attributes).toEqual({ ...defaultAttributes, sketch: 'true' }) }) +test('transforms the `sketch` value to a boolean if set to `false`', () => { + const attributes = getAttributes('sketch=false') + + expect(attributes).toEqual({ ...defaultAttributes, sketch: false }) +}) + test('supports coersed number for `pad` and `width`', () => { const attributes = getAttributes('pad=50 width=100') diff --git a/packages/astro-d2/tests/remark.test.ts b/packages/astro-d2/tests/remark.test.ts index cfb6a55..05901cd 100644 --- a/packages/astro-d2/tests/remark.test.ts +++ b/packages/astro-d2/tests/remark.test.ts @@ -133,7 +133,7 @@ ${defaultDiagram} }) test('uses the `sketch` attribute if specified', async () => { - await transformMd(`\`\`\`d2 sketch + await transformMd(`\`\`\`d2 sketch=true ${defaultDiagram} \`\`\` `) @@ -141,15 +141,43 @@ ${defaultDiagram} expectD2ToHaveBeenCalledWithArg('--sketch=true') }) -test('disables the `sketch` attribute if specified', async () => { - await transformMd(`\`\`\`d2 sketch=false +test('uses the `sketch` shorthand attribute if specified', async () => { + await transformMd(`\`\`\`d2 sketch ${defaultDiagram} \`\`\` `) + expectD2ToHaveBeenCalledWithArg('--sketch=true') +}) + +test('uses the `sketch` attribute to disable the `sketch` config if specified', async () => { + const config = { sketch: true } + + await transformMd( + `\`\`\`d2 sketch=false +${defaultDiagram} +\`\`\` +`, + config, + ) + expectD2ToHaveBeenCalledWithArg('--sketch=false') }) +test('uses the `sketch` attribute to enable the `sketch` config if specified', async () => { + const config = { sketch: false } + + await transformMd( + `\`\`\`d2 sketch +${defaultDiagram} +\`\`\` +`, + config, + ) + + expectD2ToHaveBeenCalledWithArg('--sketch=true') +}) + test('uses the `pad` attribute if specified', async () => { await transformMd(`\`\`\`d2 pad=50 ${defaultDiagram} @@ -159,6 +187,20 @@ ${defaultDiagram} expectD2ToHaveBeenCalledWithArg('--pad=50') }) +test('uses the `pad` attribute to override the `pad` config if specified', async () => { + const config = { pad: 200 } + + await transformMd( + `\`\`\`d2 pad=25 +${defaultDiagram} +\`\`\` +`, + config, + ) + + expectD2ToHaveBeenCalledWithArg('--pad=25') +}) + test('uses the `animateInterval` attribute if specified', async () => { await transformMd(`\`\`\`d2 animateInterval=1000 ${defaultDiagram}