unified

Project: rehypejs/rehype-infer-reading-time-meta

Package: rehype-infer-reading-time-meta@1.0.0

  1. Dependents: 0
  2. rehype plugin to infer reading time as file metadata from the document
  1. unified 174
  2. plugin 137
  3. html 121
  4. rehype 85
  5. hast 72
  6. rehype-plugin 60
  7. file 34
  8. meta 9
  9. reading 3
  10. time 2
  11. reading-time 2

rehype-infer-reading-time-meta

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to infer the estimated reading time from a document as file metadata. This plugin sets file.data.meta.readingTime. This is mostly useful with rehype-meta.

Contents

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install rehype-infer-reading-time-meta

Use

Say example.js looks as follows:

import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeInferReadingTimeMeta from 'rehype-infer-reading-time-meta'
import rehypeDocument from 'rehype-document'
import rehypeMeta from 'rehype-meta'
import rehypeFormat from 'rehype-format'
import rehypeStringify from 'rehype-stringify'

main()

async function main() {
  const file = await unified()
    .use(rehypeParse, {fragment: true})
    .use(rehypeInferReadingTimeMeta)
    .use(rehypeDocument)
    .use(rehypeMeta, {twitter: true})
    .use(rehypeFormat)
    .use(rehypeStringify)
    .process(
      '<h1>Build</h1><p><strong>We provide the building blocks</strong>: from tiny, focussed, modular utilities to plugins that combine them to perform bigger tasks. And much, much more. You can build on unified, mixing and matching building blocks together, to make all kinds of interesting new things.</p>'
    )

  console.log(String(file))
}

Now, running node example yields:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:label1" content="Reading time">
    <meta name="twitter:data1" content="1 minute">
  </head>
  <body>
    <h1>Build</h1>
    <p><strong>We provide the building blocks</strong>: from tiny, focussed, modular utilities to plugins that combine them to perform bigger tasks. And much, much more. You can build on unified, mixing and matching building blocks together, to make all kinds of interesting new things.</p>
  </body>
</html>

API

This package exports no identifiers. The default export is rehypeInferReadingTimeMeta.

unified().use(rehypeInferReadingTimeMeta, options?)

Plugin to infer the estimated reading time from a document as file metadata.

The reading time is inferred not just on words per minute, but also takes readability into account.

options.age

Target age group (number or [number, number], default: [16, 18]). 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.

options.mainSelector

CSS selector to body of content (string, optional, example: 'main'). Useful to exclude other things, such as the head, ads, styles, scripts, and other random stuff, by focussing on one element.

Security

Use of rehype-infer-reading-time-meta is safe.

Contribute

See contributing.md in rehypejs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer