UNPKG

@mui/internal-docs-infra

Version:

MUI Infra - internal documentation creation tools.

66 lines (62 loc) 2.24 kB
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper"; /** * Known compound extensions that should be treated as a single unit */ var COMPOUND_EXTENSIONS = ['.module.css', '.module.scss', '.module.sass', '.module.less', '.d.ts', '.test.js', '.test.jsx', '.test.ts', '.test.tsx', '.spec.js', '.spec.jsx', '.spec.ts', '.spec.tsx', '.config.js', '.config.ts', '.setup.js', '.setup.ts', '.stories.js', '.stories.jsx', '.stories.ts', '.stories.tsx']; /** * Extracts the filename and extension from a URL or file path. * This function is isomorphic and works in both Node.js and browser environments. * It properly handles compound extensions like .module.css, .d.ts, .test.js, etc. * * @param url - The URL or file path to extract the filename from * @returns An object containing the filename and extension */ export function getFileNameFromUrl(url) { try { // Use URL constructor to handle various URL formats var urlObj = new URL(url); var pathname = urlObj.pathname; var fileName = pathname.split('/').pop() || ''; return extractFileNameAndExtension(fileName); } catch (_unused) { // If URL parsing fails, fall back to simple string manipulation var _fileName = url.split('/').pop() || url; return extractFileNameAndExtension(_fileName); } } /** * Helper function to extract filename and extension, handling compound extensions */ function extractFileNameAndExtension(fileName) { if (!fileName) { return { fileName: '', extension: '' }; } // Check for compound extensions first var _iterator = _createForOfIteratorHelper(COMPOUND_EXTENSIONS), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var compoundExt = _step.value; if (fileName.endsWith(compoundExt)) { return { fileName: fileName, extension: compoundExt }; } } // Fall back to simple extension detection } catch (err) { _iterator.e(err); } finally { _iterator.f(); } var lastDotIndex = fileName.lastIndexOf('.'); var extension = lastDotIndex > 0 ? fileName.substring(lastDotIndex) : ''; return { fileName: fileName, extension: extension }; }