添加上次修改时间
了解如何构建 remark 插件,将上次修改时间添加到 Markdown 和 MDX 文件的 frontmatter 中。使用此属性在你的页面中显示修改时间。
¥Learn how to build a remark plugin that adds the last modified time to the frontmatter of your Markdown and MDX files. Use this property to display the modified time in your pages.
秘诀
标题部分 秘诀¥Recipe
-
Install Helper Packages
Install
Day.js
to modify and format times: -
Create a Remark Plugin
This plugin uses
execSync
to run a Git command that returns the timestamp of the latest commit in ISO 8601 format. The timestamp is then added to the frontmatter of the file.Using the file system instead of Git
Although using Git is the recommended way to get the last modified timestamp from a file, it is possible to use the file system modified time. This plugin uses
statSync
to get themtime
(modified time) of the file in ISO 8601 format. The timestamp is then added to the frontmatter of the file. -
Add the plugin to your config
Now all Markdown documents will have a
lastModified
property in their frontmatter. -
Display Last Modified Time
If your content is stored in a content collection, access the
remarkPluginFrontmatter
from theentry.render()
function. Then renderlastModified
in your template wherever you would like it to appear.If you’re using a Markdown layout, use the
lastModified
frontmatter property fromAstro.props
in your layout template.