mirror of
https://gitea.com/gitea/docs.git
synced 2026-09-19 04:28:53 +00:00
Fix navbar links and hide runner's development (#477)
Reviewed-on: https://gitea.com/gitea/docs/pulls/477
This commit is contained in:
@@ -1,15 +1,39 @@
|
||||
import React from 'react';
|
||||
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem';
|
||||
import {useLocation} from '@docusaurus/router';
|
||||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
|
||||
|
||||
// ensure a single trailing slash so paths can be compared as prefixes
|
||||
function withTrailingSlash(path) {
|
||||
return path.endsWith('/') ? path : `${path}/`;
|
||||
}
|
||||
|
||||
// baseUrl contains the locale prefix on localized builds (e.g. "/zh-cn/"),
|
||||
// while the configured item paths never do
|
||||
function stripBaseUrl(pathname, baseUrl) {
|
||||
return pathname.startsWith(baseUrl)
|
||||
? `/${pathname.slice(baseUrl.length)}`
|
||||
: pathname;
|
||||
}
|
||||
|
||||
export default function DropDown(props) {
|
||||
const {pathname} = useLocation();
|
||||
const {siteConfig} = useDocusaurusContext();
|
||||
const {routerRgx, classNames} = props;
|
||||
const r = new RegExp(routerRgx);
|
||||
let isMatched = r.test(pathname);
|
||||
let newLabel = props.label;
|
||||
if (isMatched) {
|
||||
newLabel = props.items.filter(item => item.to === pathname)[0]?.label ?? newLabel;
|
||||
const currentPath = withTrailingSlash(
|
||||
stripBaseUrl(pathname, siteConfig.baseUrl)
|
||||
);
|
||||
// the latest version is served without a version segment (e.g. "/api/"),
|
||||
// so match the longest item path that prefixes the current location
|
||||
// instead of requiring an exact match
|
||||
const bestMatch = props.items
|
||||
.filter(item => currentPath.startsWith(withTrailingSlash(item.to)))
|
||||
.sort((a, b) => b.to.length - a.to.length)[0];
|
||||
newLabel = bestMatch?.label ?? newLabel;
|
||||
}
|
||||
const newProps = {...props, label: newLabel};
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user