summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/utils.ts
blob: 1091e851173abd9ed98d232b60634c11e98ec37f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import type { AstroConfig } from 'astro';

function appendForwardSlash(path: string) {
	return path.endsWith('/') ? path : path + '/';
}

/** @see 'vite-plugin-utils' for source */
export function getFileInfo(id: string, config: AstroConfig) {
	const sitePathname = appendForwardSlash(
		config.site ? new URL(config.base, config.site).pathname : config.base
	);

	const fileId = id.split('?')[0];
	let fileUrl = fileId.includes('/pages/')
		? fileId.replace(/^.*?\/pages\//, sitePathname).replace(/(\/index)?\.mdx$/, '')
		: undefined;
	if (fileUrl && config.trailingSlash === 'always') {
		fileUrl = appendForwardSlash(fileUrl);
	}
	return { fileId, fileUrl };
}