File tree Expand file tree Collapse file tree 3 files changed +19
-7
lines changed Expand file tree Collapse file tree 3 files changed +19
-7
lines changed Original file line number Diff line number Diff line change @@ -62,18 +62,24 @@ export function readingTime(tree, options = {}) {
62
62
Math . max ( firstGradeAge , Math . round ( options . age || 16 ) )
63
63
)
64
64
const text = toText ( tree )
65
- const scores = readabilityScores ( text )
66
- /** @type {number } */
67
- const readabilityAge =
68
- firstGradeAge +
69
- median ( [
65
+ const scores = readabilityScores ( text ) || { }
66
+ const score = median (
67
+ [
70
68
scores . daleChall ,
71
69
scores . ari ,
72
70
scores . colemanLiau ,
73
71
scores . fleschKincaid ,
74
72
scores . smog ,
75
73
scores . gunningFog
76
- ] )
74
+ ] . filter ( ( d ) => d !== undefined )
75
+ )
76
+
77
+ if ( score === null ) {
78
+ return 0
79
+ }
80
+
81
+ /** @type {number } */
82
+ const readabilityAge = firstGradeAge + score
77
83
78
84
// WPM the target audience normally reads.
79
85
const targetWpm = baseWpm + ( targetAge - firstGradeAge ) * addedWpmPerGrade
Original file line number Diff line number Diff line change @@ -25,7 +25,7 @@ npm install hast-util-reading-time
25
25
## Use
26
26
27
27
Say we have the following HTML from [ “Word per minute: Alphanumeric entry” on
28
- Wikipedia] ( https://en.wikipedia.org/wiki/Words_per_minute#Alphanumeric_entry ) in
28
+ Wikipedia] ( https://en.wikipedia.org/wiki/Words \_ per \_ minute#Alphanumeric \_ entry ) in
29
29
` example.html ` :
30
30
31
31
``` html
Original file line number Diff line number Diff line change @@ -41,6 +41,12 @@ test('hastUtilReadingTime', (t) => {
41
41
'should estimate (somewhat simple)'
42
42
)
43
43
44
+ t . deepEqual (
45
+ readingTime ( { type : 'root' , children : [ ] } ) ,
46
+ 0 ,
47
+ 'should estimate (empty)'
48
+ )
49
+
44
50
t . deepEqual (
45
51
readingTime ( tree , { age : 12 } ) ,
46
52
2.437_14 ,
You can’t perform that action at this time.
0 commit comments