javascript Gatsby / Contentful - link.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Gatsby / Contentful - link.js相关的知识,希望对你有一定的参考价值。

import React from 'react'
import {Link as GatsbyLink} from 'gatsby'
import PropTypes from 'prop-types'

export const getPagePath = page => {
  let base = ''
  let slug = page.slug

  switch (page.internal.type) {
    case 'ContentfulFoodCategory': base = '/food'; break;
    case 'ContentfulBlogPost': base = '/community'; break;
    default: base = '';
  }

  if (slug === 'home') slug = ''

  return `${base}/${slug}`
}

export const getPageUrl = page => {
  const path = getPagePath(page)

  return `https://www.joesquared.com${path}`
}

const Link = ({
  page,
  url,
  className,
  children
}) => {
  if (page) {
    const path = getPagePath(page)

    return (
      <GatsbyLink 
        className={className}
        to={path}
      >
        {children}
      </GatsbyLink>
    )
  }
  else if (url) {
    if (url[0] === '/') {
      return (
        <GatsbyLink 
          className={className}
          to={url}
        >
          {children}
        </GatsbyLink>
      )
    }
    else {
      return (
        <a 
          className={className}
          href={url}
          target='__black'
        >
          {children}
        </a>
      )
    }
  }
  else {
    return <div {...{className}}>{children}</div>
  }
}

Link.propTypes = {
  url: PropTypes.string
}

export default Link

以上是关于javascript Gatsby / Contentful - link.js的主要内容,如果未能解决你的问题,请参考以下文章

javascript gatsby / contentful - createContentTypePages

javascript Gatsby / Contentful - link.js

javascript Gatsby / Contentful - createPages

javascript 结合Gatsby.js风格导入

javascript Gatsby / Webpack - 相对于root导入

javascript Gatsby / Contentful - image-background.js