diff --git a/packages/astro-d2/index.ts b/packages/astro-d2/index.ts
index 114f016..65cc872 100644
--- a/packages/astro-d2/index.ts
+++ b/packages/astro-d2/index.ts
@@ -45,7 +45,17 @@ export default function astroD2Integration(userConfig?: AstroD2UserConfig): Astr
updateConfig({
markdown: {
- remarkPlugins: [[remarkAstroD2, { ...config, base: astroConfig.base, root: astroConfig.root }]],
+ remarkPlugins: [
+ [
+ remarkAstroD2,
+ {
+ ...config,
+ base: astroConfig.base,
+ publicDir: astroConfig.publicDir,
+ root: astroConfig.root,
+ },
+ ],
+ ],
},
})
},
diff --git a/packages/astro-d2/libs/remark.ts b/packages/astro-d2/libs/remark.ts
index 12dd7f4..cd2b96d 100644
--- a/packages/astro-d2/libs/remark.ts
+++ b/packages/astro-d2/libs/remark.ts
@@ -1,4 +1,5 @@
import path from 'node:path'
+import url from 'node:url'
import type { Code, Html, Parent, Root } from 'mdast'
import { SKIP, visit } from 'unist-util-visit'
@@ -83,7 +84,7 @@ function getOutputPaths(config: RemarkAstroD2Config, file: VFile, nodeIndex: num
const relativeOutputPath = path.join(parsedRelativePath.dir, `${parsedRelativePath.name}-${nodeIndex}.svg`)
return {
- fsPath: path.join(file.cwd, 'public', config.output, relativeOutputPath),
+ fsPath: path.join(url.fileURLToPath(config.publicDir), config.output, relativeOutputPath),
imgPath: path.posix.join(config.base, config.output, relativeOutputPath),
}
}
@@ -109,5 +110,6 @@ interface VisitorContext {
export interface RemarkAstroD2Config extends AstroD2Config {
base: string
+ publicDir: URL
root: URL
}
diff --git a/packages/astro-d2/tests/remark.test.ts b/packages/astro-d2/tests/remark.test.ts
index 195bf00..55d63d5 100644
--- a/packages/astro-d2/tests/remark.test.ts
+++ b/packages/astro-d2/tests/remark.test.ts
@@ -7,11 +7,12 @@ import { afterEach, expect, test, vi } from 'vitest'
import { AstroD2ConfigSchema, type AstroD2UserConfig } from '../config'
import { exec } from '../libs/exec'
-import { remarkAstroD2 } from '../libs/remark'
+import { remarkAstroD2, type RemarkAstroD2Config } from '../libs/remark'
const defaultProcessor = remark().use(remarkAstroD2, {
...AstroD2ConfigSchema.parse({}),
base: '/',
+ publicDir: new URL('public', import.meta.url),
root: new URL('.', import.meta.url),
})
const defaultDiagram = 'x -> y'
@@ -260,7 +261,7 @@ ${defaultDiagram}
})
test('uses the specified base option', async () => {
- const result = await transformMd(defaultMd, {}, '/test')
+ const result = await transformMd(defaultMd, {}, { base: '/test' })
expect(result).toMatchInlineSnapshot(`
"
@@ -289,11 +290,20 @@ ${defaultDiagram}
expectD2ToHaveBeenCalledWithArg('--layout=tala')
})
-async function transformMd(md: string, userConfig?: AstroD2UserConfig, base = '/') {
+test('uses the specified publicDir option', async () => {
+ const astroConfig = { publicDir: new URL('my-custom-publicDir-directory/', import.meta.url) }
+
+ await transformMd(defaultMd, {}, astroConfig)
+
+ expectD2ToHaveBeenNthCalledWith(1, 0, defaultDiagram, {}, astroConfig)
+})
+
+async function transformMd(md: string, userConfig?: AstroD2UserConfig, astroConfig?: Partial) {
const processor = userConfig
? remark().use(remarkAstroD2, {
...AstroD2ConfigSchema.parse(userConfig),
- base,
+ base: astroConfig?.base ?? '/',
+ publicDir: astroConfig?.publicDir ?? new URL('public/', import.meta.url),
root: new URL('.', import.meta.url),
})
: defaultProcessor
@@ -317,6 +327,7 @@ function expectD2ToHaveBeenNthCalledWith(
diagramIndex: number,
input: string,
userConfig: AstroD2UserConfig = {},
+ astroConfig?: Partial,
) {
const config = AstroD2ConfigSchema.parse(userConfig)
@@ -330,7 +341,11 @@ function expectD2ToHaveBeenNthCalledWith(
`--pad=${config.pad}`,
`--dark-theme=${config.theme.dark}`,
'-',
- fileURLToPath(new URL(`../public/${config.output}/tests/index-${diagramIndex}.svg`, import.meta.url)),
+ fileURLToPath(
+ astroConfig?.publicDir
+ ? new URL(`${config.output}/tests/index-${diagramIndex}.svg`, astroConfig.publicDir)
+ : new URL(`public/${config.output}/tests/index-${diagramIndex}.svg`, import.meta.url),
+ ),
],
input,
expect.stringMatching(/astro-d2[/\\]packages[/\\]astro-d2[/\\]tests$/),