NextLink.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { pagePathUtils } from '@growi/core';
  2. import Link, { LinkProps } from 'next/link';
  3. import { useSiteUrl } from '~/stores/context';
  4. import loggerFactory from '~/utils/logger';
  5. const logger = loggerFactory('growi:components:NextLink');
  6. const isAnchorLink = (href: string): boolean => {
  7. return href.toString().length > 0 && href[0] === '#';
  8. };
  9. const isExternalLink = (href: string, siteUrl: string | undefined): boolean => {
  10. try {
  11. const baseUrl = new URL(siteUrl ?? 'https://example.com');
  12. const hrefUrl = new URL(href, baseUrl);
  13. return baseUrl.host !== hrefUrl.host;
  14. }
  15. catch (err) {
  16. logger.debug(err);
  17. return false;
  18. }
  19. };
  20. const isCreatablePage = (href: string) => {
  21. try {
  22. const url = new URL(href);
  23. const pathName = url.pathname;
  24. return pagePathUtils.isCreatablePage(pathName);
  25. }
  26. catch (err) {
  27. logger.debug(err);
  28. return false;
  29. }
  30. };
  31. type Props = Omit<LinkProps, 'href'> & {
  32. children: React.ReactNode,
  33. id?: string,
  34. href?: string,
  35. className?: string,
  36. };
  37. export const NextLink = (props: Props): JSX.Element => {
  38. const {
  39. id, href, children, className, ...rest
  40. } = props;
  41. const { data: siteUrl } = useSiteUrl();
  42. if (href == null) {
  43. return <a className={className}>{children}</a>;
  44. }
  45. // extract 'data-*' props
  46. const dataAttributes = Object.fromEntries(
  47. Object.entries(rest).filter(([key]) => key.startsWith('data-')),
  48. );
  49. if (isExternalLink(href, siteUrl)) {
  50. return (
  51. <a id={id} href={href} className={className} target="_blank" rel="noopener noreferrer" {...dataAttributes}>
  52. {children}&nbsp;<i className='icon-share-alt small'></i>
  53. </a>
  54. );
  55. }
  56. // when href is an anchor link or not-creatable path
  57. if (isAnchorLink(href) || !isCreatablePage(href)) {
  58. return (
  59. <a id={id} href={href} className={className} {...dataAttributes}>{children}</a>
  60. );
  61. }
  62. return (
  63. <Link {...rest} href={href} prefetch={false} legacyBehavior>
  64. <a href={href} className={className} {...dataAttributes}>{children}</a>
  65. </Link>
  66. );
  67. };