feat: display last modified and wordcount on article

This commit is contained in:
草师傅 2025-08-10 18:37:37 +08:00
parent f8d69ac954
commit 666349c5b8
Signed by: gb
GPG key ID: 43330A030E2D6478
2 changed files with 11 additions and 5 deletions

View file

@ -19,12 +19,16 @@ export async function getStaticPaths() {
}
const { entry } = Astro.props;
const { Content, headings } = await entry.render();
const { Content, headings, remarkPluginFrontmatter } = await entry.render();
const noscript = siteConfig.noClientJavaScript
const slug = Astro.params.slug;
const author = Array.isArray(entry.data.author) ? entry.data.author : (entry.data.author !== undefined ? [entry.data.author] : [{collection: 'authors', id: siteConfig.defaultAuthor.id}]);
// Get Wordcount and Last Updated Date
const wordcount = remarkPluginFrontmatter.wordcount;
const lastUpdated = remarkPluginFrontmatter.lastModified;
// Get author data
const authorData = await Promise.all((author).map((singleAuthor) => getEntry(singleAuthor).then(authorEntry => authorEntry?.data)))
const authorInfo = authorData.includes(undefined) ? [{data: siteConfig.defaultAuthor}] : authorData;
@ -53,6 +57,9 @@ const cover = customFeaturedImage || matchedImage_src?.src || firstImageURL || `
<h1 class="title">{entry.data.title}</h1>
{authorInfo.map((a: any) => <AuthorInfo data={a} />)}
<span class="date">{new Date(entry.data.pubDate).toISOString().split('T')[0]}</span>
<span class="date">(Updated on {new Date(lastUpdated).toISOString().split('T')[0]})</span>
<span>|</span>
<span class="wordcount">{wordcount.words} words</span>
{headings.length !== 0 && <TableOfContents headings={headings} />}
{entry.data.summary && <p class="summary">{entry.data.summary}</p> }
<div class="content">

View file

@ -1,10 +1,9 @@
import { execSync } from "child_process";
import { statSync } from "node:fs";
import {execSync} from "child_process";
import {statSync} from "node:fs";
export function remarkModifiedTime() {
return function (tree, file) {
const filepath = file.history[0];
const result = execSync(`git log -1 --pretty="format:%cI" "${filepath}"`).toString() || statSync(filepath).mtime.toISOString();
file.data.astro.frontmatter.lastModified = result.toString();
file.data.astro.frontmatter.lastModified = execSync(`git log -1 --pretty="format:%cI" "${filepath}"`).toString() || statSync(filepath).mtime.toISOString();
};
}