Skip to content

Commit

Permalink
Merge pull request #25 from HiDeoo/hd-fix-nested-target
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo authored Sep 26, 2024
2 parents b456c95 + d006ff4 commit 72eca00
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 169 deletions.
314 changes: 157 additions & 157 deletions docs/public/d2/docs/examples/Attributes/target-0.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 15 additions & 5 deletions docs/src/content/docs/examples/Attributes/target.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,28 @@ title: Target

Use the [`target` attribute](/attributes/#target) to define the target board to render when using [composition](https://d2lang.com/tour/composition).

````md title="src/content/docs/example.md" "target=starlight"
```d2 target=starlight
````md title="src/content/docs/example.md" 'target="storefront"'
```d2 target="storefront"
# Root board
Content -> Website: Astro

layers: {
# A different board named "starlight"
# Board named "starlight" that does not inherit anything from root
starlight: {
Documentation -> Website: Starlight
}

# Board named "storefront" that does not inherit anything from root
storefront: {
E-commerce -> Website: Storefront
}
}
```
````

The above code block will be rendered as the following diagram with only the `starlight` board being visible:
The above code block will be rendered as the following diagram with only the `storefront` board being visible:

```d2 target=starlight
```d2 target="storefront"
# Root board
Content -> Website: Astro
Expand All @@ -29,6 +34,11 @@ layers: {
starlight: {
Documentation -> Website: Starlight
}
# Board named "storefront" that does not inherit anything from root
storefront: {
E-commerce -> Website: Storefront
}
}
```

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 @@ -39,7 +39,7 @@ export async function generateD2Diagram(
}

if (attributes.target !== undefined) {
extraArgs.push(`--target='${attributes.target}'`)
extraArgs.push(`--target=${attributes.target}`)
}

if (config.fonts?.regular) {
Expand Down
6 changes: 5 additions & 1 deletion packages/astro-d2/libs/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function exec(command: string, args: string[], stdin?: string, cwd?: stri
})

const output: string[] = []
const errorMessage = `Unable to run command: '${command} ${args.join(' ')}'.`
let errorMessage = `Unable to run command: '${command} ${args.join(' ')}'.`

child.stdout.on('data', (data: Buffer) => {
const lines = data
Expand All @@ -19,6 +19,10 @@ export function exec(command: string, args: string[], stdin?: string, cwd?: stri
output.push(...lines)
})

child.stderr.on('data', (data: Buffer) => {
errorMessage += `\n${data.toString()}`
})

child.on('error', (error) => {
reject(new Error(errorMessage, { cause: error }))
})
Expand Down
4 changes: 2 additions & 2 deletions packages/astro-d2/libs/integration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AstroError } from 'astro/errors'

export function throwErrorWithHint(message: string): never {
export function throwErrorWithHint(message: string, cause?: Error): never {
throw new AstroError(
message,
message + (cause ? `\n\n${cause.message}` : ''),
`See the error report above for more informations.\n\nIf you believe this is a bug, please file an issue at https://github.com/HiDeoo/astro-d2/issues/new/choose`,
)
}
3 changes: 2 additions & 1 deletion packages/astro-d2/libs/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ export function remarkAstroD2(config: RemarkAstroD2Config) {
outputPath.fsPath,
file.history[0] ? path.dirname(file.history[0]) : file.cwd,
)
} catch {
} catch (error) {
throwErrorWithHint(
`Failed to generate the D2 diagram at ${node.position?.start.line ?? 0}:${node.position?.start.column ?? 0}.`,
error instanceof Error ? (error.cause instanceof Error ? error.cause : error) : undefined,
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro-d2/tests/remark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,12 @@ ${defaultDiagram}
})

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

expectD2ToHaveBeenCalledWithArg("--target=''")
expectD2ToHaveBeenCalledWithArg('--target=test')
})

test('uses the `width` attribute if specified and computes the height', async () => {
Expand Down

0 comments on commit 72eca00

Please sign in to comment.