Skip to content

Last modified time

With BrainDB

  1. Install BrainDB
  2. Then you can access modification date, like this
    const doc = (await bdb.documents({ slug }))[0];
    doc.updatedAt();

With Starlight

Starlight already has this feature, but value not exposed in content collection e.g. if you set

astro.config.mjs
export default defineConfig({
integrations: [
starlight({
lastUpdated: true,
}),
],
});

You would see Last updated: on the page, but at the same time page.data.lastUpdated would be undefined

With remark plugin

  1. Create remark plugin

    remark-modified-time.mjs
    import { execSync } from "child_process";
    export function remarkModifiedTime() {
    return function (tree, file) {
    const filepath = file.history[0];
    const result = execSync(
    `git log -1 --pretty="format:%cI" "${filepath}"`
    );
    file.data.astro.frontmatter.lastUpdated = result.toString();
    };
    }
  2. Configure Astro

    astro.config.mjs
    import { remarkModifiedTime } from "./remark-modified-time.mjs";
    export default defineConfig({
    markdown: {
    remarkPlugins: [remarkModifiedTime],
    },
    });
  3. You may need to adjust content schema

    src/content/config.ts
    import { z, defineCollection } from "astro:content";
    const blog = defineCollection({
    schema: z.object({
    lastUpdated: z.string().transform((str) => new Date(str)),
    }),
    });

But the value is accessible only after render e.g.

const { remarkPluginFrontmatter } = await page.render();
console.log(remarkPluginFrontmatter.lastUpdated);

Based on: Astro recipes

Further improvements

Use last modification date (for content pages) in: