@alauda/doom
Version:
Doctor Doom making docs.
41 lines (40 loc) • 2.14 kB
JavaScript
import { jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { NoSSR, useLang, useSite } from '@rspress/core/runtime';
import { addTrailingSlash, isExternalUrl, normalizeHref, parseUrl, removeLeadingSlash, } from '@rspress/shared';
import { DEFAULT_PAGE_EXTENSIONS } from '@rspress/shared/constants';
import { clsx } from 'clsx';
import virtual from 'doom-@global-virtual';
import { useMemo } from 'react';
import classes from '../../../styles/link.module.scss';
import { isUnversioned } from '../../shared/helpers.js';
import { useIsPrint } from '../hooks/index.js';
import { Directive } from './Directive.js';
const ExternalSiteLink_ = ({ name, href = '', className, ...props }) => {
const isPrint = useIsPrint();
const { site: siteData } = useSite();
const site = useMemo(() => virtual.sites?.find((s) => s.name === name), [name]);
const lang = useLang();
if (!site) {
return (_jsxs(Directive, { type: "danger", children: ["No site with name `", name, "` found, please ensure it's already defined at `sites.yaml`"] }));
}
if (isExternalUrl(href)) {
return _jsxs(Directive, { type: "danger", children: ["Invalid href `", href, "` found"] });
}
let { url, hash } = parseUrl(href);
const extname = url.split('.').at(-1);
if (extname) {
if (DEFAULT_PAGE_EXTENSIONS.includes(`.${extname}`)) {
url = url.replace(new RegExp(`\\.${extname}$`), '');
}
}
url = removeLeadingSlash(normalizeHref(url));
return (_jsx("a", { href: (isPrint ? 'https://docs.alauda.io' : '') +
(virtual.prefix || '') +
(isUnversioned(virtual.version)
? site.base
: addTrailingSlash(site.base + site.version)) +
(lang && lang !== siteData.lang ? addTrailingSlash(lang) : '') +
(hash ? `${url}#${hash}` : url), target: "_blank", rel: "noopener noreferrer", className: clsx(classes.link, 'rp-cursor-pointer', className), ...props }));
};
export const ExternalSiteLink = (props) => (_jsx(NoSSR, { children: _jsx(ExternalSiteLink_, { ...props }) }));
export default ExternalSiteLink;