【发布时间】:2020-05-31 15:31:52
【问题描述】:
我已经使用 Gatsby 的入门模板建立了一个博客。现在,当我打开一篇文章时,它显示的网址是-http://localhost:8000/JavaScript:%20Behind%20The%20Scenes/。我查找了this answer 并更改了path 属性,但是页面无法加载,它只是显示了一个具有相同url 的空页面。我不知道为什么它会在路径中附加 %20。
注意:路径其实就是文件夹名。例如,在目录/content/blog/JavaScript:Behind The Scenes/index.md 中,url 中的路径实际上是文件夹名称。我不知道为什么。路径应该是我在该文件夹的index.md 中写的标题。
index.md
---
title: 'The Execution Context'
date: '2020-02-16'
category: "JavaScript"
---
Blog Content..............
gatsby-node.js
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions
const blogPostTemplate = path.resolve(`./src/templates/blog-post.js`)
return graphql(
`
{
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
limit: 1000
) {
edges {
node {
fields {
slug
}
frontmatter {
title
category
}
}
}
}
}
`
).then(result => {
if (result.errors) {
throw result.errors
}
// Create blog posts pages.
const posts = result.data.allMarkdownRemark.edges.filter(
({ node }) => !!node.frontmatter.category
)
posts.forEach((post, index) => {
const previous = index === posts.length - 1 ? null : posts[index + 1].node
const next = index === 0 ? null : posts[index - 1].node
createPage({
path: post.node.fields.slug,
component: blogPostTemplate,
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,
})
}
}
另外,我对 Github 和 Twitter 链接有一些问题。当我单击它们时,它显示找不到页面。它显示奇怪的网址:https://github.com/https://github.com/myGithubName
https://twitter.com/https://twitter.com/myTwitterName
我检查了它的位置并发现:
gatsby-meta-config.js
module.exports = {
title: `My Blog`,
description: `Blog posted about ...`,
author: `myName`,
introduction: `I explain with words and code.`,
siteUrl: `https://gatsby-starter-bee.netlify.com`, // Your blog site url
social: {
twitter: `https://twitter.com/myTwitterName`, // Your Twitter account
github: `https://github.com/myGithubName`,
medium: ``,
facebook: ``
},
icon: `content/assets/profile.jpeg`, // Add your favicon
keywords: [`blog`],
comment: {
disqusShortName: '', // Your disqus-short-name. check disqus.com.
utterances: 'JaeYeopHan/gatsby-starter-bee', // Your repository for archive comment
},
configs: {
countOfInitialPost: 10, // Config your initial count of post
},
sponsor: {
buyMeACoffeeId: 'jbee',
},
share: {
facebookAppId: '', // Add facebookAppId for using facebook share feature v3.2
},
ga: '', // Add your google analytics tranking ID
}
gatsby-meta-config.js 中的链接似乎正确。
【问题讨论】:
标签: javascript url gatsby slug