From 87a0171185292aeed1b792a1dba4ae436360f880 Mon Sep 17 00:00:00 2001 From: grassblock Date: Sun, 10 Aug 2025 18:20:51 +0800 Subject: [PATCH] feat: add remark modified time plugin --- astro.config.mjs | 5 +++-- src/plugins/remark/modified-time.mjs | 10 ++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 src/plugins/remark/modified-time.mjs diff --git a/astro.config.mjs b/astro.config.mjs index 5c66377..19c2d18 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -4,13 +4,14 @@ import sitemap from '@astrojs/sitemap'; import mdx from '@astrojs/mdx'; -import { remarkWordCount } from './src/plugins/wordcount.js'; +import { remarkWordCount } from './src/plugins/remark/wordcount.js'; import cloudflare from '@astrojs/cloudflare'; import remarkMath from "remark-math"; import rehypeKatex from "rehype-katex"; import partytown from '@astrojs/partytown'; +import {remarkModifiedTime} from "./src/plugins/remark/modified-time.mjs"; export default defineConfig({ site: 'https://terminal-blog.example.com', @@ -30,7 +31,7 @@ export default defineConfig({ theme: 'nord', wrap: true }, - remarkPlugins: [ remarkMath, remarkWordCount ], + remarkPlugins: [ remarkMath, remarkWordCount, remarkModifiedTime ], rehypePlugins: [ rehypeKatex ] }, diff --git a/src/plugins/remark/modified-time.mjs b/src/plugins/remark/modified-time.mjs new file mode 100644 index 0000000..6772830 --- /dev/null +++ b/src/plugins/remark/modified-time.mjs @@ -0,0 +1,10 @@ +import { execSync } from "child_process"; +import { statSync } from "node:fs"; + +export function remarkModifiedTime() { + return function (tree, file) { + const filepath = file.history[0]; + const result = execSync(`git log -1 --pretty="format:%cI" "${filepath}"`).toString() || statSync(filepath).mtime.toISOString(); + file.data.astro.frontmatter.lastModified = result.toString(); + }; +} \ No newline at end of file