Skip to content

Commit

Permalink
fix: word count errors in client (slidevjs#1845)
Browse files Browse the repository at this point in the history
Add Chinese character set for word count
  • Loading branch information
HoshinoSuzumi authored Aug 27, 2024
1 parent 992258c commit e451480
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/client/pages/overview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ function toggleRoute(route: SlideRoute) {
}
function wordCount(str: string) {
return str.match(/[\w`'\-]+/g)?.length || 0
const pattern = /[\w`'\-\u0392-\u03c9\u00c0-\u00ff\u0600-\u06ff\u0400-\u04ff]+|[\u4e00-\u9fff\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g
const m = str.match(pattern)
let count = 0
if (!m) return 0
for (let i = 0; i < m.length; i++) {
if (m[i].charCodeAt(0) >= 0x4e00) {
count += m[i].length
} else {
count += 1
}
}
return count
}
function isElementInViewport(el: HTMLElement) {
Expand Down

0 comments on commit e451480

Please sign in to comment.