Fix remark last modified metadata handling

This commit is contained in:
2024-03-11 00:14:07 -05:00
parent 129cbce901
commit d35c03d485
2 changed files with 36 additions and 27 deletions

View File

@@ -1,35 +1,42 @@
--- ---
import { type CollectionEntry, getCollection } from 'astro:content'; import { type CollectionEntry, getCollection } from "astro:content";
import Main from '@layouts/Main.astro'; import Main from "@layouts/Main.astro";
export async function getStaticPaths() { export async function getStaticPaths() {
const posts = await getCollection('handbook') const posts = await getCollection("handbook");
return posts.map((post) => ({ return posts.map((post) => ({
params: { slug: post.slug }, params: { slug: post.slug },
props: post, props: post,
})); }));
} }
type Props = CollectionEntry<'handbook'>; type Props = CollectionEntry<"handbook">;
const isProd = import.meta.env.PROD; const isProd = import.meta.env.PROD;
const {render, data} = Astro.props; const { render, data } = Astro.props;
const {title, lastModified} = data; const { title } = data;
const { Content, headings } = await render(); const {
Content,
if (isProd && lastModified === undefined) { headings,
console.log({props: Astro.props, content: Content}) remarkPluginFrontmatter: frontmatter,
} } = await render();
--- ---
<Main {headings} { ...data}> <Main {headings} {...data}>
<h2 class="text-2xl font-semibold mb-2">{title}</h2> <h2 class="text-2xl font-semibold mb-2">{title}</h2>
<!-- lastModified can be undefined in some rare circumstances (usually development only), this might fail in production if something went wrong. --> <!-- lastModified can be undefined in some rare circumstances (usually development only), this might fail in production if something went wrong. -->
<span>{(lastModified ?? (isProd ? null : new Date()))!.toLocaleDateString('en-us', { <span
year: 'numeric', >{
month: 'short', new Date(frontmatter.lastModified).toLocaleDateString(
day: 'numeric', "en-us",
})}</span> {
<div class="prose xl:prose-lg prose-zinc dark:prose-invert"> year: "numeric",
<Content /> month: "short",
</div> day: "numeric",
}
)
}</span
>
<div class="prose xl:prose-lg prose-zinc dark:prose-invert">
<Content />
</div>
</Main> </Main>

View File

@@ -7,12 +7,14 @@ export const remarkModifiedTime: RemarkPlugins[number] = () => {
const command = `git log -1 --pretty="format:%cI" "${filepath}"`; const command = `git log -1 --pretty="format:%cI" "${filepath}"`;
let result = execSync(command).toString().trim(); let result = execSync(command).toString().trim();
// File is not in git yet // File is not in git yet
if (result === "") { if (result === "") {
// TODO: Add specific error handling for when the file is not in git
result = new Date().toISOString(); result = new Date().toISOString();
} }
// @ts-ignore // @ts-ignore
file.data.astro.frontmatter.lastModified = result; file.data.astro.frontmatter.lastModified = new Date(result);
}; };
}; };