cmc-web/packages/common/utils/resolvePath.js

22 lines
617 B
JavaScript
Raw Normal View History

2024-08-20 12:11:31 +00:00
/**
* Created by HaijunZhang on 2018/12/10.
*/
// import path from 'path';
import { startsWith } from 'lodash-es'
2024-08-21 01:17:14 +00:00
export function isExternalLink(path) {
return /^(http:|https:|mailto:|tel:)\/\//.test(path)
2024-08-20 12:11:31 +00:00
}
2024-08-21 01:17:14 +00:00
export function resolvePath(basePath, routePath) {
2024-08-20 12:11:31 +00:00
if (isExternalLink(routePath)) {
2024-08-21 01:17:14 +00:00
return routePath
2024-08-20 12:11:31 +00:00
}
// return path.resolve(basePath, routePath);
2024-08-21 01:17:14 +00:00
const basePathArr = basePath.split('/')
const routePathArr = routePath.split('/')
2024-08-20 12:11:31 +00:00
if (startsWith(routePath, '/')) {
2024-08-21 01:17:14 +00:00
return routePath
2024-08-20 12:11:31 +00:00
}
2024-08-21 01:17:14 +00:00
const res = [...basePathArr, ...routePathArr].filter(item => item)
2024-08-20 12:11:31 +00:00
return `/${res.join('/')}`
}