astro-d2 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/config.ts CHANGED
@@ -15,6 +15,18 @@ export const AstroD2ConfigSchema = z
15
15
  * @default 'd2'
16
16
  */
17
17
  output: z.string().default('d2'),
18
+ /**
19
+ * The padding (in pixels) around the rendered diagrams.
20
+ *
21
+ * @default 100
22
+ */
23
+ pad: z.number().default(100),
24
+ /**
25
+ * Whether to render the diagrams as if they were sketched by hand.
26
+ *
27
+ * @default false
28
+ */
29
+ sketch: z.boolean().default(false),
18
30
  /**
19
31
  * Whether the Astro D2 integration should skip the generation of diagrams.
20
32
  *
package/index.ts CHANGED
@@ -24,7 +24,7 @@ export default function astroD2Integration(userConfig?: AstroD2UserConfig): Astr
24
24
  return {
25
25
  name: 'astro-d2-integration',
26
26
  hooks: {
27
- 'astro:config:setup': async ({ command, logger, updateConfig }) => {
27
+ 'astro:config:setup': async ({ command, config: astroConfig, logger, updateConfig }) => {
28
28
  if (command !== 'build' && command !== 'dev') {
29
29
  return
30
30
  }
@@ -45,7 +45,7 @@ export default function astroD2Integration(userConfig?: AstroD2UserConfig): Astr
45
45
 
46
46
  updateConfig({
47
47
  markdown: {
48
- remarkPlugins: [[remarkAstroD2, config]],
48
+ remarkPlugins: [[remarkAstroD2, { ...config, base: astroConfig.base }]],
49
49
  },
50
50
  })
51
51
  },
@@ -19,17 +19,16 @@ export const AttributesSchema = z
19
19
  .optional()
20
20
  .transform((value) => (value === 'false' ? false : value)),
21
21
  /**
22
- * The padding (in pixels) around the rendered diagram.
23
- *
24
- * @default 100
22
+ * Overrides the global `pad` configuration for the diagram.
25
23
  */
26
- pad: z.coerce.number().default(100),
24
+ pad: z.coerce.number().optional(),
27
25
  /**
28
- * Whether to render the diagram as if it was sketched by hand.
29
- *
30
- * @default 'false'
26
+ * Overrides the global `sketch` configuration for the diagram.
31
27
  */
32
- sketch: z.union([z.literal('true'), z.literal('false')]).default('false'),
28
+ sketch: z
29
+ .union([z.literal('true'), z.literal('false')])
30
+ .optional()
31
+ .transform((value) => (value === 'false' ? false : value)),
33
32
  /**
34
33
  * Defines the target board to render when using composition.
35
34
  * Use `root` to target the root board.
package/libs/d2.ts CHANGED
@@ -47,8 +47,8 @@ export async function generateD2Diagram(
47
47
  [
48
48
  `--layout=${config.layout}`,
49
49
  `--theme=${attributes.theme ?? config.theme.default}`,
50
- `--sketch=${attributes.sketch}`,
51
- `--pad=${attributes.pad}`,
50
+ `--sketch=${attributes.sketch ?? config.sketch}`,
51
+ `--pad=${attributes.pad ?? config.pad}`,
52
52
  ...extraArgs,
53
53
  '-',
54
54
  outputPath,
@@ -85,7 +85,7 @@ async function getD2Version() {
85
85
  try {
86
86
  const [version] = await exec('d2', ['--version'])
87
87
 
88
- if (!version || !/^\d+\.\d+\.\d+$/.test(version)) {
88
+ if (!version || !/^v?\d+\.\d+\.\d+/.test(version)) {
89
89
  throw new Error(`Invalid D2 version, got '${version}'.`)
90
90
  }
91
91
 
package/libs/remark.ts CHANGED
@@ -10,7 +10,7 @@ import { type DiagramAttributes, getAttributes } from './attributes'
10
10
  import { generateD2Diagram, type D2Size, getD2DiagramSize } from './d2'
11
11
  import { throwErrorWithHint } from './integration'
12
12
 
13
- export function remarkAstroD2(config: AstroD2Config) {
13
+ export function remarkAstroD2(config: RemarkAstroD2Config) {
14
14
  return async function transformer(tree: Root, file: VFile) {
15
15
  const d2Nodes: [node: Code, context: VisitorContext][] = []
16
16
 
@@ -70,7 +70,7 @@ function makHtmlImgNode(attributes: DiagramAttributes, imgPath: string, size: D2
70
70
  }
71
71
  }
72
72
 
73
- function getOutputPaths(config: AstroD2Config, file: VFile, nodeIndex: number) {
73
+ function getOutputPaths(config: RemarkAstroD2Config, file: VFile, nodeIndex: number) {
74
74
  const relativePath = path.relative(file.cwd, file.path).replace(/^src\/(content|pages)\//, '')
75
75
  const parsedRelativePath = path.parse(relativePath)
76
76
 
@@ -78,7 +78,7 @@ function getOutputPaths(config: AstroD2Config, file: VFile, nodeIndex: number) {
78
78
 
79
79
  return {
80
80
  fsPath: path.join(file.cwd, 'public', config.output, relativeOutputPath),
81
- imgPath: path.posix.join('/', config.output, relativeOutputPath),
81
+ imgPath: path.posix.join(config.base, config.output, relativeOutputPath),
82
82
  }
83
83
  }
84
84
 
@@ -100,3 +100,7 @@ interface VisitorContext {
100
100
  index: number | undefined
101
101
  parent: Parent | undefined
102
102
  }
103
+
104
+ interface RemarkAstroD2Config extends AstroD2Config {
105
+ base: string
106
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro-d2",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "license": "MIT",
5
5
  "description": "Astro integration and remark plugin to transform D2 Markdown code blocks into diagrams.",
6
6
  "author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",