Skip to content
Snippets Groups Projects
Commit 08cc84d2 authored by Julien MARGAIL's avatar Julien MARGAIL
Browse files

fix Link internal check

parent c8fe920c
No related branches found
No related tags found
No related merge requests found
// From https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-link/ // From https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-link/
import React from "react" import React from "react"
import { Link as GatsbyLink } from "gatsby" import { graphql, useStaticQuery, Link as GatsbyLink } from "gatsby"
interface Props { interface Props {
children?: React.ReactNode children?: React.ReactNode
to: string to: string
activeClassName?: string activeClassName?: string
partiallyActive?: any partiallyActive?: any
onClick?:any onClick?: any
className?: any className?: any
} }
...@@ -25,34 +24,44 @@ const Link = ({ ...@@ -25,34 +24,44 @@ const Link = ({
className, className,
...other ...other
}: Props) => { }: Props) => {
// Si commence par http // Basé sur les données de l'API GraphQL = liste des pages
// Si contient const data = useStaticQuery(graphql`
// geoportal | geonetwork | metabase | ... query appPages {
allSitePage {
// Basé sur les données de l'api grapgql = liste des pages ? nodes {
path
}
}
}
`)
// Tailor the following test to your environment. // /^https?:\/\//
// This example assumes that any internal link (intended for Gatsby) // Si "to" n'est pas dans la liste des pages, c'est un lien externe
// will start with exactly one slash, and that anything else is external. // ignore le "/" à la fin du 'path' et de 'to'
const internal = /^\/(?!\/)/.test(to) const internal = data.allSitePage.nodes.find(
({ path }) => path.replace(/\/+$/, "") === to.replace(/\/+$/, "")
)
// Use Gatsby Link for internal links, and <a> for others // Use Gatsby Link for internal links, and <a> for others
if (internal) { if (internal) {
// console.log("Internal", to)
return ( return (
<GatsbyLink <GatsbyLink
to={to} to={to}
activeClassName={activeClassName} activeClassName={activeClassName}
partiallyActive={partiallyActive} partiallyActive={partiallyActive}
onClick={onClick} onClick={onClick}
className = {className} className={className}
{...other} {...other}
> >
{children} {children}
</GatsbyLink> </GatsbyLink>
) )
} }
// console.log("External", to)
return ( return (
<a href={to} {...other} onClick={onClick} className = {className}> <a href={to} {...other} onClick={onClick} className={className}>
{children} {children}
</a> </a>
) )
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment