Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with the Astro publicDir option #22

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/astro-d2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
],
],
},
})
},
Expand Down
4 changes: 3 additions & 1 deletion packages/astro-d2/libs/remark.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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),
}
}
Expand All @@ -109,5 +110,6 @@ interface VisitorContext {

export interface RemarkAstroD2Config extends AstroD2Config {
base: string
publicDir: URL
root: URL
}
25 changes: 20 additions & 5 deletions packages/astro-d2/tests/remark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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(`
"<img alt="Diagram" decoding="async" loading="lazy" src="/test/d2/tests/index-0.svg" width="128" height="64" />
Expand Down Expand Up @@ -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<RemarkAstroD2Config>) {
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
Expand All @@ -317,6 +327,7 @@ function expectD2ToHaveBeenNthCalledWith(
diagramIndex: number,
input: string,
userConfig: AstroD2UserConfig = {},
astroConfig?: Partial<RemarkAstroD2Config>,
) {
const config = AstroD2ConfigSchema.parse(userConfig)

Expand All @@ -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$/),
Expand Down