Detect internal URLs in menu using config.siteUrl

This is so that menu items defined in Ghost don't get mistakenly
processed as external sites
This commit is contained in:
Hippo 2019-10-06 17:39:25 +05:30
parent b5851935db
commit 173c5c5654

View file

@ -1,6 +1,7 @@
import React from 'react' import React from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { Link } from 'gatsby' import { Link } from 'gatsby'
import config from '../../utils/siteConfig'
/** /**
* Navigation component * Navigation component
@ -17,7 +18,9 @@ const Navigation = ({ data, navClass }) => (
{data.map((navItem, i) => { {data.map((navItem, i) => {
if (navItem.url.match(/^\s?http(s?)/gi)) { if (navItem.url.match(/^\s?http(s?)/gi)) {
return <a className={navClass} href={navItem.url} key={i} target="_blank" rel="noopener noreferrer">{navItem.label}</a> return <a className={navClass} href={navItem.url} key={i} target="_blank" rel="noopener noreferrer">{navItem.label}</a>
} else { } else if (navItem.url.slice(0, config.siteUrl.length) == config.siteUrl) {
return <Link className={navClass} to={navItem.url.slice(config.siteUrl.length, navItem.url.length)} key={i}>{navItem.label}</Link>
} else{
return <Link className={navClass} to={navItem.url} key={i}>{navItem.label}</Link> return <Link className={navClass} to={navItem.url} key={i}>{navItem.label}</Link>
} }
})} })}