内部链接无法通过remark和gatsbyjs正确解析

时间:2018-11-26 19:59:09

标签: gatsby

我在Twitter和Linkedin上发布了一篇最近的博客文章。我直接从地址栏中复制了link,但是链接解析为首页

但是当我单击iPhone时,它会转到主页

const _ = require('lodash')
const Promise = require('bluebird')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { blogPostPath } = require('./src/utils/url')

exports.createPages = ({ graphql, actions }) => {
  const { createPage } = actions

  return new Promise((resolve, reject) => {
    const blogPost = path.resolve('./src/templates/blog-post.js')
    resolve(
      graphql(
        `
          {
            allMarkdownRemark(
              sort: { fields: [frontmatter___date], order: DESC }
              limit: 1000
            ) {
              edges {
                node {
                  fields {
                    slug
                  }
                  frontmatter {
                    title
                  }
                }
              }
            }
          }
        `
      ).then(result => {
        if (result.errors) {
          console.log(result.errors)
          reject(result.errors)
        }

        // Create blog posts pages.
        const posts = result.data.allMarkdownRemark.edges

        _.each(posts, (post, index) => {
          const previous =
            index === posts.length - 1 ? null : posts[index + 1].node
          const next = index === 0 ? null : posts[index - 1].node

          const path = blogPostPath(post.node.fields.slug)

          createPage({
            path,
            component: blogPost,
            context: {
              slug: post.node.fields.slug,
              previous,
              next,
            },
          })
        })
      })
    )
  })
}

exports.onCreateNode = ({ node, actions, getNode }) => {
  const { createNodeField } = actions

  if (node.internal.type === `MarkdownRemark`) {
    const value = createFilePath({ node, getNode })
    createNodeField({
      name: `slug`,
      node,
      value,
    })
  }
}

我的blogPostPath看起来像这样:

module.exports.blogPostPath = slug => {
  const date = slug.substr(0, 11).replace(/-/g, '/')
  const title = slug.substr(12).replace('/', '')

  console.log(`/blog${date}/${title}`)

  return `/blog${date}/${title}`
}

0 个答案:

没有答案