Skip to content

Commit 492eb18

Browse files
author
li
committed
feat: add word count
1 parent 0dd0402 commit 492eb18

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@
2929
"remark-toc": "^9.0.0",
3030
"tailwindcss": "^3.4.1",
3131
"typescript": "^5.2.2"
32+
},
33+
"devDependencies": {
34+
"@types/node": "^20.11.30"
3235
}
3336
}

src/pages/[slug].astro

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import BlogLayout from "@/layouts/blogLayout.astro";
33
import "@/styles/markdown.css";
44
import type { Frontmatter } from "./types";
55
import type { MarkdownInstance } from "astro";
6+
import fs from "node:fs";
67
import { getPostWords, readingTime } from "@/utils/utils";
78
import "@fontsource/montserrat/900.css";
89
@@ -12,19 +13,23 @@ export async function getStaticPaths() {
1213
)) as MarkdownInstance<Frontmatter>[];
1314
1415
return posts.map((post) => {
16+
const fileContent = fs.readFileSync(post.file, { encoding: "utf8" });
17+
1518
return {
1619
params: {
1720
slug: post.frontmatter.href,
1821
},
1922
props: {
2023
post,
24+
fileContent,
2125
},
2226
};
2327
});
2428
}
2529
2630
const { Content, frontmatter } = Astro.props.post;
27-
const words = getPostWords("");
31+
const { fileContent } = Astro.props;
32+
const words = getPostWords(fileContent);
2833
const readTime = readingTime(words);
2934
---
3035

@@ -46,10 +51,10 @@ const readTime = readingTime(words);
4651
</p>
4752

4853
<p class="mt-2 text-[13px] text-gray-700 dark:text-gray-300">
49-
字数{words}
54+
word count{words}
5055
</p>
5156
<p class="mt-2 text-[13px] text-gray-700 dark:text-gray-300">
52-
预计阅读时间{readTime}分钟
57+
estimated reading time{readTime}minute
5358
</p>
5459
<div class="markdown mt-10">
5560
<Content />

0 commit comments

Comments
 (0)