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.
Install Helper Packages
Day.jsto modify and format times:
Create a Remark Plugin
This plugin uses
execSyncto 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
statSyncto get the
mtime(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
lastModifiedproperty in their frontmatter.
Display Last Modified Time
If your content is stored in a content collection, access the
entry.render()function. Then render
lastModifiedin your template wherever you would like it to appear.
If you’re using a Markdown layout, use the
lastModifiedfrontmatter property from
Astro.propsin your layout template.