mirror of
https://github.com/Ad-closeNN/blog-fuwari.git
synced 2026-05-31 01:40:03 -04:00
feat(component): 加入总字数统计模块
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user