Skip to content

Commit e8d0694

Browse files
committed
Add improved docs
1 parent b217b6c commit e8d0694

File tree

2 files changed

+82
-41
lines changed

2 files changed

+82
-41
lines changed

lib/index.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,22 @@ import readabilityScores from 'readability-scores'
2121
// @ts-expect-error: untyped.
2222
import median from 'compute-median'
2323

24-
// See source [1].
24+
// See <https://en.wikipedia.org/wiki/Educational_stage#United_States>
25+
// for more info on US education/grade levels.
2526
const firstGradeAge = 5
2627
const highschoolGraduationAge = 18
2728
const graduationAge = 22
2829

29-
// See source [2].
30+
// See <https://en.wikipedia.org/wiki/Words_per_minute#Reading_and_comprehension>
31+
// for the wpm of people reading English.
32+
//
3033
// Note that different other algorithms vary between 200, 230, 270, and 280.
3134
// 228 seems to at least be based in research.
3235
const reasonableWpm = 228
3336
const reasonableWpmMax = 340
3437

35-
// See source [3].
38+
// See <https://en.wikipedia.org/wiki/Reading#Reading_rate>
39+
// for information on reading rate, including how grade levels affect them.
3640
const addedWpmPerGrade = 14
3741
const baseWpm =
3842
reasonableWpm - (highschoolGraduationAge - firstGradeAge) * addedWpmPerGrade
@@ -43,22 +47,33 @@ const accuracy = 1e6
4347
* Estimate the reading time, taking readability of the document and a target
4448
* age group into account.
4549
*
46-
* * [1] For more info on US education/grade levels, see:
47-
* <https://en.wikipedia.org/wiki/Educational_stage#United_States>.
48-
* * [2] For the wpm of people reading English, see:
49-
* <https://en.wikipedia.org/wiki/Words_per_minute#Reading_and_comprehension>
50-
* * [3] For information on reading rate, including how grade levels affect
51-
* them, see: <https://en.wikipedia.org/wiki/Reading#Reading_rate>.
50+
* For some more background info/history and a few insight on where this all
51+
* comes from, see: <https://martech.org/estimated-reading-times-increase-engagement/>.
5252
*
53-
* And some more background info/history and a few insight on where this comes
54-
* from, see: <https://martech.org/estimated-reading-times-increase-engagement/>.
53+
* ###### Algorithm
54+
*
55+
* The algorithm works as follows:
56+
*
57+
* * estimate the WPM (words per minute) of the audience age based on the facts
58+
* that English can be read at ±228 WPM (Trauzettel-Klosinski), and that
59+
* reading rate increases 14 WPM per grade (Carver)
60+
* * apply the readability algorithms Dale—Chall, Automated Readability,
61+
* Coleman-Liau, Flesch, Gunning-Fog, SMOG, and Spache
62+
* * adjust the WPM of the audience for whether the readability algorithms
63+
* estimate its above or below their level
64+
* * `wordCount / adjustedWpm = readingTime`
65+
*
66+
* > ⚠️ **Important**: this algorithm is specific to English.
5567
*
5668
* @param {Node} tree
5769
* Tree to inspect.
5870
* @param {Options | null | undefined} [options]
5971
* Configuration.
6072
* @returns {number}
6173
* Estimated reading time in minutes.
74+
*
75+
* The result is not rounded so it’s possible to retrieve estimated seconds
76+
* from it.
6277
*/
6378
export function readingTime(tree, options) {
6479
const settings = options || {}

readme.md

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ document into account.
1818
* [Install](#install)
1919
* [Use](#use)
2020
* [API](#api)
21-
* [`readingTime(tree, options?)`](#readingtimetree-options)
21+
* [`readingTime(tree[, options])`](#readingtimetree-options)
22+
* [`Options`](#options)
2223
* [Types](#types)
2324
* [Compatibility](#compatibility)
2425
* [Security](#security)
@@ -32,20 +33,6 @@ This package is a utility that takes a [hast][] (HTML) syntax tree and estimates
3233
the reading time, taking readability of the document and a target age group into
3334
account.
3435

35-
The algorithm works as follows:
36-
37-
* estimate the WPM (words per minute) of the audience age based on the facts
38-
that English can be read at ±228 WPM (Trauzettel-Klosinski), and that
39-
reading rate increases 14 WPM per grade (Carver)
40-
* apply the readability algorithms [Dale—Chall][dale-chall],
41-
[Automated Readability][automated-readability], [Coleman-Liau][],
42-
[Flesch][], [Gunning-Fog][], [SMOG][], and [Spache][]
43-
* adjust the WPM of the audience for whether the readability algorithms
44-
estimate its above or below their level
45-
* `wordCount / adjustedWpm = readingTime`
46-
47-
> ⚠️ **Important**: this algorithm is specific to English.
48-
4936
## When should I use this?
5037

5138
This is a small utility useful when you have an AST, know your audience, and
@@ -58,7 +45,7 @@ wraps this utility to figure, for use with [`rehype-meta`][rehype-meta].
5845
## Install
5946

6047
This package is [ESM only][esm].
61-
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
48+
In Node.js (version 14.14+ and or 16.0+), install with [npm][]:
6249

6350
```sh
6451
npm install hast-util-reading-time
@@ -67,7 +54,7 @@ npm install hast-util-reading-time
6754
In Deno with [`esm.sh`][esmsh]:
6855

6956
```js
70-
import {readingTime} from "https://esm.sh/hast-util-reading-time@1"
57+
import {readingTime} from 'https://esm.sh/hast-util-reading-time@1'
7158
```
7259

7360
In browsers with [`esm.sh`][esmsh]:
@@ -112,40 +99,71 @@ It takes about 2-3m to read
11299

113100
## API
114101

115-
This package exports the identifier `readingTime`.
102+
This package exports the identifier [`readingTime`][readingtime].
116103
There is no default export.
117104

118-
### `readingTime(tree, options?)`
105+
### `readingTime(tree[, options])`
119106

120-
Estimate the reading time.
107+
Estimate the reading time, taking readability of the document and a target age
108+
group into account.
121109

122-
##### `options`
110+
For some more background info/history and a few insight on where this all comes
111+
from, see [How estimated reading times increase content engagement][martech].
123112

124-
Configuration (optional).
113+
###### Algorithm
125114

126-
###### `options.age`
115+
The algorithm works as follows:
127116

128-
Target age group (`number`, default: `16`).
129-
This is the age your target audience was still in school.
130-
Set it to 18 if you expect all readers to have finished high school, 21 if you
131-
expect your readers to all be college graduates, etc.
117+
* estimate the WPM (words per minute) of the audience age based on the facts
118+
that English can be read at ±228 WPM (Trauzettel-Klosinski), and that
119+
reading rate increases 14 WPM per grade (Carver)
120+
* apply the readability algorithms [Dale—Chall][dale-chall],
121+
[Automated Readability][automated-readability], [Coleman-Liau][],
122+
[Flesch][], [Gunning-Fog][], [SMOG][], and [Spache][]
123+
* adjust the WPM of the audience for whether the readability algorithms
124+
estimate its above or below their level
125+
* `wordCount / adjustedWpm = readingTime`
126+
127+
> ⚠️ **Important**: this algorithm is specific to English.
128+
129+
###### Parameters
130+
131+
* `tree` ([`Node`][node])
132+
— tree to inspect
133+
* `options` ([`Options`][options])
134+
— configuration
132135

133136
###### Returns
134137

135-
Reading time in minutes (`number`).
138+
Estimated reading time in minutes (`number`).
139+
136140
The result is not rounded so it’s possible to retrieve estimated seconds from
137141
it.
138142

143+
### `Options`
144+
145+
Configuration (TypeScript type).
146+
147+
##### Fields
148+
149+
###### `age`
150+
151+
Target age group (`number`, default: `16`).
152+
153+
This is the age your target audience was still in school.
154+
Set it to 18 if you expect all readers to have finished high school, 21 if you
155+
expect your readers to all be college graduates, etc.
156+
139157
## Types
140158

141159
This package is fully typed with [TypeScript][].
142-
It exports the additional type `Options`.
160+
It exports the additional type [`Options`][options].
143161

144162
## Compatibility
145163

146164
Projects maintained by the unified collective are compatible with all maintained
147165
versions of Node.js.
148-
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
166+
As of now, that is Node.js 14.14+ and 16.0+.
149167
Our projects sometimes work with older versions, but this is not guaranteed.
150168

151169
## Security
@@ -223,6 +241,8 @@ abide by its terms.
223241

224242
[hast]: https://github.com/syntax-tree/hast
225243

244+
[node]: https://github.com/syntax-tree/hast#nodes
245+
226246
[dale-chall]: https://github.com/words/dale-chall-formula
227247

228248
[automated-readability]: https://github.com/words/automated-readability
@@ -242,3 +262,9 @@ abide by its terms.
242262
[rehype-meta]: https://github.com/rehypejs/rehype-meta
243263

244264
[wiki]: https://en.wikipedia.org/wiki/Words_per_minute#Alphanumeric_entry
265+
266+
[martech]: https://martech.org/estimated-reading-times-increase-engagement/
267+
268+
[readingtime]: #readingtimetree-options
269+
270+
[options]: #options

0 commit comments

Comments
 (0)