Skip to content

Commit

Permalink
Merge pull request #20 from HiDeoo/hd-feat-layout-attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo authored Sep 14, 2024
2 parents 886cbaf + e2cc625 commit 7c832eb
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
uses: pnpm/action-setup@v4

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: 18
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
uses: pnpm/action-setup@v4

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: 18
Expand Down
168 changes: 168 additions & 0 deletions docs/public/d2/docs/examples/Attributes/layout-0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions docs/src/content/docs/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,16 @@ layers: {
}
```
````

### `layout`

**Example**: [Layout attribute](/examples/attributes/layout/)

Overrides the [global `layout` configuration](/configuration/#layout) and 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).

````md title="src/content/docs/example.md" "layout=elk"
```d2 layout=elk
x -> y
```
````
17 changes: 17 additions & 0 deletions docs/src/content/docs/examples/Attributes/layout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
title: Layout
---

Use the [`layout` attribute](/attributes/#layout) to use a different layout engine to generate the diagram.

````md title="src/content/docs/example.md" "layout=elk"
```d2 layout=elk
Content -> Website -> Content
```
````

The above code block will be rendered as the following diagram with the ELK layout engine:

```d2 layout=elk
Content -> Website -> Content
```
4 changes: 4 additions & 0 deletions packages/astro-d2/libs/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export const AttributesSchema = z
.string()
.optional()
.transform((value) => (value === 'false' ? false : value)),
/**
* Overrides the global `layout` configuration for the diagram.
*/
layout: z.union([z.literal('dagre'), z.literal('elk'), z.literal('tala')]).optional(),
/**
* Overrides the global `pad` configuration for the diagram.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/astro-d2/libs/d2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function generateD2Diagram(
await exec(
'd2',
[
`--layout=${config.layout}`,
`--layout=${attributes.layout ?? config.layout}`,
`--theme=${attributes.theme ?? config.theme.default}`,
`--sketch=${attributes.sketch ?? config.sketch}`,
`--pad=${attributes.pad ?? config.pad}`,
Expand Down
21 changes: 21 additions & 0 deletions packages/astro-d2/tests/remark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ test('uses the specified base option', async () => {
`)
})

test('uses the `layout` attribute if specified', async () => {
await transformMd(`\`\`\`d2 pad=50
${defaultDiagram}
\`\`\`
`)

expectD2ToHaveBeenCalledWithArg('--pad=50')
})

test('uses the `layout` attribute to override the `layout` config if specified', async () => {
await transformMd(
`\`\`\`d2 layout=tala
${defaultDiagram}
\`\`\`
`,
{ layout: 'elk' },
)

expectD2ToHaveBeenCalledWithArg('--layout=tala')
})

async function transformMd(md: string, userConfig?: AstroD2UserConfig, base = '/') {
const processor = userConfig
? remark().use(remarkAstroD2, {
Expand Down

0 comments on commit 7c832eb

Please sign in to comment.