Get Content Reading Time Estimation

Introduction

 
You must have seen the feature of estimated time to read on many websites and blogs, such as Medium. In this article, we will learn how we can get a reading time estimation on our website. This is possible with a simple node.js package; i.e., reading-time.
 

What is reading-time?

 
reading-time is a simple Node.js package which helps us to get the estimated time to read the content. This package not only works perfectly with plain-text but also, we can use this package for HTML and Markdown content.
 
Installation
 
We can use the following command to install this package in the node project.
  1. npm install --save reading-time  

How to use reading-time?

 
There are two ways to use the reading-time package.
 
Classic
  1. const readingTime = require('reading-time');//Importing reading-time package  
  2.   
  3. const result = readingTime('Any text will be here');// Getting result  
  4.   
  5. console.log(result);  
Stream
  1. const readingTime = require('reading-time/stream');//Importing reading-time package  
  2.   
  3. fs.createReadStream('<filename>')  
  4.   .pipe(readingTime)  
  5.   .on('data', result => {  
  6.    console.log(result)  
  7.   });  
Example
  1. const readingTime = require('reading-time');  
  2.   
  3. const fs=require('fs');  
  4.   
  5. var filename = './article.txt';  
  6.   
  7. fs.readFile(filename,function(err,data) {  
  8.     if(err){  
  9.         console.log("Error while reading file")  
  10.     }  
  11.     var result=readingTime(data.toString())  
  12.     console.log(result)  
  13. })  
In the above example, first, we are reading the file and getting the data using readFile function. After that, we are using the readTime function to get the estimated time.
 
Output
 


Similar Articles