Skip to content

Commit

Permalink
feat: add commit property
Browse files Browse the repository at this point in the history
  • Loading branch information
rayhanadev committed Jan 3, 2025
1 parent f8741f3 commit 79e3887
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
5 changes: 3 additions & 2 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import tailwind from "@astrojs/tailwind";
import playformCompress from "@playform/compress";

import { schema } from "./env.ts";
import { gitCommitRemarkPlugin } from "./src/lib/blog/gitCommitRemarkPlugin.ts";

const { SITE } = loadEnv(process.env.NODE_ENV!, process.cwd(), "");

console.log("SITE", SITE);

export default defineConfig({
site: SITE ?? "http://localhost:3000",
output: "static",
Expand Down Expand Up @@ -47,6 +46,8 @@ export default defineConfig({
},
markdown: {
syntaxHighlight: "prism",
// @ts-ignore: incorrectly typed in @astrojs/markdown-remark
remarkPlugins: [gitCommitRemarkPlugin],
},
redirects: {
"/work": "/resume",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
"@types/react": "^19.0.2",
"husky": "^9.1.7",
"lint-staged": "^15.2.11",
"mdast": "^3.0.0",
"prettier": "^3.4.2",
"prettier-plugin-astro": "^0.14.1",
"react-compiler-runtime": "^19.0.0-beta-b2e8e9c-20241220",
"typescript": "^5.7.2",
"unified": "^11.0.5",
"vite": "^6.0.7",
"wrangler": "^3.99.0"
}
Expand Down
30 changes: 30 additions & 0 deletions src/lib/blog/gitCommitRemarkPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { execSync } from "node:child_process";
import path from "node:path";

import type { Root } from "mdast";
import type { Plugin } from "unified";

import { isDraft } from "./utils";

type RemarkPlugin<PluginParameters extends any[] = any[]> = Plugin<
PluginParameters,
Root
>;

export function gitCommitRemarkPlugin(): RemarkPlugin {
return (_tree, file) => {
const filepath = file.history[0].replace(process.cwd(), "").slice(1);
const draft = isDraft(filepath);

if (draft) {
file.data.astro.frontmatter.commit = "<draft>";
return;
}

const commit = execSync(`git log -n 1 --pretty=format:%h -- ${path}`)
.toString()
.trim();

file.data.astro.frontmatter.commit = commit;
};
}
2 changes: 1 addition & 1 deletion src/lib/blog/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BLOG_CONTENT_POOL_PATH } from "lib/consts";
import { BLOG_CONTENT_POOL_PATH } from "../consts";

const relativePath = BLOG_CONTENT_POOL_PATH.replace(process.cwd(), "").slice(1);

Expand Down
6 changes: 4 additions & 2 deletions src/pages/thoughts/[id].astro
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dayjs.extend(advancedFormat);
const { entry } = Astro.props;
const { Content } = await render(entry);
const { Content, remarkPluginFrontmatter } = await render(entry);
export const prerender = true;
Expand Down Expand Up @@ -77,7 +77,9 @@ export async function getStaticPaths() {
.toLowerCase()
}
</p>
<p class="text-lg lg:text-[24px]">commit: aadcee0d</p>
<p class="text-lg lg:text-[24px]">
commit: {remarkPluginFrontmatter.commit}
</p>
</div>
<div>
<p class="text-lg lg:text-2xl leading-9 hidden lg:block">
Expand Down

0 comments on commit 79e3887

Please sign in to comment.