docs: Fix site link

This commit is contained in:
zombiej 2020-02-28 17:59:06 +08:00
parent ac94f96608
commit 43a5d57b81
4 changed files with 51 additions and 19 deletions

View File

@ -62,7 +62,12 @@ export default function Home() {
<BlockContent
title={<FormattedMessage id="app.home.more" />}
extra={
<Link to={getLocalizedPathname('/docs/resources', isZhCN)}>
<Link
to={getLocalizedPathname('/docs/resources', isZhCN, {
zhCN: '文章',
enUS: 'Articles',
})}
>
<FormattedMessage id="app.home.view-more" />
</Link>
}

View File

@ -172,13 +172,16 @@ class Footer extends React.Component<WrappedComponentProps> {
url: 'https://seeconf.antfin.com/',
openExternal: true,
},
{
isZhCN && {
icon: <UsergroupAddOutlined />,
title: <FormattedMessage id="app.footer.work_with_us" />,
url: getLocalizedPathname('/docs/resources', isZhCN),
url: getLocalizedPathname('/docs/resources', isZhCN, {
zhCN: '加入我们',
enUS: 'JoinUs',
}),
LinkComponent: Link,
},
],
].filter(n => n),
},
{
title: <FormattedMessage id="app.footer.help" />,

View File

@ -12,14 +12,27 @@ const { TabPane } = Tabs;
export default () => {
const containerRef = React.useRef<HTMLDivElement>(null);
const idsRef = React.useRef<string[]>([]);
const [, forceUpdate] = React.useState<{}>({});
const [loaded, setLoaded] = React.useState(false);
const [fixedId, setFixedId] = React.useState<string | null>(null);
function scrollToId(id: string) {
const newTop =
document.getElementById(id)!.offsetTop - containerRef.current!.offsetHeight - VIEW_BALANCE;
scrollTo(newTop);
}
React.useEffect(() => {
idsRef.current = Array.from(document.querySelectorAll('h2[id]')).map(({ id }) => id);
forceUpdate({});
setLoaded(true);
}, []);
React.useEffect(() => {
const hashId = decodeURIComponent((location.hash || '').slice(1));
if (hashId) {
scrollToId(hashId);
}
}, [loaded]);
const onSyncAffix = React.useMemo(() => {
function doSync() {
const scrollY = window.scrollY;
@ -63,11 +76,7 @@ export default () => {
<Tabs
activeKey={fixedId || undefined}
onChange={key => {
const newTop =
document.getElementById(key)!.offsetTop -
containerRef.current!.offsetHeight -
VIEW_BALANCE;
scrollTo(newTop);
scrollToId(key);
}}
>
{idsRef.current.map(id => (

View File

@ -114,19 +114,34 @@ export function isZhCN(pathname: string) {
return /-cn\/?$/.test(pathname);
}
export function getLocalizedPathname(path: string, zhCN?: boolean) {
export function getLocalizedPathname(
path: string,
zhCN?: boolean,
hash?: {
zhCN: string;
enUS: string;
},
) {
const pathname = path.startsWith('/') ? path : `/${path}`;
let fullPath = pathname;
if (!zhCN) {
// to enUS
return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
fullPath = /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
} else if (pathname === '/') {
fullPath = '/index-cn';
} else if (pathname.endsWith('/')) {
fullPath = pathname.replace(/\/$/, '-cn/');
} else {
fullPath = `${pathname}-cn`;
}
if (pathname === '/') {
return '/index-cn';
if (hash) {
const localHash = hash[isZhCN ? 'zhCN' : 'enUS'];
fullPath += `#${localHash}`;
}
if (pathname.endsWith('/')) {
return pathname.replace(/\/$/, '-cn/');
}
return `${pathname}-cn`;
return fullPath;
}
export function ping(callback: (status: string) => void) {