docs: add how to config statistics

This commit is contained in:
YunYouJun 2023-03-19 19:03:45 +08:00
parent a008d59edf
commit afb32bbc1e
6 changed files with 75 additions and 31 deletions

View File

@ -351,6 +351,38 @@ sponsor: false
---
```
### 阅读统计
开启阅读统计,将会在每篇文章开头展示阅读统计信息。
> 需要主题进行适配,即展示 `frontmatter` 中的 `wordCount``readingTime` 字段。
- `wordCount`:字数统计
- `readingTime`:阅读时长(分钟)
- 可以设置不同语言的阅读速度,默认 `cn` 为 300 字/分钟,`en` 为 200 字/分钟。
```ts
// site.config.ts
import { defineSiteConfig } from 'valaxy'
export default defineSiteConfig({
/**
* 开启阅读统计
*/
statistics: {
enable: true,
readTime: {
/**
* 阅读速度
*/
speed: {
cn: 300,
en: 200,
},
},
}
})
```
## 主题配置 {lang="zh-CN"}
## Theme Config {lang="en"}

View File

@ -98,9 +98,9 @@ accessibility:
# prev_page: Previous page
# next_page: Next page
wordcount:
count: Word count in article
count_total: Total words
statistics:
word: Word count in article
word_total: Total words
time: Reading time
time_total: Total reading time

View File

@ -85,9 +85,11 @@ export const defaultSiteConfig: SiteConfig = {
statistics: {
enable: false,
wordCount: {
cnSpeed: 300,
enSpeed: 200,
readTime: {
speed: {
cn: 300,
en: 100,
},
},
},

View File

@ -205,8 +205,12 @@ export async function ViteValaxyPlugins(
route.meta.frontmatter.tags = [tags]
}
if (valaxyConfig.siteConfig.statistics.enable)
presetStatistics({ route })
if (valaxyConfig.siteConfig.statistics.enable) {
presetStatistics({
options: valaxyConfig.siteConfig.statistics,
route,
})
}
valaxyConfig.extendMd?.({
route,

View File

@ -1,11 +1,9 @@
import { existsSync, readFileSync } from 'node:fs'
import type { SiteConfig } from 'valaxy/types'
import type { ValaxyExtendConfig } from '../../types'
export interface CountData { cn: number; en: number }
export interface ReadTimeOptions {
cnSpeed: number
enSpeed: number
}
export type ReadTimeOptions = SiteConfig['statistics']['readTime']
/**
* count characters
@ -25,8 +23,10 @@ export const count = (content: string): CountData => {
* @param param1
* @returns read time (minute)
*/
export const readTime = ({ cn, en }: CountData, { cnSpeed = 300, enSpeed = 160 }: ReadTimeOptions) => {
const readingTime = cn / cnSpeed + en / enSpeed
export const readTime = (
{ cn, en }: CountData,
options: ReadTimeOptions) => {
const readingTime = cn / (options.speed.cn || 300) + en / (options.speed.en || 100)
return readingTime < 1 ? 1 : Math.ceil(readingTime)
}
@ -57,16 +57,20 @@ export const statistics = (content: string, options: {
// preset addon for statistics
export const presetStatistics = ({
route,
options,
}: {
route: Parameters<Required<ValaxyExtendConfig>['extendMd']>[0]['route']
options: SiteConfig['statistics']
}) => {
if (existsSync(route.component)) {
const file = readFileSync(route.component, 'utf-8')
const { wordCount, readingTime } = statistics(file, {
readTime: {
cnSpeed: 300,
enSpeed: 160,
},
readTime: Object.assign({
speed: {
cn: 300,
en: 100,
},
}, options.readTime),
})
if (route.meta.frontmatter) {
if (!route.meta.frontmatter.wordCount)

View File

@ -212,19 +212,21 @@ export interface SiteConfig {
*/
statistics: {
enable: boolean
wordCount: {
/**
* Chinese word count speed
* @description
* @default 300 (300 /)
*/
cnSpeed: number
/**
* English word count speed
* @description
* @default 100 (200 /)
*/
enSpeed: number
readTime: {
speed: {
/**
* Chinese word count speed
* @description
* @default 300 (300 /)
*/
cn: number
/**
* English word count speed
* @description
* @default 100 (200 /)
*/
en: number
}
}
}
}