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 Main from '@layouts/Main.astro';
import { type CollectionEntry, getCollection } from "astro:content";
import Main from "@layouts/Main.astro";
export async function getStaticPaths() {
const posts = await getCollection('handbook')
return posts.map((post) => ({
params: { slug: post.slug },
props: post,
}));
const posts = await getCollection("handbook");
return posts.map((post) => ({
params: { slug: post.slug },
props: post,
}));
}
type Props = CollectionEntry<'handbook'>;
type Props = CollectionEntry<"handbook">;
const isProd = import.meta.env.PROD;
const {render, data} = Astro.props;
const {title, lastModified} = data;
const { Content, headings } = await render();
if (isProd && lastModified === undefined) {
console.log({props: Astro.props, content: Content})
}
const { render, data } = Astro.props;
const { title } = data;
const {
Content,
headings,
remarkPluginFrontmatter: frontmatter,
} = await render();
---
<Main {headings} { ...data}>
<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. -->
<span>{(lastModified ?? (isProd ? null : new Date()))!.toLocaleDateString('en-us', {
year: 'numeric',
month: 'short',
day: 'numeric',
})}</span>
<div class="prose xl:prose-lg prose-zinc dark:prose-invert">
<Content />
</div>
<Main {headings} {...data}>
<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. -->
<span
>{
new Date(frontmatter.lastModified).toLocaleDateString(
"en-us",
{
year: "numeric",
month: "short",
day: "numeric",
}
)
}</span
>
<div class="prose xl:prose-lg prose-zinc dark:prose-invert">
<Content />
</div>
</Main>

View File

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