Commit 70842f9

HPCesia <me@hpcesia.com>
2025-03-07 15:25:29
feat: add remark plugin
Add a remark plugin to generate create and modify dates for markdown files.
1 parent 50a07c9
Changed files (2)
src/plugins/remark-create-time.ts
@@ -0,0 +1,11 @@
+import type { RemarkPlugin } from '@astrojs/markdown-remark';
+import { statSync } from 'fs';
+
+export const remarkCreateTime: RemarkPlugin = function () {
+  return function (tree, file) {
+    const filepath = file.history[0];
+    const result = statSync(filepath);
+    file.data!.astro!.frontmatter!.createAt = result.birthtime.toISOString();
+    file.data!.astro!.frontmatter!.modifiedAt = result.mtime.toISOString();
+  };
+};
astro.config.mjs
@@ -3,6 +3,7 @@ import { CDN } from './src/constants/cdn.mjs';
 import { rehypeComponentsList } from './src/plugins/rehype-components-list.ts';
 import { rehypePrettierCodes } from './src/plugins/rehype-prettier-codes.ts';
 import { rehypeWrapTables } from './src/plugins/rehype-wrap-tables.ts';
+import { remarkCreateTime } from './src/plugins/remark-create-time.ts';
 import { remarkExcerpt } from './src/plugins/remark-excerpt.ts';
 import { remarkGithubBlockquote } from './src/plugins/remark-github-blockquote.ts';
 // import { remarkHeadingShift } from './src/plugins/remark-heading-shift.ts';
@@ -59,6 +60,7 @@ export default defineConfig({
       remarkDirective,
       // @ts-expect-error Types of the plugin are not correct
       remarkDirectiveRehype,
+      remarkCreateTime,
       remarkReadingTime,
       remarkExcerpt,
       remarkImageProcess,