Browse Source

fix(util): estimate reading time from plain text

main
Mohan 1 month ago
parent
commit
725dbd9478
  1. 12
      src/utils/readingTime.ts

12
src/utils/readingTime.ts

@ -1,7 +1,13 @@
export function estimateReadingTime(content: string = ''): string {
const noWhitespace = content.replace(/\s/g, '').length
const minutes = Math.ceil(noWhitespace / 400)
return `${minutes} 分钟`
const plain = content
.replace(/<[^>]*>/g, ' ')
.replace(/&[a-zA-Z#0-9]+;/g, ' ')
.replace(/\s+/g, ' ')
.trim()
const cjk = (plain.match(/[\u4e00-\u9fff\u3040-\u30ff\uac00-\ud7af]/g) ?? []).length
const words = plain.length === 0 ? 0 : plain.trim().split(/\s+/).length
const minutes = Math.max(1, Math.ceil((cjk + words) / 400))
return `${minutes} 分钟`
}
export function formatDay(iso: string): string {

Loading…
Cancel
Save