From 9290bde8c7421087984e58d4779075e71988b5b3 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 10 Mar 2024 07:43:32 -0500 Subject: [PATCH] Last modified git-based remark plugin, site URL --- astro.config.mjs | 8 +++++++- package.json | 1 + pnpm-lock.yaml | 7 +++++++ src/plugins/remark-last-modified.ts | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/plugins/remark-last-modified.ts diff --git a/astro.config.mjs b/astro.config.mjs index be70df3..685f1f0 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -3,10 +3,16 @@ import mdx from '@astrojs/mdx'; import sitemap from '@astrojs/sitemap'; import tailwind from "@astrojs/tailwind"; +import { remarkModifiedTime } from './src/plugins/remark-last-modified'; import react from "@astrojs/react"; // https://astro.build/config export default defineConfig({ - site: 'https://example.com', + markdown: { + remarkPlugins: [ + remarkModifiedTime + ] + }, + site: 'https://handbook.xevion.dev', integrations: [mdx(), sitemap(), tailwind(), react()] }); \ No newline at end of file diff --git a/package.json b/package.json index b773400..f004486 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@types/react": "^18.2.64", "@types/react-dom": "^18.2.21", "astro": "^4.4.15", + "dayjs": "^1.11.10", "react": "^18.2.0", "react-dom": "^18.2.0", "sass": "^1.71.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 225e209..7a85f3b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,6 +35,9 @@ dependencies: astro: specifier: ^4.4.15 version: 4.4.15(@types/node@20.11.25)(sass@1.71.1)(typescript@5.4.2) + dayjs: + specifier: ^1.11.10 + version: 1.11.10 react: specifier: ^18.2.0 version: 18.2.0 @@ -1665,6 +1668,10 @@ packages: resolution: {integrity: sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==} dev: false + /dayjs@1.11.10: + resolution: {integrity: sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==} + dev: false + /debug@4.3.4: resolution: {integrity: sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==} engines: {node: '>=6.0'} diff --git a/src/plugins/remark-last-modified.ts b/src/plugins/remark-last-modified.ts new file mode 100644 index 0000000..e63a689 --- /dev/null +++ b/src/plugins/remark-last-modified.ts @@ -0,0 +1,18 @@ +import type { RemarkPlugins } from "astro"; +import { execSync } from "child_process"; + +export const remarkModifiedTime: RemarkPlugins[number] = () => { + return function (_, file) { + const filepath = file.history[0]; + const command = `git log -1 --pretty="format:%cI" "${filepath}"`; + let result = execSync(command).toString(); + + // File is not in git yet + if (result === "") { + result = new Date().toISOString(); + } + + // @ts-ignore + file.data.astro.frontmatter.lastModified = result; + }; +};