如何在Gatsby URL中放置发布日期? [英] How to put post date in Gatsby URL?
问题描述
所有 Gatsby入门演示都具有类似/gatsby-starter的路径-blog/hi-folks/
如何使用/2015-05-28/hi-folks/
进行设置,或者仅使用/2015/hi-folks/
进行设置.
How do I set it up with /2015-05-28/hi-folks/
or just the year with /2015/hi-folks/
.
谢谢!
推荐答案
两个选项:
1)只需将博客文章放在您想要的URL命名的目录中,在本例中为/2015-05-28/hi-folks/index.md
.2)您可以通过从 gatsby-node.js
导出名为 rewritePath
的函数来以编程方式设置路径.每个页面都将调用该页面,其中包含该页面来自的文件的文件系统数据+页面的元数据.假设您想在Markdown的最前面设置帖子的日期,并让每个帖子成为一个简单的Markdown文件,其路径类似于/a-great-blog-post.md
1) Just put the blog posts in directories named like you want the url to be so in this case /2015-05-28/hi-folks/index.md
.
2) You can programmatically set paths by exporting a function from gatsby-node.js
called rewritePath
. It is called for each page with filesystem data for the file the page comes from + the page's metadata. So say you want to set the date of the post in your markdown's frontmatter and have each post be a simple markdown file with paths like /a-great-blog-post.md
因此,要执行您想要的操作,请在您的gatsby-node.js中添加以下内容:
So to do what you want, add to your gatsby-node.js something like:
import moment from 'moment'
exports.rewritePath = (parsedFilePath, metadata) => {
if (parsedFilePath.ext === "md") {
return `/${moment(metadata.createdAt).format('YYYY')}/${parsedFilePath.name}/`
}
}
这篇关于如何在Gatsby URL中放置发布日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!