Skip to content

Commit 91aaef7

Browse files
committed
Add improved docs
1 parent fb7f4f6 commit 91aaef7

File tree

1 file changed

+111
-35
lines changed

1 file changed

+111
-35
lines changed

readme.md

Lines changed: 111 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,80 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
**[hast][]** utility to estimate the reading time, taking readability of the
12-
document and a target age group into account.
11+
[hast][] utility to estimate the reading time, taking readability of the
12+
document into account.
13+
14+
## Contents
15+
16+
* [What is this?](#what-is-this)
17+
* [When should I use this?](#when-should-i-use-this)
18+
* [Install](#install)
19+
* [Use](#use)
20+
* [API](#api)
21+
* [`readingTime(tree, options?)`](#readingtimetree-options)
22+
* [Types](#types)
23+
* [Compatibility](#compatibility)
24+
* [Security](#security)
25+
* [Related](#related)
26+
* [Contribute](#contribute)
27+
* [License](#license)
28+
29+
## What is this?
30+
31+
This package is a utility that takes a [hast][] (HTML) syntax tree and estimates
32+
the reading time, taking readability of the document and a target age group into
33+
account.
1334

14-
## Install
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+
49+
## When should I use this?
50+
51+
This is a small utility useful when you have an AST, know your audience, and
52+
want a really smart reading time algorithm.
1553

16-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
17-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
54+
The rehype plugin
55+
[`rehype-infer-reading-time-meta`][rehype-infer-reading-time-meta]
56+
wraps this utility to figure, for use with [`rehype-meta`][rehype-meta].
1857

19-
[npm][]:
58+
## Install
59+
60+
This package is [ESM only][esm].
61+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
2062

2163
```sh
2264
npm install hast-util-reading-time
2365
```
2466

67+
In Deno with [`esm.sh`][esmsh]:
68+
69+
```js
70+
import {readingTime} from "https://esm.sh/hast-util-reading-time@1"
71+
```
72+
73+
In browsers with [`esm.sh`][esmsh]:
74+
75+
```html
76+
<script type="module">
77+
import {readingTime} from "https://esm.sh/hast-util-reading-time@1?bundle"
78+
</script>
79+
```
80+
2581
## Use
2682

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
29-
`example.html`:
83+
Say our document `example.html` contains (from [“Word per minute: Alphanumeric
84+
entry” on Wikipedia][wiki]:
3085

3186
```html
3287
<p>Since the length or duration of words is clearly variable, for the purpose of measurement of text entry, the definition of each "word" is often standardized to be five characters or keystrokes long in English, including spaces and punctuation. For example, under such a method applied to plain English text the phrase "I run" counts as one word, but "rhinoceros" and "let's talk" would both count as two.</p>
@@ -35,51 +90,40 @@ Wikipedia](https://en.wikipedia.org/wiki/Words_per_minute#Alphanumeric_entry) in
3590
<p>An average professional typist types usually in speeds of 43 to 80 wpm, while some positions can require 80 to 95 (usually the minimum required for dispatch positions and other time-sensitive typing jobs), and some advanced typists work at speeds above 120 wpm. Two-finger typists, sometimes also referred to as "hunt and peck" typists, commonly reach sustained speeds of about 37 wpm for memorized text and 27 wpm when copying text, but in bursts may be able to reach much higher speeds. From the 1920s through the 1970s, typing speed (along with shorthand speed) was an important secretarial qualification and typing contests were popular and often publicized by typewriter companies as promotional tools.</p>
3691
```
3792

38-
And our module, `example.js`:
93+
…and our module `example.js` looks as follows:
3994

4095
```js
41-
import fs from 'node:fs'
96+
import fs from 'node:fs/promises'
4297
import {unified} from 'unified'
4398
import rehypeParse from 'rehype-parse'
4499
import {readingTime} from 'hast-util-reading-time'
45100

46-
const example = fs.readFileSync('example.html')
101+
const example = await fs.readFile('example.html')
47102
const tree = unified().use(rehypeParse, {fragment: true}).parse(example)
48103

49104
console.log(
50105
`It takes about ${Math.ceil(readingTime(tree, {age: 18}))}-${Math.ceil(readingTime(tree, {age: 14}))}m to read`
51106
)
52107
```
53108

54-
Now, running `node example.js` yields:
109+
…now running `node example.js` yields:
55110

56111
```txt
57112
It takes about 2-3m to read
58113
```
59114

60115
## API
61116

62-
This package exports the following identifiers: `readingTime`.
117+
This package exports the identifier `readingTime`.
63118
There is no default export.
64119

65120
### `readingTime(tree, options?)`
66121

67-
Utility to estimate the reading time, taking readability of the document and
68-
a target age group into account.
122+
Estimate the reading time.
69123

70-
The algorithm works as follows:
124+
##### `options`
71125

72-
* Estimate the WPM of the audience age based on the facts that English can be
73-
read at ±228 WPM (Trauzettel-Klosinski), and that reading rate increases 14
74-
WPM per grade (Carver)
75-
* Apply the readability algorithms [Dale—Chall][dale-chall],
76-
[Automated Readability][automated-readability], [Coleman-Liau][],
77-
[Flesch][], [Gunning-Fog][], [SMOG][], and [Spache][]
78-
* Adjust the WPM of the audience for whether the readability algorithms
79-
estimate its above or below their level
80-
* `wordCount / adjustedWpm`
81-
82-
Note: this algorithm is specific to English!
126+
Configuration (optional).
83127

84128
###### `options.age`
85129

@@ -90,19 +134,37 @@ expect your readers to all be college graduates, etc.
90134

91135
###### Returns
92136

93-
`number` — Reading time in minutes.
94-
The result’s not rounded so it’s possible to retrieve estimated seconds from it.
137+
Reading time in minutes (`number`).
138+
The result is not rounded so it’s possible to retrieve estimated seconds from
139+
it.
140+
141+
## Types
142+
143+
This package is fully typed with [TypeScript][].
144+
It exports the additional type `Options`.
145+
146+
## Compatibility
147+
148+
Projects maintained by the unified collective are compatible with all maintained
149+
versions of Node.js.
150+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
151+
Our projects sometimes work with older versions, but this is not guaranteed.
95152

96153
## Security
97154

98155
Use of `hast-util-reading-time` is safe.
99156

100157
## Related
101158

159+
* [`rehype-infer-reading-time-meta`][rehype-infer-reading-time-meta]
160+
— infer reading time as file metadata from the document
161+
* [`rehype-meta`][rehype-meta]
162+
— add metadata to the head of a document
163+
102164
## Contribute
103165

104-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
105-
started.
166+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
167+
ways to get started.
106168
See [`support.md`][support] for ways to get help.
107169

108170
This project has a [code of conduct][coc].
@@ -143,15 +205,23 @@ abide by its terms.
143205

144206
[npm]: https://docs.npmjs.com/cli/install
145207

208+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
209+
210+
[esmsh]: https://esm.sh
211+
212+
[typescript]: https://www.typescriptlang.org
213+
146214
[license]: license
147215

148216
[author]: https://wooorm.com
149217

150-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
218+
[health]: https://github.com/syntax-tree/.github
151219

152-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
220+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
153221

154-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
222+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
223+
224+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
155225

156226
[hast]: https://github.com/syntax-tree/hast
157227

@@ -168,3 +238,9 @@ abide by its terms.
168238
[spache]: https://github.com/words/spache-formula
169239

170240
[smog]: https://github.com/words/smog-formula
241+
242+
[rehype-infer-reading-time-meta]: https://github.com/rehypejs/rehype-infer-reading-time-meta
243+
244+
[rehype-meta]: https://github.com/rehypejs/rehype-meta
245+
246+
[wiki]: https://en.wikipedia.org/wiki/Words_per_minute#Alphanumeric_entry

0 commit comments

Comments
 (0)