Instantly share code, notes, and snippets.

Embed
What would you like to do?
Read this in X minutes Liquid Filter Plugin (for Jekyll)
# Outputs the reading time
# Read this in “about 4 minutes”
# Put into your _plugins dir in your Jekyll site
# Usage: Read this in about {{ page.content | reading_time }}
module ReadingTimeFilter
def reading_time( input )
words_per_minute = 180
words = input.split.size;
minutes = ( words / words_per_minute ).floor
minutes_label = minutes === 1 ? " minute" : " minutes"
minutes > 0 ? "about #{minutes} #{minutes_label}" : "less than 1 minute"
end
end
Liquid::Template.register_filter(ReadingTimeFilter)
@samzeng

This comment has been minimized.

Show comment
Hide comment
@samzeng

samzeng Mar 11, 2014

it can not use in chinese....plz fix

samzeng commented Mar 11, 2014

it can not use in chinese....plz fix

@alan-andrade

This comment has been minimized.

Show comment
Hide comment
@alan-andrade

alan-andrade Mar 25, 2014

@samzeng I'd be nice if you can share the fomula to calculate the time for chinese.

alan-andrade commented Mar 25, 2014

@samzeng I'd be nice if you can share the fomula to calculate the time for chinese.

@arashbm

This comment has been minimized.

Show comment
Hide comment
@arashbm

arashbm May 23, 2014

The problem with Chinese is apparently the assumption that there is white-space between words. read more about it on Wikipedia

arashbm commented May 23, 2014

The problem with Chinese is apparently the assumption that there is white-space between words. read more about it on Wikipedia

@ghprince

This comment has been minimized.

Show comment
Hide comment
@ghprince

ghprince Jul 15, 2014

Reading speed in Chinese is normally represented by characters per minute. So using input.length is enough for the calculation. The only problem I think is how to determine if the article is in English or Chinese.

ghprince commented Jul 15, 2014

Reading speed in Chinese is normally represented by characters per minute. So using input.length is enough for the calculation. The only problem I think is how to determine if the article is in English or Chinese.

@01010000101001100

This comment has been minimized.

Show comment
Hide comment
@01010000101001100

01010000101001100 Sep 1, 2014

Thank you! Works great with new 2.3.0 jekyll!

01010000101001100 commented Sep 1, 2014

Thank you! Works great with new 2.3.0 jekyll!

@alessandrococco

This comment has been minimized.

Show comment
Hide comment
@alessandrococco

alessandrococco Nov 23, 2014

Thank you, it works like a charm!

alessandrococco commented Nov 23, 2014

Thank you, it works like a charm!

@anishjajodia

This comment has been minimized.

Show comment
Hide comment
@anishjajodia

anishjajodia Dec 4, 2014

Works perfect. Cheers.

anishjajodia commented Dec 4, 2014

Works perfect. Cheers.

@tohuw

This comment has been minimized.

Show comment
Hide comment
@tohuw

tohuw Feb 22, 2015

Very nice, thanks for providing this.

I've forked this script to remove the dependence on the surrounding text (i.e. "about...") and remove all the text, so it now returns only an integer. This should make it more useful for multi-language implementations. There's already plenty of capability in Liquid's conditionals to handle proper pluralization. Optionally, one could leverage the Jekyll "pluralize" plugin to assist with this.

This is a rather opinionated set of changes, so I've gone so far as to change the Filter name as well, to avoid confusion with your work. However, if you'd like to take on or collaborate on these changes, we can certainly come up with a reasonable fusion, or you may of course simply use them how you wish. Thanks again for this work.

A sample implementation of these edits is here.

tohuw commented Feb 22, 2015

Very nice, thanks for providing this.

I've forked this script to remove the dependence on the surrounding text (i.e. "about...") and remove all the text, so it now returns only an integer. This should make it more useful for multi-language implementations. There's already plenty of capability in Liquid's conditionals to handle proper pluralization. Optionally, one could leverage the Jekyll "pluralize" plugin to assist with this.

This is a rather opinionated set of changes, so I've gone so far as to change the Filter name as well, to avoid confusion with your work. However, if you'd like to take on or collaborate on these changes, we can certainly come up with a reasonable fusion, or you may of course simply use them how you wish. Thanks again for this work.

A sample implementation of these edits is here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment