@hashicorp/react-components 0.0.0-nightly.8b95253 → 0.0.0-nightly.91abcef

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hashicorp/react-components",
3
- "version": "0.0.0-nightly.8b95253",
3
+ "version": "0.0.0-nightly.91abcef",
4
4
  "main": "src/index.ts",
5
5
  "scripts": {
6
6
  "lint": "next-hashicorp lint",
@@ -13,6 +13,7 @@ export interface ResourceCardProps {
13
13
  link: string
14
14
  productBadges?: CardProps['productBadges']
15
15
  thumbnail: ThumbnailProps
16
+ isExternal?: CardProps['isExternal']
16
17
  }
17
18
 
18
19
  export function ResourceCard({
@@ -22,6 +23,7 @@ export function ResourceCard({
22
23
  link,
23
24
  productBadges,
24
25
  thumbnail,
26
+ isExternal,
25
27
  }: ResourceCardProps) {
26
28
  const meta: string[] = date ? [date, category] : [category]
27
29
 
@@ -32,6 +34,7 @@ export function ResourceCard({
32
34
  meta={meta}
33
35
  productBadges={productBadges}
34
36
  thumbnail={thumbnail}
37
+ isExternal={isExternal}
35
38
  />
36
39
  )
37
40
  }
@@ -7,6 +7,7 @@
7
7
 
8
8
  import { useEffect } from 'react'
9
9
  import { useFormContext } from 'react-hook-form'
10
+ import { removeLocaleFromPathname } from '@web/utils/supported-locales/index'
10
11
  import type { MarketoFormHiddenField } from '../../../types'
11
12
 
12
13
  const FormPageUrlField = ({ field }: { field: MarketoFormHiddenField }) => {
@@ -14,7 +15,15 @@ const FormPageUrlField = ({ field }: { field: MarketoFormHiddenField }) => {
14
15
 
15
16
  useEffect(() => {
16
17
  const { protocol, host, pathname } = window.location
17
- setValue(field.id, `${protocol}//${host}${pathname}`)
18
+ // Marketo campaigns expect a URL that does not include the locale.
19
+ // This is because the campaigns were created prior to locales being
20
+ // added to hashicorp.com paths. As a result, we need to strip out the
21
+ // locale from the path in order for Marketo campaigns to work.
22
+ // In the future, we should better understand why this dependency exists
23
+ // and explore ways to make this less brittle.
24
+ const pathnameWithoutLocale = removeLocaleFromPathname(pathname)
25
+ const url = `${protocol}//${host}${pathnameWithoutLocale}`
26
+ setValue(field.id, url)
18
27
  }, [])
19
28
 
20
29
  return <input type="hidden" {...register(field.id)} />
@@ -46,11 +46,6 @@ export interface MarketoBaseFormField {
46
46
  validationMessage?: string
47
47
  autoFill?: Autofill
48
48
  visibilityRules?: VisibilityRules
49
- /**
50
- * Properties that are not part of the Marketo API response but are
51
- * attached to the field at runtime for additional functionality
52
- */
53
- runtimeAttributes?: Record<string, unknown>
54
49
  }
55
50
 
56
51
  export interface MarketoFormTextField extends MarketoBaseFormField {
@@ -1,11 +1,12 @@
1
1
  import Link from 'next/link'
2
- import { parseUrl } from '../../../../lib/parse-url'
3
2
  import { vercelSplit } from '../../../../lib/v'
3
+ import { getLocalizedLinkProps } from '../../../../lib/get-localized-link-props'
4
4
  import FlightIcon from '@hashicorp/react-hds/src/components/flight-icon'
5
5
  import * as NavigationMenu from '@radix-ui/react-navigation-menu'
6
6
  import s from './style.module.css'
7
7
 
8
8
  export interface LinkListProps {
9
+ locale?: string
9
10
  links: {
10
11
  title: string
11
12
  url: string
@@ -37,7 +38,11 @@ const GRADIENT_VALUE_MAP = new Map([
37
38
  ],
38
39
  ])
39
40
 
40
- export default function LinkList({ links, linkClickHandler }: LinkListProps) {
41
+ export default function LinkList({
42
+ locale,
43
+ links,
44
+ linkClickHandler,
45
+ }: LinkListProps) {
41
46
  return (
42
47
  <ul className={s.linkList}>
43
48
  {links.map((link) => {
@@ -54,7 +59,7 @@ export default function LinkList({ links, linkClickHandler }: LinkListProps) {
54
59
  <NavigationMenu.Link asChild>
55
60
  <Link
56
61
  className={s.link}
57
- href={parseUrl(link.url).href}
62
+ {...getLocalizedLinkProps(link.url, locale)}
58
63
  onClickCapture={() => {
59
64
  linkClickHandler && linkClickHandler(link.title, link.url)
60
65
  }}
@@ -2,13 +2,14 @@ import { useCallback } from 'react'
2
2
  import classNames from 'classnames'
3
3
  import { vercelSplit } from '../../../../lib/v'
4
4
  import Link from 'next/link'
5
- import { parseUrl } from '../../../../lib/parse-url'
5
+ import { getLocalizedLinkProps } from '../../../../lib/get-localized-link-props'
6
6
  import { trackNavClickEvent } from '../../../../lib/track-nav-click-event'
7
7
  import FlightIcon from '@hashicorp/react-hds/src/components/flight-icon'
8
8
  import * as NavigationMenu from '@radix-ui/react-navigation-menu'
9
9
  import s from './style.module.css'
10
10
 
11
11
  export interface PromoProps {
12
+ locale?: string
12
13
  title: string
13
14
  description: string
14
15
  linkUrl: string
@@ -20,6 +21,7 @@ export interface PromoProps {
20
21
  }
21
22
 
22
23
  export default function Promo({
24
+ locale,
23
25
  title,
24
26
  description,
25
27
  linkUrl,
@@ -72,7 +74,8 @@ export default function Promo({
72
74
  </div>
73
75
  <NavigationMenu.Link asChild>
74
76
  <Link
75
- href={parseUrl(linkUrl).href}
77
+ locale={locale ? locale : undefined}
78
+ {...getLocalizedLinkProps(linkUrl, locale)}
76
79
  className={s.link}
77
80
  onClickCapture={() => handleClick(title, linkUrl, linkTitle, slug)}
78
81
  >
@@ -2,10 +2,10 @@ import classNames from 'classnames'
2
2
  import Link from 'next/link'
3
3
  import * as NavPanel from '../components/panel'
4
4
  import Promo, { type PromoProps } from '../components/promo'
5
+ import { getLocalizedLinkProps } from '../../../lib/get-localized-link-props'
5
6
  import { Products } from '@hashicorp/platform-product-meta'
6
7
  import FlightIcon from '@hashicorp/react-hds/src/components/flight-icon'
7
8
  import { TextPlus } from '@hashicorp/react-hds/src/components/text'
8
- import { parseUrl } from '../../../lib/parse-url'
9
9
  import { trackNavClickEvent } from '../../../lib/track-nav-click-event'
10
10
  import * as NavigationMenu from '@radix-ui/react-navigation-menu'
11
11
  import s from './style.module.css'
@@ -30,6 +30,7 @@ const PRODUCT_LOGO_EXCEPTION_CLASSNAMES = {
30
30
  }
31
31
 
32
32
  export interface ProductsPanelProps {
33
+ locale: string
33
34
  productCategories: any
34
35
  promo?: PromoProps
35
36
  sidePanel?: {
@@ -44,6 +45,7 @@ export interface ProductsPanelProps {
44
45
  }
45
46
 
46
47
  const ProductPanel = ({
48
+ locale,
47
49
  productCategories,
48
50
  promo,
49
51
  sidePanel,
@@ -67,7 +69,7 @@ const ProductPanel = ({
67
69
  {products.map((product) => {
68
70
  return (
69
71
  <li key={product.url}>
70
- <NavProduct {...product} />
72
+ <NavProduct locale={locale} {...product} />
71
73
  </li>
72
74
  )
73
75
  })}
@@ -97,7 +99,8 @@ const ProductPanel = ({
97
99
  <li key={title}>
98
100
  <Link
99
101
  aria-label={`${title} - ${description}`}
100
- href={parseUrl(url).href}
102
+ {...getLocalizedLinkProps(url, locale)}
103
+ locale={locale ? locale : undefined}
101
104
  className={classNames(s.sidePanelItem, s.focusIndicator)}
102
105
  >
103
106
  <div className={s.iconBackground}>
@@ -125,17 +128,19 @@ const ProductPanel = ({
125
128
  }
126
129
 
127
130
  export interface NavProductProps {
131
+ locale?: string
128
132
  url: string
129
133
  product: Products | string
130
134
  description: string
131
135
  }
132
136
 
133
- function NavProduct({ url, product, description }: NavProductProps) {
137
+ function NavProduct({ locale, url, product, description }: NavProductProps) {
134
138
  return (
135
139
  <NavigationMenu.Link asChild>
136
140
  <Link
137
141
  aria-label={`${product} - ${description}`}
138
- href={parseUrl(url).href}
142
+ {...getLocalizedLinkProps(url, locale)}
143
+ locale={locale ? locale : undefined}
139
144
  onClickCapture={() => {
140
145
  trackNavClickEvent(product, url, 'products')
141
146
  }}
@@ -12,12 +12,14 @@ interface StandardPanelProps {
12
12
  navData: NavItem[]
13
13
  promo?: PromoProps
14
14
  sectionName?: string
15
+ locale?: any
15
16
  }
16
17
 
17
18
  const StandardPanel = ({
18
19
  navData,
19
20
  promo,
20
21
  sectionName = '',
22
+ locale,
21
23
  }: StandardPanelProps) => {
22
24
  return (
23
25
  <NavPanel.Root>
@@ -26,6 +28,7 @@ const StandardPanel = ({
26
28
  return (
27
29
  <NavPanel.Column key={stableIdx}>
28
30
  <LinkList
31
+ locale={locale}
29
32
  links={navItems}
30
33
  linkClickHandler={(text, href) => {
31
34
  trackNavClickEvent(text, href, sectionName)
@@ -46,6 +46,7 @@ type SubnavDropdown = {
46
46
  type MenuItem = SubnavLink | SubnavDropdown | 'divider'
47
47
 
48
48
  export interface SubnavInnerProps {
49
+ locale?: string
49
50
  hasOverflow?: boolean
50
51
  titleLink: { text: string; url: string }
51
52
  product?: Products
@@ -61,6 +62,7 @@ export interface SubnavInnerProps {
61
62
  }
62
63
 
63
64
  function SubnavInner({
65
+ locale,
64
66
  hasOverflow,
65
67
  titleLink,
66
68
  product,
@@ -109,17 +111,21 @@ function SubnavInner({
109
111
  >
110
112
  <NavigationMenu.Link asChild>
111
113
  <TitleLink
114
+ locale={locale}
112
115
  text={titleLink.text}
113
116
  url={titleLink.url}
114
117
  product={product}
115
118
  theme={theme}
116
119
  />
117
120
  </NavigationMenu.Link>
121
+
118
122
  {!hasOverflow && (
119
123
  <div className={s.subNavRight}>
120
124
  <MenuItemsDefault
121
125
  menuItems={menuItemsWithActive}
122
126
  activatePanel={activatePanel}
127
+ locale={locale}
128
+ product={product}
123
129
  />
124
130
  {ctaLinks?.length > 0 && (
125
131
  <CtaLinks
@@ -10,6 +10,7 @@ import DropdownTrigger from '../DropdownTrigger'
10
10
  import * as NavigationMenu from '@radix-ui/react-navigation-menu'
11
11
  import s from './style.module.css'
12
12
  import FlightIcon from '@hashicorp/react-hds/src/components/flight-icon'
13
+ import { getLocalizedLinkProps } from '../../../../lib/get-localized-link-props'
13
14
 
14
15
  interface MenuItemsDefaultProps {
15
16
  menuItems: (
@@ -27,10 +28,12 @@ interface MenuItemsDefaultProps {
27
28
  | 'divider'
28
29
  )[]
29
30
  activatePanel: (panel: string) => void
31
+ locale?: string
32
+ product?: string
30
33
  }
31
34
 
32
35
  function MenuItemsDefault(props: MenuItemsDefaultProps) {
33
- const { menuItems, activatePanel } = props
36
+ const { menuItems, activatePanel, locale, product } = props
34
37
  return (
35
38
  <NavigationMenu.List className={s.root}>
36
39
  {menuItems.map((menuItem, stableIdx) => {
@@ -47,6 +50,7 @@ function MenuItemsDefault(props: MenuItemsDefaultProps) {
47
50
  text={text}
48
51
  submenu={submenu}
49
52
  activatePanel={activatePanel}
53
+ locale={locale}
50
54
  />
51
55
  ) : (
52
56
  <NavLink
@@ -56,6 +60,8 @@ function MenuItemsDefault(props: MenuItemsDefaultProps) {
56
60
  text={text}
57
61
  url={url}
58
62
  isActive={_isActiveUrl}
63
+ locale={locale}
64
+ product={product}
59
65
  />
60
66
  )
61
67
  })}
@@ -75,14 +81,19 @@ interface NavLinkProps {
75
81
  text: string
76
82
  url: string
77
83
  isActive: boolean
84
+ locale?: string
85
+ product?: string
78
86
  }
79
87
 
80
88
  function NavLink(props: NavLinkProps) {
81
- const { text, url, isActive } = props
89
+ const { text, url, isActive, locale, product } = props
82
90
  return (
83
91
  <NavigationMenu.Item className={s.listItem}>
84
92
  <NavigationMenu.Link asChild>
85
- <Link className={s.navLink} href={url}>
93
+ <Link
94
+ className={s.navLink}
95
+ {...getLocalizedLinkProps(url, locale, Boolean(product))}
96
+ >
86
97
  <NavItemText isActive={isActive} text={text} />
87
98
  </Link>
88
99
  </NavigationMenu.Link>
@@ -94,6 +105,7 @@ interface NavLinkWithDropdownProps {
94
105
  text: string
95
106
  submenu: { icon: string; text: string; url: string; _isActiveUrl: string }[]
96
107
  activatePanel: (panel: string) => void
108
+ locale?: string
97
109
  }
98
110
 
99
111
  interface NavLinkWithDropdownState {
@@ -105,7 +117,7 @@ class NavLinkWithDropdown extends React.Component<
105
117
  NavLinkWithDropdownState
106
118
  > {
107
119
  render() {
108
- const { text, submenu, activatePanel } = this.props
120
+ const { text, submenu, activatePanel, locale } = this.props
109
121
  const hasActiveChild = submenu.some((s) => s._isActiveUrl)
110
122
 
111
123
  const handleFocusOutside = () => {
@@ -139,7 +151,10 @@ class NavLinkWithDropdown extends React.Component<
139
151
  // eslint-disable-next-line react/no-array-index-key
140
152
  <NavigationMenu.Item key={stableIdx}>
141
153
  <NavigationMenu.Link asChild>
142
- <Link className={s.submenuItem} href={url}>
154
+ <Link
155
+ className={s.submenuItem}
156
+ {...getLocalizedLinkProps(url, locale)}
157
+ >
143
158
  {icon ? <FlightIcon name={icon} /> : null}
144
159
  {text}
145
160
  </Link>
@@ -13,6 +13,7 @@ import * as NavigationMenu from '@radix-ui/react-navigation-menu'
13
13
  import classNames from 'classnames'
14
14
  import s from './style.module.css'
15
15
  import FlightIcon from '@hashicorp/react-hds/src/components/flight-icon'
16
+ import { getLocalizedLinkProps } from '../../../../lib/get-localized-link-props'
16
17
 
17
18
  interface MenuItemsOverflowProps {
18
19
  menuItems: (
@@ -29,6 +30,7 @@ interface MenuItemsOverflowProps {
29
30
  menuText: string
30
31
  constrainWidth?: boolean
31
32
  activatePanel: (panel: string) => void
33
+ locale?: string
32
34
  }
33
35
 
34
36
  interface MenuItemsOverflowState {
@@ -48,6 +50,7 @@ class MenuItemsOverflow extends React.Component<
48
50
  menuText,
49
51
  constrainWidth,
50
52
  activatePanel,
53
+ locale,
51
54
  } = this.props
52
55
 
53
56
  const handleFocusOutside = () => {
@@ -91,6 +94,8 @@ class MenuItemsOverflow extends React.Component<
91
94
  url={subItem.url}
92
95
  text={subItem.text}
93
96
  icon={subItem.icon}
97
+ locale={locale}
98
+ product={product}
94
99
  />
95
100
  ))}
96
101
  <hr className={s.divider} />
@@ -103,6 +108,7 @@ class MenuItemsOverflow extends React.Component<
103
108
  key={stableIdx}
104
109
  url={url}
105
110
  text={text}
111
+ product={product}
106
112
  />
107
113
  )
108
114
  }
@@ -125,13 +131,19 @@ interface SubmenuItemProps {
125
131
  icon?: string
126
132
  url: string
127
133
  text: string
134
+ locale?: string
135
+ product?: string
128
136
  }
129
137
 
130
- function SubmenuItem({ icon, url, text }: SubmenuItemProps) {
138
+ function SubmenuItem({ icon, url, text, locale, product }: SubmenuItemProps) {
131
139
  return (
132
140
  <li>
133
141
  <NavigationMenu.Link asChild>
134
- <Link className={s.submenuItem} href={url} title={vercelSplit(text)}>
142
+ <Link
143
+ className={s.submenuItem}
144
+ title={vercelSplit(text)}
145
+ {...getLocalizedLinkProps(url, locale, Boolean(product))}
146
+ >
135
147
  {icon ? <FlightIcon name={icon} /> : null}
136
148
  {text}
137
149
  </Link>
@@ -22,6 +22,7 @@ import WaypointLogoAttrOnDark from '@hashicorp/web-mktg-logos/src/waypoint/hashi
22
22
  import HCPTerraformLogoOnLight from '@hashicorp/web-mktg-logos/src/terraform/hcp-terraform/hcp-terraform_on-light.svg'
23
23
  import HCPTerraformLogoOnDark from '@hashicorp/web-mktg-logos/src/terraform/hcp-terraform/hcp-terraform_on-dark.svg'
24
24
  import s from './style.module.css'
25
+ import { getLocalizedLinkProps } from '../../../../lib/get-localized-link-props'
25
26
 
26
27
  const logoDict = {
27
28
  light: {
@@ -41,6 +42,7 @@ const logoDict = {
41
42
  }
42
43
 
43
44
  interface TitleLinkProps {
45
+ locale?: string
44
46
  text: string
45
47
  url: string
46
48
  product?: string
@@ -80,7 +82,7 @@ function getLogo(
80
82
  }
81
83
 
82
84
  const TitleLink = forwardRef<HTMLAnchorElement, TitleLinkProps>(
83
- ({ text, url, product, theme }, ref) => {
85
+ ({ text, url, product, theme, locale }, ref) => {
84
86
  const parsedText = vercelSplit(text)
85
87
  const Logo = getLogo(parsedText, theme)
86
88
 
@@ -91,9 +93,9 @@ const TitleLink = forwardRef<HTMLAnchorElement, TitleLinkProps>(
91
93
  s[`brand-${product}`],
92
94
  s[camelCase(parsedText)]
93
95
  )}
94
- href={url}
95
96
  title={parsedText}
96
97
  ref={ref}
98
+ {...getLocalizedLinkProps(url, locale)}
97
99
  >
98
100
  {Logo ? (
99
101
  <Image
@@ -44,7 +44,7 @@
44
44
 
45
45
  .subnavInner {
46
46
  display: grid;
47
- grid-template-columns: minmax(max-content, 1fr) min-content;
47
+ grid-template-columns: max-content min-content;
48
48
  align-items: center;
49
49
  justify-content: space-between;
50
50
  min-height: 62px;
@@ -0,0 +1,30 @@
1
+ import { getLocalizedLinkProps } from '.'
2
+
3
+ describe('getLocalizedLinkProps', () => {
4
+ it('does not format localized homepage', () => {
5
+ const localizedLinkProps = getLocalizedLinkProps('/en', 'ja')
6
+ expect(localizedLinkProps.href).toBe('/en')
7
+ expect(localizedLinkProps.hrefLang).toBe('en')
8
+ })
9
+
10
+ it('does not format localized path hrefs', () => {
11
+ const localizedLinkProps = getLocalizedLinkProps('/ja/solutions', 'de')
12
+ expect(localizedLinkProps.href).toBe('/ja/solutions')
13
+ expect(localizedLinkProps.hrefLang).toBe('ja')
14
+ })
15
+
16
+ it('does format paths that start with a locale string', () => {
17
+ const localizedLinkProps = getLocalizedLinkProps(
18
+ '/deconstructing-cloud',
19
+ 'en'
20
+ )
21
+ expect(localizedLinkProps.href).toBe('/en/deconstructing-cloud')
22
+ expect(localizedLinkProps.hrefLang).toBe('en')
23
+ })
24
+
25
+ it('does not format external paths', () => {
26
+ const localizedLinkProps = getLocalizedLinkProps('https://google.com', 'ja')
27
+ expect(localizedLinkProps.href).toBe('https://google.com')
28
+ expect(localizedLinkProps.hrefLang).toBe(undefined)
29
+ })
30
+ })
@@ -0,0 +1,46 @@
1
+ import { parseUrl } from '../parse-url'
2
+ import { allSupportedLocales } from '../supported-locales'
3
+
4
+ export const getLocalizedLinkProps = (
5
+ url: string,
6
+ locale?: string,
7
+ forceExternal?: boolean
8
+ ): {
9
+ href: string
10
+ hrefLang: string | undefined
11
+ } => {
12
+ const parsedHref = parseUrl(url, forceExternal).href
13
+
14
+ // example: /en
15
+ const hrefIsLocalizedHomepage = allSupportedLocales.includes(
16
+ parsedHref.substring(1)
17
+ )
18
+
19
+ // true example: /en/solutions
20
+ // false example: /entry-point
21
+ const hrefIsLocalized =
22
+ allSupportedLocales.includes(parsedHref.substring(1, 3)) &&
23
+ parsedHref[3] === '/'
24
+
25
+ // If the href is already localized:
26
+ // - do not format the href
27
+ // - use the locale from the href for the hrefLang
28
+ if (hrefIsLocalizedHomepage || hrefIsLocalized) {
29
+ return {
30
+ href: parsedHref,
31
+ hrefLang: parsedHref.substring(1, 3),
32
+ }
33
+ }
34
+
35
+ if (parsedHref.startsWith('/') && locale) {
36
+ return {
37
+ href: `/${locale}${parsedHref}`,
38
+ hrefLang: locale,
39
+ }
40
+ }
41
+
42
+ return {
43
+ href: parsedHref,
44
+ hrefLang: undefined,
45
+ }
46
+ }
@@ -1,4 +1,4 @@
1
- import { parseUrl } from '.'
1
+ import { parseUrl, hasHashiCorpDestination } from '.'
2
2
 
3
3
  describe('parseUrl', () => {
4
4
  it('detects hashicorp.com URLs as internal', () => {
@@ -7,6 +7,12 @@ describe('parseUrl', () => {
7
7
  expect(url.linkType).toBe('inbound')
8
8
  })
9
9
 
10
+ it('detects www.hashicorp.com URLs as internal', () => {
11
+ const url = parseUrl('https://hashicorp.com/foo')
12
+ expect(url.href).toBe('/foo')
13
+ expect(url.linkType).toBe('inbound')
14
+ })
15
+
10
16
  it('detects URLs not containing hashicorp.com as external', () => {
11
17
  const externalUrl = 'https://google.com/example'
12
18
  const url = parseUrl(externalUrl)
@@ -38,3 +44,26 @@ describe('parseUrl', () => {
38
44
  expect(url.href).toBe('https://developer.hashicorp.com/terraform')
39
45
  })
40
46
  })
47
+
48
+ describe('hasHashiCorpDestination', () => {
49
+ it('returns true for supported hashicorp domains', () => {
50
+ const isHashiCorpDestination = hasHashiCorpDestination(
51
+ 'https://portal.cloud.hashicorp.com/foo'
52
+ )
53
+ expect(isHashiCorpDestination).toBe(true)
54
+ })
55
+ it('returns false for external domains', () => {
56
+ const isHashiCorpDestination = hasHashiCorpDestination(
57
+ 'https://foo.com/bar'
58
+ )
59
+ expect(isHashiCorpDestination).toBe(false)
60
+ })
61
+ it('returns true for relative links', () => {
62
+ const isHashiCorpDestination = hasHashiCorpDestination('/bar')
63
+ expect(isHashiCorpDestination).toBe(true)
64
+ })
65
+ it('returns true for hashes', () => {
66
+ const isHashiCorpDestination = hasHashiCorpDestination('#someHash')
67
+ expect(isHashiCorpDestination).toBe(true)
68
+ })
69
+ })
@@ -1,4 +1,46 @@
1
- export function parseUrl(url: string | undefined): {
1
+ export const hashicorpDestinations: string[] = [
2
+ 'app.terraform.io',
3
+ 'boundaryproject.io',
4
+ 'cloud.hashicorp.com',
5
+ 'consul.io',
6
+ 'developer.hashicorp.com',
7
+ 'hashicorp.com',
8
+ 'nomadproject.io',
9
+ 'packer.io',
10
+ 'portal.cloud.hashicorp.com',
11
+ 'registry.terraform.io',
12
+ 'terraform.io',
13
+ 'vagrantup.com',
14
+ 'vaultproject.io',
15
+ 'waypointproject.io',
16
+ ] as const
17
+
18
+ const containsHashiCorpDestination = (str: string): boolean =>
19
+ hashicorpDestinations.some(function (destination) {
20
+ return str.indexOf(destination) >= 0
21
+ })
22
+
23
+ /**
24
+ * This function determines if a `url` is a HashiCorp link (relative URLs, hashes,
25
+ * or a url with a HashiCorp domain) or a non-HashiCorp link.
26
+ *
27
+ * @param {string | undefined} url - The URL to be parsed. It may be a relative URL, an anchor, or a full external/internal URL.
28
+ *
29
+ * @returns {{ linkType: 'inbound' | 'outbound', href: string }}
30
+ * An object with two properties:
31
+ * - `linkType`: Indicates whether the URL is 'inbound' or 'outbound'. Can be used to conditionally
32
+ * render the value of the target attribute.
33
+ * - `href`: The parsed href (may include pathname, search, and hash for internal URLs).
34
+ *
35
+ * @throws {Error} Throws an error if the URL is undefined or invalid.
36
+ */
37
+
38
+ // @TODO: delete forceExternal when io sites are shutdown
39
+
40
+ export function parseUrl(
41
+ url: string | undefined,
42
+ forceExternal?: boolean
43
+ ): {
2
44
  linkType: 'inbound' | 'outbound' | ''
3
45
  href: string
4
46
  } {
@@ -11,7 +53,7 @@ export function parseUrl(url: string | undefined): {
11
53
  return { linkType: 'inbound', href: url }
12
54
  }
13
55
 
14
- if (isInternal(url)) {
56
+ if (!forceExternal && isInternal(url)) {
15
57
  try {
16
58
  const { pathname, search, hash } = new URL(url)
17
59
  const href = pathname + search + hash
@@ -25,10 +67,38 @@ export function parseUrl(url: string | undefined): {
25
67
  return { linkType: 'outbound', href: url }
26
68
  }
27
69
 
28
- const isRelative = (url) => url.startsWith('/')
29
- const isAnchor = (url) => url.startsWith('#')
70
+ const isRelative = (url: string) => url.startsWith('/')
71
+ const isAnchor = (url: string) => url.startsWith('#')
72
+
73
+ /**
74
+ * Determines whether the provided url uses hashicorp.com or www.hashicorp.com as the hostname.
75
+ *
76
+ * @param {string} url - The URL to check.
77
+ * @returns {boolean} - Returns true if the link uses a HashiCorp domain, otherwise false.
78
+ */
30
79
 
31
- export const isInternal = (url) => {
80
+ export const isInternal = (url: string): boolean => {
32
81
  const hostname = new URL(url).hostname
33
82
  return hostname === 'hashicorp.com' || hostname === 'www.hashicorp.com'
34
83
  }
84
+
85
+ /**
86
+ * Determines whether the provided url uses a HashiCorp hostname. This is used in several
87
+ * components to open a new tab for non-HashiCorp links.
88
+ *
89
+ * @param {string} url - The URL to check.
90
+ * @returns {boolean} - Returns true if the link uses a HashiCorp domain, otherwise false.
91
+ */
92
+
93
+ export const hasHashiCorpDestination = (url: string): boolean => {
94
+ // Fast return in case a relative URL or anchor is passed in
95
+ if (isRelative(url) || isAnchor(url)) return true
96
+ try {
97
+ const hostname = new URL(url).hostname
98
+ const isHashiCorpDestination = containsHashiCorpDestination(hostname)
99
+ return isHashiCorpDestination
100
+ } catch (e) {
101
+ console.error('Error: `url` is not a valid URL', e)
102
+ return false
103
+ }
104
+ }
@@ -0,0 +1 @@
1
+ export const allSupportedLocales = ['en', 'de', 'es', 'fr', 'ja', 'ko', 'pt']