diff options
Diffstat (limited to 'packages/integrations/mdx/src/utils.ts')
-rw-r--r-- | packages/integrations/mdx/src/utils.ts | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/packages/integrations/mdx/src/utils.ts b/packages/integrations/mdx/src/utils.ts index 1091e8511..c824c39c3 100644 --- a/packages/integrations/mdx/src/utils.ts +++ b/packages/integrations/mdx/src/utils.ts @@ -4,16 +4,34 @@ function appendForwardSlash(path: string) { return path.endsWith('/') ? path : path + '/'; } +interface FileInfo { + fileId: string; + fileUrl: string; +} + /** @see 'vite-plugin-utils' for source */ -export function getFileInfo(id: string, config: AstroConfig) { +export function getFileInfo(id: string, config: AstroConfig): FileInfo { const sitePathname = appendForwardSlash( config.site ? new URL(config.base, config.site).pathname : config.base ); + // Try to grab the file's actual URL + let url: URL | undefined = undefined; + try { + url = new URL(`file://${id}`); + } catch {} + const fileId = id.split('?')[0]; - let fileUrl = fileId.includes('/pages/') - ? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, '') - : undefined; + let fileUrl: string; + const isPage = fileId.includes('/pages/'); + if(isPage) { + fileUrl = fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, ''); + } else if(url && url.pathname.startsWith(config.root.pathname)) { + fileUrl = url.pathname.slice(config.root.pathname.length); + } else { + fileUrl = fileId; + } + if (fileUrl && config.trailingSlash === 'always') { fileUrl = appendForwardSlash(fileUrl); } |