|
| 1 | +import React, { type ReactNode } from "react"; |
| 2 | +import Link from "@docusaurus/Link"; |
| 3 | +import useBaseUrl from "@docusaurus/useBaseUrl"; |
| 4 | +import isInternalUrl from "@docusaurus/isInternalUrl"; |
| 5 | +import { isRegexpStringMatch } from "@docusaurus/theme-common"; |
| 6 | +import IconExternalLink from "@theme/Icon/ExternalLink"; |
| 7 | +import type { Props } from "@theme/NavbarItem/NavbarNavLink"; |
| 8 | +import useDocusaurusContext from "@docusaurus/useDocusaurusContext"; |
| 9 | +const EN_DOCS_HOME_LINK = "https://docs.databend.com/"; |
| 10 | +const CN_DOCS_HOME_LINK = "https://docs.databend.cn/"; |
| 11 | + |
| 12 | +export default function NavbarNavLink({ |
| 13 | + activeBasePath, |
| 14 | + activeBaseRegex, |
| 15 | + to, |
| 16 | + href, |
| 17 | + label, |
| 18 | + html, |
| 19 | + isDropdownLink, |
| 20 | + prependBaseUrlToHref, |
| 21 | + ...props |
| 22 | +}: Props): ReactNode { |
| 23 | + const { |
| 24 | + siteConfig: { |
| 25 | + customFields: { isChina }, |
| 26 | + }, |
| 27 | + } = useDocusaurusContext(); |
| 28 | + |
| 29 | + function replacePathname(url: string) { |
| 30 | + if (isChina) { |
| 31 | + if (url?.startsWith("pathname:///en/")) { |
| 32 | + return url.replace("pathname:///en/", EN_DOCS_HOME_LINK); |
| 33 | + } else if (url?.startsWith("pathname:///")) { |
| 34 | + return url.replace("pathname:///", CN_DOCS_HOME_LINK); |
| 35 | + } |
| 36 | + } else { |
| 37 | + if (url?.startsWith("pathname:///zh/")) { |
| 38 | + return url.replace("pathname:///zh/", CN_DOCS_HOME_LINK); |
| 39 | + } else if (url?.startsWith("pathname:///")) { |
| 40 | + return url.replace("pathname:///", EN_DOCS_HOME_LINK); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return url; |
| 45 | + } |
| 46 | + |
| 47 | + // TODO all this seems hacky |
| 48 | + // {to: 'version'} should probably be forbidden, in favor of {to: '/version'} |
| 49 | + const toUrl = useBaseUrl(to); |
| 50 | + console.log(toUrl); |
| 51 | + const activeBaseUrl = useBaseUrl(activeBasePath); |
| 52 | + const normalizedHref = useBaseUrl(href, { forcePrependBaseUrl: true }); |
| 53 | + const isExternalLink = label && href && !isInternalUrl(href); |
| 54 | + |
| 55 | + // Link content is set through html XOR label |
| 56 | + const linkContentProps = html |
| 57 | + ? { dangerouslySetInnerHTML: { __html: html } } |
| 58 | + : { |
| 59 | + children: ( |
| 60 | + <> |
| 61 | + {label} |
| 62 | + {isExternalLink && ( |
| 63 | + <IconExternalLink |
| 64 | + {...(isDropdownLink && { width: 12, height: 12 })} |
| 65 | + /> |
| 66 | + )} |
| 67 | + </> |
| 68 | + ), |
| 69 | + }; |
| 70 | + |
| 71 | + if (href) { |
| 72 | + return ( |
| 73 | + <Link |
| 74 | + href={prependBaseUrlToHref ? normalizedHref : href} |
| 75 | + {...props} |
| 76 | + {...linkContentProps} |
| 77 | + /> |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + return ( |
| 82 | + <Link |
| 83 | + to={replacePathname(toUrl)} |
| 84 | + isNavLink |
| 85 | + {...((activeBasePath || activeBaseRegex) && { |
| 86 | + isActive: (_match, location) => |
| 87 | + activeBaseRegex |
| 88 | + ? isRegexpStringMatch(activeBaseRegex, location.pathname) |
| 89 | + : location.pathname.startsWith(activeBaseUrl), |
| 90 | + })} |
| 91 | + {...props} |
| 92 | + {...linkContentProps} |
| 93 | + /> |
| 94 | + ); |
| 95 | +} |
0 commit comments