@mintlify/common
Version:
Commonly shared code within Mintlify
29 lines (28 loc) • 1.04 kB
JavaScript
import { posix } from 'path';
const isRelativeImport = (source) => {
return source.startsWith('./') || source.startsWith('../');
};
const isLocalImport = (source) => {
return source.startsWith('/') || isRelativeImport(source);
};
/**
* Resolves an import path to an absolute path with leading slash.
*
* @param source The import source path (e.g., '/components/foo.mdx' or '../bar.mdx')
* @param importingFilePath The path of the file containing the import
* @returns The resolved absolute path with leading slash, or null if it's an external/package import
*/
export const resolveImportPath = (source, importingFilePath) => {
if (!isLocalImport(source)) {
return null;
}
let resolvedPath;
if (source.startsWith('/')) {
resolvedPath = posix.normalize(source);
}
else {
const importingDir = posix.dirname(importingFilePath.startsWith('/') ? importingFilePath : '/' + importingFilePath);
resolvedPath = posix.resolve(importingDir, source);
}
return resolvedPath;
};