feat(component): 加入总字数统计模块

This commit is contained in:
Ad-closeNN
2026-04-20 01:16:10 +08:00
parent 9d7af32e25
commit 8f004a9959
5 changed files with 96 additions and 40 deletions
+19 -1
View File
@@ -1,4 +1,4 @@
import { type CollectionEntry, getCollection } from "astro:content";
import { render, type CollectionEntry, getCollection } from "astro:content";
import I18nKey from "@i18n/i18nKey";
import { i18n } from "@i18n/translation";
import { getCategoryUrl } from "@utils/url-utils.ts";
@@ -46,6 +46,24 @@ export async function getSortedPostsList(): Promise<PostForList[]> {
return sortedPostsList;
}
let totalWordsCache: number | undefined;
export async function getTotalWords(): Promise<number> {
if (totalWordsCache !== undefined) {
return totalWordsCache;
}
const posts = await getRawSortedPosts();
const renderedPosts = await Promise.all(posts.map((post) => render(post)));
const totalWords = renderedPosts.reduce((sum, { remarkPluginFrontmatter }) => {
const words = Number(remarkPluginFrontmatter?.words ?? 0);
return sum + (Number.isFinite(words) ? words : 0);
}, 0);
totalWordsCache = totalWords;
return totalWords;
}
export type Tag = {
name: string;
count: number;