Skip to content

Commit 259096a

Browse files
committed
.
0 parents  commit 259096a

File tree

13 files changed

+475
-0
lines changed

13 files changed

+475
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.github/workflows/bb.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: bb
2+
on:
3+
issues:
4+
types: [opened, reopened, edited, closed, labeled, unlabeled]
5+
pull_request_target:
6+
types: [opened, reopened, edited, closed, labeled, unlabeled]
7+
jobs:
8+
main:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: unifiedjs/beep-boop-beta@main
12+
with:
13+
repo-token: ${{secrets.GITHUB_TOKEN}}

.github/workflows/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: main
2+
on:
3+
- pull_request
4+
- push
5+
jobs:
6+
main:
7+
name: ${{matrix.node}}
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: dcodeIO/setup-node-nvm@master
12+
with:
13+
node-version: ${{matrix.node}}
14+
- run: npm install
15+
- run: npm test
16+
- uses: codecov/codecov-action@v1
17+
strategy:
18+
matrix:
19+
node:
20+
- lts/erbium
21+
- node

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.d.ts
3+
*.log
4+
coverage/
5+
node_modules/
6+
yarn.lock

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
*.html
3+
*.md

fixture.html

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @typedef {import('hast').Root} Root
3+
* @typedef {import('hast').Element} Element
4+
*
5+
* @typedef Options
6+
* Configuration.
7+
* @property {number|[number, number]} [age=[16, 18]]
8+
* Target age group.
9+
* This is the age your target audience was still in school.
10+
* Set it to 18 if you expect all readers to have finished high school,
11+
* 21 if you expect your readers to all be college graduates, etc.
12+
* Can be two numbers in an array to get two estimates.
13+
* @property {string} [mainSelector]
14+
* CSS selector to body of content.
15+
* Useful to exclude other things, such as the head, ads, styles, scripts, and
16+
* other random stuff, by focussing all strategies in one element.
17+
*/
18+
19+
import {select} from 'hast-util-select'
20+
import {readingTime} from 'hast-util-reading-time'
21+
22+
/**
23+
* Plugin to infer file metadata from the document.
24+
*
25+
* @type {import('unified').Plugin<[Options?]|[], Root>}
26+
*/
27+
export default function rehypeInferReadingTimeMeta(options = {}) {
28+
const {age = [14, 18], mainSelector} = options
29+
30+
return (tree, file) => {
31+
const main = mainSelector ? select(mainSelector, tree) : tree
32+
/** @type {[number, number]|number|undefined} */
33+
let time
34+
35+
if (main) {
36+
if (Array.isArray(age)) {
37+
// @ts-expect-error: hush, always two items.
38+
time = age
39+
.slice(0, 2)
40+
.map((age) => readingTime(main, {age}))
41+
.sort((a, b) => a - b)
42+
} else {
43+
time = readingTime(main, {age})
44+
}
45+
}
46+
47+
if (time) {
48+
const matter = /** @type {Record<string, unknown>} */ (
49+
file.data.matter || {}
50+
)
51+
const meta = /** @type {Record<string, unknown>} */ (
52+
file.data.meta || (file.data.meta = {})
53+
)
54+
55+
if (!matter.readingTime && !meta.readingTime) {
56+
meta.readingTime = time
57+
}
58+
}
59+
}
60+
}

license

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2021 Titus Wormer <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{
2+
"name": "rehype-infer-reading-time-meta",
3+
"version": "0.0.0",
4+
"description": "rehype plugin to infer reading time as file metadata from the document",
5+
"license": "MIT",
6+
"keywords": [
7+
"unified",
8+
"rehype",
9+
"rehype-plugin",
10+
"plugin",
11+
"html",
12+
"hast",
13+
"file",
14+
"meta",
15+
"reading",
16+
"time",
17+
"estimate",
18+
"reading-time"
19+
],
20+
"repository": "rehypejs/rehype-infer-reading-time-meta",
21+
"bugs": "https://github.com/rehypejs/rehype-infer-reading-time-meta/issues",
22+
"funding": {
23+
"type": "opencollective",
24+
"url": "https://opencollective.com/unified"
25+
},
26+
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
27+
"contributors": [
28+
"Titus Wormer <[email protected]> (https://wooorm.com)"
29+
],
30+
"sideEffects": false,
31+
"type": "module",
32+
"main": "index.js",
33+
"types": "index.d.ts",
34+
"files": [
35+
"index.d.ts",
36+
"index.js"
37+
],
38+
"dependencies": {
39+
"@types/hast": "^2.0.0",
40+
"hast-util-reading-time": "^1.0.0",
41+
"hast-util-select": "^5.0.0",
42+
"unified": "^10.0.0"
43+
},
44+
"devDependencies": {
45+
"@types/tape": "^4.0.0",
46+
"c8": "^7.0.0",
47+
"prettier": "^2.0.0",
48+
"rehype": "^12.0.0",
49+
"rehype-meta": "^3.0.0",
50+
"remark-cli": "^10.0.0",
51+
"remark-preset-wooorm": "^9.0.0",
52+
"rimraf": "^3.0.0",
53+
"tape": "^5.0.0",
54+
"type-coverage": "^2.0.0",
55+
"typescript": "^4.0.0",
56+
"xo": "^0.44.0"
57+
},
58+
"scripts": {
59+
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
60+
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
61+
"test-api": "node --conditions development test.js",
62+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
63+
"test": "npm run build && npm run format && npm run test-coverage"
64+
},
65+
"prettier": {
66+
"tabWidth": 2,
67+
"useTabs": false,
68+
"singleQuote": true,
69+
"bracketSpacing": false,
70+
"semi": false,
71+
"trailingComma": "none"
72+
},
73+
"xo": {
74+
"prettier": true
75+
},
76+
"remarkConfig": {
77+
"plugins": [
78+
"preset-wooorm"
79+
]
80+
},
81+
"typeCoverage": {
82+
"atLeast": 100,
83+
"detail": true,
84+
"strict": true,
85+
"ignoreCatch": true
86+
}
87+
}

0 commit comments

Comments
 (0)