DEV Community

Cover image for Calculate the 'reading time' for your blog β±πŸ¦‰

Calculate the 'reading time' for your blog β±πŸ¦‰

Rob OLeary on April 24, 2020

You can subscribe to my RSS feed to get my latest posts. Can we add anything to a standard blog that would enhance the reading experience? How...
Collapse
 
robole profile image
Rob OLeary •

Do you have this in your blog?

Collapse
 
corentinbettiol profile image
Corentin Bettiol •

Nope, will consider adding it on list page, it may be useful :)

Collapse
 
robole profile image
Rob OLeary • • Edited

🀟 I hope so! If you add it, add the link here, I'd love to see it πŸ™‚πŸ‘

Thread Thread
 
corentinbettiol profile image
Corentin Bettiol • • Edited

screenshot l3m.in blog

I just finished adding it on my (french) website (on blog posts & projects list)!

(also added a small explication at the bottom of the about page)

Thread Thread
 
robole profile image
Rob OLeary •

😎 Very good, Corentin! Thanks for the update. I like your avatar πŸ™‚

Thread Thread
 
corentinbettiol profile image
Corentin Bettiol •

Thanks :D

I like it too, so I added it on almost all my online profiles :)

Collapse
 
binyamin profile image
Binyamin Green •

I had wanted to do this; I was going to get messy and parse the html string, stripping out tags and attributes. You saved me so much time, thanks!

Collapse
 
robole profile image
Rob OLeary •

Phew! πŸ˜… Glad I saved you the trouble then

Collapse
 
binyamin profile image
Binyamin Green •

By the way, I would use the regexp /(\w\S*)+/g. That allows for apostrophes and dashes, and doesn't break a url into separate words.

Thread Thread
 
robole profile image
Rob OLeary • • Edited

Whatever regex you choose, the result is an approximate wordcount.

For instance, it's , do we count this as 1 or 2 words? It is written like a single word, but technically it is 2, it is shorthand for: it is. Apostrophes can also be used to describe possession like: Bob's cat, do we count Bob's as one word? Different contexts can have different meaning.

Hyphens can be used to make compound words such as: vice-president, twenty-one, rock-hard, and load-bearing. Can we say all of these are the same (1 or 2 words)? Some compound words have no hyphen such as headache.

Whichever regex you use, to have a consistent interpretation is probably not possible ! You need natural language processing to make it more accurate and consistent.

Collapse
 
f0cus10 profile image
Iftikhar K. •

I don’t think I’m reading that JavaScript in 26 seconds, unfortunately 😞

Really good layout of your thought process btw.

Collapse
 
robole profile image
Rob OLeary • • Edited

I wouldn't read that JavaScript fragment in 26 seconds, and I wrote it! πŸ˜…

Thanks for the compliment!