Add last modified time
Questi contenuti non sono ancora disponibili nella tua lingua.
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
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.