如何在 Gatsby URL 中添加发布日期?

2024-02-28

All the Gatsby 入门演示 https://github.com/gatsbyjs/gatsby#gatsby-starters有一条像这样的路径/gatsby-starter-blog/hi-folks/

我该如何设置/2015-05-28/hi-folks/或者只是那一年/2015/hi-folks/.

Thanks!


两种选择:

1) 只需将博客文章放入您希望 url 命名的目录中即可(在本例中)/2015-05-28/hi-folks/index.md。 2) 您可以通过从以下位置导出函数以编程方式设置路径gatsby-node.js called rewritePath。为每个页面调用它,其中包含页面来自的文件的文件系统数据+页面的元数据。假设您想在 Markdown 的 frontmatter 中设置帖子的日期,并让每个帖子都是一个简单的 Markdown 文件,其路径如下/a-great-blog-post.md

因此,要做你想做的事,请在 gatsby-node.js 中添加如下内容:

import moment from 'moment'

exports.rewritePath = (parsedFilePath, metadata) => {
  if (parsedFilePath.ext === "md") {
    return `/${moment(metadata.createdAt).format('YYYY')}/${parsedFilePath.name}/`
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Gatsby URL 中添加发布日期? 的相关文章

随机推荐