You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
import { useLocation as useDumiLocation } from 'dumi';
|
|
import * as React from 'react';
|
|
import useLocale from './useLocale';
|
|
|
|
function clearPath(path: string) {
|
|
return path.replace('-cn', '').replace(/\/$/, '');
|
|
}
|
|
|
|
export default function useLocation() {
|
|
const location = useDumiLocation();
|
|
const { search } = location;
|
|
const [, localeType] = useLocale();
|
|
|
|
const getLink = React.useCallback(
|
|
(path: string, hash?: string | { cn: string; en: string }) => {
|
|
let pathname = clearPath(path);
|
|
|
|
if (localeType === 'cn') {
|
|
pathname = `${pathname}-cn`;
|
|
}
|
|
|
|
if (search) {
|
|
pathname = `${pathname}${search}`;
|
|
}
|
|
|
|
if (hash) {
|
|
let hashStr: string;
|
|
if (typeof hash === 'object') {
|
|
hashStr = hash[localeType];
|
|
} else {
|
|
hashStr = hash;
|
|
}
|
|
|
|
pathname = `${pathname}#${hashStr}`;
|
|
}
|
|
|
|
return pathname;
|
|
},
|
|
[localeType, search],
|
|
);
|
|
|
|
return {
|
|
...location,
|
|
pathname: clearPath(location.pathname),
|
|
getLink,
|
|
};
|
|
}
|
|
|