Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Sep 10, 2021
0 parents commit 259096a
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
13 changes: 13 additions & 0 deletions .github/workflows/bb.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: bb
on:
issues:
types: [opened, reopened, edited, closed, labeled, unlabeled]
pull_request_target:
types: [opened, reopened, edited, closed, labeled, unlabeled]
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: unifiedjs/beep-boop-beta@main
with:
repo-token: ${{secrets.GITHUB_TOKEN}}
21 changes: 21 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: main
on:
- pull_request
- push
jobs:
main:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: dcodeIO/setup-node-nvm@master
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v1
strategy:
matrix:
node:
- lts/erbium
- node
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
*.d.ts
*.log
coverage/
node_modules/
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage/
*.html
*.md
1 change: 1 addition & 0 deletions fixture.html

Large diffs are not rendered by default.

60 changes: 60 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Element} Element
*
* @typedef Options
* Configuration.
* @property {number|[number, number]} [age=[16, 18]]
* Target age group.
* This is the age your target audience was still in school.
* Set it to 18 if you expect all readers to have finished high school,
* 21 if you expect your readers to all be college graduates, etc.
* Can be two numbers in an array to get two estimates.
* @property {string} [mainSelector]
* CSS selector to body of content.
* Useful to exclude other things, such as the head, ads, styles, scripts, and
* other random stuff, by focussing all strategies in one element.
*/

import {select} from 'hast-util-select'
import {readingTime} from 'hast-util-reading-time'

/**
* Plugin to infer file metadata from the document.
*
* @type {import('unified').Plugin<[Options?]|[], Root>}
*/
export default function rehypeInferReadingTimeMeta(options = {}) {
const {age = [14, 18], mainSelector} = options

return (tree, file) => {
const main = mainSelector ? select(mainSelector, tree) : tree
/** @type {[number, number]|number|undefined} */
let time

if (main) {
if (Array.isArray(age)) {
// @ts-expect-error: hush, always two items.
time = age
.slice(0, 2)
.map((age) => readingTime(main, {age}))
.sort((a, b) => a - b)
} else {
time = readingTime(main, {age})
}
}

if (time) {
const matter = /** @type {Record<string, unknown>} */ (
file.data.matter || {}
)
const meta = /** @type {Record<string, unknown>} */ (
file.data.meta || (file.data.meta = {})
)

if (!matter.readingTime && !meta.readingTime) {
meta.readingTime = time
}
}
}
}
22 changes: 22 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2021 Titus Wormer <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
87 changes: 87 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"name": "rehype-infer-reading-time-meta",
"version": "0.0.0",
"description": "rehype plugin to infer reading time as file metadata from the document",
"license": "MIT",
"keywords": [
"unified",
"rehype",
"rehype-plugin",
"plugin",
"html",
"hast",
"file",
"meta",
"reading",
"time",
"estimate",
"reading-time"
],
"repository": "rehypejs/rehype-infer-reading-time-meta",
"bugs": "https://github.com/rehypejs/rehype-infer-reading-time-meta/issues",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/unified"
},
"author": "Titus Wormer <[email protected]> (https://wooorm.com)",
"contributors": [
"Titus Wormer <[email protected]> (https://wooorm.com)"
],
"sideEffects": false,
"type": "module",
"main": "index.js",
"types": "index.d.ts",
"files": [
"index.d.ts",
"index.js"
],
"dependencies": {
"@types/hast": "^2.0.0",
"hast-util-reading-time": "^1.0.0",
"hast-util-select": "^5.0.0",
"unified": "^10.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"prettier": "^2.0.0",
"rehype": "^12.0.0",
"rehype-meta": "^3.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^9.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.44.0"
},
"scripts": {
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --conditions development test.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node --conditions development test.js",
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"prettier": true
},
"remarkConfig": {
"plugins": [
"preset-wooorm"
]
},
"typeCoverage": {
"atLeast": 100,
"detail": true,
"strict": true,
"ignoreCatch": true
}
}
Loading

0 comments on commit 259096a

Please sign in to comment.