mirror of https://github.com/YunYouJun/valaxy
docs: add how to config statistics
This commit is contained in:
parent
a008d59edf
commit
afb32bbc1e
|
@ -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"}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -85,9 +85,11 @@ export const defaultSiteConfig: SiteConfig = {
|
|||
|
||||
statistics: {
|
||||
enable: false,
|
||||
wordCount: {
|
||||
cnSpeed: 300,
|
||||
enSpeed: 200,
|
||||
readTime: {
|
||||
speed: {
|
||||
cn: 300,
|
||||
en: 100,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue