B7 review-response batch: derived about counter, CJK-aware reading time, ZH copy fixes, hero leads with capability
Deploy / build (push) Successful in 26s
Deploy / build (push) Successful in 26s
This commit is contained in:
1 parent
0b23312948
commit
5941e858cc
10 files changed
+87
-27
No files matched your search
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Reading time and word count that also work for Chinese.
|
||||
*
|
||||
* A whitespace split is meaningless for CJK prose: an entire Chinese article has
|
||||
* almost no interior spaces, so it counted as one "word" and every ZH post showed
|
||||
* "1 min read" while its English twin showed five. CJK ideographs are counted as
|
||||
* characters (~400/min); everything else keeps the existing ~200 words/min.
|
||||
*
|
||||
* For pure-ASCII bodies the result is identical to the old whitespace/200 maths.
|
||||
*/
|
||||
|
||||
const CJK_CHAR = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uac00-\ud7af]/;
|
||||
|
||||
function counts(body?: string): { cjk: number; latin: number } {
|
||||
const text = body ?? '';
|
||||
const cjk = (text.match(new RegExp(CJK_CHAR.source, 'g')) ?? []).length;
|
||||
const latin = text
|
||||
.replace(new RegExp(CJK_CHAR.source, 'g'), ' ')
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.filter(Boolean).length;
|
||||
return { cjk, latin };
|
||||
}
|
||||
|
||||
/** Total count used for schema.org `wordCount` (CJK counts per character). */
|
||||
export function countWords(body?: string): number {
|
||||
const { cjk, latin } = counts(body);
|
||||
return cjk + latin;
|
||||
}
|
||||
|
||||
/** Minutes of reading time, never below 1. */
|
||||
export function readingMinutes(body?: string): number {
|
||||
const { cjk, latin } = counts(body);
|
||||
return Math.max(1, Math.round(cjk / 400 + latin / 200));
|
||||
}
|
||||
Reference in new issue
Block a user