Add last modified time
本頁內容尚未翻譯。
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.
This recipe calculates time based on your repository’s Git history and may not be accurate on some deployment platforms. Your host may be performing shallow clones which do not retrieve the full git history.
Recipe
Section titled 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.