NextLink.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type { AnchorHTMLAttributes, JSX } from 'react';
  2. import type { LinkProps } from 'next/link';
  3. import Link from 'next/link';
  4. import { pagePathUtils } from '@growi/core/dist/utils';
  5. import { useSiteUrl } from '~/states/global';
  6. import loggerFactory from '~/utils/logger';
  7. const logger = loggerFactory('growi:components:NextLink');
  8. const hasAnchorLink = (href: string): boolean => {
  9. return href.toString().length > 0 && href[0] === '#';
  10. };
  11. const isExternalLink = (href: string, siteUrl: string | undefined): boolean => {
  12. try {
  13. const baseUrl = new URL(siteUrl ?? 'https://example.com');
  14. const hrefUrl = new URL(href, baseUrl);
  15. return baseUrl.host !== hrefUrl.host;
  16. } catch (err) {
  17. logger.debug(err);
  18. return false;
  19. }
  20. };
  21. const isCreatablePage = (href: string) => {
  22. try {
  23. const url = new URL(href, 'http://example.com');
  24. const pathName = url.pathname;
  25. return pagePathUtils.isCreatablePage(pathName);
  26. } catch (err) {
  27. logger.debug(err);
  28. return false;
  29. }
  30. };
  31. type Props = AnchorHTMLAttributes<HTMLAnchorElement> &
  32. Omit<LinkProps, 'href'> & {
  33. children: React.ReactNode;
  34. id?: string;
  35. href?: string;
  36. className?: string;
  37. };
  38. export const NextLink = (props: Props): JSX.Element => {
  39. const { id, href, children, className, target, onClick, ...rest } = props;
  40. const siteUrl = useSiteUrl();
  41. if (href == null) {
  42. // biome-ignore lint/a11y/useValidAnchor: ignore
  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) || target === '_blank') {
  50. return (
  51. <a
  52. id={id}
  53. href={href}
  54. className={className}
  55. target="_blank"
  56. onClick={onClick}
  57. rel="noopener noreferrer"
  58. {...dataAttributes}
  59. >
  60. {children}
  61. {target === '_blank' && (
  62. <span style={{ userSelect: 'none' }}>
  63. &nbsp;
  64. <span className="growi-custom-icons">external_link</span>
  65. </span>
  66. )}
  67. </a>
  68. );
  69. }
  70. // when href is an anchor link or not-creatable path
  71. if (hasAnchorLink(href) || !isCreatablePage(href) || target != null) {
  72. return (
  73. <a
  74. id={id}
  75. href={href}
  76. className={className}
  77. target={target}
  78. onClick={onClick}
  79. {...dataAttributes}
  80. >
  81. {children}
  82. </a>
  83. );
  84. }
  85. return (
  86. <Link {...rest} href={href} prefetch={false} legacyBehavior>
  87. <a
  88. href={href}
  89. className={className}
  90. {...dataAttributes}
  91. onClick={onClick}
  92. >
  93. {children}
  94. </a>
  95. </Link>
  96. );
  97. };