Salta ai contenuti

Add last modified time

Questi contenuti non sono ancora disponibili nella tua lingua.

Learn how to build a mdast plugin that adds the last modified time as a custom frontmatter property of your Markdown and MDX files. Use this property to display the modified time in your pages.

  1. Install the following packages depending on the Markdown processor used:

    Terminal window
    npm install dayjs @astrojs/markdown-satteri satteri
  2. Create a mdast 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.

    src/mdast/mdast-modified-time.ts
    import { execSync } from "node:child_process";
    import { fileURLToPath } from "node:url";
    import { defineMdastPlugin } from "satteri";
    export const mdastModifiedTimePlugin = defineMdastPlugin({
    name: "mdast-modified-time",
    before(root, context) {
    if (!context.fileURL) return;
    const filepath = fileURLToPath(context.fileURL);
    const result = execSync(`git log -1 --pretty="format:%cI" "${filepath}"`);
    if (context.data.astro !== undefined) {
    context.data.astro.frontmatter.lastModified = result.toString();
    }
    },
    });
    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 the mtime (modified time) of the file in ISO 8601 format. The timestamp is then added to the frontmatter of the file.

    src/mdast/mdast-modified-time.ts
    import { statSync } from "node:fs";
    import { fileURLToPath } from "node:url";
    import { defineMdastPlugin } from "satteri";
    export const mdastModifiedTimePlugin = defineMdastPlugin({
    name: "mdast-modified-time",
    before(node, context) {
    if (!context.fileURL) return;
    const filepath = fileURLToPath(context.fileURL);
    const result = statSync(filepath);
    if (context.data.astro !== undefined) {
    context.data.astro.frontmatter.lastModified = result.mtime.toISOString();
    }
    },
    });
  3. Add the plugin to your config:

    astro.config.mjs
    import { satteri } from "@astrojs/markdown-satteri";
    import { defineConfig } from "astro/config";
    import { mdastModifiedTimePlugin } from "./src/mdast/mdast-modified-time";
    export default defineConfig({
    markdown: {
    processor: satteri({
    mdastPlugins: [mdastModifiedTimePlugin],
    }),
    },
    });

    Now all Markdown documents will have a lastModified property in their frontmatter.

  4. Display Last Modified Time

    If your content is stored in a content collection, access the remarkPluginFrontmatter from the render() function. Then render lastModified in your template wherever you would like it to appear.

    src/pages/posts/[slug].astro
    ---
    import { getCollection, render } from "astro:content";
    import dayjs from "dayjs";
    import utc from "dayjs/plugin/utc";
    dayjs.extend(utc);
    export async function getStaticPaths() {
    const blog = await getCollection("blog");
    return blog.map((entry) => ({
    params: { slug: entry.id },
    props: { entry },
    }));
    }
    const { entry } = Astro.props;
    const { Content, remarkPluginFrontmatter } = await render(entry);
    const lastModified = dayjs(remarkPluginFrontmatter.lastModified)
    .utc()
    .format("HH:mm:ss DD MMMM YYYY UTC");
    ---
    <html>
    <head>
    <meta charset="utf-8" />
    <title>{entry.data.title}</title>
    </head>
    <body>
    <title>{entry.data.title}</title>
    <p>Last Modified: {lastModified}</p>
    <Content />
    </body>
    </html>

    If you’re using a Markdown layout, use the lastModified frontmatter property from Astro.props in your layout template.

    src/layouts/BlogLayout.astro
    ---
    import dayjs from "dayjs";
    import utc from "dayjs/plugin/utc";
    dayjs.extend(utc);
    const lastModified = dayjs()
    .utc(Astro.props.frontmatter.lastModified)
    .format("HH:mm:ss DD MMMM YYYY UTC");
    ---
    <html>
    <head>
    <meta charset="utf-8" />
    </head>
    <body>
    <p>{lastModified}</p>
    <slot />
    </body>
    </html>
Contribuisci Comunità Sponsor